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 with trait objects and HRTBs #90177

Closed
patrick-gu opened this issue Oct 22, 2021 · 10 comments · Fixed by #90536
Closed

ICE with trait objects and HRTBs #90177

patrick-gu opened this issue Oct 22, 2021 · 10 comments · Fixed by #90536
Labels
C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@patrick-gu
Copy link
Contributor

Code

trait Base<'f> {
    type Assoc;

    fn do_something(&self);
}

trait ForAnyLifetime: for<'f> Base<'f> {}

impl<T> ForAnyLifetime for T where T: for<'f> Base<'f> {}

trait CanBeDynamic: ForAnyLifetime + for<'f> Base<'f, Assoc = ()> {}

fn foo(a: &dyn CanBeDynamic) {
    a.do_something();
}

fn main() {}

(Playground)

Meta

rustc --version --verbose:

rustc 1.56.0 (09c42c458 2021-10-18)
binary: rustc
commit-hash: 09c42c45858d5f3aedfa670698275303a3d19afa
commit-date: 2021-10-18
host: x86_64-pc-windows-msvc
release: 1.56.0
LLVM version: 13.0.0

This is also present in beta and nightly versions, but it is not present on 1.55.0 stable.

Error output

error: internal compiler error: compiler\rustc_trait_selection\src\traits\mod.rs:755:9: Failed to find info for expected trait in vtable

thread 'rustc' panicked at 'Box<dyn Any>', compiler\rustc_errors\src\lib.rs:1147:9
stack backtrace:
(see the backtrace section for the full 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.56.0 (09c42c458 2021-10-18) running on x86_64-pc-windows-msvc

note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C linker=C:\Program Files\LLVM\bin\lld-link.exe -C incremental --crate-type bin

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

query stack during panic:
#0 [typeck] type-checking `foo`
#1 [typeck_item_bodies] type-checking all item bodies
#2 [analysis] running analysis passes on this crate
end of query stack
error: could not compile `rust_ice`
Backtrace

   0:     0x7ffc97f07a9e - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h07cd892bde293768
   1:     0x7ffc97f3268a - core::fmt::write::h29abf31093ab5935
   2:     0x7ffc97efac88 - <std::io::IoSliceMut as core::fmt::Debug>::fmt::h40386600605061bb
   3:     0x7ffc97f0b586 - std::panicking::take_hook::he03d4c8985881b36
   4:     0x7ffc97f0b074 - std::panicking::take_hook::he03d4c8985881b36
   5:     0x7ffc8b17a525 - <tracing_subscriber::fmt::format::json::WriteAdaptor as std::io::Write>::flush::hde67080ba3658306
   6:     0x7ffc97f0be99 - std::panicking::rust_panic_with_hook::h806e5bba08cf7c42
   7:     0x7ffc8f7814c0 - <rustc_errors::styled_buffer::StyledChar as core::fmt::Debug>::fmt::h0f7f115d95d6e009
   8:     0x7ffc8f781449 - <rustc_errors::styled_buffer::StyledChar as core::fmt::Debug>::fmt::h0f7f115d95d6e009
   9:     0x7ffc8fa53bd1 - rustc_query_system::query::job::report_cycle::h9a6203922a678bda
  10:     0x7ffc8f7af1b0 - <rustc_errors::json::Diagnostic::from_errors_diagnostic::BufWriter as std::io::Write>::flush::h4514f0044ddd3ee4
  11:     0x7ffc8f7b7265 - rustc_errors::HandlerInner::emit_diagnostic::he222b80642dd446e
  12:     0x7ffc8f7b4d12 - rustc_errors::Handler::bug::h2c8c5efa855ae4be
  13:     0x7ffc8f49e3a7 - rustc_middle::ty::structural_impls::<impl core::fmt::Debug for rustc_middle::ty::closure::UpvarBorrow>::fmt::hd678a25acca54787
  14:     0x7ffc8f49c660 - rustc_middle::ty::consts::valtree::ValTree::zst::h8aa8eda0b4c752ce
  15:     0x7ffc8f49c603 - rustc_middle::ty::consts::valtree::ValTree::zst::h8aa8eda0b4c752ce
  16:     0x7ffc8f49e2d9 - rustc_middle::ty::structural_impls::<impl core::fmt::Debug for rustc_middle::ty::closure::UpvarBorrow>::fmt::hd678a25acca54787
  17:     0x7ffc8fa4a2c7 - rustc_middle::util::bug::bug_fmt::hdc415a5169316300
  18:     0x7ffc8f2ebd5e - rustc_trait_selection::traits::impossible_predicates::h6168446ca8ba4d2c
  19:     0x7ffc8f2face6 - <rustc_trait_selection::traits::select::TraitObligationStack as core::fmt::Debug>::fmt::h42371074e4d717d0
  20:     0x7ffc8f3013aa - rustc_trait_selection::traits::select::SelectionContext::select::hca123c58bc622ce1 
  21:     0x7ffc8f28d42e - <rustc_trait_selection::traits::fulfill::FulfillmentContext as rustc_infer::traits::engine::TraitEngine>::normalize_projection_type::h90c75a2320931b77
  22:     0x7ffc8f28ba9e - rustc_trait_selection::traits::fulfill::FulfillProcessor::progress_changed_obligations::hc42fb10a0d8c0dcc
  23:     0x7ffc8f2d5458 - <rustc_trait_selection::traits::specialize::OverlapError as core::fmt::Debug>::fmt::hab9f03740d18f5e8
  24:     0x7ffc8f289ffc - rustc_trait_selection::traits::fulfill::FulfillmentContext::new_ignoring_regions::h39d25fbf849faa7e
  25:     0x7ffc8f28a9e4 - <rustc_trait_selection::traits::fulfill::FulfillmentContext as rustc_infer::traits::engine::TraitEngine>::select_with_constness_where_possible::h14ff21ddcf1865f6
  26:     0x7ffc8d842fcb - rustc_typeck::check::fn_ctxt::_impl::<impl rustc_typeck::check::fn_ctxt::FnCtxt>::structurally_resolved_type::h713d93049989fe32
  27:     0x7ffc8d84234a - rustc_typeck::check::fn_ctxt::_impl::<impl rustc_typeck::check::fn_ctxt::FnCtxt>::structurally_resolved_type::h713d93049989fe32
  28:     0x7ffc8d827516 - rustc_typeck::check::demand::<impl rustc_typeck::check::fn_ctxt::FnCtxt>::check_for_cast::h4bd4dec8c57ead7f
  29:     0x7ffc8d8863b6 - rustc_typeck::check::expr::<impl rustc_typeck::check::fn_ctxt::FnCtxt>::check_expr_with_expectation::he4c4873ab2d8c9f3
  30:     0x7ffc8d847408 - rustc_typeck::check::fn_ctxt::checks::<impl rustc_typeck::check::fn_ctxt::FnCtxt>::check_stmt::h466a803b691a6b63
  31:     0x7ffc8d847ea1 - rustc_typeck::check::fn_ctxt::checks::<impl rustc_typeck::check::fn_ctxt::FnCtxt>::check_block_no_value::he72514003acfb9d8
  32:     0x7ffc8d8863b6 - rustc_typeck::check::expr::<impl rustc_typeck::check::fn_ctxt::FnCtxt>::check_expr_with_expectation::he4c4873ab2d8c9f3
  33:     0x7ffc8d831357 - rustc_typeck::check::demand::<impl rustc_typeck::check::fn_ctxt::FnCtxt>::check_for_cast::h4bd4dec8c57ead7f
  34:     0x7ffc8da65f41 - rustc_typeck::check::check::check_wf_new::hdffafb8990846ac0
  35:     0x7ffc8d9ae4cc - <<dyn rustc_typeck::astconv::AstConv>::create_substs_for_ast_path::SubstsForAstPathCtxt as rustc_typeck::astconv::CreateSubstsForGenericArgsCtxt>::inferred_kind::h930fec7b8dca6715
  36:     0x7ffc8d8b1443 - rustc_typeck::check::provide::h6a0f4ba684d1e07c
  37:     0x7ffc8e8fc306 - <rustc_span::def_id::DefIndex as rustc_query_impl::profiling_support::SpecIntoSelfProfilingString>::spec_to_self_profile_string::he71a7fa6f0493805
  38:     0x7ffc8e89e643 - <rustc_span::def_id::DefIndex as rustc_query_impl::profiling_support::SpecIntoSelfProfilingString>::spec_to_self_profile_string::he71a7fa6f0493805
  39:     0x7ffc8e9c6be9 - rustc_query_impl::on_disk_cache::__ty_decoder_impl::<impl rustc_serialize::serialize::Decoder for rustc_query_impl::on_disk_cache::CacheDecoder>::error::hbd644ab6b1f9b216
  40:     0x7ffc8e6a434a - <fixedbitset::FixedBitSet as core::fmt::Debug>::fmt::h6dcd24be3bf40fc6
  41:     0x7ffc8e61110f - <fixedbitset::FixedBitSet as core::fmt::Debug>::fmt::h6dcd24be3bf40fc6
  42:     0x7ffc8e9519e1 - <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::try_mark_green::h43f6f7a6dd139d2e
  43:     0x7ffc8d9190db - <rustc_typeck::check::PlaceOp as core::fmt::Debug>::fmt::h2be515651766726e
  44:     0x7ffc8d8b7873 - <rustc_typeck::check::CheckItemTypesVisitor as rustc_hir::itemlikevisit::ItemLikeVisitor>::visit_item::h7580caa56f13530b
  45:     0x7ffc8e90261b - <rustc_span::def_id::DefIndex as rustc_query_impl::profiling_support::SpecIntoSelfProfilingString>::spec_to_self_profile_string::he71a7fa6f0493805
  46:     0x7ffc8e8a9776 - <rustc_span::def_id::DefIndex as rustc_query_impl::profiling_support::SpecIntoSelfProfilingString>::spec_to_self_profile_string::he71a7fa6f0493805
  47:     0x7ffc8e9cf92b - rustc_query_impl::on_disk_cache::__ty_decoder_impl::<impl rustc_serialize::serialize::Decoder for rustc_query_impl::on_disk_cache::CacheDecoder>::error::hbd644ab6b1f9b216
  48:     0x7ffc8e6c2469 - <fixedbitset::FixedBitSet as core::fmt::Debug>::fmt::h6dcd24be3bf40fc6
  49:     0x7ffc8e57fc28 - <fixedbitset::FixedBitSet as core::fmt::Debug>::fmt::h6dcd24be3bf40fc6
  50:     0x7ffc8e95191f - <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::try_mark_green::h43f6f7a6dd139d2e
  51:     0x7ffc8d91bbd8 - <rustc_typeck::check::PlaceOp as core::fmt::Debug>::fmt::h2be515651766726e
  52:     0x7ffc8d8eb212 - rustc_typeck::check_crate::h60ac79a04151c346
  53:     0x7ffc8b2b8e42 - rustc_interface::passes::analysis::ha6bb758b7871ca78
  54:     0x7ffc8e9004cb - <rustc_span::def_id::DefIndex as rustc_query_impl::profiling_support::SpecIntoSelfProfilingString>::spec_to_self_profile_string::he71a7fa6f0493805
  55:     0x7ffc8e8b49ed - <rustc_span::def_id::DefIndex as rustc_query_impl::profiling_support::SpecIntoSelfProfilingString>::spec_to_self_profile_string::he71a7fa6f0493805
  56:     0x7ffc8e9cf085 - rustc_query_impl::on_disk_cache::__ty_decoder_impl::<impl rustc_serialize::serialize::Decoder for rustc_query_impl::on_disk_cache::CacheDecoder>::error::hbd644ab6b1f9b216
  57:     0x7ffc8e6aca6b - <fixedbitset::FixedBitSet as core::fmt::Debug>::fmt::h6dcd24be3bf40fc6
  58:     0x7ffc8e54803e - <fixedbitset::FixedBitSet as core::fmt::Debug>::fmt::h6dcd24be3bf40fc6
  59:     0x7ffc8e94d02c - <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::try_mark_green::h43f6f7a6dd139d2e
  60:     0x7ffc8b1d0a1b - <rustc_ast::ast::Item as rustc_ast::ast_like::AstLike>::attrs::h46107129069754a8   
  61:     0x7ffc8b199215 - rustc_driver::pretty::print_after_hir_lowering::hf8699be6512bcdbc
  62:     0x7ffc8b1d22b7 - <rustc_ast::ast::Item as rustc_ast::ast_like::AstLike>::attrs::h46107129069754a8   
  63:     0x7ffc8b19dd90 - <tracing_subscriber::util::TryInitError as core::fmt::Display>::fmt::h9a86ae0cce8205b9
  64:     0x7ffc8b19a018 - rustc_driver::pretty::print_after_hir_lowering::hf8699be6512bcdbc
  65:     0x7ffc8b18e8fd - <rustc_driver::Compilation as core::fmt::Debug>::fmt::h80a36c605c7f9e5b
  66:     0x7ffc97f1a1cc - std::sys::windows::thread::Thread::new::h1fb620a4b45ff0e1
  67:     0x7ffcfb057034 - BaseThreadInitThunk
  68:     0x7ffcfb542651 - RtlUserThreadStart

@patrick-gu patrick-gu 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 Oct 22, 2021
@SNCPlay42
Copy link
Contributor

This is also present in beta and nightly versions, but it is not present on 1.55.0 stable.

1.56.0 is released on stable, and it ICEs there as well.

@rustbot label regression-from-stable-to-stable

@rustbot rustbot added regression-from-stable-to-stable Performance or correctness regression from one stable version to another. I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Oct 22, 2021
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Oct 22, 2021
@hkratz
Copy link
Contributor

hkratz commented Oct 23, 2021

Bisection led to nightly-2021-07-26. Automatic commit bisection failed. Manual commit bisection:

searched toolchains 4a1f419e641c7ec56a60f1714ced5c343e0a2b38 through d9aa28767287670df6cf823b94629122e04442c0


********************************************************************************
Regression in f9b95f92c8af07a24a870e5f6117aa5dfcee5f17
********************************************************************************

Regression comes from #86461.

@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 Oct 28, 2021
@crlf0710
Copy link
Member

crlf0710 commented Nov 2, 2021

Oops. Sorry for creating this ICE. I haven't really dig into this issue yet, but from the description, it seems this ICE comes from

if trait_ref == trait_to_be_found {

where the equality should be true within a certain call, but didn't because of the HRTB region.

This inequality is because the two parameters comes from two different routes.
The first comes from supertraits() iterator, which is called from

let mut supertraits = util::supertraits(tcx, ty::Binder::dummy(object_trait_ref));

The second comes from vtable_entries() generator, which lived in

.filter_map(move |(pred, _)| {
pred.subst_supertrait(tcx, &inner_most_trait_ref).to_opt_poly_trait_ref()
});

, i think the subst_supertrait() call is the real difference here.

This is my current theory, and i'll try to validate it with debugging rustc this week. I'm not totally sure what's the best way to fix this. I think maybe @jackh726 could give me some advice dealing with HRTBs here?

@crlf0710
Copy link
Member

crlf0710 commented Nov 2, 2021

By the way, i've printed out the contents of the vtable for CanBeDynamic on playground:

error: vtable entries for `<S as CanBeDynamic>`: [
    MetadataDropInPlace,
    MetadataSize,
    MetadataAlign,
    Method(<S as Base>::do_something),
]

@jackh726
Copy link
Member

jackh726 commented Nov 2, 2021

Should it just use trait_ref.def_id()? Or I guess it needs the subst too... Hmm, probably since this is around codegen, we need to erase regions in both trait refs.

But, I'd like to see what the two trait refs are.

@crlf0710
Copy link
Member

crlf0710 commented Nov 3, 2021

trait_ref is
for<'f> <dyn CanBeDynamic<for<'f> Assoc = ()> as Base<'f>>
and
trait_to_be_found is
for<'f> <dyn CanBeDynamic<for<'f> Assoc = ()> as Base<'f>>

So... They're the same with fmt::Display, but inequal? I'm a little confused...

So i switched to use fmt::Debug, and they're a little different:
trait_ref is
Binder(<dyn CanBeDynamic<for<'f> Assoc = ()> as Base<'f>>, [Region(BrNamed(DefId(0:8 ~ vtable_hrtb[66bc]::ForAnyLifetime::'f), 'f))])
and
trait_to_be_found is
Binder(<dyn CanBeDynamic<for<'f> Assoc = ()> as Base<'f>>, [Region(BrNamed(DefId(0:13 ~ vtable_hrtb[66bc]::CanBeDynamic::'f), 'f))])

Complete diagnostics output:

Binder(<dyn CanBeDynamic<for<'f> Assoc = ()> as Base<'f>>, [Region(BrNamed(DefId(0:13 ~ vtable_hrtb[66bc]::CanBeDynamic::'f), 'f))])
segment: Binder(<dyn CanBeDynamic<for<'f> Assoc = ()> as Base<'f>>, [Region(BrNamed(DefId(0:8 ~ vtable_hrtb[66bc]::ForAnyLifetime::'f), 'f))])
segment: Binder(<dyn CanBeDynamic<for<'f> Assoc = ()> as ForAnyLifetime>, [])
segment: Binder(<dyn CanBeDynamic<for<'f> Assoc = ()> as CanBeDynamic>, [])

@jackh726

@jackh726
Copy link
Member

jackh726 commented Nov 3, 2021

This could "just" be fixed by anonymizing both trait refs.

The 'f here ForAnyLifetime: for<'f> Base<'f> is not the same as here CanBeDynamic: ForAnyLifetime + for<'f> Base<'f, Assoc = ()>.

I have to remember how we deduplicate these predicates elsewhere.

@crlf0710
Copy link
Member

crlf0710 commented Nov 3, 2021

Created #90536 for erasing regions before comparison. Let me know if there's a more proper approach, and i'll update the pr accordingly.

@bors bors closed this as completed in 2cff30b Nov 5, 2021
@jackh726
Copy link
Member

jackh726 commented Nov 5, 2021

Anyone think this needs a backport?

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. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. 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.

8 participants