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

Calling nonexistent function on a trait causes spurious object safety errors #58734

Closed
SNCPlay42 opened this issue Feb 25, 2019 · 0 comments
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@SNCPlay42
Copy link
Contributor

trait Trait {
    fn exists(self) -> ();
    
    fn not_object_safe() -> Self;
}

impl Trait for () {
    fn exists(self) -> () {
    }
    
    fn not_object_safe() -> Self {
        ()
    }
}

fn main() {
    // object-safe or not, this call is OK
    Trait::exists(());
    // but, in addition to nonexistence, this causes an object safety error
    Trait::nonexistent(());
}

Gives

error[E0599]: no function or associated item named `nonexistent` found for type `dyn Trait` in the current scope
  --> src/main.rs:20:5
   |
20 |     Trait::nonexistent(());
   |     ^^^^^^^^^^^^^^^^^^ function or associated item not found in `dyn Trait`

error[E0038]: the trait `Trait` cannot be made into an object
  --> src/main.rs:20:5
   |
20 |     Trait::nonexistent(());
   |     ^^^^^^^^^^^^^^^^^^ the trait `Trait` cannot be made into an object
   |
   = note: method `not_object_safe` has no receiver

error: aborting due to 2 previous errors

As the first call shows, it is in fact possible to call a non-object-safe trait's methods in this manner, but calling a nonexistent method gives an object safety error, adding noise to the output.

Note as well the E0599 error says the receiver type is dyn Trait.

Playground

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Mar 5, 2019
@estebank estebank self-assigned this Mar 5, 2019
estebank added a commit to estebank/rust that referenced this issue Mar 15, 2019
bors added a commit that referenced this issue Mar 23, 2019
Elide object safety errors on non-existent trait function

Fix #58734.

r? @zackmdavis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

2 participants