Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal compiler error when using Self in type definitions. #56199

Closed
caiopsouza opened this issue Nov 24, 2018 · 10 comments
Closed

Internal compiler error when using Self in type definitions. #56199

caiopsouza opened this issue Nov 24, 2018 · 10 comments
Labels
A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-type-system Area: Type system I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@caiopsouza
Copy link

As I commented is the issue #51994 a variant of what is not being stabilized gave me an internal compiler error.

Code:

#![feature(self_struct_ctor)]
#![allow(dead_code)]

enum Foo {}

impl Foo {
    fn bar() {
        Self(1u8);
    }
}

fn main() {}

Compiler output:

C:/Users/Caio/.cargo/bin/cargo.exe run --package nespera --bin nespera
   Compiling nespera v0.1.0 (C:\Users\Caio\projects\nespera)
error: internal compiler error: cat_expr Errd
 --> src\main.rs:7:14
  |
7 |       fn bar() {
  |  ______________^
8 | |         Self(1u8);
9 | |     }
  | |_____^

error: internal compiler error: cat_expr Errd
 --> src\main.rs:8:9
  |
8 |         Self(1u8);
  |         ^^^^^^^^^

error: internal compiler error: cat_expr Errd
 --> src\main.rs:8:9
  |
8 |         Self(1u8);
  |         ^^^^

error: internal compiler error: QualifyAndPromoteConstants: Mir had errors
 --> src\main.rs:7:5
  |
7 | /     fn bar() {
8 | |         Self(1u8);
9 | |     }
  | |_____^

error: internal compiler error: broken MIR in DefId(0/0:5 ~ nespera[1ed1]::{{impl}}[0]::bar[0]) ("return type"): bad type [type error]
 --> src\main.rs:7:5
  |
7 | /     fn bar() {
8 | |         Self(1u8);
9 | |     }
  | |_____^

error: internal compiler error: broken MIR in DefId(0/0:5 ~ nespera[1ed1]::{{impl}}[0]::bar[0]) (LocalDecl { mutability: Mut, is_user_variable: None, internal: false, is_block_tail: None, ty: [type error], user_ty: UserTypeProjections { contents: [] }, name: None, source_info: SourceInfo { span: src\main.rs:7:5: 9:6, scope: scope[0] }, visibility_scope: scope[0] }): bad type [type error]
 --> src\main.rs:7:5
  |
7 | /     fn bar() {
8 | |         Self(1u8);
9 | |     }
  | |_____^

thread 'main' panicked at 'no errors encountered even though `delay_span_bug` issued', src\librustc_errors\lib.rs:334:17
stack backtrace:
   0: std::sys_common::alloc::realloc_fallback
   1: std::panicking::take_hook
   2: std::panicking::take_hook
   3: rustc::ty::structural_impls::<impl rustc::ty::context::Lift<'tcx> for rustc::ty::instance::InstanceDef<'a>>::lift_to_tcx
   4: std::panicking::rust_panic_with_hook
   5: <rustc_errors::diagnostic::SubDiagnostic as core::fmt::Debug>::fmt
   6: <rustc_errors::Handler as core::ops::drop::Drop>::drop
   7: <humantime::duration::Error as std::error::Error>::cause
   8: <humantime::duration::Error as std::error::Error>::cause
   9: <humantime::duration::Error as std::error::Error>::cause
  10: <rustc_driver::derive_registrar::Finder as rustc::hir::itemlikevisit::ItemLikeVisitor<'v>>::visit_item
  11: _rust_maybe_catch_panic
  12: rustc_driver::profile::dump
  13: rustc_driver::main
  14: <unknown>
  15: std::panicking::update_panic_count
  16: _rust_maybe_catch_panic
  17: std::rt::lang_start_internal
  18: <unknown>
  19: <unknown>
  20: BaseThreadInitThunk
  21: RtlUserThreadStart
query stack during panic:
end of query stack

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.32.0-nightly (1f57e4841 2018-11-23) running on x86_64-pc-windows-msvc

note: compiler flags: -C debuginfo=2 -C incremental --crate-type bin

note: some of the compiler flags provided by cargo are hidden

error: Could not compile `nespera`.

To learn more, run the command again with --verbose.

Process finished with exit code 101
@Centril Centril added A-type-system Area: Type system I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html labels Nov 24, 2018
@Centril
Copy link
Contributor

Centril commented Nov 24, 2018

cc @alexreg @oli-obk


Reduced:

#![feature(self_struct_ctor)]

enum Foo {}

impl Foo {
    fn bar() {
        Self
    }
}

also:

#![feature(self_struct_ctor)]

enum Foo {}

impl Foo {
    fn bar() {
        Self();
    }
}

@estebank
Copy link
Contributor

estebank commented Nov 30, 2018

Possibly fixed by #56205. Verify after that PR is merged and add regression test.

@estebank estebank added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. labels Nov 30, 2018
@kevgrasso
Copy link
Contributor

I'd like to handle the verification & test.

@alexreg
Copy link
Contributor

alexreg commented Nov 30, 2018

Thanks @kevgrasso, that would be great!

@kevgrasso
Copy link
Contributor

I tried compiling it with the nightly toolchain and it still panicked:

applesaucers@DESKTOP-I15PRGM:/mnt/c/Users/applesaucers/Documents/rust_test$ RUST_BACKTRACE=1 rustc +nightly i56199.rs
warning: the feature `self_struct_ctor` has been stable since 1.32.0 and no longer requires an attribute to enable
 --> i56199.rs:1:12
  |
1 | #![feature(self_struct_ctor)]
  |            ^^^^^^^^^^^^^^^^
  |
  = note: #[warn(stable_features)] on by default

warning: path statement with no effect
 --> i56199.rs:8:9
  |
8 |         Self;
  |         ^^^^^
  |
  = note: #[warn(path_statements)] on by default

error: internal compiler error: cat_expr Errd
 --> i56199.rs:7:14
  |
7 |       fn bar() {
  |  ______________^
8 | |         Self;
9 | |     }
  | |_____^

error: internal compiler error: cat_expr Errd
 --> i56199.rs:8:9
  |
8 |         Self;
  |         ^^^^

error: internal compiler error: broken MIR in DefId(0/0:5 ~ i56199[317d]::{{impl}}[0]::bar[0]) ("return type"): bad type [type error]
 --> i56199.rs:7:5
  |
7 | /     fn bar() {
8 | |         Self;
9 | |     }
  | |_____^

error: internal compiler error: broken MIR in DefId(0/0:5 ~ i56199[317d]::{{impl}}[0]::bar[0]) (LocalDecl { mutability: Mut, is_user_variable: None, internal: false, is_block_tail: None, ty: [type error], user_ty: UserTypeProjections { contents: [] }, name: None, source_info: SourceInfo { span: i56199.rs:7:5: 9:6, scope: scope[0] }, visibility_scope: scope[0] }): bad type [type error]
 --> i56199.rs:7:5
  |
7 | /     fn bar() {
8 | |         Self;
9 | |     }
  | |_____^

error: internal compiler error: QualifyAndPromoteConstants: Mir had errors
 --> i56199.rs:7:5
  |
7 | /     fn bar() {
8 | |         Self;
9 | |     }
  | |_____^

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:334:17
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at src/libstd/sys_common/backtrace.rs:59
             at src/libstd/panicking.rs:211
   3: std::panicking::default_hook
             at src/libstd/panicking.rs:227
   4: rustc::util::common::panic_hook
   5: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:495
   6: std::panicking::begin_panic
   7: <rustc_errors::Handler as core::ops::drop::Drop>::drop
   8: core::ptr::real_drop_in_place
   9: core::ptr::real_drop_in_place
  10: core::ptr::real_drop_in_place
  11: rustc_driver::monitor::{{closure}}
  12: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:102
  13: <F as alloc::boxed::FnBox<A>>::call_box
  14: std::sys::unix::thread::Thread::new::thread_start
             at /rustc/0c999ed132d67bf2520643e9bd619972cf3888ba/src/liballoc/boxed.rs:683
             at src/libstd/sys_common/thread.rs:24
             at src/libstd/sys/unix/thread.rs:90
  15: start_thread
  16: __clone
query stack during panic:
end of query stack

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.32.0-nightly (0c999ed13 2018-12-03) running on x86_64-unknown-linux-gnu

for the sample code:

#![feature(self_struct_ctor)]
#![allow(dead_code)]

enum Foo {}

impl Foo {
    fn bar() {
        Self;
    }
}

fn main() {}

Would you still want me to do a regression test for this, even though it's still bugged?

@estebank estebank removed E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. labels Dec 5, 2018
@estebank
Copy link
Contributor

estebank commented Dec 5, 2018

@kevgrasso we probably should fix the bug, we normally don't add tests for ICEs that haven't been fixed. CC @alexreg follow up to #56365.

@alexreg
Copy link
Contributor

alexreg commented Dec 11, 2018

On a related note, this also fails (see #56611):

#![feature(self_struct_ctor)]

struct Foo {}

impl Foo {
    fn bar() {
        Self()
    }
}

with

error: internal compiler error: src/librustc/hir/def.rs:265: attempted .def_id() on invalid def: SelfCtor(DefId(0/0:4 ~ playground[6463]::{{impl}}[0]))

@alexreg
Copy link
Contributor

alexreg commented Dec 11, 2018

I've fixed all of the above cases in #56225 now.

alexreg added a commit to alexreg/rust that referenced this issue Dec 11, 2018
bors added a commit that referenced this issue Dec 11, 2018
Implement RFC 2338, "Type alias enum variants"

This PR implements [RFC 2338](rust-lang/rfcs#2338), allowing one to write code like the following.

```rust
#![feature(type_alias_enum_variants)]

enum Foo {
    Bar(i32),
    Baz { i: i32 },
}

type Alias = Foo;

fn main() {
    let t = Alias::Bar(0);
    let t = Alias::Baz { i: 0 };
    match t {
        Alias::Bar(_i) => {}
        Alias::Baz { i: _i } => {}
    }
}
```

Since `Self` can be considered a type alias in this context, it also enables using `Self::Variant` as both a constructor and pattern.

Fixes issues #56199 and #56611.

N.B., after discussing the syntax for type arguments on enum variants with @petrochenkov and @eddyb (there are also a few comments on the [tracking issue](#49683)), the consensus seems to be treat the syntax as follows, which ought to be backwards-compatible.

```rust
Option::<u8>::None; // OK
Option::None::<u8>; // OK, but lint in near future (hard error next edition?)
Alias::<u8>::None; // OK
Alias::None::<u8>; // Error
```

I do not know if this will need an FCP, but let's start one if so.

r? @petrochenkov
alexreg added a commit to alexreg/rust that referenced this issue Dec 15, 2018
alexreg added a commit to alexreg/rust that referenced this issue Dec 15, 2018
alexreg added a commit to alexreg/rust that referenced this issue Dec 15, 2018
alexreg added a commit to alexreg/rust that referenced this issue Dec 15, 2018
@pnkfelix pnkfelix added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Dec 20, 2018
bors added a commit that referenced this issue Dec 29, 2018
Implement RFC 2338, "Type alias enum variants"

This PR implements [RFC 2338](rust-lang/rfcs#2338), allowing one to write code like the following.

```rust
#![feature(type_alias_enum_variants)]

enum Foo {
    Bar(i32),
    Baz { i: i32 },
}

type Alias = Foo;

fn main() {
    let t = Alias::Bar(0);
    let t = Alias::Baz { i: 0 };
    match t {
        Alias::Bar(_i) => {}
        Alias::Baz { i: _i } => {}
    }
}
```

Since `Self` can be considered a type alias in this context, it also enables using `Self::Variant` as both a constructor and pattern.

Fixes issues #56199 and #56611.

N.B., after discussing the syntax for type arguments on enum variants with @petrochenkov and @eddyb (there are also a few comments on the [tracking issue](#49683)), the consensus seems to be treat the syntax as follows, which ought to be backwards-compatible.

```rust
Option::<u8>::None; // OK
Option::None::<u8>; // OK, but lint in near future (hard error next edition?)
Alias::<u8>::None; // OK
Alias::None::<u8>; // Error
```

I do not know if this will need an FCP, but let's start one if so.
pietroalbini pushed a commit to pietroalbini/rust that referenced this issue Dec 31, 2018
@Centril
Copy link
Contributor

Centril commented Jan 11, 2019

@alexreg should this be closed?

@estebank
Copy link
Contributor

The output of the original issue is now:

error: the `Self` constructor can only be used with tuple or unit structs
 --> src/main.rs:8:9
  |
8 |         Self(1u8);
  |         ^^^^
  |
  = help: did you mean to use one of the enum's variants?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-type-system Area: Type system I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants