Skip to content

Ambiguous method call - should this error? #37741

@mitchmindtree

Description

@mitchmindtree

Hey folks,

I had a contributor track down a bug that slipped through in one of my crates. It took them a few hours, and turned out to be caused by an ambiguity in next method calls. Here's the example of the bug that they gave:

pub trait Window {}

pub struct PistonWindow {}
impl Window for PistonWindow {}


struct WindowEvents {}
impl WindowEvents {
    fn next<W: Window>(self, arg: &W) {
        println!("WindowEvents::next");
    }
}

trait EventWindow {
    fn next(self, arg: &PistonWindow);
}
impl EventWindow for WindowEvents {
    fn next(self, arg: &PistonWindow) {
        println!("EventWindow::next");
    }
}

fn main() {
    let struct1 = PistonWindow {};
    let struct2 = WindowEvents {};
    
    struct2.next(&struct1);
}

playpen

Here, I would normally assume rustc to throw an error about an ambiguity between the next method call, something like:

multiple possible sources of next method:
1. inherent method `WindowEvents::next`
2. trait method `EventWindow::next`

Instead, the program compiles and the WindowEvents inherent method is called.

Perhaps this is expected behaviour and I'm missing something, but just thought I'd report in case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions