From 57f500512bec1e29dbae876499899b579273aa2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Fri, 22 Mar 2024 19:06:46 +0100 Subject: [PATCH] add test for stack overflow with recursive type #98842 Fixes #98842 --- .../sized/stack-overflow-trait-infer-98842.rs | 15 +++++++++++ .../stack-overflow-trait-infer-98842.stderr | 25 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/ui/sized/stack-overflow-trait-infer-98842.rs create mode 100644 tests/ui/sized/stack-overflow-trait-infer-98842.stderr diff --git a/tests/ui/sized/stack-overflow-trait-infer-98842.rs b/tests/ui/sized/stack-overflow-trait-infer-98842.rs new file mode 100644 index 0000000000000..2bba0777be49f --- /dev/null +++ b/tests/ui/sized/stack-overflow-trait-infer-98842.rs @@ -0,0 +1,15 @@ +// #98842 stack overflow in trait inference +//@ check-fail +//@ edition:2021 +//~^^^ ERROR cycle detected when computing layout of `Foo` + +// If the inner `Foo` is named through an associated type, +// the "infinite size" error does not occur. +struct Foo(<&'static Foo as ::core::ops::Deref>::Target); +// But Rust will be unable to know whether `Foo` is sized or not, +// and it will infinitely recurse somewhere trying to figure out the +// size of this pointer (is my guess): +const _: *const Foo = 0 as _; +//~^ ERROR it is undefined behavior to use this value + +pub fn main() {} diff --git a/tests/ui/sized/stack-overflow-trait-infer-98842.stderr b/tests/ui/sized/stack-overflow-trait-infer-98842.stderr new file mode 100644 index 0000000000000..42b2718e33246 --- /dev/null +++ b/tests/ui/sized/stack-overflow-trait-infer-98842.stderr @@ -0,0 +1,25 @@ +error[E0391]: cycle detected when computing layout of `Foo` + | + = note: ...which requires computing layout of `<&'static Foo as core::ops::deref::Deref>::Target`... + = note: ...which again requires computing layout of `Foo`, completing the cycle +note: cycle used when const-evaluating + checking `_` + --> $DIR/stack-overflow-trait-infer-98842.rs:12:1 + | +LL | const _: *const Foo = 0 as _; + | ^^^^^^^^^^^^^^^^^^^ + = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information + +error[E0080]: it is undefined behavior to use this value + --> $DIR/stack-overflow-trait-infer-98842.rs:12:1 + | +LL | const _: *const Foo = 0 as _; + | ^^^^^^^^^^^^^^^^^^^ a cycle occurred during layout computation + | + = note: the raw bytes of the constant (size: 8, align: 8) { + 00 00 00 00 00 00 00 00 │ ........ + } + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0080, E0391. +For more information about an error, try `rustc --explain E0080`.