Comments (2)
while I wouldn't mind seeing this fixed and still write this way in my own code atm, I could also see an argument that it should be left as-is since it would better parallel with function body code that the comptime keyword is only used to elevate otherwise-runtime things to be comptime-known. in bodies we even have an error about using it redundantly.
from zig.
This problem is a little trickier than it seems, because we don't necessarily know if a function call is comptime until after we evaluate its parameters. Consider:
fn f(T: type, x: anytype) @TypeOf(x) {
_ = T;
return x;
}
If f
is called in a runtime context, the call itself might still be comptime depending on whether x
has a comptime-only type. That means we require a comptime
annotation on T
only if x
has a runtime type. This might be non-trivial - I forget how the Sema logic is structured.
If that's hard, it'd be easiest to instead tighten the rule a bit and say that the comptime
is only optional if the call site is already being comptime-evaluated, i.e. it's not just because the function is comptime-only. I'm not sure if this would regress use cases however, and it feels like a more complex rule.
By the way, a good separate enhancement might be to tighten the rules further by requiring the annotation on any argument which is not generic. We could check in AstGen that no identifier is a prior parameter, or a generic capture, or a decl derived from a generic capture. In that case, we could completely change the rule to the following, which would be nicer and also more generally helpful:
"A function parameter with a comptime-only type must be marked comptime
if its type is comptime-only or the return type is comptime-only, and the parameter type is not generic."
from zig.
Related Issues (20)
- `error: TODO: implement @ptrCast between slices changing the length`
- Compiler allows initialisation of enum types with invalid enum Values HOT 1
- Runtime safety check added in comptime block HOT 2
- Cannot `@cImport` `windows.h` when targetting `msvc` abi HOT 5
- Errdefer payload type doesn't match function signature
- `callconv(.Naked)` functions with parameters cause silent LLVM crash when compiling for PowerPC
- default local cache directory to `.zig-cache` instead of `zig-cache` HOT 4
- std.debug.bufPrintZ padding on the wrong side of output
- observed error.Unexpected NTSTATUS=0xc0000056 (DELETE_PENDING) in makeOpenDirAccessMaskW
- add `math.approxEqUlp` for numerical precision testing
- Missing error for runtime `@ptrFromInt` to comptime-only type HOT 2
- ReleaseSafe panic error trace points at line 0 when freeing an undefined pointer HOT 4
- Zig does not report errors or sliently fails on `.S` / `assembler_with_cpp` files
- Can't link on PPC64LE HOT 1
- ErrorUnion typeInfo debug print causes GenericPoison HOT 2
- @cInclude("stdio.h") doesn't work for -Dtarget=wasm32-emscripten
- Unsure how to handle duplicate symbol linker error
- SIGTRAP When attempting to write to a runtime index in comptime variable HOT 1
- Undefined struct fields are initialized to 0 instead of 0xAA in debug mode (with llvm backend)
- zig fetch -h should include examples
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zig.