Skip to content

Warn if type-name in parameter-position #67069

@lukaslueg

Description

@lukaslueg

I was surprised that one can confuse himself like in the cooked-up example below: Specifying a concrete type for a parameter on an impl does what one expects. This might lead to the expectation that you can specify a concrete type for a parameter on an fn, which is not the case:

struct Foobar<T>(T);

trait Foo {
    fn bar<H>(&self, b: H) where H: std::fmt::Display ;
}

// `String` is a type...
impl Foo for Foobar<String> {
    // `String` is not a type, it's a type-parameter named `String`.
    // This is almost guaranteed to be wrong!
    fn bar<String>(&self, _b: String) where String: std::fmt::Display {}
}

Here, String does not refer to the type String but is still a generic parameter, now named String instead of H.

If we don't specify the Display-bound, the compiler even helpfully suggests to add the requirement String: Display, which we can do right away. We can use fn bar<u32>(...) and the compiler will warn that u32 should be upper-cased.

Maybe the compiler could warn if a parameter is given the name of a type that is in scope? Something to the tune of

The type-parameter named String in fn bar<String>(...) has the same name as the type std::string::String in the local scope. Be aware that this declares a type-parameter named String. It does not specify a concrete type and might lead to confusion. The original definition is fn bar<H>(...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions