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 skip out of inner const when looking for body for suggestion #125469

Merged
merged 1 commit into from
May 27, 2024
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
9 changes: 3 additions & 6 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1871,11 +1871,8 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
// If this is due to a block, then maybe we forgot a `return`/`break`.
if due_to_block
&& let Some(expr) = expression
&& let Some((parent_fn_decl, parent_id)) = fcx
.tcx
.hir()
.parent_iter(block_or_return_id)
.find_map(|(_, node)| Some((node.fn_decl()?, node.associated_body()?.0)))
Copy link
Member Author

Choose a reason for hiding this comment

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

Specifically this will return None if node is an item with a body, but it has no fn_decl (e.g. consts). That means that this doesn't return the parent of the body, but possibly some other ancestor, which will cause astconv to ICE.

&& let Some(parent_fn_decl) =
fcx.tcx.hir().fn_decl_by_hir_id(fcx.tcx.local_def_id_to_hir_id(fcx.body_id))
{
fcx.suggest_missing_break_or_return_expr(
&mut err,
Expand All @@ -1884,7 +1881,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
expected,
found,
block_or_return_id,
parent_id,
fcx.body_id,
);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/ui/return/dont-suggest-through-inner-const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fn f() -> usize {
//~^ ERROR mismatched types
const FIELD: usize = loop {
0
//~^ ERROR mismatched types
};
}

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/return/dont-suggest-through-inner-const.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0308]: mismatched types
--> $DIR/dont-suggest-through-inner-const.rs:4:9
|
LL | 0
| ^ expected `()`, found integer

error[E0308]: mismatched types
--> $DIR/dont-suggest-through-inner-const.rs:1:17
|
LL | const fn f() -> usize {
| - ^^^^^ expected `usize`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression

error: aborting due to 2 previous errors

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