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

rustc panic with "assertion failed: !value.needs_subst()" #50518

Closed
tuxmark5 opened this issue May 7, 2018 · 8 comments · Fixed by #60627
Closed

rustc panic with "assertion failed: !value.needs_subst()" #50518

tuxmark5 opened this issue May 7, 2018 · 8 comments · Fixed by #60627
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-traits Area: Trait system C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tuxmark5
Copy link

tuxmark5 commented May 7, 2018

The below example panics with: thread 'main' panicked at 'assertion failed: !value.needs_subst()', librustc/traits/query/normalize_erasing_regions.rs:69:9

I've tried minimizing the example as much as possible and that's the best I can do for now.

Version: rustc 1.27.0-nightly (428ea5f 2018-05-06)

use std::marker::PhantomData;

struct Meta<A> {
    value: i32,
    type_: PhantomData<A>
}

trait MetaTrait {
    fn get_value(&self) -> i32;
}

impl<A> MetaTrait for Meta<A> {
    fn get_value(&self) -> i32 { self.value }
}

trait Bar {
    fn get_const(&self) -> &dyn MetaTrait;
}

struct Foo<A> {
    value: A
}

impl<A: 'static> Foo<A> {
    const CONST: &'static dyn MetaTrait = &Meta::<Self> {
        value: 10,
        type_: PhantomData
    };
}

impl<A: 'static> Bar for Foo<A> {
    fn get_const(&self) -> &dyn MetaTrait { Self::CONST }
}

fn main() {
    let foo = Foo::<i32> { value: 10 };
    let bar: &dyn Bar = &foo;
    println!("const {}", bar.get_const().get_value());
}
@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ A-traits Area: Trait system labels May 7, 2018
@simon-auch
Copy link

simon-auch commented Jun 21, 2018

I have the same error, interesstingly the error seems to have something to do with PhantomData:

use std::marker::PhantomData;

struct Foo<T>(PhantomData<T>);

impl<T> Foo<T>{
  const OP: fn(&T) = |_t| {};
  
  fn op(_t: &T){}
  
  fn foo(&mut self, t: T){
    //does not work
    Self::OP(&t);
    //does work
    Self::op(&t);
  }
}

fn main(){
  Foo(PhantomData).foo(1);
}

Edit 1 & 2: Smaller example

@simon-auch
Copy link

simon-auch commented Jun 21, 2018

After some testing i found out it has nothing to do with the PhantomData, since the following code still gives the same error:

struct Foo<T>(T);

impl<T> Foo<T>{
  const OP: fn(T) = |_t| {};
  
  fn foo(self){
    //does not work
    Self::OP(self.0);
  }
}

fn main(){
  Foo(1).foo();
}

edit: further simplification

@tuxmark5
Copy link
Author

tuxmark5 commented Jun 21, 2018

I think it's related to promoting references to fat pointers in const contexts. In your example you promote a closure |_t| {} to dyn FnOnce(T) (?). In my example, I promote Meta to dyn MetaTrait.

@simon-auch
Copy link

It seems that the Problem only occurs, when using the associated constant from inside the implementation block:
http://play.rust-lang.org/?gist=e7f9f10cde5ca4b0c52588c26b6388cf&version=stable&mode=debug&edition=2015

@nikolads
Copy link

I found a different example which gives the same error:

struct MyBox<T>(*mut T);

impl<T> MyBox<T> {
    fn get(&self) -> Option<&T> {
        match self.0 {
            Self::NULL => None,
            ptr => unsafe { Some(&*ptr) },
        }
    }

    const NULL: *mut T = ::std::ptr::null_mut();
}
thread 'main' panicked at 'assertion failed: !value.needs_subst()', librustc/traits/query/normalize_erasing_regions.rs:69:9
error: internal compiler error: unexpected panic
note: rustc 1.27.0 (3eda71b00 2018-06-19) running on x86_64-unknown-linux-gnu

It only ICEs if I initialize NULL with a const fn. It compiles after replacing the line with

    const NULL: *mut T = 0 as *mut T;

Moving the function outside the impl block still ICEs, but with a different error:

struct MyBox<T>(*mut T);

impl<T> MyBox<T> {
    const NULL: *mut T = ::std::ptr::null_mut();
}

fn get<'a, T>(ptr: *mut T) -> Option<&'a T> {
    match ptr {
        MyBox::<T>::NULL => None,
        ptr => unsafe { Some(&*ptr) },
    }
}
error: internal compiler error: librustc/ty/subst.rs:493: Type parameter `T/#1` (T/1) out of range when substituting (root type=Some(T)) substs=[T]

thread 'main' panicked at 'Box<Any>', librustc_errors/lib.rs:499:9

@matklad
Copy link
Member

matklad commented Aug 4, 2018

Hit this in https://github.com/matklad/once_cell, in stable(1.28.0), beta(1.29.0-beta.1) and nightly ( 2018-08-03).

Here's the commit which exemplifies the issue: matklad/once_cell@ea1c14b (run cargo test --no-default-features to trigger). Basically, I cant newtype-wrap a struct with INIT: Self, which requires me to duplicate doc comments :(

@XAMPPRocky XAMPPRocky added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Oct 2, 2018
@matklad
Copy link
Member

matklad commented May 7, 2019

Triage: both the original example and the once_cell bug no longer reproduce for me on 1.31.1 (2018 release) and 1.34.1 (stable at the time of writing).

The issue should probably be closed. Or do we want to add run-pass test just in case?

@estebank estebank added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. and removed I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ labels May 8, 2019
@estebank
Copy link
Contributor

estebank commented May 8, 2019

Lets add a regression test.

matklad added a commit to matklad/rust that referenced this issue May 8, 2019
It was fixed somewhere between 1.28.0 and 1.31.1

closes rust-lang#50518
Centril added a commit to Centril/rust that referenced this issue May 8, 2019
test for rust-lang#50518

It was fixed somewhere between 1.28.0 and 1.31.1

closes rust-lang#50518

r? @estebank

Where's the best place to add this test? I *think* we want "compile-pass" for this test (no need to run a binary, and not running saves us a millisecond of process creation) , but there's no compile-pass anymore.

Should this be UI test with empty stdout, stderr and zero return code?
Centril added a commit to Centril/rust that referenced this issue May 8, 2019
test for rust-lang#50518

It was fixed somewhere between 1.28.0 and 1.31.1

closes rust-lang#50518

r? @estebank

Where's the best place to add this test? I *think* we want "compile-pass" for this test (no need to run a binary, and not running saves us a millisecond of process creation) , but there's no compile-pass anymore.

Should this be UI test with empty stdout, stderr and zero return code?
Centril added a commit to Centril/rust that referenced this issue May 8, 2019
test for rust-lang#50518

It was fixed somewhere between 1.28.0 and 1.31.1

closes rust-lang#50518

r? @estebank

Where's the best place to add this test? I *think* we want "compile-pass" for this test (no need to run a binary, and not running saves us a millisecond of process creation) , but there's no compile-pass anymore.

Should this be UI test with empty stdout, stderr and zero return code?
bors added a commit that referenced this issue May 8, 2019
Rollup of 8 pull requests

Successful merges:

 - #59979 (to_xe_bytes for isize and usize returns an array of different size)
 - #60491 (std: Update compiler-builtins crate)
 - #60550 (Add tests for concrete const types)
 - #60572 (Add test for #59972)
 - #60627 (test for #50518)
 - #60634 (Document + Cleanup lang_items.rs)
 - #60641 (Instead of ICEing on incorrect pattern, use delay_span_bug)
 - #60644 (Use `delay_span_bug` for "Failed to unify obligation")

Failed merges:

r? @ghost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-traits Area: Trait system C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. 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.

6 participants