Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Dr-Emann opened this issue Jun 30, 2014 · 3 comments
Closed

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

Dr-Emann opened this issue Jun 30, 2014 · 3 comments
Labels
A-traits Area: Trait system

Comments

@Dr-Emann
Copy link

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
}
@huonw
Copy link
Member

huonw commented Jun 30, 2014

Relevant issues: this will become callable with rust-lang/rfcs#132 (not really true, since the param is only in the impl); #11658 a similar case: warning when generics are shadowed.

@steveklabnik
Copy link
Member

Triage: still an issue.

@steveklabnik steveklabnik added the A-traits Area: Trait system label Jan 23, 2015
@steveklabnik
Copy link
Member

This kind of code isn't even legal anymore:

hello.rs:2:6: 2:7 error: the type parameter `A` is not constrained by the impl trait, self type, or predicates [E0207]
hello.rs:2 impl<A: std::fmt::Display, B> Foo {
                ^
hello.rs:2:28: 2:29 error: the type parameter `B` is not constrained by the impl trait, self type, or predicates [E0207]
hello.rs:2 impl<A: std::fmt::Display, B> Foo {
                                      ^

This would seem to be ' Alternatively, we could disallow generics on impl blocks that are not used as type parameters on the implementing type.'

Closing!

bors added a commit to rust-lang-ci/rust that referenced this issue Jul 17, 2023
Give real discriminant_type to chalk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system
Projects
None yet
Development

No branches or pull requests

3 participants