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

No way to call inherent method on dyn Trait when a function of the same name exists in the trait #109077

Closed
SparkyPotato opened this issue Mar 13, 2023 · 1 comment
Labels
C-bug Category: This is a bug.

Comments

@SparkyPotato
Copy link
Contributor

Given:

trait Foo { fn foo(&self) -> i32 { 0 } }

impl<'a> dyn Foo + 'a {
    fn foo(&self) -> i32 { 1 }
}

struct Bar;
impl Foo for Bar {}

There seems to be no way to call foo implemented on dyn Foo + 'a:

let x = Bar;
let dx = &x as &dyn Foo;
println!("{}", dx.foo());

Gives the expected error:

error[E0034]: multiple applicable items in scope
  --> src/main.rs:13:23
   |
13 |     println!("{}", dx.foo());
   |                       ^^^ multiple `foo` found
   |
note: candidate #1 is defined in an impl for the type `(dyn Foo + 'a)`
  --> src/main.rs:4:5
   |
4  |     fn foo(&self) -> i32 { 1 }
   |     ^^^^^^^^^^^^^^^^^^^^
note: candidate #2 is defined in the trait `Foo`
  --> src/main.rs:1:13
   |
1  | trait Foo { fn foo(&self) -> i32 { 0 } }
   |             ^^^^^^^^^^^^^^^^^^^^
help: disambiguate the associated function for candidate #2
   |
13 |     println!("{}", Foo::foo(&dx));
   |                    ~~~~~~~~~~~~~

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

The compiler suggests a way to call candidate #​2, but even with

let x = Bar;
let dx = &x as &dyn Foo;
println!("{}", <dyn Foo>::foo(dx));

The error persists, with no seeming way to call candidate #​1.

Meta

Version:

rustc 1.70.0-nightly (8a73f50d8 2023-03-11)
@SparkyPotato SparkyPotato added the C-bug Category: This is a bug. label Mar 13, 2023
@fmease
Copy link
Member

fmease commented Mar 13, 2023

Duplicate of #51402. #82357 related.

@Noratrieb Noratrieb closed this as not planned Won't fix, can't repro, duplicate, stale Mar 14, 2023
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.
Projects
None yet
Development

No branches or pull requests

3 participants