Skip to content

Rust allows uncallable methods to be defined in generic impl blocks #15282

Closed

Description

Rust allows impl blocks to be generic, even if the type is not generic. However, any methods contained in such a generic impl block must use every generic parameter, or the functions become uncallable (because the other types cannot be specified). Rust will not find any errors or warnings in such a defintion, only at the call site.

Rust should at least complain at the declaration site if all generic parameters are not used by every method. Alternatively, we could disallow generics on impl blocks that are not used as type parameters on the implementing type.

Example Code

struct Foo;
impl<A: std::fmt::Show, B> Foo {

    fn func(&mut self, a: A)
    {
        println!("{}", a);
    }
}

fn main() {
    let mut f = Foo;
    // uncomment the following line, program fails to compile.
    //f.func(42i); // error: cannot determine a type for this expression: unconstrained type
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-traitsArea: Trait systemArea: Trait system

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions