Skip to content

Not using a bounded generic type triggers a "cannot determine a type for this bounded type parameter" error. #15908

@miselin

Description

@miselin

This is a (very) contrived example that exhibits the error:

struct X {
    x: bool,
}

impl X {
    fn new() -> X {
        X{x: false}
    }
}

trait Thing {
    fn toggle(&mut self);
}

impl Thing for X {
    fn toggle(&mut self) {
        self.x = !self.x;
    }
}

fn bounded<T: Thing>(x: &mut X) {
    x.toggle()
}

fn main() {
    let mut x = X::new();
    bounded(&mut x);
}

Fires the error:

bounded.rs:28:5: 28:12 error: cannot determine a type for this bounded type parameter: unconstrained type
bounded.rs:28     bounded(&mut x);
                  ^~~~~~~

The error is fixed by redefining bounded:

fn bounded<T: Thing>(x: &mut T) { // Using 'T' instead of hardcoding the type.
    x.toggle()
}

Should this be caught by the linter?

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