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

Don't suggest break through nested items #112024

Merged
merged 1 commit into from
Jun 1, 2023
Merged
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
10 changes: 9 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let found = self.resolve_vars_with_obligations(found);

let in_loop = self.is_loop(id)
|| self.tcx.hir().parent_iter(id).any(|(parent_id, _)| self.is_loop(parent_id));
|| self
.tcx
.hir()
.parent_iter(id)
.take_while(|(_, node)| {
// look at parents until we find the first body owner
node.body_id().is_none()
})
.any(|(parent_id, _)| self.is_loop(parent_id));

let in_local_statement = self.is_local_statement(id)
|| self
Expand Down
55 changes: 55 additions & 0 deletions tests/ui/loops/dont-suggest-break-thru-item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// edition:2021

#![feature(inline_const)]

fn closure() {
loop {
let closure = || {
if true {
Err(1)
//~^ ERROR mismatched types
Comment on lines +9 to +10
Copy link
Member

Choose a reason for hiding this comment

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

Hm, I would expect this to suggest return. (still better than the status quo ig)

}

Ok(())
};
}
}

fn async_block() {
loop {
let fut = async {
if true {
Err(1)
//~^ ERROR mismatched types
}

Ok(())
};
}
}

fn fn_item() {
let _ = loop {
fn foo() -> Result<(), ()> {
if true {
Err(1)
//~^ ERROR mismatched types
}
Err(())
}
};
}

fn const_block() {
let _ = loop {
const {
if true {
Err(1)
//~^ ERROR mismatched types
}
Err(())
};
};
}

fn main() {}
55 changes: 55 additions & 0 deletions tests/ui/loops/dont-suggest-break-thru-item.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
error[E0308]: mismatched types
--> $DIR/dont-suggest-break-thru-item.rs:9:17
|
LL | / if true {
LL | | Err(1)
| | ^^^^^^ expected `()`, found `Result<_, {integer}>`
LL | |
LL | | }
| |_____________- expected this to be `()`
|
= note: expected unit type `()`
found enum `Result<_, {integer}>`

error[E0308]: mismatched types
--> $DIR/dont-suggest-break-thru-item.rs:22:17
|
LL | / if true {
LL | | Err(1)
| | ^^^^^^ expected `()`, found `Result<_, {integer}>`
LL | |
LL | | }
| |_____________- expected this to be `()`
|
= note: expected unit type `()`
found enum `Result<_, {integer}>`

error[E0308]: mismatched types
--> $DIR/dont-suggest-break-thru-item.rs:35:17
|
LL | / if true {
LL | | Err(1)
| | ^^^^^^ expected `()`, found `Result<_, {integer}>`
LL | |
LL | | }
| |_____________- expected this to be `()`
|
= note: expected unit type `()`
found enum `Result<_, {integer}>`

error[E0308]: mismatched types
--> $DIR/dont-suggest-break-thru-item.rs:47:17
|
LL | / if true {
LL | | Err(1)
| | ^^^^^^ expected `()`, found `Result<_, {integer}>`
LL | |
LL | | }
| |_____________- expected this to be `()`
|
= note: expected unit type `()`
found enum `Result<_, {integer}>`

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0308`.