-
Notifications
You must be signed in to change notification settings - Fork 12.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
Closure Does/Doesn't compile depending purely on usage of a {} body with --edition=2021 #105699
Comments
i assume this caused by fn main() {
let a: &[i32; 2] = &[10; 2];
let b: Option<&[i32]> = true.then(|| a);
} the coercion in your code example would be the type of edit: apparently fn blah(a: &[i32; 2]) -> Option<&[i32]> {
true.then(|| { a })
} I am suddenly feeling very distrustful of rust's coercions 🤣 |
I could reproduce the same issue with these examples. fn fails(arg: &mut Box<dyn Drop>) -> Option<&mut dyn Drop> {
true.then(|| arg.as_mut())
}
fn works(arg: &mut Box<dyn Drop>) -> Option<&mut dyn Drop> {
true.then(|| { arg.as_mut() })
} I would have expected them both to de-sugar to the same thing but they produce different HIR and different MIR. MIR for the
|
This is still an issue in rustc 1.74.0 and 1.76.0-nightly (2023-11-28 5facb42). It's a bit annoying, because rustfmt automatically remove the braces, so that's not a good workaround. fn works<'a>(&'a mut self) -> Option<&'a mut dyn Tr> {
true.then(|| self.obj.as_mut() as _) // ok
} Also, the following code (proposed on the forum by @steffahn) produces an invalid suggestion from the compiler: applying it breaks compilation. trait Data {}
struct Registry {
map: HashMap<String, Box<dyn Data>>,
}
impl Registry {
fn get_mut(&mut self, key: &str) -> Option<&mut dyn Data> {
// I originally wanted to write this but it triggers the previously mentioned lifetime issue
// self.map.get_mut(key).map(|b| &mut **b)
// This compiles but the compiler suggests to remove the braces, which won't compile
self.map.get_mut(key).map(move |b| return {&mut **b})
}
} |
Example available on https://rust.godbolt.org/z/bzjPv6TGT
You get a failure if you remove the edition flag, but it's completely different and that may or may not also be incorrect.
The text was updated successfully, but these errors were encountered: