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

Check for dyn providing all of its projections is incomplete w.r.t. different supertrait substitutions #133388

Closed
compiler-errors opened this issue Nov 23, 2024 · 0 comments · Fixed by #133392
Assignees
Labels
A-trait-objects Area: trait objects, vtable layout C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@compiler-errors
Copy link
Member

I tried this code:

trait Sup<T> {
    type Assoc: Default;
}

impl<T: Default> Sup<T> for () {
    type Assoc = T;
}
impl<T: Default, U: Default> Dyn<T, U> for () {}

trait Dyn<A, B>: Sup<A, Assoc = A> + Sup<B> {}

fn main() {
    let q: <dyn Dyn<i32, u32> as Sup<u32>>::Assoc = Default::default();
}

I expected to see this happen: It errors b/c we don't know what the associated type for <dyn Dyn<i32, u32> as Sup<u32>>::Assoc is.

Instead, this happened: Codegen ICE.

Meta

rustc --version --verbose:

rustc 1.85.0-nightly (a47555110 2024-11-22)
Backtrace

error: internal compiler error: compiler/rustc_middle/src/ty/instance.rs:587:21: failed to resolve instance for <<dyn Dyn<i32, u32> as Sup<u32>>::Assoc as Default>::default
  --> src/main.rs:13:53
   |
13 |     let q: <dyn Dyn<i32, u32> as Sup<u32>>::Assoc = Default::default();
   |                                                     ^^^^^^^^^^^^^^^^^^

thread 'rustc' panicked at compiler/rustc_middle/src/ty/instance.rs:587:21:
Box<dyn Any>
stack backtrace:
   0:     0x79442cbce6ea - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h304520fd6a30aa07
   1:     0x79442d419525 - core::fmt::write::hf5713710ce10ff22
   2:     0x79442e234d91 - std::io::Write::write_fmt::hda708db57927dacf
   3:     0x79442cbd0dbb - std::panicking::default_hook::{{closure}}::he1ad87607d0c11c5
   4:     0x79442cbd0a2e - std::panicking::default_hook::h81c8cd2e7c59ee33
   5:     0x79442bd9a5d7 - std[5204e9590b4985ef]::panicking::update_hook::<alloc[fd15fd9026f491e1]::boxed::Box<rustc_driver_impl[c41f2638408ed175]::install_ice_hook::{closure#0}>>::{closure#0}
   6:     0x79442cbd16d7 - std::panicking::rust_panic_with_hook::had2118629c312a4a
   7:     0x79442bdd1591 - std[5204e9590b4985ef]::panicking::begin_panic::<rustc_errors[98c49d204a493357]::ExplicitBug>::{closure#0}
   8:     0x79442bdc51b6 - std[5204e9590b4985ef]::sys::backtrace::__rust_end_short_backtrace::<std[5204e9590b4985ef]::panicking::begin_panic<rustc_errors[98c49d204a493357]::ExplicitBug>::{closure#0}, !>
   9:     0x79442bdc0716 - std[5204e9590b4985ef]::panicking::begin_panic::<rustc_errors[98c49d204a493357]::ExplicitBug>
  10:     0x79442bdda611 - <rustc_errors[98c49d204a493357]::diagnostic::BugAbort as rustc_errors[98c49d204a493357]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  11:     0x79442c2d448d - <rustc_errors[98c49d204a493357]::DiagCtxtHandle>::span_bug::<rustc_span[233999951ced9cd1]::span_encoding::Span, alloc[fd15fd9026f491e1]::string::String>
  12:     0x79442c376108 - rustc_middle[c83967c7761a8780]::util::bug::opt_span_bug_fmt::<rustc_span[233999951ced9cd1]::span_encoding::Span>::{closure#0}
  13:     0x79442c35b11a - rustc_middle[c83967c7761a8780]::ty::context::tls::with_opt::<rustc_middle[c83967c7761a8780]::util::bug::opt_span_bug_fmt<rustc_span[233999951ced9cd1]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  14:     0x79442c35afcb - rustc_middle[c83967c7761a8780]::ty::context::tls::with_context_opt::<rustc_middle[c83967c7761a8780]::ty::context::tls::with_opt<rustc_middle[c83967c7761a8780]::util::bug::opt_span_bug_fmt<rustc_span[233999951ced9cd1]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  15:     0x79442a17cf27 - rustc_middle[c83967c7761a8780]::util::bug::span_bug_fmt::<rustc_span[233999951ced9cd1]::span_encoding::Span>
  16:     0x79442daa9de0 - <rustc_middle[c83967c7761a8780]::ty::instance::Instance>::expect_resolve
  17:     0x79442a8bb0d8 - rustc_monomorphize[d67c1690e64672e3]::collector::collect_items_rec::{closure#0}
  18:     0x79442e3d1f21 - rustc_monomorphize[d67c1690e64672e3]::collector::collect_items_rec
  19:     0x79442d9bfc06 - rustc_monomorphize[d67c1690e64672e3]::partitioning::collect_and_partition_mono_items
  20:     0x79442e2ca1d6 - rustc_query_impl[2ecbb548ea5419f8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[2ecbb548ea5419f8]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2}::{closure#0}, rustc_middle[c83967c7761a8780]::query::erase::Erased<[u8; 24usize]>>
  21:     0x79442e2ca19b - <rustc_query_impl[2ecbb548ea5419f8]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2} as core[d89802b8f5f07590]::ops::function::FnOnce<(rustc_middle[c83967c7761a8780]::ty::context::TyCtxt, ())>>::call_once
  22:     0x79442e2c9e33 - rustc_query_system[842c6bba149f2c70]::query::plumbing::try_execute_query::<rustc_query_impl[2ecbb548ea5419f8]::DynamicConfig<rustc_query_system[842c6bba149f2c70]::query::caches::SingleCache<rustc_middle[c83967c7761a8780]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[2ecbb548ea5419f8]::plumbing::QueryCtxt, false>
  23:     0x79442e2c9ba1 - rustc_query_impl[2ecbb548ea5419f8]::query_impl::collect_and_partition_mono_items::get_query_non_incr::__rust_end_short_backtrace
  24:     0x79442e26b942 - <rustc_codegen_llvm[1041eb84fe8a92c6]::LlvmCodegenBackend as rustc_codegen_ssa[4b0e0146219e1624]::traits::backend::CodegenBackend>::codegen_crate
  25:     0x79442e562737 - <rustc_interface[706ab71263ce060a]::queries::Linker>::codegen_and_build_linker
  26:     0x79442e1861d7 - rustc_interface[706ab71263ce060a]::interface::run_compiler::<core[d89802b8f5f07590]::result::Result<(), rustc_span[233999951ced9cd1]::ErrorGuaranteed>, rustc_driver_impl[c41f2638408ed175]::run_compiler::{closure#0}>::{closure#1}
  27:     0x79442e23ed16 - std[5204e9590b4985ef]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[706ab71263ce060a]::util::run_in_thread_with_globals<rustc_interface[706ab71263ce060a]::interface::run_compiler<core[d89802b8f5f07590]::result::Result<(), rustc_span[233999951ced9cd1]::ErrorGuaranteed>, rustc_driver_impl[c41f2638408ed175]::run_compiler::{closure#0}>::{closure#1}, core[d89802b8f5f07590]::result::Result<(), rustc_span[233999951ced9cd1]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[d89802b8f5f07590]::result::Result<(), rustc_span[233999951ced9cd1]::ErrorGuaranteed>>
  28:     0x79442e2779b0 - <<std[5204e9590b4985ef]::thread::Builder>::spawn_unchecked_<rustc_interface[706ab71263ce060a]::util::run_in_thread_with_globals<rustc_interface[706ab71263ce060a]::interface::run_compiler<core[d89802b8f5f07590]::result::Result<(), rustc_span[233999951ced9cd1]::ErrorGuaranteed>, rustc_driver_impl[c41f2638408ed175]::run_compiler::{closure#0}>::{closure#1}, core[d89802b8f5f07590]::result::Result<(), rustc_span[233999951ced9cd1]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[d89802b8f5f07590]::result::Result<(), rustc_span[233999951ced9cd1]::ErrorGuaranteed>>::{closure#1} as core[d89802b8f5f07590]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  29:     0x79442e277d2b - std::sys::pal::unix::thread::Thread::new::thread_start::hcdbd1049068002f4
  30:     0x79442f806a94 - <unknown>
  31:     0x79442f893a34 - clone
  32:                0x0 - <unknown>

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.82.0 (f6e511eec 2024-10-15) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type bin -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2

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

query stack during panic:
#0 [collect_and_partition_mono_items] collect_and_partition_mono_items

@compiler-errors compiler-errors added A-trait-objects Area: trait objects, vtable layout C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-types Relevant to the types team, which will review and decide on the PR/issue. labels Nov 23, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 23, 2024
@compiler-errors compiler-errors self-assigned this Nov 23, 2024
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 23, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 23, 2024
Fix ICE when multiple supertrait substitutions need assoc but only one is provided

I'll write more here when the crate run is done.

Fixes rust-lang#133388

r? lcnr
`@rustbot` author
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 25, 2024
Some minor dyn-related tweaks

Each commit should be self-explanatory, but I'm happy to explain what's going on if not. These are tweaks I pulled out of rust-lang#133388, but they can be reviewed sooner than that.

r? types
jhpratt added a commit to jhpratt/rust that referenced this issue Nov 26, 2024
…=lcnr

Bail on more errors in dyn ty lowering

If we have more than one principal trait, or if we have a principal trait with errors in it, then bail with `TyKind::Error` rather than attempting lowering. Lowering a dyn trait with more than one principal just arbitrarily chooses the first one and drops the subsequent ones, and lowering a dyn trait path with errors in it is just kinda useless.

This suppresses unnecessary errors which I think is net-good, but also is important to make sure that we don't end up leaking `{type error}` in rust-lang#133388 error messaging :)

r? types
compiler-errors added a commit to compiler-errors/rust that referenced this issue Nov 26, 2024
…=lcnr

Bail on more errors in dyn ty lowering

If we have more than one principal trait, or if we have a principal trait with errors in it, then bail with `TyKind::Error` rather than attempting lowering. Lowering a dyn trait with more than one principal just arbitrarily chooses the first one and drops the subsequent ones, and lowering a dyn trait path with errors in it is just kinda useless.

This suppresses unnecessary errors which I think is net-good, but also is important to make sure that we don't end up leaking `{type error}` in rust-lang#133388 error messaging :)

r? types
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 27, 2024
Rollup merge of rust-lang#133394 - compiler-errors:dyn-more-errors, r=lcnr

Bail on more errors in dyn ty lowering

If we have more than one principal trait, or if we have a principal trait with errors in it, then bail with `TyKind::Error` rather than attempting lowering. Lowering a dyn trait with more than one principal just arbitrarily chooses the first one and drops the subsequent ones, and lowering a dyn trait path with errors in it is just kinda useless.

This suppresses unnecessary errors which I think is net-good, but also is important to make sure that we don't end up leaking `{type error}` in rust-lang#133388 error messaging :)

r? types
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 27, 2024
…pastorino

Some minor dyn-related tweaks

Each commit should be self-explanatory, but I'm happy to explain what's going on if not. These are tweaks I pulled out of rust-lang#133388, but they can be reviewed sooner than that.

r? types
@bors bors closed this as completed in 67278cd Dec 15, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 15, 2024
Rollup merge of rust-lang#133392 - compiler-errors:object-sup, r=lcnr

Fix ICE when multiple supertrait substitutions need assoc but only one is provided

Dyn traits must have all of their associated types constrained either by:
1. writing them in the dyn trait itself as an associated type bound, like `dyn Iterator<Item = u32>`,
2. A supertrait bound, like `trait ConstrainedIterator: Iterator<Item = u32> {}`, then you may write `dyn ConstrainedIterator` which doesn't need to mention `Item`.

However, the object type lowering code did not consider the fact that there may be multiple supertraits with different substitutions, so it just used the associated type's *def id* as a key for keeping track of which associated types are missing:

https://github.com/rust-lang/rust/blob/1fc691e6ddc24506b5234d586a5c084eb767f1ad/compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs#L131

This means that we can have missing associated types when there are mutliple supertraits with different substitutions and only one of them is constrained, like:

```rust
trait Sup<T> {
    type Assoc: Default;
}

impl<T: Default> Sup<T> for () {
    type Assoc = T;
}
impl<T: Default, U: Default> Dyn<T, U> for () {}

trait Dyn<A, B>: Sup<A, Assoc = A> + Sup<B> {}
```

The above example allows you to name `<dyn Dyn<i32, u32> as Sup<u32>>::Assoc` even though it is not possible to project since it's neither constrained by a manually written projection bound or a supertrait bound. This successfully type-checks, but leads to a codegen ICE since we are not able to project the associated type.

This PR fixes the validation for checking that a dyn trait mentions all of its associated type bounds. This is theoretically a breaking change, since you could technically use that `dyn Dyn<A, B>` type mentionedin the example above without actually *projecting* to the bad associated type, but I don't expect it to ever be relevant to a user since it's almost certainly a bug. This is corroborated with the crater results[^crater], which show no failures[^unknown].

Crater: rust-lang#133392 (comment)

Fixes rust-lang#133388

[^crater]: I cratered this originally with rust-lang#133397, which is a PR that is stacked on top, then re-ran crater with just the failures from that PR.
[^unknown]: If you look at the crater results, it shows all of the passes as "unknown". I believe this is a crater bug, since looking at the results manually shows them as passes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-objects Area: trait objects, vtable layout C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
3 participants