Skip to content

checking for structural recursion can cause a stack overflow #84611

Closed
@lcnr

Description

@lcnr
Contributor
struct Foo<T> {
    x: Foo<[T; 1]>,
    y: T,
}

struct Bar {
    x: Foo<Bar>,
}

has the following output:

error[E0072]: recursive type `Foo` has infinite size
 --> src/lib.rs:1:1
  |
1 | struct Foo<T> {
  | ^^^^^^^^^^^^^ recursive type has infinite size
2 |     x: Foo<[T; 1]>,
  |        ----------- recursive without indirection
  |
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to make `Foo` representable
  |
2 |     x: Box<Foo<[T; 1]>>,
  |        ^^^^           ^


thread 'rustc' has overflowed its stack
fatal runtime error: stack overflow

the stack overflow should not happen ^^

Activity

lcnr

lcnr commented on Apr 27, 2021

@lcnr
ContributorAuthor

the recursion happens in

// We also need to know whether the first item contains other types
// that are structurally recursive. If we don't catch this case, we
// will recurse infinitely for some inputs.
//
// It is important that we DO take generic parameters into account
// here, so that code like this is considered SelfRecursive, not
// ContainsRecursive:
//
// struct Foo { Option<Option<Foo>> }
for &seen_type in iter {
if ty::TyS::same_type(ty, seen_type) {
debug!("ContainsRecursive: {:?} contains {:?}", seen_type, ty);
return Representability::ContainsRecursive;
}
}
}

Because Foo is not trivially self recursive this check is never hit and we keep looking into Foo<[Bar; 1]>, Foo<[[Bar; 1]; 1]> and so on.

added
I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.
on Apr 27, 2021
lcnr

lcnr commented on Apr 27, 2021

@lcnr
ContributorAuthor
added a commit that references this issue on May 10, 2021

Rollup merge of rust-lang#85012 - FabianWolff:struct-rec, r=davidtwco

9243c1e
added a commit that references this issue on May 11, 2021

Auto merge of rust-lang#85012 - FabianWolff:struct-rec, r=davidtwco

d4d129d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @jonas-schievink@lcnr

      Issue actions

        checking for structural recursion can cause a stack overflow · Issue #84611 · rust-lang/rust