Skip to content

Does not fail on multiple methods definition #58942

Closed
@rom1v

Description

@rom1v

This obviously fails to compile:

struct X;
impl X { 
   fn f(&self) {}
}
impl X {
   fn f(&self) {}
}
error[E0592]: duplicate definitions with name `f`
 --> a.rs:3:4
  |
3 |    fn f(&self) {}
  |    ^^^^^^^^^^^^^^ duplicate definitions for `f`
...
6 |    fn f(&self) {}
  |    -------------- other definition for `f`

But this does not:

struct X;
impl X {
   fn f(&self) {}
}
macro_rules! do_impl {
    ($name: ident) => {
        impl $name {
            fn f(&self) {}
        }
    }
}
do_impl!(X);

unless you use it:

fn main() {
    X.f()
}
error[E0034]: multiple applicable items in scope
  --> a.rs:14:7
   |
14 |     X.f()
   |       ^ multiple `f` found
   |
note: candidate #1 is defined in an impl for the type `X`
  --> a.rs:3:4
   |
3  |    fn f(&self) {}
   |    ^^^^^^^^^^^
note: candidate #2 is defined in an impl for the type `X`
  --> a.rs:8:13
   |
8  |             fn f(&self) {}
   |             ^^^^^^^^^^^
...
12 | do_impl!(X);
   | ------------ in this macro invocation

I think it should fail even when we don't use the duplicate function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions