Skip to content

Commit

Permalink
add test for stack overflow with recursive type rust-lang#98842
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaskrgr authored and RenjiSann committed Mar 25, 2024
1 parent 8d794a9 commit 659f916
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/ui/sized/stack-overflow-trait-infer-98842.rs
Original file line number Diff line number Diff line change
@@ -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() {}
25 changes: 25 additions & 0 deletions tests/ui/sized/stack-overflow-trait-infer-98842.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 659f916

Please sign in to comment.