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

ICE with Const Generics cast of self in supertrait list #70167

Closed
JulianKnodt opened this issue Mar 19, 2020 · 6 comments · Fixed by #70223
Closed

ICE with Const Generics cast of self in supertrait list #70167

JulianKnodt opened this issue Mar 19, 2020 · 6 comments · Fixed by #70223
Assignees
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` 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

@JulianKnodt
Copy link
Contributor

JulianKnodt commented Mar 19, 2020

I was writing some generic code over vectors using the under-construction feature of const generics, and had a circular chain of super-traits that required me to cast the Self type to the trait as can be seen in the Div supertrait requirement. I believe that is causing the MIR error.
I think this may be related to #66906 but it is subtly different so I'm putting the issue up regardless.

Code

#![feature(const_generics)]
use std::ops::Div;
use num::Float;

/// Abstract vector over some finite field
pub trait Vector<const N: usize>: Div<<Self as Vector<N>>::Field, Output = Self> + Copy {
  type Field: Float;
  fn cons(vs: [Self::Field; N]) -> Self { todo!() }
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=3856486297085a463f98c2fc9f9a99c6

Meta

rustc --version --verbose:
I updated my rust version on nightly right before I ran this so hopefully it is up to date:

rustc 1.44.0-nightly (f509b26a7 2020-03-18)
binary: rustc
commit-hash: f509b26a7730d721ef87423a72b3fdf8724b4afa
commit-date: 2020-03-18
host: x86_64-apple-darwin
release: 1.44.0-nightly
LLVM version: 9.0

Error output

... Warnings Elided for convenience...

warning: Error finalizing incremental compilation session directory `/Users/julianknodt/Desktop/programming/projects/rustjects/gfx/target/debug/incremental/ray_weekend-1jydezk8mz1tf/s-flpmnsfnok-1ritjvd-working`: No such file or directory (os error 2)

error: internal compiler error: mir_const_qualif: MIR had errors
 --> src/vec.rs:8:55
  |
8 | pub trait Vector<const N: usize>: Div<<Self as Vector<N>>::Field, Output = Self> + Copy {
  |                                                       ^

error: internal compiler error: PromoteTemps: MIR had errors
 --> src/vec.rs:8:55
  |
8 | pub trait Vector<const N: usize>: Div<<Self as Vector<N>>::Field, Output = Self> + Copy {
  |                                                       ^

error: internal compiler error: broken MIR in DefId(0:2682 ~ ray_weekend[6635]::vec[0]::Vector[0]::{{constant}}[0]) ("return type"): bad type [type error]
 --> src/vec.rs:8:55
  |
8 | pub trait Vector<const N: usize>: Div<<Self as Vector<N>>::Field, Output = Self> + Copy {
  |                                                       ^

error: internal compiler error: broken MIR in DefId(0:2682 ~ ray_weekend[6635]::vec[0]::Vector[0]::{{constant}}[0]) (LocalDecl { mutability: Mut, local_info: Other, internal: false, is_block_tail: None, ty: [type error], user_ty: UserTypeProjections { contents: [] }, source_info: SourceInfo { span: src/vec.rs:8:55: 8:56, scope: scope[0] } }): bad type [type error]
 --> src/vec.rs:8:55
  |
8 | pub trait Vector<const N: usize>: Div<<Self as Vector<N>>::Field, Output = Self> + Copy {
  |                                                       ^

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:360:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

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.44.0-nightly (f509b26a7 2020-03-18) running on x86_64-apple-darwin

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

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

error: could not compile `ray_weekend`.

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

...Warnings elided for clarity...

warning: Error finalizing incremental compilation session directory `/Users/julianknodt/Desktop/programming/projects/rustjects/gfx/target/debug/incremental/ray_weekend-1jydezk8mz1tf/s-flpmrtevdl-1q49gen-working`: No such file or directory (os error 2)

error: internal compiler error: mir_const_qualif: MIR had errors
 --> src/vec.rs:8:55
  |
8 | pub trait Vector<const N: usize>: Div<<Self as Vector<N>>::Field, Output = Self> + Copy {
  |                                                       ^

error: internal compiler error: PromoteTemps: MIR had errors
 --> src/vec.rs:8:55
  |
8 | pub trait Vector<const N: usize>: Div<<Self as Vector<N>>::Field, Output = Self> + Copy {
  |                                                       ^

error: internal compiler error: broken MIR in DefId(0:2682 ~ ray_weekend[6635]::vec[0]::Vector[0]::{{constant}}[0]) ("return type"): bad type [type error]
 --> src/vec.rs:8:55
  |
8 | pub trait Vector<const N: usize>: Div<<Self as Vector<N>>::Field, Output = Self> + Copy {
  |                                                       ^

error: internal compiler error: broken MIR in DefId(0:2682 ~ ray_weekend[6635]::vec[0]::Vector[0]::{{constant}}[0]) (LocalDecl { mutability: Mut, local_info: Other, internal: false, is_block_tail: None, ty: [type error], user_ty: UserTypeProjections { contents: [] }, source_info: SourceInfo { span: src/vec.rs:8:55: 8:56, scope: scope[0] } }): bad type [type error]
 --> src/vec.rs:8:55
  |
8 | pub trait Vector<const N: usize>: Div<<Self as Vector<N>>::Field, Output = Self> + Copy {
  |                                                       ^

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:360:17
stack backtrace:
   0: <unknown>
   1: <unknown>
   2: <unknown>
   3: <unknown>
   4: <unknown>
   5: <unknown>
   6: <unknown>
   7: <unknown>
   8: <unknown>
   9: <unknown>
  10: <unknown>
  11: <unknown>
  12: <unknown>
  13: <unknown>
  14: <unknown>
  15: <unknown>
  16: <unknown>
  17: <unknown>
  18: <unknown>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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.44.0-nightly (f509b26a7 2020-03-18) running on x86_64-apple-darwin

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

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

query stack during panic:
end of query stack
error: could not compile `ray_weekend`.

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

This issue has been assigned to @lcnr via this comment.

@JulianKnodt JulianKnodt added C-bug Category: This is a bug. 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. labels Mar 19, 2020
@jonas-schievink jonas-schievink added A-const-generics Area: const generics (parameters and arguments) F-const_generics `#![feature(const_generics)]` labels Mar 19, 2020
@lcnr
Copy link
Contributor

lcnr commented Mar 20, 2020

minimized:

#![feature(const_generics)]

pub trait Trait<const N: usize>: From<<Self as Trait<N>>::Item> {
  type Item;
}

@rustbot claim

@rustbot rustbot self-assigned this Mar 20, 2020
@eddyb
Copy link
Member

eddyb commented Mar 20, 2020

Looks like maybe constants in trait paths don't get their type correctly?
cc @varkor @yodaldevoid

@eddyb
Copy link
Member

eddyb commented Mar 21, 2020

Initially I thought this was about bounds but no, this also fails: (playground)

#![feature(const_generics)]

pub trait Trait<const N: usize> {
  type Item;
  const VAL: <Self as Trait<N>>::Item;
}

@eddyb
Copy link
Member

eddyb commented Mar 21, 2020

I believe this code is responsible:

// This is no generic parameter associated with the arg. This is
// probably from an extra arg where one is not needed.
.unwrap_or(tcx.types.err)

And the problem is that this doesn't handle associated types which need the same treatment:

Res::Def(DefKind::Ctor(..), def_id) => {
tcx.generics_of(tcx.parent(def_id).unwrap())
}

@eddyb
Copy link
Member

eddyb commented Mar 21, 2020

Alternatively, instead of using path.res to get the DefId, maybe we could use the Resolution from the segment we found the const parameter in?
I believe nowadays we have per-segment resolutions, which would avoid breaking GATs in the future.

Actually, I expect Struct::<N>::method::<M>() is also broken, let's try it... Yupp: (playground)

#![feature(const_generics)]

struct Struct<const N: usize>;

impl<const N: usize> Struct<N> {
    fn method<const M: usize>(&self) {}
}

fn test<const N: usize, const M: usize>(x: Struct<N>) {
    Struct::<N>::method::<M>(&x);
    x.method::<N>();
}

EDIT: took a second look and that M is in a QPath::TypeRelative, which has to be resolved by the body of test, which means it's as bad as the method call form (which I just added).

Maybe we should disallow passing explicit generic args to methods for now?
Since I have no idea how one would figure out the expected type.

Centril added a commit to Centril/rust that referenced this issue Mar 22, 2020
fix type of const params in associated types.

fixes rust-lang#66906
fixes rust-lang#70167

r? @eddyb
@bors bors closed this as completed in 3c8f8b6 Mar 22, 2020
@eddyb
Copy link
Member

eddyb commented Mar 22, 2020

@varkor @yodaldevoid If you look at this later btw, my comment above (#70167 (comment)) still isn't addressed, but I haven't made an issue yet about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` 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

Successfully merging a pull request may close this issue.

5 participants