-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using Return Type Of Fallible Function In Another Function Causes Incorrect Comptime Error #15023
Comments
I don't think it makes sense to be able to refect a generic function's inferred error set, but here's a reduction: fn use(_: u8) void {}
fn func(comptime param: u8) !void {
use(param); // ok: runtime use of comptime param
comptime use(param); // not ok: comptime use of comptime param
// ^~~~~
// example.zig:5:18: error: unable to resolve comptime value
// example.zig:5:18: note: argument to function being called at comptime must be comptime-known
}
comptime {
func(undefined) catch unreachable; // doesn't matter if `func` already instantiated
const ErrorUnion = @typeInfo(@TypeOf(func)).Fn.return_type.?;
const ErrorSet = @typeInfo(ErrorUnion).ErrorUnion.error_set;
_ = @typeInfo(ErrorSet).ErrorSet; // not ok: comptime use of inferred error set of generic function
} Some useful observations:
It looks like reflecting on a generic function's inferred error set is analyzing the function with Related: #12806 |
Yeah, the inferred error set is only known for specific instantiations since a comptime-known argument can change it arbitrarily. So the function return type depends on the instantiation, meaning |
maybe also related to #14991 |
Yeah, that's what I meant by disallow entirely. However, that's sort of a sledgehammer solution since the unwrapped return type could be static and not depend on |
Mhm, I agree. The error should reflect this though. But there are cases where it could make sense to do this. It could be useful to know the payload of the error union if that's known, as is the case with These are probably questions worth asking, personally I'd say they probably shouldn't be allowed. However, the compiler should probably be able to determine whether or not the return type is invariant for a generic function, and allow it, like in this case. |
Duplicate of #14991 |
Zig Version
0.11.0-dev.2168+322ace70f
Steps to Reproduce and Observed Behavior
This is the following minimum reproduction case I could determine for this:
The following compile error is observed on invocation of
zig test repro.zig
:It is worth noting that
helper
doesn't need to be called for this to happen, and that using@typeInfo
to get the error set forhelper
and use it as part of an error union forconcat
's return value also exhibits this behaviour.Making
helper
aninline fn
allows the program to compile successfully, as does removing theconcat
invocation. Writing_ = HelperReturnType
without aconcat
invocation does not reproduce the error.Expected Behavior
The code should have compiled successfully.
The text was updated successfully, but these errors were encountered: