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

Trait object (vtable) construction seems too imprecise about constraints on methods #135462

Closed
steffahn opened this issue Jan 13, 2025 · 2 comments · Fixed by #135466
Closed

Trait object (vtable) construction seems too imprecise about constraints on methods #135462

steffahn opened this issue Jan 13, 2025 · 2 comments · Fixed by #135466
Assignees
Labels
A-monomorphization Area: Monomorphization 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) ❄️ S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@steffahn
Copy link
Member

If I write a trait like this

#![allow(unused)]

type A = fn(&'static ());
type B = fn(&());

trait Bound<P> {}
impl Bound<B> for String {}

trait Trt<T> {
    fn __(&self)
    where
        T: Bound<A>,
    {
    }
}

impl<T, S> Trt<T> for S {}

const C: &'static dyn Trt<String> = &();

where the method comes with a bound T: Bound<A>, and I instantiate a trait object with a type T (here, String) which only meets T: Bound<B>, the method seems to be monomorphized nonetheless, despite the mismatching type; as long as the type B only differs from A in higher-ranked-lifetime stuff, and the Bound<B> for String impl is the only one among types sufficiently “similar” to A.

#[no_mangle]
fn c() -> &'static dyn Trt<String> {
    C
}
example::Trt::__::h36d3dc0848d6ceff:
        ret

c:
        lea     rdx, [rip + .L__unnamed_1]
        mov     eax, 1
        ret

.L__unnamed_1:
        .asciz  "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001\000\000\000\000\000\000"
        .quad   example::Trt::__::h36d3dc0848d6ceff

(compiler explorer)

The behavior I would have expected: The method's entry becomes 0 instead, and it's never monomorphized.


This monomorphization then can become … “unhappy” … about the bad types:

#![allow(unused)]

type A = fn(&'static ());
type B = fn(&());

trait Bound<P: WithAssoc>: From<GetAssoc<P>> {
}
impl Bound<B> for String {}

trait Trt<T> {
    fn __(&self, x: T) where T: Bound<A> {
        T::from(());
    }
}

impl<T, S> Trt<T> for S {}

const C: &'static dyn Trt<String> = &();

type GetAssoc<T> = <T as WithAssoc>::Ty;

trait WithAssoc {
    type Ty;
}

impl WithAssoc for B {
    type Ty = String;
}

impl WithAssoc for A {
    type Ty = ();
}

(playground)

error: internal compiler error: compiler/rustc_middle/src/ty/instance.rs:585:21: failed to resolve instance for <String as From<()>>::from
  --> src/lib.rs:12:9
   |
12 |         T::from(());
   |         ^^^^^^^^^^^


thread 'rustc' panicked at compiler/rustc_middle/src/ty/instance.rs:585:21:
Box<dyn Any>
stack backtrace:
   0:     0x7097b3ef872a - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::heeeab96efd312504
   1:     0x7097b4612d26 - core::fmt::write::hc5affe1a961fb87a
   2:     0x7097b5598d91 - std::io::Write::write_fmt::h8190bce8dcf4a154
   3:     0x7097b3ef8582 - std::sys::backtrace::BacktraceLock::print::h8064f6c690594717
   4:     0x7097b3efab27 - std::panicking::default_hook::{{closure}}::h733c6fe54345b370
   5:     0x7097b3efa910 - std::panicking::default_hook::h4e4a01d045e6d2c7
   6:     0x7097b305fc68 - std[d57b542980c61ed0]::panicking::update_hook::<alloc[99f5d28b839bf241]::boxed::Box<rustc_driver_impl[4a0a86913583d400]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x7097b3efb3b3 - std::panicking::rust_panic_with_hook::he9f7cf885214d25c
   8:     0x7097b3098251 - std[d57b542980c61ed0]::panicking::begin_panic::<rustc_errors[7e07cb41e763bd24]::ExplicitBug>::{closure#0}
   9:     0x7097b308d156 - std[d57b542980c61ed0]::sys::backtrace::__rust_end_short_backtrace::<std[d57b542980c61ed0]::panicking::begin_panic<rustc_errors[7e07cb41e763bd24]::ExplicitBug>::{closure#0}, !>
  10:     0x7097b308d13d - std[d57b542980c61ed0]::panicking::begin_panic::<rustc_errors[7e07cb41e763bd24]::ExplicitBug>
  11:     0x7097b30a2181 - <rustc_errors[7e07cb41e763bd24]::diagnostic::BugAbort as rustc_errors[7e07cb41e763bd24]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  12:     0x7097b35f9bcc - <rustc_errors[7e07cb41e763bd24]::DiagCtxtHandle>::span_bug::<rustc_span[cd440fa8db25c8dc]::span_encoding::Span, alloc[99f5d28b839bf241]::string::String>
  13:     0x7097b367f227 - rustc_middle[72ef72ec76711232]::util::bug::opt_span_bug_fmt::<rustc_span[cd440fa8db25c8dc]::span_encoding::Span>::{closure#0}
  14:     0x7097b366422a - rustc_middle[72ef72ec76711232]::ty::context::tls::with_opt::<rustc_middle[72ef72ec76711232]::util::bug::opt_span_bug_fmt<rustc_span[cd440fa8db25c8dc]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  15:     0x7097b36640bb - rustc_middle[72ef72ec76711232]::ty::context::tls::with_context_opt::<rustc_middle[72ef72ec76711232]::ty::context::tls::with_opt<rustc_middle[72ef72ec76711232]::util::bug::opt_span_bug_fmt<rustc_span[cd440fa8db25c8dc]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  16:     0x7097b206c227 - rustc_middle[72ef72ec76711232]::util::bug::span_bug_fmt::<rustc_span[cd440fa8db25c8dc]::span_encoding::Span>
  17:     0x7097b4d8ace0 - <rustc_middle[72ef72ec76711232]::ty::instance::Instance>::expect_resolve
  18:     0x7097b1ee79f2 - rustc_monomorphize[4a4f940bf384f062]::collector::items_of_instance
  19:     0x7097b51498d0 - rustc_query_impl[d82db6e05a7d2fd4]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[d82db6e05a7d2fd4]::query_impl::items_of_instance::dynamic_query::{closure#2}::{closure#0}, rustc_middle[72ef72ec76711232]::query::erase::Erased<[u8; 32usize]>>
  20:     0x7097b5149134 - rustc_query_system[86c8aee4be1e78a3]::query::plumbing::try_execute_query::<rustc_query_impl[d82db6e05a7d2fd4]::DynamicConfig<rustc_query_system[86c8aee4be1e78a3]::query::caches::DefaultCache<(rustc_middle[72ef72ec76711232]::ty::instance::Instance, rustc_middle[72ef72ec76711232]::mir::mono::CollectionMode), rustc_middle[72ef72ec76711232]::query::erase::Erased<[u8; 32usize]>>, false, false, false>, rustc_query_impl[d82db6e05a7d2fd4]::plumbing::QueryCtxt, false>
  21:     0x7097b5148d6c - rustc_query_impl[d82db6e05a7d2fd4]::query_impl::items_of_instance::get_query_non_incr::__rust_end_short_backtrace
  22:     0x7097b51457cc - rustc_monomorphize[4a4f940bf384f062]::collector::collect_items_rec::{closure#0}
  23:     0x7097b57458c8 - rustc_monomorphize[4a4f940bf384f062]::collector::collect_items_rec
  24:     0x7097b4a12f2f - rustc_monomorphize[4a4f940bf384f062]::partitioning::collect_and_partition_mono_items
  25:     0x7097b55e0c56 - rustc_query_impl[d82db6e05a7d2fd4]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[d82db6e05a7d2fd4]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2}::{closure#0}, rustc_middle[72ef72ec76711232]::query::erase::Erased<[u8; 24usize]>>
  26:     0x7097b55e0c37 - <rustc_query_impl[d82db6e05a7d2fd4]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2} as core[7cf2ec004b4a7e06]::ops::function::FnOnce<(rustc_middle[72ef72ec76711232]::ty::context::TyCtxt, ())>>::call_once
  27:     0x7097b55e07bc - rustc_query_system[86c8aee4be1e78a3]::query::plumbing::try_execute_query::<rustc_query_impl[d82db6e05a7d2fd4]::DynamicConfig<rustc_query_system[86c8aee4be1e78a3]::query::caches::SingleCache<rustc_middle[72ef72ec76711232]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[d82db6e05a7d2fd4]::plumbing::QueryCtxt, false>
  28:     0x7097b55e04b0 - rustc_query_impl[d82db6e05a7d2fd4]::query_impl::collect_and_partition_mono_items::get_query_non_incr::__rust_end_short_backtrace
  29:     0x7097b4d873d3 - rustc_codegen_ssa[1e5bf92c47e8401f]::back::symbol_export::exported_symbols_provider_local
  30:     0x7097b482e3a5 - rustc_query_impl[d82db6e05a7d2fd4]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[d82db6e05a7d2fd4]::query_impl::exported_symbols::dynamic_query::{closure#2}::{closure#0}, rustc_middle[72ef72ec76711232]::query::erase::Erased<[u8; 16usize]>>
  31:     0x7097b482e363 - <rustc_query_impl[d82db6e05a7d2fd4]::query_impl::exported_symbols::dynamic_query::{closure#2} as core[7cf2ec004b4a7e06]::ops::function::FnOnce<(rustc_middle[72ef72ec76711232]::ty::context::TyCtxt, rustc_span[cd440fa8db25c8dc]::def_id::CrateNum)>>::call_once
  32:     0x7097b5411458 - rustc_query_system[86c8aee4be1e78a3]::query::plumbing::try_execute_query::<rustc_query_impl[d82db6e05a7d2fd4]::DynamicConfig<rustc_data_structures[f04a785ad63d9793]::vec_cache::VecCache<rustc_span[cd440fa8db25c8dc]::def_id::CrateNum, rustc_middle[72ef72ec76711232]::query::erase::Erased<[u8; 16usize]>, rustc_query_system[86c8aee4be1e78a3]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[d82db6e05a7d2fd4]::plumbing::QueryCtxt, false>
  33:     0x7097b5410fea - rustc_query_impl[d82db6e05a7d2fd4]::query_impl::exported_symbols::get_query_non_incr::__rust_end_short_backtrace
  34:     0x7097b526784b - <rustc_metadata[d3196428b34b576e]::rmeta::encoder::EncodeContext>::encode_crate_root
  35:     0x7097b561dece - rustc_metadata[d3196428b34b576e]::rmeta::encoder::encode_metadata
  36:     0x7097b562d576 - rustc_metadata[d3196428b34b576e]::fs::encode_and_write_metadata
  37:     0x7097b562c3be - <rustc_interface[18d7993094f7ee66]::queries::Linker>::codegen_and_build_linker
  38:     0x7097b5621e12 - rustc_interface[18d7993094f7ee66]::passes::create_and_enter_global_ctxt::<core[7cf2ec004b4a7e06]::option::Option<rustc_interface[18d7993094f7ee66]::queries::Linker>, rustc_driver_impl[4a0a86913583d400]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  39:     0x7097b5604256 - rustc_interface[18d7993094f7ee66]::interface::run_compiler::<(), rustc_driver_impl[4a0a86913583d400]::run_compiler::{closure#0}>::{closure#1}
  40:     0x7097b548b755 - std[d57b542980c61ed0]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[18d7993094f7ee66]::util::run_in_thread_with_globals<rustc_interface[18d7993094f7ee66]::util::run_in_thread_pool_with_globals<rustc_interface[18d7993094f7ee66]::interface::run_compiler<(), rustc_driver_impl[4a0a86913583d400]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  41:     0x7097b548bc04 - <<std[d57b542980c61ed0]::thread::Builder>::spawn_unchecked_<rustc_interface[18d7993094f7ee66]::util::run_in_thread_with_globals<rustc_interface[18d7993094f7ee66]::util::run_in_thread_pool_with_globals<rustc_interface[18d7993094f7ee66]::interface::run_compiler<(), rustc_driver_impl[4a0a86913583d400]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[7cf2ec004b4a7e06]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  42:     0x7097b548d1c1 - std::sys::pal::unix::thread::Thread::new::thread_start::hf232c9576e29b140
  43:     0x7097b6cbda94 - <unknown>
  44:     0x7097b6d4aa34 - clone
  45:                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: please make sure that you have updated to the latest nightly

note: please attach the file at `/playground/rustc-ice-2025-01-13T23_16_23-22.txt` to your bug report

note: compiler flags: --crate-type lib -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 [items_of_instance] collecting items used by `<() as Trt<alloc::string::String>>::__`
#1 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack

@rustbot label T-compiler, A-trait-objects, A-monomorphization, I-ICE

@steffahn steffahn added the C-bug Category: This is a bug. label Jan 13, 2025
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-monomorphization Area: Monomorphization A-trait-objects Area: trait objects, vtable layout 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 Jan 13, 2025
@compiler-errors compiler-errors self-assigned this Jan 14, 2025
@compiler-errors
Copy link
Member

lol, lcnr turned this into an equivalent ICE a few days ago but I never opened a PR. I'll open a quick PR to fix this.

@compiler-errors
Copy link
Member

Here's a more resilient ICE of the same nature:

trait Foo {}
impl Foo for fn(&'static ()) {}

trait Bar {
    type Assoc: Default;
}
impl<T: Foo> Bar for T {
    type Assoc = usize;
} 
impl Bar for fn(&()) {
    type Assoc = ();
}

fn needs_foo<T: Foo>() -> usize {
    needs_bar::<T>()
}

fn needs_bar<T: Bar>() -> <T as Bar>::Assoc {
    Default::default()
}

trait Evil<T> {
    fn bad(&self) where T: Foo  {
        needs_foo::<T>();
    }    
}

impl Evil<fn(&())> for () {}

fn main() {
    let x: &dyn Evil<fn(&())> = &();
}

courtesy of @lcnr

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jan 14, 2025
…ble, r=lcnr

Leak check in `impossible_predicates` to avoid monomorphizing impossible instances

Fixes rust-lang#135462

r? lcnr
@jieyouxu jieyouxu added S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jan 14, 2025
@bors bors closed this as completed in 866e61a Jan 14, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 14, 2025
Rollup merge of rust-lang#135466 - compiler-errors:leak-check-impossible, r=lcnr

Leak check in `impossible_predicates` to avoid monomorphizing impossible instances

Fixes rust-lang#135462

r? lcnr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-monomorphization Area: Monomorphization 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) ❄️ S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants