rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.87k stars 12.52k forks source link

ice: stack overflow #116321

Open matthiaskrgr opened 11 months ago

matthiaskrgr commented 11 months ago

Code

from #93022

#![recursion_limit = "256000"]

struct Z;
struct S<T>(T);

trait Add<Rhs> {
    type Sum;
}

type SumOf<N, M> = <N as Add<M>>::Sum;

impl<N> Add<N> for Z {
    type Sum = N;
}

impl<N, M> Add<M> for S<N>
where
    N: Add<S<M>>,
{
    type Sum = SumOf<N, S<M>>;
}

type One = S<Z>;
type Two = SumOf<One, One>;
type Three = SumOf<One, Two>;
type Five = SumOf<Two, Three>;
type Ten = SumOf<Five, Five>;
type TwentyFive = SumOf<Five, SumOf<Ten, Ten>>;
type Fifty = SumOf<TwentyFive, TwentyFive>;
type OneHundred = SumOf<Fifty, Fifty>;

type x2 = SumOf<OneHundred, OneHundred>;
type x4 = SumOf<x2, x2>;
type x8 = SumOf<x4, x4>;
type x16 = SumOf<x8, x8>;

//

trait NumericValue {
    const VALUE: usize;
}

impl NumericValue for Z {
    const VALUE: usize = 0;
}

impl<N> NumericValue for S<N>
where
    N: NumericValue,
{
    const VALUE: usize = N::VALUE + 1;
}

const value: usize = <x16 as NumericValue>::VALUE;

fn main() {}

Meta

rustc --version --verbose:

rustc 1.75.0-nightly (6c29b4543 2023-10-01)
binary: rustc
commit-hash: 6c29b45439a9523e2f2cdf99e40cd5443db9677d
commit-date: 2023-10-01
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.2

Error output

warning: type `x2` should have an upper camel case name
  --> treereduce.out:32:6
   |
32 | type x2 = SumOf<OneHundred, OneHundred>;
   |      ^^ help: convert the identifier to upper camel case (notice the capitalization): `X2`
   |
   = note: `#[warn(non_camel_case_types)]` on by default

warning: type `x4` should have an upper camel case name
  --> treereduce.out:33:6
   |
33 | type x4 = SumOf<x2, x2>;
   |      ^^ help: convert the identifier to upper camel case (notice the capitalization): `X4`

warning: type `x8` should have an upper camel case name
  --> treereduce.out:34:6
   |
34 | type x8 = SumOf<x4, x4>;
   |      ^^ help: convert the identifier to upper camel case (notice the capitalization): `X8`

warning: type `x16` should have an upper camel case name
  --> treereduce.out:35:6
   |
35 | type x16 = SumOf<x8, x8>;
   |      ^^^ help: convert the identifier to upper camel case (notice the capitalization): `X16`

warning: type alias `One` is never used
  --> treereduce.out:23:6
   |
23 | type One = S<Z>;
   |      ^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: type alias `Two` is never used
  --> treereduce.out:24:6
   |
24 | type Two = SumOf<One, One>;
   |      ^^^

warning: type alias `Three` is never used
  --> treereduce.out:25:6
   |
25 | type Three = SumOf<One, Two>;
   |      ^^^^^

warning: type alias `Five` is never used
  --> treereduce.out:26:6
   |
26 | type Five = SumOf<Two, Three>;
   |      ^^^^

warning: type alias `Ten` is never used
  --> treereduce.out:27:6
   |
27 | type Ten = SumOf<Five, Five>;
   |      ^^^

warning: type alias `TwentyFive` is never used
  --> treereduce.out:28:6
   |
28 | type TwentyFive = SumOf<Five, SumOf<Ten, Ten>>;
   |      ^^^^^^^^^^

warning: type alias `Fifty` is never used
  --> treereduce.out:29:6
   |
29 | type Fifty = SumOf<TwentyFive, TwentyFive>;
   |      ^^^^^

warning: type alias `OneHundred` is never used
  --> treereduce.out:30:6
   |
30 | type OneHundred = SumOf<Fifty, Fifty>;
   |      ^^^^^^^^^^

warning: type alias `x2` is never used
  --> treereduce.out:32:6
   |
32 | type x2 = SumOf<OneHundred, OneHundred>;
   |      ^^

warning: type alias `x4` is never used
  --> treereduce.out:33:6
   |
33 | type x4 = SumOf<x2, x2>;
   |      ^^

warning: type alias `x8` is never used
  --> treereduce.out:34:6
   |
34 | type x8 = SumOf<x4, x4>;
   |      ^^

warning: type alias `x16` is never used
  --> treereduce.out:35:6
   |
35 | type x16 = SumOf<x8, x8>;
   |      ^^^

warning: constant `value` is never used
  --> treereduce.out:56:7
   |
56 | const value: usize = <x16 as NumericValue>::VALUE;
   |       ^^^^^

warning: constant `value` should have an upper case name
  --> treereduce.out:56:7
   |
56 | const value: usize = <x16 as NumericValue>::VALUE;
   |       ^^^^^ help: convert the identifier to upper case: `VALUE`
   |
   = note: `#[warn(non_upper_case_globals)]` on by default
Backtrace

``` error: rustc interrupted by SIGSEGV, printing backtrace /home/matthias/.rustup/toolchains/master/bin/../lib/librustc_driver-d069aac035ada932.so(+0x2c2ba26)[0x7f40e5c2ba26] /usr/lib/libc.so.6(+0x3e710)[0x7f40e2c3e710] /home/matthias/.rustup/toolchains/master/bin/../lib/librustc_driver-d069aac035ada932.so(_RNvMs_NtNtCscV0DyRuadRV_12rustc_middle2ty7contextNtB4_13CtxtInterners9intern_ty+0x11)[0x7f40e3e87631] /home/matthias/.rustup/toolchains/master/bin/../lib/librustc_driver-d069aac035ada932.so(+0xe7a797)[0x7f40e3e7a797] ### cycle encountered after 4 frames with period 6 /home/matthias/.rustup/toolchains/master/bin/../lib/librustc_driver-d069aac035ada932.so(+0xe891f6)[0x7f40e3e891f6] /home/matthias/.rustup/toolchains/master/bin/../lib/librustc_driver-d069aac035ada932.so(+0xe7b8a9)[0x7f40e3e7b8a9] /home/matthias/.rustup/toolchains/master/bin/../lib/librustc_driver-d069aac035ada932.so(+0xe891f6)[0x7f40e3e891f6] /home/matthias/.rustup/toolchains/master/bin/../lib/librustc_driver-d069aac035ada932.so(+0xe7b8a9)[0x7f40e3e7b8a9] /home/matthias/.rustup/toolchains/master/bin/../lib/librustc_driver-d069aac035ada932.so(+0xe891f6)[0x7f40e3e891f6] /home/matthias/.rustup/toolchains/master/bin/../lib/librustc_driver-d069aac035ada932.so(+0xe7b8a9)[0x7f40e3e7b8a9] ### recursed 42 times note: rustc unexpectedly overflowed its stack! this is a bug note: maximum backtrace depth reached, frames may have been lost note: we would appreciate a report at https://github.com/rust-lang/rust [1] 3482749 segmentation fault ~/.rustup/toolchains/master/bin/rustc treereduce.out ```

matthiaskrgr commented 11 months ago

This crashes since 871407a0341262d2a86703ca43b449d35fa5f236 / #115554 :thinking: cc @Kobzol

maybe another miscompilation?

Kobzol commented 11 months ago

This code required at least 8 MiB of stack size before, now it requires more than 8 MiB, but less than 16 MiB (RUST_MIN_STACK=16777216 cargo +871407a0341262d2a86703ca43b449d35fa5f236 build works). I think that this is just a result of different optimization/inlining decisions stemming from the 1 CGU change.

matthiaskrgr commented 11 months ago

This also causes some test code to no longer compile, rustc tests/ui/issues/issue-29466.rs -Cdebuginfo=2 will stack overflow now

matthiaskrgr commented 11 months ago

I'm a bit worried that this will cause problems with crates that use large amounts of auto-generated code. :/

Kobzol commented 11 months ago

I wonder what to do about that though. I don't think that stack usage is a part of the Rust stability guarantees (?). And whether we want or not, it will eventually change organically, and make some programs stop compiling.

I think that this is actually showing us more bugs in rustc, where manual stack growth is missing, and these should be fixed.

We could also perhaps add some helper message to the stack overflow to guide people to use the env. var to increase stack size.