-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Getting the address of an error can be used to ignore error handling #9931
Comments
I mean, getting the address of a temporary should probably be a entirely different error to begin with |
It is status-quo the compiler does not force use of const std = @import("std");
pub fn main() void {
const x = foo(); // @TypeOf(x) == FooError!void
std.debug.warn("--> {}\n", .{x});
}
const FooError = error { Yikes };
fn foo() FooError!void {
return error.Yikes;
} |
But wouldn't this be more comparable? const std = @import("std");
pub fn main() void {
_ = foo(); // @TypeOf(x) == FooError!void
}
const FooError = error{Yikes};
fn foo() FooError!void {
return error.Yikes;
} $ zig run test.zig
./test.zig:4:12: error: error is discarded. consider using `try`, `catch`, or `if`
_ = foo(); // @TypeOf(x) == FooError!void I'm essentially discarding the error. Edit:Even this would be an error too: // Omitted code
pub fn main() void {
const x = foo(); // @TypeOf(x) == FooError!void
_ = x;
} |
tbh I'm not sure what the reasoning is behind |
Here's my attempt at an explanation: From what I understand, the compiler probably decides to emit the error based on the discarded expression's type, in which case that could be extended to pointer-to-single-error-union. It might be easier to special-case taking the address of function-returned error unions. I think introducing a temporary variable is a workable workaround, if that were the intended behaviour.
There is an error planned for returning a dangling pointer to to-be-destroyed stack space: #2646 Outdated:However, I don't think that's what's happening here. Afaik, current language semantics are to extend the lifetime of objects involved with address-of-expressions... I think until the end of the function, but I might be wrong. This is similar to [compound literals introduced in C99, which give automatic storage duration until the end of the current block](https://en.cppreference.com/w/c/language/compound_literal).I think this was introduced some years back to make the EDIT: For anyone reading this now, I think I've read in some stage2 discussion that the automatic lifetime extension I originally mentioned above has already been scrapped. So avoid dangling pointers. |
Problem:
Prepending
_ = &
to an error value can be used to skip error handling. This didn't seem like intended behavior.How to reproduce:
Sample
test.zig
file:Output:
Notes:
Tested on
0.9.0-dev.1343+75cecef63
.I would think the proper way to get this kind of behavior would be to do:
I have no idea what the correct approach to fixing this should be, but it didn't seem right.
The text was updated successfully, but these errors were encountered: