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 error involving HRTBs #115807

Closed
dlubarov opened this issue Sep 13, 2023 · 2 comments · Fixed by #115834
Closed

Compiler error involving HRTBs #115807

dlubarov opened this issue Sep 13, 2023 · 2 comments · Fixed by #115834
Assignees
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

@dlubarov
Copy link

Code

trait Chip: for<'a> TraitWithLifetime<'a> + SomeMarker {
    fn compute(&self);
}

trait SomeMarker {}

trait TraitWithLifetime<'a>: SomeMarker {}

trait Machine {
    fn run();
}

struct BasicMachine;

impl Machine for BasicMachine {
    fn run() {
        let chips: [&dyn Chip; 0] = [];
        let _ = chips.map(|chip| chip.compute());
    }
}

Meta

rustc --version --verbose:

rustc 1.72.0 (5680fa18f 2023-08-23)
binary: rustc
commit-hash: 5680fa18feaa87f3ff04063800aec256c3d4b4be
commit-date: 2023-08-23
host: aarch64-apple-darwin
release: 1.72.0
LLVM version: 16.0.5

The bug seems to be present in the latest nightly at well as stable.

Error output

error: internal compiler error: compiler/rustc_trait_selection/src/traits/vtable.rs:344:9: Failed to find info for expected trait in vtable
Backtrace

thread 'rustc' panicked at 'Box<dyn Any>', /rustc/5680fa18feaa87f3ff04063800aec256c3d4b4be/compiler/rustc_errors/src/lib.rs:1651:9
stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: <rustc_errors::HandlerInner>::bug::<alloc::string::String>
   2: <rustc_errors::Handler>::bug::<alloc::string::String>
   3: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>::{closure#0}
   4: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}
   5: rustc_middle::ty::context::tls::with_context_opt::<rustc_middle::ty::context::tls::with_opt<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
   6: rustc_middle::util::bug::bug_fmt
   7: rustc_trait_selection::traits::vtable::vtable_trait_first_method_offset
   8: <rustc_trait_selection::traits::select::SelectionContext>::confirm_candidate
   9: <rustc_trait_selection::traits::select::SelectionContext>::poly_select
  10: <rustc_trait_selection::traits::fulfill::FulfillProcessor>::process_trait_obligation
  11: <rustc_trait_selection::traits::fulfill::FulfillProcessor as rustc_data_structures::obligation_forest::ObligationProcessor>::process_obligation
  12: <rustc_data_structures::obligation_forest::ObligationForest<rustc_trait_selection::traits::fulfill::PendingPredicateObligation>>::process_obligations::<rustc_trait_selection::traits::fulfill::FulfillProcessor>
  13: <rustc_trait_selection::traits::fulfill::FulfillmentContext as rustc_infer::traits::engine::TraitEngine>::select_where_possible
  14: rustc_traits::codegen::codegen_select_candidate
  15: rustc_ty_utils::instance::resolve_instance
  16: <rustc_middle::ty::instance::Instance>::resolve
  17: <rustc_middle::ty::instance::Instance>::expect_resolve
  18: <rustc_monomorphize::collector::MirUsedCollector as rustc_middle::mir::visit::Visitor>::visit_terminator
  19: rustc_monomorphize::collector::collect_used_items
  20: rustc_monomorphize::collector::collect_items_rec
  21: rustc_monomorphize::collector::collect_items_rec
  22: rustc_monomorphize::collector::collect_items_rec
  23: rustc_monomorphize::collector::collect_items_rec
  24: rustc_monomorphize::collector::collect_items_rec
  25: rustc_monomorphize::collector::collect_items_rec
  26: rustc_monomorphize::collector::collect_items_rec
  27: rustc_monomorphize::collector::collect_items_rec
  28: rustc_monomorphize::collector::collect_items_rec
  29: rustc_monomorphize::collector::collect_items_rec
  30: rustc_monomorphize::collector::collect_items_rec
  31: <core::panic::unwind_safe::AssertUnwindSafe<rustc_data_structures::sync::par_for_each_in<alloc::vec::Vec<rustc_middle::mir::mono::MonoItem>, rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>::{closure#0}::{closure#0}> as core::ops::function::FnOnce<()>>::call_once
  32: rustc_data_structures::sync::par_for_each_in::<alloc::vec::Vec<rustc_middle::mir::mono::MonoItem>, rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>
  33: <rustc_session::session::Session>::time::<(), rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}>
  34: rustc_monomorphize::collector::collect_crate_mono_items
  35: rustc_monomorphize::partitioning::collect_and_partition_mono_items
  36: <rustc_metadata::rmeta::encoder::EncodeContext>::encode_crate_root
  37: rustc_metadata::rmeta::encoder::encode_metadata_impl
  38: rustc_data_structures::sync::join::<rustc_metadata::rmeta::encoder::encode_metadata::{closure#0}, rustc_metadata::rmeta::encoder::encode_metadata::{closure#1}, (), ()>
  39: rustc_metadata::rmeta::encoder::encode_metadata
  40: rustc_metadata::fs::encode_and_write_metadata
  41: rustc_interface::passes::start_codegen
  42: <rustc_middle::ty::context::GlobalCtxt>::enter::<<rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_span::ErrorGuaranteed>>
  43: <rustc_interface::queries::Queries>::ongoing_codegen
  44: <rustc_interface::interface::Compiler>::enter::<rustc_driver_impl::run_compiler::{closure#1}::{closure#2}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_span::ErrorGuaranteed>>
  45: rustc_span::set_source_map::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#1}>::{closure#0}::{closure#0}>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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.72.0 (5680fa18f 2023-08-23) running on aarch64-apple-darwin

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked -C incremental=[REDACTED]

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

query stack during panic:
#0 [codegen_select_candidate] computing candidate for `<dyn Chip as Chip>`
#1 [resolve_instance] resolving instance `<dyn Chip as Chip>::compute`
#2 [collect_and_partition_mono_items] collect_and_partition_mono_items
#3 [exported_symbols] collecting exported symbols for crate `0`
end of query stack

@dlubarov dlubarov 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 Sep 13, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 13, 2023
@dlubarov
Copy link
Author

It seems like this has something to do with SomeMarker appearing multiple times, once in Chip's bound, then again in TraitWithLifetime's. Switching the order of the two Chip bounds seems to avoid the error.

@matthiaskrgr
Copy link
Member

Regression in nightly-2021-07-25
commit[0] 2021-07-23: Auto merge of #87413 - JohnTitor:rollup-dht22jk, r=JohnTitor
commit[1] 2021-07-23: Auto merge of #86429 - JohnTitor:get-by-key-enum-part-2, r=oli-obk
commit[2] 2021-07-24: Auto merge of #87338 - SparrowLii:MaybeTrait, r=wesleywiser
commit[3] 2021-07-24: Auto merge of #87415 - Mark-Simulacrum:bump-version, r=Mark-Simulacrum
commit[4] 2021-07-24: Auto merge of #84589 - In-line:zircon-thread-name, r=JohnTitor
commit[5] 2021-07-24: Auto merge of #86461 - crlf0710:rich_vtable, r=nikomatsakis
commit[6] 2021-07-24: Auto merge of #87296 - Aaron1011:inert-warn, r=petrochenkov
commit[7] 2021-07-24: Auto merge of #87434 - Manishearth:rollup-b09njin, r=Manishearth
commit[8] 2021-07-24: Auto merge of #86580 - BoxyUwU:cgd-subst-ice, r=nikomatsakis

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 13, 2023
@compiler-errors compiler-errors self-assigned this Sep 13, 2023
@bors bors closed this as completed in 5737082 Sep 14, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 14, 2023
Rollup merge of rust-lang#115834 - compiler-errors:binder-vars, r=jackh726

Properly consider binder vars in `HasTypeFlagsVisitor`

Given a PolyTraitRef like `for<'a> Ty: Trait` (where neither `Ty` nor `Trait` mention `'a`), we do *not* return true for `.has_type_flags(TypeFlags::HAS_LATE_BOUND)`, even though binders are supposed to act as if they have late-bound vars even if they don't mention them in their bound value: 31ae3b2. This is because we use `HasTypeFlagsVisitor`, which only computes the type flags for `Ty`, `Const` and `Region` and `Predicates`, and we consequently skip any binders (and setting flags for their vars) that are not contained in one of these types.

This ends up causing a problem, because when we call `TyCtxt::erase_regions` (which both erases regions *and* anonymizes bound vars), we will skip such a PolyTraitRef, not anonymizing it, and therefore not making it structurally equal to other binders. This breaks vtable computations.

This PR computes the flags for all binders we enter in `HasTypeFlagsVisitor` if we're looking for `TypeFlags::HAS_LATE_BOUND` (or `TypeFlags::HAS_{RE,TY,CT}_LATE_BOUND`).

Fixes rust-lang#115807
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

Successfully merging a pull request may close this issue.

5 participants