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 stable panics when optimizing MIR, attempting to unwrap trait object #86351

Closed
JoelCourtney opened this issue Jun 16, 2021 · 5 comments · Fixed by #109969
Closed

Rustc stable panics when optimizing MIR, attempting to unwrap trait object #86351

JoelCourtney opened this issue Jun 16, 2021 · 5 comments · Fixed by #109969
Assignees
Labels
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) ❄️ P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@JoelCourtney
Copy link

Code

pub trait TestTrait {
    type MyType;
    fn func() -> Option<Self> where Self: Sized;
}

impl<T> dyn TestTrait<MyType = T> where Self: Sized {
    fn other_func() -> Option<Self> {
        match Self::func() {
            Some(me) => Some(me),
            None => None
        }
    }
}

This only happens when compiled in a library, not an executable.

The associated type MyType, both of the where Self: Sized's, the Option<Self> (instead of just Self), and the match statement are all needed to make it panic.

Replacing the match statement with Some(Self::func().unwrap()) errors gracefully, and says that I cannot call unwrap on a trait object; so I assume the error that should have been emitted for the match statement is that I cannot destructure a trait object.

Replacing the match with Self::func() compiles successfully.

Meta

This happens on stable, nightly, and beta.

rustc --version --verbose:

rustc 1.52.1 (9bc8c42bb 2021-05-09)
binary: rustc
commit-hash: 9bc8c42bb2f19e745a63f3445f1ac248fb015e53
commit-date: 2021-05-09
host: x86_64-apple-darwin
release: 1.52.1
LLVM version: 12.0.0

Error output

error: internal compiler error: compiler/rustc_mir/src/interpret/place.rs:44:17: expected wide pointer extra data (e.g. slice length or trait object vtable)

thread 'rustc' panicked at 'Box<Any>', /rustc/9bc8c42bb2f19e745a63f3445f1ac248fb015e53/library/std/src/panic.rs:59:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.52.1 (9bc8c42bb 2021-05-09) running on x86_64-apple-darwin

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

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

query stack during panic:
#0 [optimized_mir] optimizing MIR for `<impl at src/lib.rs:6:1: 13:2>::other_func`
end of query stack
Backtrace

stack backtrace:
   0: std::panicking::begin_panic
   1: std::panic::panic_any
   2: rustc_errors::HandlerInner::bug
   3: rustc_errors::Handler::bug
   4: rustc_middle::util::bug::opt_span_bug_fmt::{{closure}}
   5: rustc_middle::ty::context::tls::with_opt::{{closure}}
   6: rustc_middle::ty::context::tls::with_opt
   7: rustc_middle::util::bug::opt_span_bug_fmt
   8: rustc_middle::util::bug::bug_fmt
   9: rustc_mir::interpret::eval_context::InterpCx<M>::size_and_align_of
  10: rustc_mir::interpret::place::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::place_field
  11: rustc_mir::interpret::place::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::eval_place
  12: rustc_mir::interpret::step::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::eval_rvalue_into_place
  13: <rustc_mir::transform::const_prop::ConstPropagator as rustc_middle::mir::visit::MutVisitor>::visit_statement
  14: <rustc_mir::transform::const_prop::ConstPropagator as rustc_middle::mir::visit::MutVisitor>::visit_body
  15: <rustc_mir::transform::const_prop::ConstProp as rustc_mir::transform::MirPass>::run_pass
  16: rustc_mir::transform::run_passes
  17: rustc_mir::transform::optimized_mir
  18: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  19: rustc_data_structures::stack::ensure_sufficient_stack
  20: rustc_query_system::query::plumbing::force_query_with_job
  21: rustc_query_system::query::plumbing::get_query_impl
  22: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::optimized_mir
  23: rustc_metadata::rmeta::encoder::EncodeContext::encode_crate_root
  24: rustc_metadata::rmeta::encoder::encode_metadata_impl
  25: rustc_data_structures::sync::join
  26: rustc_metadata::rmeta::decoder::cstore_impl::<impl rustc_middle::middle::cstore::CrateStore for rustc_metadata::creader::CStore>::encode_metadata
  27: rustc_middle::ty::context::TyCtxt::encode_metadata
  28: rustc_interface::passes::QueryContext::enter
  29: rustc_interface::queries::Queries::ongoing_codegen
  30: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  31: rustc_span::with_source_map
  32: rustc_interface::interface::create_compiler_and_run
  33: scoped_tls::ScopedKey<T>::set

@JoelCourtney JoelCourtney 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 Jun 16, 2021
@jonas-schievink
Copy link
Contributor

also happens on nightly

@jonas-schievink jonas-schievink added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jun 16, 2021
@apiraino
Copy link
Contributor

Assigning priority as discussed in the Zulip thread of the Prioritization Working Group.

@rustbot label -I-prioritize +P-medium

@rustbot rustbot added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jun 17, 2021
fanninpm added a commit to fanninpm/glacier that referenced this issue Jun 18, 2021
JohnTitor pushed a commit to fanninpm/glacier that referenced this issue Jun 21, 2021
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Jun 21, 2021
@JoelCourtney
Copy link
Author

This ICE still exists, but the "internal compiler error" and "query stack during panic" sections of the error output have changed on 1.58.0-nightly.

Meta

rustc --version --verbose

rustc 1.58.0-nightly (8b09ba6a5 2021-11-09)
binary: rustc
commit-hash: 8b09ba6a5d5c644fe0f1c27c7f9c80b334241707
commit-date: 2021-11-09
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0

I'm running this on a different machine than the one I initially posted the issue from.

Error Output

error: internal compiler error: /rustc/8b09ba6a5d5c644fe0f1c27c7f9c80b334241707/compiler/rustc_trait_selection/src/traits/util.rs:314:13: get_vtable_index_of_object_method: DefId(0:5 ~ scratch[c192]::TestTrait::func) was not found

thread 'rustc' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1169:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.58.0-nightly (8b09ba6a5 2021-11-09) running on x86_64-unknown-linux-gnu

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

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

query stack during panic:
#0 [resolve_instance] resolving instance `<dyn TestTrait<MyType = T> as TestTrait>::func`
#1 [mir_built] building MIR for `<impl at src/lib.rs:6:1: 13:2>::other_func`
end of query stack

The output in 1.56.1-stable is still the same as it was when I originally posted the issue.

Backtrace

stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: std::panic::panic_any::<rustc_errors::ExplicitBug>
   2: <rustc_errors::HandlerInner>::bug
   3: <rustc_errors::Handler>::bug
   4: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, ()>
   5: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>
   6: rustc_middle::util::bug::bug_fmt
   7: rustc_trait_selection::traits::util::get_vtable_index_of_object_method::<()>
   8: rustc_ty_utils::instance::inner_resolve_instance
   9: rustc_ty_utils::instance::resolve_instance
  10: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, rustc_middle::ty::ParamEnvAnd<(rustc_span::def_id::DefId, &rustc_middle::ty::list::List<rustc_middle::ty::subst::GenericArg>)>, core::result::Result<core::option::Option<rustc_middle::ty::instance::Instance>, rustc_errors::ErrorReported>>
  11: rustc_query_system::query::plumbing::get_query::<rustc_query_impl::queries::resolve_instance, rustc_query_impl::plumbing::QueryCtxt>
  12: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::resolve_instance
  13: <rustc_middle::ty::instance::Instance>::resolve
  14: <rustc_mir_build::lints::Search>::is_recursive_call
  15: <rustc_data_structures::graph::iterate::TriColorDepthFirstSearch<&rustc_middle::mir::Body>>::run_from_start::<rustc_mir_build::lints::Search>
  16: rustc_mir_build::lints::check
  17: <rustc_infer::infer::InferCtxtBuilder>::enter::<rustc_middle::mir::Body, rustc_mir_build::build::mir_build::{closure#1}>
  18: rustc_mir_build::build::mir_built
  19: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, rustc_middle::ty::WithOptConstParam<rustc_span::def_id::LocalDefId>, &rustc_data_structures::steal::Steal<rustc_middle::mir::Body>>
  20: rustc_data_structures::stack::ensure_sufficient_stack::<(&rustc_data_structures::steal::Steal<rustc_middle::mir::Body>, rustc_query_system::dep_graph::graph::DepNodeIndex), rustc_query_system::query::plumbing::execute_job<rustc_query_impl::plumbing::QueryCtxt, rustc_middle::ty::WithOptConstParam<rustc_span::def_id::LocalDefId>, &rustc_data_structures::steal::Steal<rustc_middle::mir::Body>>::{closure#3}>
  21: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::plumbing::QueryCtxt, rustc_query_system::query::caches::DefaultCache<rustc_middle::ty::WithOptConstParam<rustc_span::def_id::LocalDefId>, &rustc_data_structures::steal::Steal<rustc_middle::mir::Body>>>
  22: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::mir_built
  23: rustc_mir_transform::check_unsafety::unsafety_check_result
  24: <rustc_mir_transform::check_unsafety::provide::{closure#0} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_span::def_id::LocalDefId)>>::call_once
  25: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, rustc_span::def_id::LocalDefId, &rustc_middle::mir::query::UnsafetyCheckResult>
  26: rustc_data_structures::stack::ensure_sufficient_stack::<(&rustc_middle::mir::query::UnsafetyCheckResult, rustc_query_system::dep_graph::graph::DepNodeIndex), rustc_query_system::query::plumbing::execute_job<rustc_query_impl::plumbing::QueryCtxt, rustc_span::def_id::LocalDefId, &rustc_middle::mir::query::UnsafetyCheckResult>::{closure#3}>
  27: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::plumbing::QueryCtxt, rustc_query_system::query::caches::DefaultCache<rustc_span::def_id::LocalDefId, &rustc_middle::mir::query::UnsafetyCheckResult>>
  28: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::unsafety_check_result
  29: rustc_mir_transform::mir_const
  30: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, rustc_middle::ty::WithOptConstParam<rustc_span::def_id::LocalDefId>, &rustc_data_structures::steal::Steal<rustc_middle::mir::Body>>
  31: rustc_data_structures::stack::ensure_sufficient_stack::<(&rustc_data_structures::steal::Steal<rustc_middle::mir::Body>, rustc_query_system::dep_graph::graph::DepNodeIndex), rustc_query_system::query::plumbing::execute_job<rustc_query_impl::plumbing::QueryCtxt, rustc_middle::ty::WithOptConstParam<rustc_span::def_id::LocalDefId>, &rustc_data_structures::steal::Steal<rustc_middle::mir::Body>>::{closure#3}>
  32: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::plumbing::QueryCtxt, rustc_query_system::query::caches::DefaultCache<rustc_middle::ty::WithOptConstParam<rustc_span::def_id::LocalDefId>, &rustc_data_structures::steal::Steal<rustc_middle::mir::Body>>>
  33: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::mir_const
  34: rustc_mir_transform::mir_promoted
  35: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, rustc_middle::ty::WithOptConstParam<rustc_span::def_id::LocalDefId>, (&rustc_data_structures::steal::Steal<rustc_middle::mir::Body>, &rustc_data_structures::steal::Steal<rustc_index::vec::IndexVec<rustc_middle::mir::Promoted, rustc_middle::mir::Body>>)>
  36: rustc_data_structures::stack::ensure_sufficient_stack::<((&rustc_data_structures::steal::Steal<rustc_middle::mir::Body>, &rustc_data_structures::steal::Steal<rustc_index::vec::IndexVec<rustc_middle::mir::Promoted, rustc_middle::mir::Body>>), rustc_query_system::dep_graph::graph::DepNodeIndex), rustc_query_system::query::plumbing::execute_job<rustc_query_impl::plumbing::QueryCtxt, rustc_middle::ty::WithOptConstParam<rustc_span::def_id::LocalDefId>, (&rustc_data_structures::steal::Steal<rustc_middle::mir::Body>, &rustc_data_structures::steal::Steal<rustc_index::vec::IndexVec<rustc_middle::mir::Promoted, rustc_middle::mir::Body>>)>::{closure#3}>
  37: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::plumbing::QueryCtxt, rustc_query_system::query::caches::DefaultCache<rustc_middle::ty::WithOptConstParam<rustc_span::def_id::LocalDefId>, (&rustc_data_structures::steal::Steal<rustc_middle::mir::Body>, &rustc_data_structures::steal::Steal<rustc_index::vec::IndexVec<rustc_middle::mir::Promoted, rustc_middle::mir::Body>>)>>
  38: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::mir_promoted
  39: rustc_borrowck::mir_borrowck
  40: <rustc_borrowck::provide::{closure#0} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_span::def_id::LocalDefId)>>::call_once
  41: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, rustc_span::def_id::LocalDefId, &rustc_middle::mir::query::BorrowCheckResult>
  42: rustc_data_structures::stack::ensure_sufficient_stack::<(&rustc_middle::mir::query::BorrowCheckResult, rustc_query_system::dep_graph::graph::DepNodeIndex), rustc_query_system::query::plumbing::execute_job<rustc_query_impl::plumbing::QueryCtxt, rustc_span::def_id::LocalDefId, &rustc_middle::mir::query::BorrowCheckResult>::{closure#3}>
  43: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::plumbing::QueryCtxt, rustc_query_system::query::caches::DefaultCache<rustc_span::def_id::LocalDefId, &rustc_middle::mir::query::BorrowCheckResult>>
  44: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::mir_borrowck
  45: <rustc_middle::hir::map::Map>::par_body_owners::<rustc_interface::passes::analysis::{closure#2}::{closure#0}>
  46: <rustc_session::session::Session>::time::<(), rustc_interface::passes::analysis::{closure#2}>
  47: rustc_interface::passes::analysis
  48: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, (), core::result::Result<(), rustc_errors::ErrorReported>>
  49: rustc_data_structures::stack::ensure_sufficient_stack::<(core::result::Result<(), rustc_errors::ErrorReported>, rustc_query_system::dep_graph::graph::DepNodeIndex), rustc_query_system::query::plumbing::execute_job<rustc_query_impl::plumbing::QueryCtxt, (), core::result::Result<(), rustc_errors::ErrorReported>>::{closure#3}>
  50: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::plumbing::QueryCtxt, rustc_query_system::query::caches::DefaultCache<(), core::result::Result<(), rustc_errors::ErrorReported>>>
  51: rustc_query_system::query::plumbing::get_query::<rustc_query_impl::queries::analysis, rustc_query_impl::plumbing::QueryCtxt>
  52: <rustc_interface::passes::QueryContext>::enter::<rustc_driver::run_compiler::{closure#1}::{closure#2}::{closure#3}, core::result::Result<(), rustc_errors::ErrorReported>>
  53: <rustc_interface::interface::Compiler>::enter::<rustc_driver::run_compiler::{closure#1}::{closure#2}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_errors::ErrorReported>>
  54: rustc_span::with_source_map::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_interface::interface::create_compiler_and_run<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#0}>
  55: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>
query stack during panic:
#0 [resolve_instance] resolving instance `<dyn TestTrait<MyType = T> as TestTrait>::func`
#1 [mir_built] building MIR for `<impl at src/lib.rs:6:1: 13:2>::other_func`
#2 [unsafety_check_result] unsafety-checking `<impl at src/lib.rs:6:1: 13:2>::other_func`
#3 [mir_const] processing MIR for `<impl at src/lib.rs:6:1: 13:2>::other_func`
#4 [mir_promoted] processing `<impl at src/lib.rs:6:1: 13:2>::other_func`
#5 [mir_borrowck] borrow-checking `<impl at src/lib.rs:6:1: 13:2>::other_func`
#6 [analysis] running analysis passes on this crate
end of query stack

@JoelCourtney
Copy link
Author

JoelCourtney commented Nov 10, 2021

Additionally, the new ICE now happens when the code is compiled into an executable, but only on nightly. This didn't happen before. On 1.56.1-stable in src/main.rs, the compiler emits a warning that other_func is never used, and compiles successfully.

Edit: the error now happens on 1.60.0 stable when compiling in a binary as well.

@JoelCourtney JoelCourtney changed the title Rustc stable panics when optimizing MIR, attempting to unwrap trait object in lib.rs Rustc stable panics when optimizing MIR, attempting to unwrap trait object May 4, 2022
@JohnTitor
Copy link
Member

Triage: the issue has been fixed on the latest nightly, marking as E-needs-test.

@JohnTitor JohnTitor added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Feb 5, 2023
@JohnTitor JohnTitor self-assigned this Apr 5, 2023
JohnTitor added a commit to JohnTitor/rust that referenced this issue Apr 5, 2023
Signed-off-by: Yuki Okushi <jtitor@2k36.org>
JohnTitor added a commit to JohnTitor/rust that referenced this issue Apr 5, 2023
…errors

Add regression test for rust-lang#86351

r? `@compiler-errors`
Closes rust-lang#86351
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 5, 2023
Rollup of 7 pull requests

Successful merges:

 - rust-lang#109909 (Deny `use`ing tool paths)
 - rust-lang#109921 (Don't ICE when encountering `dyn*` in statics or consts)
 - rust-lang#109922 (Disable `has_thread_local` on OpenHarmony)
 - rust-lang#109926 (write threads info into log only when debugging)
 - rust-lang#109968 (Add regression test for rust-lang#80409)
 - rust-lang#109969 (Add regression test for rust-lang#86351)
 - rust-lang#109973 (rustdoc: Improve logo display very small screen)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
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. 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) ❄️ P-medium Medium priority 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