-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Change desugaring of let-else to ensure temporary is dropped earlier #94012
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
638d9c3
let-else: test for issue 93951 / temporary lifetimes
cormacrelf 14283b2
let-else: add a test for warnings on let-else with diverging tail
cormacrelf 07539b8
let-else: use DropTemps to fix borrowck complaints
cormacrelf d549ede
let-else: add test for linting unused variables in the first place
cormacrelf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// check-pass | ||
// | ||
// from issue #93951, where borrowck complained the temporary that `foo(&x)` was stored in was to | ||
// be dropped sometime after `x` was. It then suggested adding a semicolon that was already there. | ||
|
||
#![feature(let_else)] | ||
use std::fmt::Debug; | ||
|
||
fn foo<'a>(x: &'a str) -> Result<impl Debug + 'a, ()> { | ||
Ok(x) | ||
} | ||
|
||
fn let_else() { | ||
let x = String::from("Hey"); | ||
let Ok(s) = foo(&x) else { return }; | ||
cormacrelf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
fn if_let() { | ||
let x = String::from("Hey"); | ||
let s = if let Ok(s) = foo(&x) { s } else { return }; | ||
} | ||
|
||
fn main() { | ||
let_else(); | ||
if_let(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
cormacrelf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// popped up in in #94012, where an alternative desugaring was | ||
// causing unreachable code errors | ||
|
||
#![feature(let_else)] | ||
#![deny(unused_variables)] | ||
#![deny(unreachable_code)] | ||
|
||
fn let_else_diverge() -> bool { | ||
let Some(_) = Some("test") else { | ||
let x = 5; //~ ERROR unused variable: `x` | ||
return false; | ||
}; | ||
return true; | ||
} | ||
|
||
fn main() { | ||
let_else_diverge(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error: unused variable: `x` | ||
--> $DIR/let-else-then-diverge.rs:11:13 | ||
| | ||
LL | let x = 5; | ||
| ^ help: if this is intentional, prefix it with an underscore: `_x` | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/let-else-then-diverge.rs:6:9 | ||
| | ||
LL | #![deny(unused_variables)] | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to be consistent with
let
we would have to actually drop temps for thelet_expr
orinit_expr
, wouldn't we?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, gut feeling this would fix the new problem. Good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I imagine the most straightforward solution will drop temps before the
else
block instead of after it. Maybe that's okay, but just want to make sure that detail is considered.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My proposed fix does not fix the general discrepancy between let and let-else, e.g https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=57e814fa57019bc62930bc9d61c894da
after the last lang meeting either @nikomatsakis or @pnkfelix should take over the review here. i am not familar enough with rusts temporary rules
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should a new issue be filed? Or work it within this issue