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

Compiler panics during incremental recompilation, while using an associated constant as array size #54242

Closed
vkorenev opened this issue Sep 15, 2018 · 10 comments · Fixed by #58432
Labels
A-associated-items Area: Associated items (types, constants & functions) A-const-eval Area: Constant evaluation (MIR interpretation) A-incr-comp Area: Incremental compilation I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@vkorenev
Copy link

Code

use std::mem::size_of;

trait Tr {
    type Arr;

    const C: usize = size_of::<u64>();
}

impl Tr for str {
    type Arr = [u8; Self::C];
}

rustc --version --verbose

rustc 1.30.0-nightly (f2302da 2018-09-12)
binary: rustc
commit-hash: f2302da
commit-date: 2018-09-12
host: x86_64-unknown-linux-gnu
release: 1.30.0-nightly
LLVM version: 8.0

Stack trace

thread 'main' panicked at 'did not find a cycle', librustc/ty/query/job.rs:144:9
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::print
             at libstd/sys_common/backtrace.rs:71
             at libstd/sys_common/backtrace.rs:59
   2: std::panicking::default_hook::{{closure}}
             at libstd/panicking.rs:211
   3: std::panicking::default_hook
             at libstd/panicking.rs:227
   4: rustc::util::common::panic_hook
   5: std::panicking::rust_panic_with_hook
             at libstd/panicking.rs:481
   6: std::panicking::begin_panic
   7: rustc::ty::query::job::QueryJob::await
   8: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
   9: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_const
  10: rustc::ty::structural_impls::<impl rustc::ty::fold::TypeFoldable<'tcx> for &'tcx rustc::ty::TyS<'tcx>>::super_fold_with
  11: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  12: rustc::traits::project::opt_normalize_projection_type
  13: rustc::traits::project::normalize_projection_type
  14: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  15: <smallvec::SmallVec<A> as core::iter::traits::FromIterator<<A as smallvec::Array>::Item>>::from_iter
  16: rustc::ty::fold::TypeFoldable::fold_with
  17: rustc::ty::fold::TypeFoldable::fold_with
  18: rustc::ty::fold::TypeFoldable::fold_with
  19: rustc::traits::project::normalize
  20: rustc::infer::InferCtxt::partially_normalize_associated_types_in
  21: rustc_typeck::check::FnCtxt::instantiate_bounds
  22: rustc_typeck::check::FnCtxt::instantiate_value_path
  23: rustc_typeck::check::FnCtxt::check_expr_kind
  24: rustc_typeck::check::FnCtxt::check_expr_with_expectation_and_needs
  25: rustc::ty::context::tls::with_related_context
  26: rustc::infer::InferCtxtBuilder::enter
  27: rustc_typeck::check::typeck_tables_of
  28: rustc::ty::query::__query_compute::typeck_tables_of
  29: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::typeck_tables_of<'tcx>>::compute
  30: rustc::ty::context::tls::with_context
  31: rustc::dep_graph::graph::DepGraph::with_task_impl
  32: rustc::ty::context::tls::with_related_context
  33: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query_with_job
  34: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query
  35: rustc::ty::query::plumbing::force_from_dep_node
  36: rustc::dep_graph::graph::DepGraph::try_mark_green
  37: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::try_mark_green_and_read
  38: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  39: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_const
  40: rustc::ty::structural_impls::<impl rustc::ty::fold::TypeFoldable<'tcx> for &'tcx rustc::ty::TyS<'tcx>>::super_fold_with
  41: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  42: rustc::traits::project::opt_normalize_projection_type
  43: rustc::traits::project::normalize_projection_type
  44: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  45: rustc::ty::fold::TypeFoldable::fold_with
  46: rustc::ty::fold::TypeFoldable::fold_with
  47: rustc::traits::project::normalize
  48: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &'a mut F>::call_once
  49: <core::iter::FlatMap<I, U, F> as core::iter::iterator::Iterator>::next
  50: <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T, I>>::from_iter
  51: rustc::ty::wf::WfPredicates::normalize
  52: rustc::ty::wf::trait_obligations
  53: rustc::ty::context::tls::with_related_context
  54: rustc::infer::InferCtxtBuilder::enter
  55: rustc_typeck::check::wfcheck::check_item_well_formed
  56: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::check_item_well_formed<'tcx>>::compute
  57: rustc::ty::context::tls::with_context
  58: rustc::dep_graph::graph::DepGraph::with_task_impl
  59: rustc::ty::context::tls::with_related_context
  60: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query_with_job
  61: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  62: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::ensure_query
  63: rustc::hir::Crate::visit_all_item_likes
  64: rustc::session::Session::track_errors
  65: rustc::util::common::time
  66: rustc_typeck::check_crate
  67: rustc::ty::context::tls::enter_context
  68: <std::thread::local::LocalKey<T>>::with
  69: rustc::ty::context::TyCtxt::create_and_enter
  70: rustc_driver::driver::compile_input
  71: rustc_driver::run_compiler_with_pool
  72: <scoped_tls::ScopedKey<T>>::set
  73: rustc_driver::run_compiler
  74: syntax::with_globals
  75: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:102
  76: rustc_driver::run
  77: rustc_driver::main
  78: std::rt::lang_start::{{closure}}
  79: std::panicking::try::do_call
             at libstd/rt.rs:59
             at libstd/panicking.rs:310
  80: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:102
  81: std::rt::lang_start_internal
             at libstd/panicking.rs:289
             at libstd/panic.rs:392
             at libstd/rt.rs:58
  82: main
  83: __libc_start_main
  84: <unknown>
query stack during panic:
#0 [typeck_tables_of] processing `<str as ice::Tr>::Arr::{{constant}}`
#1 [check_item_well_formed] processing `<str as ice::Tr>`
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.30.0-nightly (f2302daef 2018-09-12) running on x86_64-unknown-linux-gnu

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

note: some of the compiler flags provided by cargo are hidden
@memoryruins
Copy link
Contributor

Do you experience an ICE on the latest nightly?

rustc 1.30.0-nightly (2ab3eba30 2018-09-14)

error[E0391]: cycle detected when const-evaluating `<impl at src/main.rs:9:1: 11:2>::Arr::{{constant}}`
  --> src/main.rs:10:21
   |
10 |     type Arr = [u8; Self::C];
   |                     ^^^^^^^
   |
note: ...which requires processing `<impl at src/main.rs:9:1: 11:2>::Arr::{{constant}}`...
  --> src/main.rs:10:21
   |
10 |     type Arr = [u8; Self::C];
   |                     ^^^^^^^
   = note: ...which again requires const-evaluating `<impl at src/main.rs:9:1: 11:2>::Arr::{{constant}}`, completing the cycle
note: cycle used when processing `<impl at src/main.rs:9:1: 11:2>`
  --> src/main.rs:9:1
   |
9  | impl Tr for str {
   | ^^^^^^^^^^^^^^^

error: aborting due to previous error

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

@memoryruins memoryruins added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Sep 15, 2018
@vkorenev
Copy link
Author

No longer occurs on rustc 1.30.0-nightly (2ab3eba30 2018-09-14).

@memoryruins
Copy link
Contributor

@RalfJung do you think #54076 helped prevent the ICE? I could be overlooking another PR that might have helped. Either way, should this example be added to the test suite?

@RalfJung
Copy link
Member

Uh, no idea. I would be rather surprised. But yeah it seems prudent to add this as a test-case.

Though, should this really be a cycle? C does not actually depend on Arr. Cc @eddyb @oli-obk

@memoryruins memoryruins reopened this Sep 16, 2018
@memoryruins memoryruins added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. A-associated-items Area: Associated items (types, constants & functions) A-const-eval Area: Constant evaluation (MIR interpretation) labels Sep 16, 2018
@memoryruins
Copy link
Contributor

memoryruins commented Sep 16, 2018

The same cycle detected error occurs on current stable as well.
rust version 1.29.0 (aa3ca1994 2018-09-11)

Edit: The same cycle error, with some minor tweaks to diagnostic wording, occurs on every stable version since 1.20. Associated constants were experimental in 1.19.

@eddyb
Copy link
Member

eddyb commented Sep 16, 2018

I guess this should work, given that Self::C is <str as Tr>::C and so shouldn't fall under #43408.

cc @nikomatsakis

@memoryruins
Copy link
Contributor

In hindsight, I should have done this originally. Tried 1.30.0-nightly (f2302daef 2018-09-12) from the original post on Linux and Windows, but I was unable to recreate the ICE. @vkorenev did you experience it in a new/clean project or pre-existing project?

@vkorenev
Copy link
Author

I have found steps to reproduce it on the latest nightly (2224a42 2018-09-17):

  1. Create a project with the following lib.rs
use std::mem::size_of;

trait Tr {
    type Arr;

    const C: usize = size_of::<u64>();

    fn get_arr() -> Self::Arr;
}

impl Tr for str {
    type Arr = [u8; 8];

    fn get_arr() -> Self::Arr {
        [0; Self::C]
    }
}
  1. cargo clean
  2. cargo build
  3. Replace type Arr = [u8; 8]; with type Arr = [u8; Self::C];
  4. cargo build results in
thread 'main' panicked at 'did not find a cycle', librustc/ty/query/job.rs:144:9                         
stack backtrace:                                                                                         
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace                                          
             at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49                                            
   1: std::sys_common::backtrace::print                                                                  
             at libstd/sys_common/backtrace.rs:71                                                        
             at libstd/sys_common/backtrace.rs:59                                                        
   2: std::panicking::default_hook::{{closure}}                                                          
             at libstd/panicking.rs:211                                                                  
   3: std::panicking::default_hook                                                                       
             at libstd/panicking.rs:227                                                                  
   4: rustc::util::common::panic_hook                                                                    
   5: std::panicking::rust_panic_with_hook                                                               
             at libstd/panicking.rs:481                                                                  
   6: std::panicking::begin_panic                                                                        
   7: rustc::ty::query::job::QueryJob::await                                                             
   8: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query           
   9: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_const
  10: rustc::ty::structural_impls::<impl rustc::ty::fold::TypeFoldable<'tcx> for &'tcx rustc::ty::TyS<'tcx>>::super_fold_with
  11: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  12: rustc::traits::project::opt_normalize_projection_type                                              
  13: rustc::traits::project::normalize_projection_type                                                  
  14: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  15: <smallvec::SmallVec<A> as core::iter::traits::FromIterator<<A as smallvec::Array>::Item>>::from_iter
  16: rustc::ty::fold::TypeFoldable::fold_with                                                           
  17: rustc::ty::fold::TypeFoldable::fold_with                                                           
  18: rustc::ty::fold::TypeFoldable::fold_with                                                           
  19: rustc::traits::project::normalize                                                                  
  20: rustc::infer::InferCtxt::partially_normalize_associated_types_in                                   
  21: rustc_typeck::check::FnCtxt::instantiate_bounds                                                    
  22: rustc_typeck::check::FnCtxt::instantiate_value_path                                                
  23: rustc_typeck::check::FnCtxt::check_expr_kind                                                       
  24: rustc_typeck::check::FnCtxt::check_expr_with_expectation_and_needs                                 
  25: rustc::ty::context::tls::with_related_context                                                      
  26: rustc::infer::InferCtxtBuilder::enter                                                              
  27: rustc_typeck::check::typeck_tables_of                                                              
  28: rustc::ty::query::__query_compute::typeck_tables_of                                                
  29: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::typeck_tables_of<'tcx>>::compute
  30: rustc::ty::context::tls::with_context                                                              
  31: rustc::dep_graph::graph::DepGraph::with_task_impl                                                  
  32: rustc::ty::context::tls::with_related_context                                                      
  33: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query_with_job
  34: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query         
  35: rustc::ty::query::plumbing::force_from_dep_node                                                    
  36: rustc::dep_graph::graph::DepGraph::try_mark_green                                                  
  37: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::try_mark_green_and_read
  38: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query           
  39: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_const
  40: rustc::ty::structural_impls::<impl rustc::ty::fold::TypeFoldable<'tcx> for &'tcx rustc::ty::TyS<'tcx>>::super_fold_with
  41: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  42: rustc::traits::project::opt_normalize_projection_type                                              
  43: rustc::traits::project::normalize_projection_type                                                  
  44: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  45: rustc::ty::fold::TypeFoldable::fold_with                                                           
  46: rustc::ty::fold::TypeFoldable::fold_with                                                           
  47: rustc::traits::project::normalize                                                                  
  48: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &'a mut F>::call_once         
  49: <core::iter::FlatMap<I, U, F> as core::iter::iterator::Iterator>::next                             
  50: <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T, I>>::from_iter                                    
  51: rustc::ty::wf::WfPredicates::normalize                                                             
  52: rustc::ty::wf::trait_obligations                                                                   
  53: rustc::ty::context::tls::with_related_context                                                      
  54: rustc::infer::InferCtxtBuilder::enter                                                              
  55: rustc_typeck::check::wfcheck::check_item_well_formed                                               
  56: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::check_item_well_formed<'tcx>>::compute
  57: rustc::ty::context::tls::with_context                                                              
  58: rustc::dep_graph::graph::DepGraph::with_task_impl                                                  
  59: rustc::ty::context::tls::with_related_context                                                      
  60: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query_with_job
  61: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query           
  62: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::ensure_query        
  63: rustc::hir::Crate::visit_all_item_likes                                                            
  64: rustc::session::Session::track_errors                                                              
  65: rustc_typeck::check_crate                                                                          
  66: rustc::ty::context::tls::enter_context                                                             
  67: <std::thread::local::LocalKey<T>>::with                                                            
  68: rustc::ty::context::TyCtxt::create_and_enter                                                       
  69: rustc_driver::driver::compile_input                                                                
  70: rustc_driver::run_compiler_with_pool                                                               
  71: rustc_driver::driver::spawn_thread_pool                                                            
  72: rustc_driver::run_compiler                                                                         
  73: <scoped_tls::ScopedKey<T>>::set                                                                    
  74: syntax::with_globals                                                                               
  75: __rust_maybe_catch_panic                                                                           
             at libpanic_unwind/lib.rs:102                                                               
  76: rustc_driver::run                                                                                  
  77: rustc_driver::main                                                                                 
  78: std::rt::lang_start::{{closure}}                                                                   
  79: std::panicking::try::do_call                                                                       
             at libstd/rt.rs:59                                                                          
             at libstd/panicking.rs:310                                                                  
  80: __rust_maybe_catch_panic                                                                           
             at libpanic_unwind/lib.rs:102                                                               
  81: std::rt::lang_start_internal                                                                       
             at libstd/panicking.rs:289                                                                  
             at libstd/panic.rs:392                                                                      
             at libstd/rt.rs:58                                                                          
  82: main                                                                                               
  83: __libc_start_main                                                                                  
  84: <unknown>                                                                                          
query stack during panic:                                                                                
#0 [typeck_tables_of] processing `<str as Tr>::Arr::{{constant}}`                                        
#1 [check_item_well_formed] processing `<str as Tr>`                                                     
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.30.0-nightly (2224a42c3 2018-09-17) running on x86_64-unknown-linux-gnu                    
                                                                                                         
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 `associated-const`.                                                             

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

@eddyb eddyb added A-incr-comp Area: Incremental compilation and removed E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. labels Sep 18, 2018
@eddyb eddyb changed the title Compiler panics when using an associated constant as array size Compiler panics during incremental recompilation, while using an associated constant as array size Sep 18, 2018
@eddyb
Copy link
Member

eddyb commented Sep 18, 2018

cc @nikomatsakis @michaelwoerister

@Zoxc
Copy link
Contributor

Zoxc commented Feb 12, 2019

This is because we try to mark a query A as green with try_mark_green_and_read without properly starting the current query job with JobOwner::start. try_mark_green_and_read can force queries, including the query A which initially called it, but we can't generate the cycle error because query A won't be on the query stack.

bors added a commit that referenced this issue Feb 13, 2019
Rollup of 10 pull requests

Successful merges:

 - #58110 (libpanic_unwind => 2018)
 - #58167 (HirId-ify hir::BodyId)
 - #58202 (Ignore future deprecations in #[deprecated])
 - #58272 (Cut down on number formating code size)
 - #58276 (Improve the error messages for missing stability attributes)
 - #58354 (Fix ICE and invalid filenames in MIR printing code)
 - #58381 (Only suggest imports if not imported.)
 - #58386 (Fix #54242)
 - #58400 (Fix rustc_driver swallowing errors when compilation is stopped)
 - #58420 (target/uefi: clarify documentation)

Failed merges:

r? @ghost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-const-eval Area: Constant evaluation (MIR interpretation) A-incr-comp Area: Incremental compilation I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
5 participants