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

if clauses following an if let are compiled to match guards #41272

Closed
nox opened this issue Apr 13, 2017 · 9 comments · Fixed by #41316
Closed

if clauses following an if let are compiled to match guards #41272

nox opened this issue Apr 13, 2017 · 9 comments · Fixed by #41316
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nox
Copy link
Contributor

nox commented Apr 13, 2017

https://is.gd/2ZnMND

is this code:

struct Foo;

impl Foo {
    fn bar(&mut self) -> bool { true }
}

fn error(foo: &mut Foo) {
    if let Some(_) = Some(true) {
    } else if foo.bar() {}
}

fn ok(foo: &mut Foo) {
    if let Some(_) = Some(true) {
    } else {
        if foo.bar() {}
    }
}

fn main() {}

yielding:

rustc 1.16.0 (30cf806ef 2017-03-10)
error[E0301]: cannot mutably borrow in a pattern guard
 --> <anon>:9:15
  |
9 |     } else if foo.bar() {}
  |               ^^^ borrowed mutably in pattern guard
@eddyb eddyb added I-nominated T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 13, 2017
@eddyb
Copy link
Member

eddyb commented Apr 13, 2017

Where this happens. I don't know why we do this, the "else" part of "if" or "if-let" is an arbitrary expression:

if let pat = expr1 {...} else expr2
// can be lowered to
match expr1 {
    pat => {...}
    _ => expr2
}

This seems simpler, less error prone and it won't hit arbitrary limitations specific to match guards.

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Apr 13, 2017

The RFC states:

Optionally, one or more else if (not else if let) blocks can be placed in the same match using pattern guards on _. This could be done to simplify the code when pretty-printing the expansion result. Otherwise, this is an unnecessary transformation.

This doesn't feel like a compelling reason, particularly since the desugaring is not otherwise user visible. I would call this a bug.

@nikomatsakis
Copy link
Contributor

triage: P-medium

@rust-highfive rust-highfive added P-medium Medium priority and removed I-nominated labels Apr 13, 2017
@nikomatsakis nikomatsakis added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels Apr 13, 2017
@nikomatsakis
Copy link
Contributor

This is a great mentoring bug! I'm going to write up some very quick pointers for now. Please reach out for more details. In short, the code that needs to be changed is in the "HIR lowering", which converts from the AST (what the user wrote) to the compiler's internal HIR (also an abstract syntax tree, but somewhat desugared). The code in question is in lowering.rs, specifically right around here. There is also some more logic. You'll want to change how it is desugaring along the lines that @eddyb suggested in this comment.

@cuviper
Copy link
Member

cuviper commented Apr 14, 2017

I'm interested in taking this bug.

@ghost
Copy link

ghost commented Apr 14, 2017

@nikomatsakis @eddyb @nox Hello I'm new to open source and Rust project. I wanted to contribute to the project. May I work on this bug?

@eddyb
Copy link
Member

eddyb commented Apr 14, 2017

@suchithjn225 Sure thing! Let us know how far you get with #41272 (comment). We're also on IRC.

@cuviper
Copy link
Member

cuviper commented Apr 14, 2017

@suchithjn225 I'll yield to you -- good luck!

@ghost
Copy link

ghost commented Apr 14, 2017

struct Foo;

impl Foo {
    fn bar(&mut self) -> bool { true }
}

fn error(foo: &mut Foo) {
    if let Some(_) = Some(true) {
    } else if foo.bar() {}
}

fn desugar_test(foo: &mut Foo) {
	let x = Some(true);
	match x {
		Some(_) => {},
		_ => {
			if foo.bar() { }
		}
	}
}

fn desugar_error(foo: &mut Foo) {
	let x = Some(true);
	match x {
		Some(_) => {},
		_ if foo.bar() => {},
		_ => {}
	}
}

fn ok(foo: &mut Foo) {
    if let Some(_) = Some(true) {
    } else {
        if foo.bar() {}
    }
}

fn main() {}

If IfLet expression is desugared to the one shown in desugar_test() then it works as suggested by @eddyb. I have also reproduced the bug after desugaring the expression.

bors added a commit that referenced this issue Apr 15, 2017
Do not desugar if-let-else to match arm guards

Fixes #41272
Changed the desugaring code

**Before**
```rust
match <sub_expr> {
     <pat> => <body>,
     [_ if <else_opt_if_cond> => <else_opt_if_body>,]
     _ => [<else_opt> | ()]
}
```

**After**
```rust
match <sub_expr> {
    <pat> => <body>,
     _       => [<else_opt> | ()]
}
```
With this fix, it doesn't cause E0301
jieyouxu added a commit to jieyouxu/rust that referenced this issue Feb 5, 2025
Rename and Move some UI tests to more suitable subdirs

## Affected Tests
- tests/ui/issues/issue-48838.rs -> tests/ui/enum/closure-in-enum-issue-48838.rs rust-lang#48838
- tests/ui/issues/issue-40350.rs -> tests/ui/enum/enum-inside-enum-issue-40350.rs rust-lang#40350
- tests/ui/issues/issue-41272.rs -> tests/ui/expr/if/if-let-no-match-guards-issue-41272.rs rust-lang#41272
- tests/ui/issues/issue-40408.rs -> tests/ui/lexer/floating-point-0e10-issue-40408.rs rust-lang#40408
- tests/ui/issues/issue-40136.rs -> tests/ui/macros/const-expr-invocations-issue-40136.rs rust-lang#40136
- tests/ui/issues/issue-40845.rs -> tests/ui/macros/macros-in-trait-positions-issue-40845.rs rust-lang#40845
- tests/ui/issues/issue-41213.rs -> tests/ui/match/enum-and-break-in-match-issue-41213.rs rust-lang#41213
- tests/ui/issues/issue-40782.rs -> tests/ui/suggestions/for-loop-missing-in.rs rust-lang#40782
- tests/ui/issues/issue-40827.rs -> tests/ui/trait-bounds/deep-level-Send-bound-check-issue-40827.rs rust-lang#40827
- tests/ui/issues/issue-40610.rs -> tests/ui/typeck/coercion-check-for-addition-issue-40610.rs rust-lang#40610
- tests/ui/issues/issue-40883.rs -> tests/ui/codegen/StackColoring-not-blowup-stack-issue-40883.rs rust-lang#40883
- tests/ui/issues/issue-40861.rs -> tests/ui/typeck/coercion-check-for-indexing-expression-issue-40861.rs rust-lang#40861
- tests/ui/issues/issue-41139.rs -> tests/ui/typeck/unsized-rvalue-issue-41139.rs rust-lang#41139
- tests/ui/issues/issue-40749.rs -> tests/ui/wf/range-expr-root-of-constant-issue-40749.rs rust-lang#40749
- tests/ui/issues/issue-40235.rs -> tests/ui/while/while-let-scope-issue-40235.rs rust-lang#40235
jieyouxu added a commit to jieyouxu/rust that referenced this issue Feb 5, 2025
Rename and Move some UI tests to more suitable subdirs

## Affected Tests
- tests/ui/issues/issue-48838.rs -> tests/ui/enum/closure-in-enum-issue-48838.rs rust-lang#48838
- tests/ui/issues/issue-40350.rs -> tests/ui/enum/enum-inside-enum-issue-40350.rs rust-lang#40350
- tests/ui/issues/issue-41272.rs -> tests/ui/expr/if/if-let-no-match-guards-issue-41272.rs rust-lang#41272
- tests/ui/issues/issue-40408.rs -> tests/ui/lexer/floating-point-0e10-issue-40408.rs rust-lang#40408
- tests/ui/issues/issue-40136.rs -> tests/ui/macros/const-expr-invocations-issue-40136.rs rust-lang#40136
- tests/ui/issues/issue-40845.rs -> tests/ui/macros/macros-in-trait-positions-issue-40845.rs rust-lang#40845
- tests/ui/issues/issue-41213.rs -> tests/ui/match/enum-and-break-in-match-issue-41213.rs rust-lang#41213
- tests/ui/issues/issue-40782.rs -> tests/ui/suggestions/for-loop-missing-in.rs rust-lang#40782
- tests/ui/issues/issue-40827.rs -> tests/ui/trait-bounds/deep-level-Send-bound-check-issue-40827.rs rust-lang#40827
- tests/ui/issues/issue-40610.rs -> tests/ui/typeck/coercion-check-for-addition-issue-40610.rs rust-lang#40610
- tests/ui/issues/issue-40883.rs -> tests/ui/codegen/StackColoring-not-blowup-stack-issue-40883.rs rust-lang#40883
- tests/ui/issues/issue-40861.rs -> tests/ui/typeck/coercion-check-for-indexing-expression-issue-40861.rs rust-lang#40861
- tests/ui/issues/issue-41139.rs -> tests/ui/typeck/unsized-rvalue-issue-41139.rs rust-lang#41139
- tests/ui/issues/issue-40749.rs -> tests/ui/wf/range-expr-root-of-constant-issue-40749.rs rust-lang#40749
- tests/ui/issues/issue-40235.rs -> tests/ui/while/while-let-scope-issue-40235.rs rust-lang#40235
jieyouxu added a commit to jieyouxu/rust that referenced this issue Feb 5, 2025
Rename and Move some UI tests to more suitable subdirs

## Affected Tests
- tests/ui/issues/issue-48838.rs -> tests/ui/enum/closure-in-enum-issue-48838.rs rust-lang#48838
- tests/ui/issues/issue-40350.rs -> tests/ui/enum/enum-inside-enum-issue-40350.rs rust-lang#40350
- tests/ui/issues/issue-41272.rs -> tests/ui/expr/if/if-let-no-match-guards-issue-41272.rs rust-lang#41272
- tests/ui/issues/issue-40408.rs -> tests/ui/lexer/floating-point-0e10-issue-40408.rs rust-lang#40408
- tests/ui/issues/issue-40136.rs -> tests/ui/macros/const-expr-invocations-issue-40136.rs rust-lang#40136
- tests/ui/issues/issue-40845.rs -> tests/ui/macros/macros-in-trait-positions-issue-40845.rs rust-lang#40845
- tests/ui/issues/issue-41213.rs -> tests/ui/match/enum-and-break-in-match-issue-41213.rs rust-lang#41213
- tests/ui/issues/issue-40782.rs -> tests/ui/suggestions/for-loop-missing-in.rs rust-lang#40782
- tests/ui/issues/issue-40827.rs -> tests/ui/trait-bounds/deep-level-Send-bound-check-issue-40827.rs rust-lang#40827
- tests/ui/issues/issue-40610.rs -> tests/ui/typeck/coercion-check-for-addition-issue-40610.rs rust-lang#40610
- tests/ui/issues/issue-40883.rs -> tests/ui/codegen/StackColoring-not-blowup-stack-issue-40883.rs rust-lang#40883
- tests/ui/issues/issue-40861.rs -> tests/ui/typeck/coercion-check-for-indexing-expression-issue-40861.rs rust-lang#40861
- tests/ui/issues/issue-41139.rs -> tests/ui/typeck/unsized-rvalue-issue-41139.rs rust-lang#41139
- tests/ui/issues/issue-40749.rs -> tests/ui/wf/range-expr-root-of-constant-issue-40749.rs rust-lang#40749
- tests/ui/issues/issue-40235.rs -> tests/ui/while/while-let-scope-issue-40235.rs rust-lang#40235
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 6, 2025
Rollup merge of rust-lang#136536 - DuskyElf:master, r=jieyouxu

Rename and Move some UI tests to more suitable subdirs

## Affected Tests
- tests/ui/issues/issue-48838.rs -> tests/ui/enum/closure-in-enum-issue-48838.rs rust-lang#48838
- tests/ui/issues/issue-40350.rs -> tests/ui/enum/enum-inside-enum-issue-40350.rs rust-lang#40350
- tests/ui/issues/issue-41272.rs -> tests/ui/expr/if/if-let-no-match-guards-issue-41272.rs rust-lang#41272
- tests/ui/issues/issue-40408.rs -> tests/ui/lexer/floating-point-0e10-issue-40408.rs rust-lang#40408
- tests/ui/issues/issue-40136.rs -> tests/ui/macros/const-expr-invocations-issue-40136.rs rust-lang#40136
- tests/ui/issues/issue-40845.rs -> tests/ui/macros/macros-in-trait-positions-issue-40845.rs rust-lang#40845
- tests/ui/issues/issue-41213.rs -> tests/ui/match/enum-and-break-in-match-issue-41213.rs rust-lang#41213
- tests/ui/issues/issue-40782.rs -> tests/ui/suggestions/for-loop-missing-in.rs rust-lang#40782
- tests/ui/issues/issue-40827.rs -> tests/ui/trait-bounds/deep-level-Send-bound-check-issue-40827.rs rust-lang#40827
- tests/ui/issues/issue-40610.rs -> tests/ui/typeck/coercion-check-for-addition-issue-40610.rs rust-lang#40610
- tests/ui/issues/issue-40883.rs -> tests/ui/codegen/StackColoring-not-blowup-stack-issue-40883.rs rust-lang#40883
- tests/ui/issues/issue-40861.rs -> tests/ui/typeck/coercion-check-for-indexing-expression-issue-40861.rs rust-lang#40861
- tests/ui/issues/issue-41139.rs -> tests/ui/typeck/unsized-rvalue-issue-41139.rs rust-lang#41139
- tests/ui/issues/issue-40749.rs -> tests/ui/wf/range-expr-root-of-constant-issue-40749.rs rust-lang#40749
- tests/ui/issues/issue-40235.rs -> tests/ui/while/while-let-scope-issue-40235.rs rust-lang#40235
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants