Skip to content
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
25 changes: 24 additions & 1 deletion compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,31 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.expr_call(overall_span, constructor, std::slice::from_ref(expr))
}

fn is_guard_if_let(&self, arm: &Arm) -> bool {
let mut if_let = false;
let _guard = arm.guard.as_ref().map(|cond| {
if let ExprKind::Let(..) = cond.kind {
if_let = true;
} else {
if_let = false;
}
});
if_let
}

fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
let pat = self.lower_pat(&arm.pat);
let pat;

// If we are using 'IfLet' guard with 'PatKind::Or' each subpattern gets it's own local
// which in confuses borrow checker about which of those locals are initialzied are which are not.
// This makes it so we only use last 'pat' in 'Or' pattern.
// See issue #88015 for more info.
if let PatKind::Or(pats) = &arm.pat.kind && self.is_guard_if_let(arm){
pat = self.lower_pat(pats.last().unwrap());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this lost parts of the patters in the or pattern? Doesn't this change the meaning of the program?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this is indeed the wrong way to approach this problem, going to close it as I look for a new solution.

}else{
pat = self.lower_pat(&arm.pat);
}

let guard = arm.guard.as_ref().map(|cond| {
if let ExprKind::Let(ref pat, ref scrutinee, span) = cond.kind {
hir::Guard::IfLet(self.arena.alloc(hir::Let {
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/rfc-2294-if-let-guard/run-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ fn main() {
Some(x) if let Foo::Qux(y) = qux(x) => assert_eq!(y, 84),
_ => panic!(),
}
match () {
() | () if let x = 0 => {
x;
},
_ => {}
};
}