Skip to content

Conflict name resolution of <dyn Trait>::method and <dyn Trait as Trait>::method #145708

@frank-king

Description

@frank-king

I tried this code:

use std::any::{Any, TypeId};

pub trait Intent: Any {
    fn get_type_id(&self) -> TypeId 
    where
        Self: Sized,
    {
        TypeId::of::<Self>()
    }
}

impl dyn Intent {
    pub fn get_type_id(&self) -> TypeId {
        (self as &dyn Any).type_id()
    }
}

fn foo(intent: &dyn Intent) {
    let _ = intent.get_type_id();
    let _ = <dyn Intent>::get_type_id(intent);
    // let _ = Intent::get_type_id(intent); // intended error
    //~^ ERROR[E0277]: the size for values of type `(dyn Intent + 'static)` cannot be known at compilation time
    // let _ = <dyn Intent as Intent>::get_type_id(intent); // intended error
    //~^ ERROR[E0277]: the size for values of type `(dyn Intent + 'static)` cannot be known at compilation time
}

I expected to see this happen:

  • both intent.get_type_id() and <dyn Intent>::get_type_id should work,
  • or at least <dyn Intent>::get_type_id should work.

Instead, this happened:

   Compiling intent v0.1.0 (/tmp/intent)
error[E0034]: multiple applicable items in scope
  --> src/lib.rs:19:20
   |
19 |     let _ = intent.get_type_id();
   |                    ^^^^^^^^^^^ multiple `get_type_id` found
   |
note: candidate #1 is defined in the trait `Intent`
  --> src/lib.rs:4:5
   |
 4 | /     fn get_type_id(&self) -> TypeId
 5 | |     where
 6 | |         Self: Sized,
   | |____________________^
note: candidate #2 is defined in an impl for the type `(dyn Intent + 'static)`
  --> src/lib.rs:13:5
   |
13 |     pub fn get_type_id(&self) -> TypeId {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: disambiguate the method for candidate #1
   |
19 -     let _ = intent.get_type_id();
19 +     let _ = Intent::get_type_id(&intent);
   |

error[E0034]: multiple applicable items in scope
  --> src/lib.rs:20:27
   |
20 |     let _ = <dyn Intent>::get_type_id(intent);
   |             --------------^^^^^^^^^^^ multiple `get_type_id` found
   |             |
   |             help: use fully-qualified syntax to disambiguate: `Intent::`
   |
note: candidate #1 is defined in the trait `Intent`
  --> src/lib.rs:4:5
   |
 4 | /     fn get_type_id(&self) -> TypeId
 5 | |     where
 6 | |         Self: Sized,
   | |____________________^
note: candidate #2 is defined in an impl for the type `(dyn Intent + 'static)`
  --> src/lib.rs:13:5
   |
13 |     pub fn get_type_id(&self) -> TypeId {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0034`.

Meta

rustc --version --verbose:

rustc 1.91.0-nightly (040a98af7 2025-08-20)
binary: rustc
commit-hash: 040a98af70f0a7da03f3d5356531b28a2a7a77e4
commit-date: 2025-08-20
host: x86_64-unknown-linux-gnu
release: 1.91.0-nightly
LLVM version: 21.1.0
Backtrace

With -Ztreat-err-as-bug:

thread 'rustc' (948354) panicked at compiler/rustc_errors/src/lib.rs:1839:17:
aborting due to `-Z treat-err-as-bug=1`
stack backtrace:
   0:     0x7e3c24040e23 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h814617dc0d18f1af
   1:     0x7e3c24801a18 - core::fmt::write::hdf77621c7a740bfd
   2:     0x7e3c2402bb33 - std::io::Write::write_fmt::h8a38155612d9dac4
   3:     0x7e3c24040c82 - std::sys::backtrace::BacktraceLock::print::h5647739c4d6bfe09
   4:     0x7e3c240236aa - std::panicking::default_hook::{{closure}}::h4c895046f98cefd2
   5:     0x7e3c240231d6 - std::panicking::default_hook::h9c7123c52cfc1ada
   6:     0x7e3c23090775 - std[c43901696ce7cf4b]::panicking::update_hook::<alloc[8e61ca5b365a5afb]::boxed::Box<rustc_driver_impl[76a6965d5427f1fd]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x7e3c24023d67 - std::panicking::panic_with_hook::hb155cf1df1c68594
   8:     0x7e3c24041206 - std::panicking::panic_handler::{{closure}}::hae9427cc4ab3dfa7
   9:     0x7e3c24041199 - std::sys::backtrace::__rust_end_short_backtrace::h6a35da8b38cf7868
  10:     0x7e3c24022cad - __rustc[861857bac794ed70]::rust_begin_unwind
  11:     0x7e3c206db8a0 - core::panicking::panic_fmt::h3656f9582a0fbe40
  12:     0x7e3c230a9e67 - <rustc_errors[f262a1ba9be511f5]::DiagCtxtInner>::panic_if_treat_err_as_bug
  13:     0x7e3c25cd5a39 - <rustc_errors[f262a1ba9be511f5]::DiagCtxtInner>::emit_diagnostic::{closure#3}
  14:     0x7e3c25cd3594 - rustc_interface[92d8b4472dd23e98]::callbacks::track_diagnostic::<core[e95fe81648e79824]::option::Option<rustc_span[b83af41131622c8b]::ErrorGuaranteed>>
  15:     0x7e3c25cd25b6 - <rustc_errors[f262a1ba9be511f5]::DiagCtxtInner>::emit_diagnostic
  16:     0x7e3c25cd247f - <rustc_errors[f262a1ba9be511f5]::DiagCtxtHandle>::emit_diagnostic
  17:     0x7e3c20f3f790 - <rustc_span[b83af41131622c8b]::ErrorGuaranteed as rustc_errors[f262a1ba9be511f5]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  18:     0x7e3c25caea10 - rustc_passes[a94ffb68c3f006c1]::entry::entry_fn
  19:     0x7e3c25cadc18 - rustc_query_impl[60b2903f58170003]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[60b2903f58170003]::query_impl::entry_fn::dynamic_query::{closure#2}::{closure#0}, rustc_middle[c80a1a42626d4f62]::query::erase::Erased<[u8; 12usize]>>
  20:     0x7e3c25cadbdb - <rustc_query_impl[60b2903f58170003]::query_impl::entry_fn::dynamic_query::{closure#2} as core[e95fe81648e79824]::ops::function::FnOnce<(rustc_middle[c80a1a42626d4f62]::ty::context::TyCtxt, ())>>::call_once
  21:     0x7e3c25cad5ee - rustc_query_system[9393b87f5a3e06e4]::query::plumbing::try_execute_query::<rustc_query_impl[60b2903f58170003]::DynamicConfig<rustc_query_system[9393b87f5a3e06e4]::query::caches::SingleCache<rustc_middle[c80a1a42626d4f62]::query::erase::Erased<[u8; 12usize]>>, false, false, false>, rustc_query_impl[60b2903f58170003]::plumbing::QueryCtxt, false>
  22:     0x7e3c25cad39a - rustc_query_impl[60b2903f58170003]::query_impl::entry_fn::get_query_non_incr::__rust_end_short_backtrace
  23:     0x7e3c24bba3ae - rustc_interface[92d8b4472dd23e98]::passes::analysis
  24:     0x7e3c24bba28d - rustc_query_impl[60b2903f58170003]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[60b2903f58170003]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[c80a1a42626d4f62]::query::erase::Erased<[u8; 0usize]>>
  25:     0x7e3c258865da - rustc_query_system[9393b87f5a3e06e4]::query::plumbing::try_execute_query::<rustc_query_impl[60b2903f58170003]::DynamicConfig<rustc_query_system[9393b87f5a3e06e4]::query::caches::SingleCache<rustc_middle[c80a1a42626d4f62]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[60b2903f58170003]::plumbing::QueryCtxt, false>
  26:     0x7e3c25886210 - rustc_query_impl[60b2903f58170003]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  27:     0x7e3c25bcd74b - rustc_interface[92d8b4472dd23e98]::passes::create_and_enter_global_ctxt::<core[e95fe81648e79824]::option::Option<rustc_interface[92d8b4472dd23e98]::queries::Linker>, rustc_driver_impl[76a6965d5427f1fd]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  28:     0x7e3c25b0fe43 - rustc_interface[92d8b4472dd23e98]::interface::run_compiler::<(), rustc_driver_impl[76a6965d5427f1fd]::run_compiler::{closure#0}>::{closure#1}
  29:     0x7e3c2594eff8 - std[c43901696ce7cf4b]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[92d8b4472dd23e98]::util::run_in_thread_with_globals<rustc_interface[92d8b4472dd23e98]::util::run_in_thread_pool_with_globals<rustc_interface[92d8b4472dd23e98]::interface::run_compiler<(), rustc_driver_impl[76a6965d5427f1fd]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  30:     0x7e3c2594ecc8 - <<std[c43901696ce7cf4b]::thread::Builder>::spawn_unchecked_<rustc_interface[92d8b4472dd23e98]::util::run_in_thread_with_globals<rustc_interface[92d8b4472dd23e98]::util::run_in_thread_pool_with_globals<rustc_interface[92d8b4472dd23e98]::interface::run_compiler<(), rustc_driver_impl[76a6965d5427f1fd]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[e95fe81648e79824]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  31:     0x7e3c2595084b - std::sys::pal::unix::thread::Thread::new::thread_start::hf2446a80ce73adb8
  32:     0x7e3c1f09caa4 - start_thread
                               at ./nptl/pthread_create.c:447:8
  33:     0x7e3c1f129c3c - clone3
                               at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:78:0
  34:                0x0 - <unknown>

error: 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: please make sure that you have updated to the latest nightly

note: please attach the file at `/tmp/intent/rustc-ice-2025-08-21T11_39_10-948328.txt` to your bug report

note: compiler flags: -Z treat-err-as-bug

query stack during panic:
#0 [entry_fn] looking up the entry function of a crate
#1 [analysis] running analysis passes on this crate
end of query stack

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-resolveArea: Name/path resolution done by `rustc_resolve` specificallyC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions