Skip to content

Commit b65b2b6

Browse files
Rollup merge of #125469 - compiler-errors:dont-skip-inner-const-body, r=cjgillot
Don't skip out of inner const when looking for body for suggestion Self-explanatory title, I'll point out the important logic in an inline comment. Fixes #125370
2 parents 09e7592 + c58b7c9 commit b65b2b6

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1871,11 +1871,8 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
18711871
// If this is due to a block, then maybe we forgot a `return`/`break`.
18721872
if due_to_block
18731873
&& let Some(expr) = expression
1874-
&& let Some((parent_fn_decl, parent_id)) = fcx
1875-
.tcx
1876-
.hir()
1877-
.parent_iter(block_or_return_id)
1878-
.find_map(|(_, node)| Some((node.fn_decl()?, node.associated_body()?.0)))
1874+
&& let Some(parent_fn_decl) =
1875+
fcx.tcx.hir().fn_decl_by_hir_id(fcx.tcx.local_def_id_to_hir_id(fcx.body_id))
18791876
{
18801877
fcx.suggest_missing_break_or_return_expr(
18811878
&mut err,
@@ -1884,7 +1881,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
18841881
expected,
18851882
found,
18861883
block_or_return_id,
1887-
parent_id,
1884+
fcx.body_id,
18881885
);
18891886
}
18901887

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fn f() -> usize {
2+
//~^ ERROR mismatched types
3+
const FIELD: usize = loop {
4+
0
5+
//~^ ERROR mismatched types
6+
};
7+
}
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/dont-suggest-through-inner-const.rs:4:9
3+
|
4+
LL | 0
5+
| ^ expected `()`, found integer
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/dont-suggest-through-inner-const.rs:1:17
9+
|
10+
LL | const fn f() -> usize {
11+
| - ^^^^^ expected `usize`, found `()`
12+
| |
13+
| implicitly returns `()` as its body has no tail or `return` expression
14+
15+
error: aborting due to 2 previous errors
16+
17+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)