diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 467962b39bb..81de70c03d4 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -701,6 +701,8 @@ pub fn get_return_block(self, id: HirId) -> Option { // expressions. ignore_tail = true; } + + let mut prev_hir_id = None; while let Some((hir_id, node)) = iter.next() { if let (Some((_, next_node)), false) = (iter.peek(), ignore_tail) { match next_node { @@ -715,7 +717,14 @@ pub fn get_return_block(self, id: HirId) -> Option { | Node::ForeignItem(_) | Node::TraitItem(_) | Node::Expr(Expr { kind: ExprKind::Closure { .. }, .. }) - | Node::ImplItem(_) => return Some(hir_id), + | Node::ImplItem(_) + // The input node `id` must be enclosed in the method's body as opposed + // to some other place such as its return type (fixes #114918). + // We verify that indirectly by checking that the previous node is the + // current node's body + if node.body_id().map(|b| b.hir_id) == prev_hir_id => { + return Some(hir_id) + } // Ignore `return`s on the first iteration Node::Expr(Expr { kind: ExprKind::Loop(..) | ExprKind::Ret(..), .. }) | Node::Local(_) => { @@ -723,6 +732,8 @@ pub fn get_return_block(self, id: HirId) -> Option { } _ => {} } + + prev_hir_id = Some(hir_id); } None } diff --git a/tests/ui/typeck/issue-114918/const-in-fn-return-type.rs b/tests/ui/typeck/issue-114918/const-in-fn-return-type.rs new file mode 100644 index 00000000000..d939633290e --- /dev/null +++ b/tests/ui/typeck/issue-114918/const-in-fn-return-type.rs @@ -0,0 +1,10 @@ +// Regression test for #114918 +// Test that a const generic enclosed in a block within a return type +// produces a type mismatch error instead of triggering a const eval cycle + +#[allow(unused_braces)] +fn func() -> [u8; { () } ] { //~ ERROR mismatched types + loop {} +} + +fn main() {} diff --git a/tests/ui/typeck/issue-114918/const-in-fn-return-type.stderr b/tests/ui/typeck/issue-114918/const-in-fn-return-type.stderr new file mode 100644 index 00000000000..88ed96e148c --- /dev/null +++ b/tests/ui/typeck/issue-114918/const-in-fn-return-type.stderr @@ -0,0 +1,9 @@ +error[E0308]: mismatched types + --> $DIR/const-in-fn-return-type.rs:6:21 + | +LL | fn func() -> [u8; { () } ] { + | ^^ expected `usize`, found `()` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/typeck/issue-114918/const-in-impl-fn-return-type.rs b/tests/ui/typeck/issue-114918/const-in-impl-fn-return-type.rs new file mode 100644 index 00000000000..a1b9a7eba4d --- /dev/null +++ b/tests/ui/typeck/issue-114918/const-in-impl-fn-return-type.rs @@ -0,0 +1,20 @@ +// Regression test for #114918 +// Test that a const generic enclosed in a block within the return type +// of an impl fn produces a type mismatch error instead of triggering +// a const eval cycle + + +trait Trait { + fn func() -> [ (); N ]; +} + +struct S {} + +#[allow(unused_braces)] +impl Trait for S { + fn func() -> [ (); { () }] { //~ ERROR mismatched types + N + } +} + +fn main() {} diff --git a/tests/ui/typeck/issue-114918/const-in-impl-fn-return-type.stderr b/tests/ui/typeck/issue-114918/const-in-impl-fn-return-type.stderr new file mode 100644 index 00000000000..9843651b1e6 --- /dev/null +++ b/tests/ui/typeck/issue-114918/const-in-impl-fn-return-type.stderr @@ -0,0 +1,9 @@ +error[E0308]: mismatched types + --> $DIR/const-in-impl-fn-return-type.rs:15:40 + | +LL | fn func() -> [ (); { () }] { + | ^^ expected `usize`, found `()` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/typeck/issue-114918/const-in-struct-type-arg.rs b/tests/ui/typeck/issue-114918/const-in-struct-type-arg.rs new file mode 100644 index 00000000000..9eee4ab3d4c --- /dev/null +++ b/tests/ui/typeck/issue-114918/const-in-struct-type-arg.rs @@ -0,0 +1,12 @@ +// Regression test for #114918 +// Test that a const generic enclosed in a block in a struct's type arg +// produces a type mismatch error instead of triggering a const eval cycle + +#[allow(unused_braces)] +struct S { + arr: [u8; N] +} + +fn main() { + let s = S::<{ () }> { arr: [5, 6, 7]}; //~ ERROR mismatched types +} diff --git a/tests/ui/typeck/issue-114918/const-in-struct-type-arg.stderr b/tests/ui/typeck/issue-114918/const-in-struct-type-arg.stderr new file mode 100644 index 00000000000..3307e76d957 --- /dev/null +++ b/tests/ui/typeck/issue-114918/const-in-struct-type-arg.stderr @@ -0,0 +1,9 @@ +error[E0308]: mismatched types + --> $DIR/const-in-struct-type-arg.rs:11:19 + | +LL | let s = S::<{ () }> { arr: [5, 6, 7]}; + | ^^ expected `usize`, found `()` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/typeck/issue-114918/const-in-trait-fn-return-type.rs b/tests/ui/typeck/issue-114918/const-in-trait-fn-return-type.rs new file mode 100644 index 00000000000..8e2eec34911 --- /dev/null +++ b/tests/ui/typeck/issue-114918/const-in-trait-fn-return-type.rs @@ -0,0 +1,13 @@ +// Regression test for #114918 +// Test that a const generic enclosed in a block within the return type +// of a trait method produces a type mismatch error instead of triggering +// a const eval cycle + +#[allow(unused_braces)] +trait Trait { + fn func() -> [ (); { () }] { //~ ERROR mismatched types + N + } +} + +fn main() {} diff --git a/tests/ui/typeck/issue-114918/const-in-trait-fn-return-type.stderr b/tests/ui/typeck/issue-114918/const-in-trait-fn-return-type.stderr new file mode 100644 index 00000000000..6bc0de77a62 --- /dev/null +++ b/tests/ui/typeck/issue-114918/const-in-trait-fn-return-type.stderr @@ -0,0 +1,9 @@ +error[E0308]: mismatched types + --> $DIR/const-in-trait-fn-return-type.rs:8:40 + | +LL | fn func() -> [ (); { () }] { + | ^^ expected `usize`, found `()` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`.