-
Notifications
You must be signed in to change notification settings - Fork 12.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
Better handling for unwrapping reference to Result (or Option) #48121
Comments
You are correct, though there is one exception for the case where both
This is no trivial matter, and is something that would probably require an RFC and require lots of discussion (and probably additions to the language) to make happen. Consider impl<T> Option<T> {
pub fn as_ref(&self) -> Option<&T> {
match *self {
Option::Some(ref x) => Option::Some(x),
Option::None => Option::None,
}
}
pub fn unwrap(self) -> T {
match self {
Option::Some(x) => x,
Option::None => panic!("unwrap on None"),
}
}
pub fn unwrap_or_default(self) -> T
where T: Default, {
match self {
Option::Some(x) => x,
Option::None => Default::default(),
}
}
} Both (well, okay; technically the One final thought: It is unfortunate that default match binding modes will further blur the line between |
I don't get it. How the code for that would look like? Anyway, If it's really non trivial case then a better error message will be good enough I think. |
Seems possible. The question is, what is the scope of the lint? Just I will cc @estebank who has been prolific with diagnostic hints and may have better ideas. |
I guess I was thinking of something like impl<T> Option<T> {
// The `&?` would be a new syntax that indicates the function
// is polymorphic over `self`, `&self`, and `&mut self`
fn unwrap(&? self) -> &? T {
// I envision `match` as the language primitive which would make it
// possible to implement a function with such a signature, by extending
// the upcoming "match default binding modes" feature to support `&?`.
match self {
Some(x) => x,
None => panic!("unwrap on None value"),
}
}
// The above function would behave as though these three functions existed.
//
// I'm including the lifetimes explicitly to show that `unwrap` would have
// a variable number of lifetime parameters based on how it is called;
// this is something not yet possible to express in rust, and I imagine
// it would be a huge barrier to adoption and implementation.
fn unwrap(self) -> T;
fn unwrap<'a>(&'a self) -> &'a T;
fn unwrap<'a>(&'a mut self) -> &'a mut T;
// `unwrap_or_default` would keep its old signature because it can not
// be made polymorphic over references
fn unwrap_or_default(self) -> T where T: Default;
} The new However, an idea like this would need to go through the RFC process, where I imagine it would be quite contentious, and where it would be competing against other ideas that solve similar problems. |
Now I got it, thanks for explanation. |
There're a couple of tickets open to turn the error into something along the lines of
This wouldn't be helpful enough, but it is incremental improvement. It could be possible that the machinery to suggest implementing |
Maybe it could be also added to https://doc.rust-lang.org/error-index.html ? |
Currently in 1.62.0 stable the output is now
However that suggestion is still wrong, and applying it will cause an error with regards to type inference which will lead you down a path that won't work. The ideal suggestion is to put the |
CC #90286 |
Triage: current output in beta is correct:
|
Current error:
|
I believe that there won't be any changes to the stdlib here, and the diagnostic is already "good enough". I'd like to fix the |
Having such simplified code:
gives:
Thanks to irc Kimundi & Amarnath I got the solution & expanation:
Explanation:
unwrap
requires full ownership because it moves valuesself
in signature?ref_result
is only borrowing, (ref_result
is a reference, a reference means borrowing)Using
.as_ref()
works because:It creates a brand new
Result
with references to inner value, like:from initial:
to brand new one:
See implementation of
Result.as_ref
hereIf i got this correctly, using
unwrap
on&Result
will always be corrupted.My question is, could this be handled internally somehow by Rust?
And/Or better compilation error would be awesome for noobs like me :)
I'm not sure what the message should look like but the current one is a bit general.
The text was updated successfully, but these errors were encountered: