Skip to content

The compiler reports conflicting even no real conflicting #22865

Closed
@laijs

Description

@laijs
trait Square { fn length(&self) -> f64; }
trait Circle { fn radius(&self) -> f64; }

// no type parameter, so &Area can cover both Square and Circle
trait Area { fn area(&self) -> f64; }

impl<T:Square> Area for T {
    fn area(&self) -> f64 { self.length() * self.length() }
}

impl<T:Circle> Area for T {
    fn area(&self) -> f64 { std::f64::consts::PI * self.radius() * self.radius() }
}

When compiling, it gives:

rustc --crate-type=rlib square_circle.rs
square_circle.rs:6:1: 8:2 error: conflicting implementations for trait `Area` [E0119]
square_circle.rs:6 impl<T:Square> Area for T {
square_circle.rs:7     fn area(&self) -> f64 { self.length() * self.length() }
square_circle.rs:8 }
square_circle.rs:10:1: 12:2 note: note conflicting implementation here
square_circle.rs:10 impl<T:Circle> Area for T {
square_circle.rs:11     fn area(&self) -> f64 { std::f64::consts::PI * self.radius() * self.radius() }
square_circle.rs:12 }

Actually, the user must impl only one of the Square/Circle to a type, there is no conflicting. But it seems that the compiler reports the conflicting too early. I think the compiler should report until the conflicting is really happened (the user impl the both traits to a type). Workaround for it is simple, but the above code is simpler and more readablity than workarounds.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions