Skip to content
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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
span,
kind: hir::ExprKind::If(let_expr, then_expr, Some(else_expr)),
});
let drop_temps = self.expr_drop_temps(span, if_expr, AttrVec::new());
Copy link
Contributor

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 the let_expr or init_expr, wouldn't we?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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

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

if !self.sess.features_untracked().let_else {
feature_err(
&self.sess.parse_sess,
Expand All @@ -168,6 +169,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
)
.emit();
}
if_expr
drop_temps
}
}
1 change: 1 addition & 0 deletions compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
| ExprKind::If(..)
| ExprKind::Let(..)
| ExprKind::Loop(..)
| ExprKind::DropTemps(..)
| ExprKind::Match(..) => {}
// If `expr` is a result of desugaring the try block and is an ok-wrapped
// diverging expression (e.g. it arose from desugaring of `try { return }`),
Expand Down
5 changes: 3 additions & 2 deletions src/test/ui/let-else/let-else-allow-unused.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// check-pass
// issue #89807

#![feature(let_else)]

#[deny(unused_variables)]

fn main() {
let value = Some(String::new());
let value = Some(5);
#[allow(unused)]
let banana = 1;
#[allow(unused)]
let Some(chaenomeles) = value else { return }; // OK
let Some(unused) = value else { return };
//~^ ERROR unused variable: `unused`
}
14 changes: 14 additions & 0 deletions src/test/ui/let-else/let-else-allow-unused.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: unused variable: `unused`
--> $DIR/let-else-allow-unused.rs:13:14
|
LL | let Some(unused) = value else { return };
| ^^^^^^ help: if this is intentional, prefix it with an underscore: `_unused`
|
note: the lint level is defined here
--> $DIR/let-else-allow-unused.rs:5:8
|
LL | #[deny(unused_variables)]
| ^^^^^^^^^^^^^^^^

error: aborting due to previous error

26 changes: 26 additions & 0 deletions src/test/ui/let-else/let-else-temp-borrowck.rs
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(_) = foo(&x) else { return };
}

fn if_let() {
let x = String::from("Hey");
let _ = if let Ok(s) = foo(&x) { s } else { return };
}

fn main() {
let_else();
if_let();
}
20 changes: 20 additions & 0 deletions src/test/ui/let-else/let-else-then-diverge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// popped up in in #94012, where an alternative desugaring was
// causing unreachable code errors, and also we needed to check
// that the desugaring's generated lints weren't applying to
// the whole else block.

#![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();
}
14 changes: 14 additions & 0 deletions src/test/ui/let-else/let-else-then-diverge.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: unused variable: `x`
--> $DIR/let-else-then-diverge.rs:12: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:7:9
|
LL | #![deny(unused_variables)]
| ^^^^^^^^^^^^^^^^

error: aborting due to previous error