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 Type parameter I/#1 (I/1) out of range when substituting #59956

Closed
DutchGhost opened this issue Apr 14, 2019 · 3 comments
Closed

ICE Type parameter I/#1 (I/1) out of range when substituting #59956

DutchGhost opened this issue Apr 14, 2019 · 3 comments
Labels
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.

Comments

@DutchGhost
Copy link
Contributor

The following code ICE's on stable, beta and nightly, on both editions:

use std::marker::PhantomData;

struct Invariant<'id, I = Self> {
    lifetime: PhantomData<*mut &'id I>,
}

trait Contract<'s> {
    type Item: for<'r> Contract<'r>;
}

impl <'a, 'b> Contract<'b> for Invariant<'a> {
    type Item = Invariant<'b>;
}
Backtrace:

error: internal compiler error: src/librustc/ty/subst.rs:480: Type parameter `I/#1` (I/1) out of range when substituting (root type=Some(Invariant<'id, I>)) substs=[ReEarlyBound(0, 'a)]
  --> src/lib.rs:11:32
   |
11 | impl <'a, 'b> Contract<'b> for Invariant<'a> {
   |                                ^^^^^^^^^^^^^

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:558:9
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:39
   1: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:70
   2: std::panicking::default_hook::{{closure}}
             at src/libstd/sys_common/backtrace.rs:58
             at src/libstd/panicking.rs:200
   3: std::panicking::default_hook
             at src/libstd/panicking.rs:215
   4: rustc::util::common::panic_hook
   5: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:482
   6: std::panicking::begin_panic
   7: rustc_errors::Handler::span_bug
   8: rustc::util::bug::opt_span_bug_fmt::{{closure}}
   9: rustc::ty::context::tls::with_opt::{{closure}}
  10: rustc::ty::context::tls::with_context_opt
  11: rustc::ty::context::tls::with_opt
  12: rustc::util::bug::opt_span_bug_fmt
  13: rustc::util::bug::span_bug_fmt
  14: <rustc::ty::subst::SubstFolder<'a, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  15: <smallvec::SmallVec<A> as core::iter::traits::collect::FromIterator<<A as smallvec::Array>::Item>>::from_iter
  16: rustc::ty::fold::TypeFoldable::fold_with
  17: rustc::ty::structural_impls::<impl rustc::ty::fold::TypeFoldable<'tcx> for &'tcx rustc::ty::TyS<'tcx>>::super_fold_with
  18: <rustc::ty::subst::SubstFolder<'a, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  19: <(dyn rustc_typeck::astconv::AstConv<'gcx, 'tcx> + 'o)>::create_substs_for_ast_path::{{closure}}
  20: <(dyn rustc_typeck::astconv::AstConv<'gcx, 'tcx> + 'o)>::create_substs_for_ast_path
  21: rustc::hir::PathSegment::with_generic_args
  22: <(dyn rustc_typeck::astconv::AstConv<'gcx, 'tcx> + 'o)>::ast_path_substs_for_ty
  23: <(dyn rustc_typeck::astconv::AstConv<'gcx, 'tcx> + 'o)>::def_to_ty
  24: <(dyn rustc_typeck::astconv::AstConv<'gcx, 'tcx> + 'o)>::ast_ty_to_ty
  25: rustc_typeck::collect::type_of
  26: rustc::ty::query::__query_compute::type_of
  27: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::type_of<'tcx>>::compute
  28: rustc::dep_graph::graph::DepGraph::with_task_impl
  29: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  30: <rustc_typeck::collect::CollectItemTypesVisitor<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_item
  31: rustc::hir::map::Map::visit_item_likes_in_module
  32: rustc_typeck::collect::collect_mod_item_types
  33: rustc::ty::query::__query_compute::collect_mod_item_types
  34: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::collect_mod_item_types<'tcx>>::compute
  35: rustc::dep_graph::graph::DepGraph::with_task_impl
  36: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  37: rustc_typeck::collect::collect_item_types
  38: rustc::util::common::time
  39: rustc_typeck::check_crate
  40: <std::thread::local::LocalKey<T>>::with
  41: rustc::ty::context::TyCtxt::create_and_enter
  42: rustc_driver::driver::compile_input
  43: rustc_driver::run_compiler_with_pool
  44: <scoped_tls::ScopedKey<T>>::set
  45: rustc_driver::run_compiler
  46: syntax::with_globals
  47: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:87
  48: <F as alloc::boxed::FnBox<A>>::call_box
  49: std::sys::unix::thread::Thread::new::thread_start
             at /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/liballoc/boxed.rs:759
             at src/libstd/sys_common/thread.rs:14
             at src/libstd/sys/unix/thread.rs:81
  50: start_thread
  51: __clone
query stack during panic:
error[E0391]: cycle detected when processing `<impl at src/lib.rs:11:1: 13:2>`
  --> src/lib.rs:11:1
   |
11 | impl <'a, 'b> Contract<'b> for Invariant<'a> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: ...which again requires processing `<impl at src/lib.rs:11:1: 13:2>`, completing the cycle
note: cycle used when collecting item types in top-level module
  --> src/lib.rs:1:1
   |
1  | / use std::marker::PhantomData;
2  | |
3  | | struct Invariant<'id, I = Self> {
4  | |     lifetime: PhantomData<*mut &'id I>,
...  |
12 | |     type Item = Invariant<'b>;
13 | | }
   | |_^

error[E0391]: cycle detected when processing `<impl at src/lib.rs:11:1: 13:2>`
  --> src/lib.rs:11:1
   |
11 | impl <'a, 'b> Contract<'b> for Invariant<'a> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: ...which requires processing `<impl at src/lib.rs:11:1: 13:2>`...
  --> src/lib.rs:11:1
   |
11 | impl <'a, 'b> Contract<'b> for Invariant<'a> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: ...which again requires processing `<impl at src/lib.rs:11:1: 13:2>`, completing the cycle
note: cycle used when collecting item types in top-level module
  --> src/lib.rs:1:1
   |
1  | / use std::marker::PhantomData;
2  | |
3  | | struct Invariant<'id, I = Self> {
4  | |     lifetime: PhantomData<*mut &'id I>,
...  |
12 | |     type Item = Invariant<'b>;
13 | | }
   | |_^

#0 [type_of] processing `<[type error] as Contract<'b>>`
#1 [collect_mod_item_types] collecting item types in top-level module
end of query stack
error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0391`.

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.34.0 (91856ed52 2019-04-10) running on x86_64-unknown-linux-gnu

note: compiler flags: -C codegen-units=1 -C debuginfo=2 --crate-type lib

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

error: Could not compile `playground`.

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

@DutchGhost
Copy link
Contributor Author

DutchGhost commented Apr 14, 2019

Update:
Even this gives the same ICE:

struct Bug<B = Self>;
impl Bug {}

@jonas-schievink jonas-schievink 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 Apr 14, 2019
@Elarnon
Copy link

Elarnon commented May 1, 2019

I got hit by the same thing and was excited that Rust now supported Self defaults in structs, but alas no (I guess technically you can define them, just not use them) :(

The ICE was introduced in 1.32.0:

$ cat bug.rs
struct Bug<B = Self>;

impl Bug {}
$ rustc +1.31.1 bug.rs
error[E0411]: cannot find type `Self` in this scope
 --> bug.rs:1:16
  |
1 | struct Bug<B = Self>;
  |                ^^^^ `Self` is only available in traits and impls

error[E0601]: `main` function not found in crate `bug`
  |
  = note: consider adding a `main` function to `bug.rs`

error[E0392]: parameter `B` is never used
 --> bug.rs:1:12
  |
1 | struct Bug<B = Self>;
  |            ^ unused type parameter
  |
  = help: consider removing `B` or using a marker such as `std::marker::PhantomData`

error: aborting due to 3 previous errors

Some errors occurred: E0392, E0411, E0601.
For more information about an error, try `rustc --explain E0392`.
$ rustc +1.32.0 bug.rs
error[E0601]: `main` function not found in crate `bug`
  |
  = note: consider adding a `main` function to `bug.rs`

error: internal compiler error: src/librustc/ty/subst.rs:491: Type parameter `B/#0` (B/0) out of range when substituting (root type=Some(Bug<B>)) substs=[]
 --> bug.rs:3:6
  |
3 | impl Bug {}
  |      ^^^

thread 'main' panicked at 'Box<Any>', src/librustc_errors/lib.rs:538:9
note: Run with `RUST_BACKTRACE=1` for a backtrace.
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0601`.

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 (9fda7c223 2019-01-16) running on x86_64-unknown-linux-gnu

As long as the Self default is not used there is no ICE, so the following is fine (note that Bug with default type parameter would be an infinitely-sized type in this case):

#[allow(dead_code)]
struct Bug<B = Self>(Option<B>);

impl Bug<u32> {}

As an aside, is there any plan or interest for supporting Self default type parameters in structs? In the following, I would expect the types A<T> and B<T> to be identical, but since the first one doesn't exist it requires special casing as a separate type:

struct A<T, U = Self>(T, Option<Box<U>>);

struct B<T>(T, Option<Box<B<T>>>);

(Yes I am aware of at least some potential issues related to recursive types and multiple default arguments, but they should be solvable with the appropriate restrictions I believe)

@Alexendoo
Copy link
Member

This was fixed by #64842, including a similar test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

No branches or pull requests

4 participants