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

Rust compiler panics when trying to reference impl-ed constants with Self #47814

Closed
rlupton20 opened this issue Jan 27, 2018 · 6 comments · Fixed by #105236
Closed

Rust compiler panics when trying to reference impl-ed constants with Self #47814

rlupton20 opened this issue Jan 27, 2018 · 6 comments · Fixed by #105236
Labels
A-lazy-normalization Area: Lazy normalization (tracking issue: #60471) 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. glacier ICE tracked in rust-lang/glacier. 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

@rlupton20
Copy link

rlupton20 commented Jan 27, 2018

EDITED: MCVE is here: #47814 (comment)

I tried to compile the following, which caused the rust compiler to panic.

...
�impl<'a> ArpIPv4<'a> {
    const LENGTH: usize = 20;
...
    pub fn to_buffer(&self) -> [u8; Self::LENGTH] {
        [0; ArpIPv4::LENGTH]
    }
}

The following compiles just fine:

...
�impl<'a> ArpIPv4<'a> {
    const LENGTH: usize = 20;
...
    pub fn to_buffer(&self) -> [u8; ArpIPv4::LENGTH] {
        [0; ArpIPv4::LENGTH]
    }
}

The error message:

[nix-shell:~/Code/nerd-stack]$ cargo run
   Compiling nerd-stack v0.1.0 (file:///home/richard/Code/nerd-stack)
warning: unused imports: `ipv4_to_string`, `mac_to_string`
 --> src/protocols/arp.rs:1:26
  |
1 | use address::{MAC, IPv4, mac_to_string, ipv4_to_string};
  |                          ^^^^^^^^^^^^^  ^^^^^^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

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.24.0-nightly (9389e23a8 2017-12-31) running on x86_64-unknown-linux-gnu

thread 'rustc' panicked at 'assertion failed: `(left == right)`
  left: `Some(NodeId(820))`,
 right: `None`: free_scope: DefId(0/0:83 ~ nerd_stack[3074]::protocols[0]::arp[0]::{{impl}}[2]) not recognized by the region scope tree for None / Some(DefId(0/0:86 ~ nerd_stack[3074]::protocols[0]::arp[0]::{{impl}}[2]::to_buffer[0]))', /checkout/src/librustc/middle/region.rs:729:13
note: Run with `RUST_BACKTRACE=1` for a backtrace.

error: Could not compile `nerd-stack`.

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

And running again with verbosity and backtrace:

[nix-shell:~/Code/nerd-stack]$ RUST_BACKTRACE=1 cargo run --verbose                                                                
       Fresh libc v0.2.36                                                                                                    
   Compiling nerd-stack v0.1.0 (file:///home/richard/Code/nerd-stack)                                                           
     Running `rustc --crate-name nerd_stack src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=320d9acf78f2e38b -C extra-filename=-320d9acf78f2e38b --out-dir /home/richard/Code/nerd-stack
/target/debug/deps -C incremental=/home/richard/Code/nerd-stack/target/debug/incremental -L dependency=/home/richard/Code/nerd-stack/target/debug/deps --extern libc=/home/richard/Code/nerd-stack/target/debug/deps
/liblibc-1d53503271082800.rlib`        
warning: unused imports: `ipv4_to_string`, `mac_to_string`      
 --> src/protocols/arp.rs:1:26               
  |                                        
1 | use address::{MAC, IPv4, mac_to_string, ipv4_to_string};                         
  |                          ^^^^^^^^^^^^^  ^^^^^^^^^^^^^^
  |                                        
  = note: #[warn(unused_imports)] on by default
                                             
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.24.0-nightly (9389e23a8 2017-12-31) running on x86_64-unknown-linux-gnu
          
note: run with `RUST_BACKTRACE=1` for a backtrace                                                                                                                                                                                                                                                                                                                                                                                       thread 'rustc' panicked at 'assertion failed: `(left == right)`        
  left: `Some(NodeId(820))`,
 right: `None`: free_scope: DefId(0/0:83 ~ nerd_stack[3074]::protocols[0]::arp[0]::{{impl}}[2]) not recognized by the region scope tree for None / Some(DefId(0/0:86 ~ nerd_stack[3074]::protocols[0]::arp[0]::{{imp
l}}[2]::to_buffer[0]))', /checkout/src/librustc/middle/region.rs:729:13
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
   1: std::sys_common::backtrace::print
   2: std::panicking::default_hook::{{closure}}
   3: std::panicking::default_hook
   4: std::panicking::rust_panic_with_hook
   5: std::panicking::begin_panic
   6: std::panicking::begin_panic_fmt
   7: rustc::middle::region::ScopeTree::early_free_scope
   8: rustc::middle::free_region::RegionRelations::is_subregion_of
   9: rustc::infer::lexical_region_resolve::resolve
  10: rustc::infer::InferCtxt::resolve_regions_and_report_errors_inner
  11: rustc_typeck::check::regionck::<impl rustc_typeck::check::FnCtxt<'a, 'gcx, 'tcx>>::regionck_expr
  12: <std::thread::local::LocalKey<T>>::with
  13: rustc::infer::InferCtxtBuilder::enter
  14: rustc_typeck::check::typeck_tables_of
  15: rustc::ty::maps::<impl rustc::ty::maps::queries::typeck_tables_of<'tcx>>::compute_result
  16: rustc::dep_graph::graph::DepGraph::with_task_impl
  17: rustc_errors::Handler::track_diagnostics
  18: rustc::ty::maps::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::cycle_check
  19: rustc::ty::maps::<impl rustc::ty::maps::queries::typeck_tables_of<'tcx>>::force
  20: rustc::ty::maps::plumbing::force_from_dep_node
  21: rustc::dep_graph::graph::DepGraph::try_mark_green
  22: rustc::ty::maps::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::try_mark_green_and_read
  23: rustc::ty::maps::<impl rustc::ty::maps::queries::const_eval<'tcx>>::try_get
  24: rustc::ty::maps::TyCtxtAt::const_eval
  25: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_const
  26: rustc::ty::structural_impls::<impl rustc::ty::fold::TypeFoldable<'tcx> for &'tcx rustc::ty::TyS<'tcx>>::super_fold_with
  27: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  28: <rustc_data_structures::accumulate_vec::AccumulateVec<A> as core::iter::traits::FromIterator<<A as rustc_data_structures::array_vec::Array>::Element>>::from_iter
  29: rustc::ty::fold::TypeFoldable::fold_with
  30: rustc::traits::project::normalize
  31: rustc_typeck::check::FnCtxt::normalize_associated_types_in
  32: <std::thread::local::LocalKey<T>>::with
  33: rustc::infer::InferCtxtBuilder::enter
  34: rustc_typeck::check::wfcheck::CheckTypeWellFormedVisitor::check_associated_item
  35: rustc::hir::Crate::visit_all_item_likes
  36: rustc::session::Session::track_errors
  37: rustc_typeck::check_crate
  38: <std::thread::local::LocalKey<T>>::with
  39: <std::thread::local::LocalKey<T>>::with
  40: rustc::ty::context::TyCtxt::create_and_enter
  41: rustc_driver::driver::compile_input
  42: rustc_driver::run_compiler

error: Could not compile `nerd-stack`.

Caused by:
  process didn't exit successfully: `rustc --crate-name nerd_stack src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=320d9acf78f2e38b -C extra-filename=-320d9acf78f2e38b --out-dir /home/richard/Code/nerd-stack/target/debug/deps -C incremental=/home/richard/Code/nerd-stack/target/debug/incremental -L dependency=/home/richard/Code/nerd-stack/target/debug/deps --extern libc=/home/richard/Code/nerd-stack/target/debug/deps/liblibc-1d53503271082800.rlib` (exit code: 101)

Running nixos 17.09 using rust nightly installed using the mozilla nixpkgs overlay. It's about a month out of date, so I'll check the updates.

@dtolnay
Copy link
Member

dtolnay commented Jan 28, 2018

This still crashes as of the most recent nightly. Self-contained repro:

struct ArpIPv4<'a> {
    s: &'a u8
}

impl<'a> ArpIPv4<'a> {
    const LENGTH: usize = 20;

    pub fn to_buffer() -> [u8; Self::LENGTH] {
        unimplemented!()
    }
}

Mentioning @eddyb who added this assertion in #41914.

@eddyb
Copy link
Member

eddyb commented Jan 28, 2018

@dtolnay That was confusing at first, heh - the multi-line assert_eq message which doesn't split custom messages on their own line doesn't help at all (can we split it off?).

So what's happening is the length expression is using a generic parameter in its outer scope. This hasn't ever been supported, but it's extremely obnoxious to prevent - we could ban Self in rustc_resolve but that already works in some specific situations, so it'd be backwards-incompatible.

This problem would get properly fixed once we actually start supporting generics-in-scope for array expressions, and that can't happen without lazy normalization (cc @nikomatsakis).

@pietroalbini pietroalbini added 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. C-bug Category: This is a bug. labels Jan 28, 2018
@internally-combusted
Copy link

internally-combusted commented Mar 19, 2019

I'm having the same problem still.

let new_quad: [VertexData; Renderer::QUAD_VERTICES().len()] = vec![];

succeeds in making the compiler cry about the function call in the array size, but with

let new_quad: [VertexData; Self::QUAD_VERTICES().len()] = vec![];

the compiler panics.

Here's my rustc --version --verbose:

rustc 1.33.0 (2aa4c46cf 2019-02-28)
binary: rustc
commit-hash: 2aa4c46cfdd726e97360c2734835aa3515e8c858
commit-date: 2019-02-28
host: x86_64-apple-darwin
release: 1.33.0
LLVM version: 8.0

@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Oct 15, 2019
@Centril Centril added the A-borrow-checker Area: The borrow checker label Mar 10, 2020
@Centril
Copy link
Contributor

Centril commented Mar 10, 2020

@dtolnay's exampe ICEs at rustc_mir::borrow_check::universal_regions::UniversalRegionIndices::to_region_vid::{{closure}}, cc @matthewjasper

@lcnr lcnr added A-lazy-normalization Area: Lazy normalization (tracking issue: #60471) and removed A-borrow-checker Area: The borrow checker labels Jul 3, 2020
Manishearth added a commit to Manishearth/rust that referenced this issue Jul 4, 2020
…arkor

add `lazy_normalization_consts` feature gate

In rust-lang#71973 I underestimated the amount of code which is influenced by lazy normalization of consts
and decided against having a separate feature flag for this.

Looking a bit more into this, the following issues are already working with lazy norm in its current state rust-lang#47814 rust-lang#57739 rust-lang#73980

I therefore think it is worth it to enable lazy norm separately. Note that `#![feature(const_generics)]` still automatically activates
this feature, so using `#![feature(const_generics, lazy_normalization_consts)]` is redundant.

r? @varkor @nikomatsakis
Manishearth added a commit to Manishearth/rust that referenced this issue Jul 5, 2020
…arkor

add `lazy_normalization_consts` feature gate

In rust-lang#71973 I underestimated the amount of code which is influenced by lazy normalization of consts
and decided against having a separate feature flag for this.

Looking a bit more into this, the following issues are already working with lazy norm in its current state rust-lang#47814 rust-lang#57739 rust-lang#73980

I therefore think it is worth it to enable lazy norm separately. Note that `#![feature(const_generics)]` still automatically activates
this feature, so using `#![feature(const_generics, lazy_normalization_consts)]` is redundant.

r? @varkor @nikomatsakis
Manishearth added a commit to Manishearth/rust that referenced this issue Jul 5, 2020
…arkor

add `lazy_normalization_consts` feature gate

In rust-lang#71973 I underestimated the amount of code which is influenced by lazy normalization of consts
and decided against having a separate feature flag for this.

Looking a bit more into this, the following issues are already working with lazy norm in its current state rust-lang#47814 rust-lang#57739 rust-lang#73980

I therefore think it is worth it to enable lazy norm separately. Note that `#![feature(const_generics)]` still automatically activates
this feature, so using `#![feature(const_generics, lazy_normalization_consts)]` is redundant.

r? @varkor @nikomatsakis
@philw07
Copy link

philw07 commented Dec 12, 2020

Issue is still present on current stable and nightly.
Just wanted to add that it only occurs when using a lifetime parameter.

@dtolnay's example ICEs whereas the following with removed lifetime parameter works fine.

struct ArpIPv4 {
    s: u8
}

impl ArpIPv4 {
    const LENGTH: usize = 20;

    pub fn to_buffer() -> [u8; Self::LENGTH] {
        unimplemented!()
    }
}

@JohnTitor
Copy link
Member

Triage:
This is no longer ICE with the latest nightly: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=fcbba8b50e86684074a03bc27d825d91

@cjgillot cjgillot added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Dec 3, 2022
JohnTitor added a commit to JohnTitor/rust that referenced this issue Dec 4, 2022
Signed-off-by: Yuki Okushi <jtitor@2k36.org>
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 4, 2022
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#101975 (Suggest to use . instead of :: when accessing a method of an object)
 - rust-lang#105141 (Fix ICE on invalid variable declarations in macro calls)
 - rust-lang#105224 (Properly substitute inherent associated types.)
 - rust-lang#105236 (Add regression test for rust-lang#47814)
 - rust-lang#105247 (Use parent function WfCheckingContext to check RPITIT.)
 - rust-lang#105253 (Update a couple of rustbuild deps)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in f3eba21 Dec 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lazy-normalization Area: Lazy normalization (tracking issue: #60471) 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. glacier ICE tracked in rust-lang/glacier. 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.