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

Warn on pattern bindings that have the same name as a variant #19115

Closed
wants to merge 2 commits into from
Closed

Warn on pattern bindings that have the same name as a variant #19115

wants to merge 2 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Nov 19, 2014

...of the type being matched.

This change will result in a better diagnostic for code like the following:

enum Enum {
    Foo,
    Bar
}

fn f(x: Enum) {
    match x {
        Foo => (),
        Bar => ()
    }
}

which would currently simply fail with an unreachable pattern error
on the 2nd arm.

The user is advised to either use a qualified path in the patterns
or import the variants explicitly into the scope.

@ghost
Copy link
Author

ghost commented Nov 19, 2014

I considered making this a warning instead but ended up going with a hard error.

I cannot think of a situation where the code that this change rejects would be intended. On the other hand, making this produce a warning would mean that the compiler would have to proceed to check exhaustiveness which would almost always fail anyway. So at the end as a user you would see the lint warning and an exhaustiveness error, and most likely only pay attention to the latter, which is guaranteed to be rather obscure.

@ghost
Copy link
Author

ghost commented Nov 19, 2014

cc @nikomatsakis @pnkfelix

@alexcrichton
Copy link
Member

As a language change, I'm not sure I saw a corresponding RFC for this, although I may have missed it as part of a recent one!

@huonw
Copy link
Member

huonw commented Nov 20, 2014

FWIW, a lint doesn't work with our current system, since the lint pass occurs too late (errors about unreachable patterns etc. occur long before linting runs).

I'm would definitely be in favour of this being changed to a help diagnostic rather than an error (to get it in the compiler without having to worry about these pesky RFCs 😛 ):

enum Foo { A, B }

fn main() {
    match A {
        A => {}
        Foo::B => {}
    }
}
error: unreachable pattern
        Foo::B => {}
        ^~~~~~
help: this pattern has the same name as a variant of `Foo`, perhaps you meant to bring it into scope?
        A
        ^

@ghost ghost changed the title Forbid pattern bindings that have the same name as a variant Lint on pattern bindings that have the same name as a variant Nov 20, 2014
@ghost ghost changed the title Lint on pattern bindings that have the same name as a variant Warn on pattern bindings that have the same name as a variant Nov 20, 2014
@ghost
Copy link
Author

ghost commented Nov 20, 2014

@alexcrichton @huonw Right. This improvement is particularly important now with the enum namespacing change recently landed, although I suppose we're already late. :-) So in the interest of moving this forward I changed the new diagnostic to be a warning instead. I think making it a hard error would still be good and I will consider writing an RFC for it.

@alexcrichton
Copy link
Member

I general I would expect -A warnings to turn off all warnings in the compiler (Cargo relies on this), so I'm somewhat wary of adding new span_warn flags to the compiler. I do find it unfortunate that a lint can't be used here (as it's kinda what it wants), however...

@ghost
Copy link
Author

ghost commented Nov 22, 2014

@alexcrichton IMO this sits somewhere in between an error and a warning in that it's not fatal enough that compilation can still proceed (or otherwise halt at exhaustiveness) but in 99% cases it points out a genuine error so the user should know about it.

@alexcrichton
Copy link
Member

Yeah I definitely agree that this is basically always an error, I just feel uneasy about baking this into the language as such (hence the warning), but then it's unfortunate to have warnings you can't turn off... I suppose I'm just a little on the fence about this in that I want to see this have a nice error message but I also don't want to bake anything too much into the language about this either...

@ghost
Copy link
Author

ghost commented Nov 24, 2014

@alexcrichton I see! Thanks for the feedback. I made it so that warnings will not be printed out if the command-line level lint level for the warnings group is allow. This isn't perfect in that ideally it should respect crate-level and item-level lint levels as well but it should address Cargo's use case. FWIW, there already are warnings in rustc that would not have respected the flag before this change.

@alexcrichton
Copy link
Member

Whoa, the solution here had not actually occurred to me, but it seems great, nice idea! (sorry for being so nitpickity)

Jakub Bukaj added 2 commits November 26, 2014 22:21
...of the type being matched.

This change will result in a better diagnostic for code like the following:

```rust
enum Enum {
    Foo,
    Bar
}

fn f(x: Enum) {
    match x {
        Foo => (),
        Bar => ()
    }
}
```

which would currently simply fail with an unreachable pattern error
on the 2nd arm.

The user is advised to either use a qualified path in the patterns
or import the variants explicitly into the scope.
bors added a commit that referenced this pull request Nov 26, 2014
...of the type being matched.

This change will result in a better diagnostic for code like the following:

```rust
enum Enum {
    Foo,
    Bar
}

fn f(x: Enum) {
    match x {
        Foo => (),
        Bar => ()
    }
}
```

which would currently simply fail with an unreachable pattern error
on the 2nd arm.

The user is advised to either use a qualified path in the patterns
or import the variants explicitly into the scope.
@bors bors closed this Nov 27, 2014
@nikomatsakis
Copy link
Contributor

On Thu, Nov 20, 2014 at 08:24:25AM -0800, Alex Crichton wrote:

I general I would expect -A warnings to turn off all warnings in
the compiler (Cargo relies on this), so I'm somewhat wary of adding
new span_warn flags to the compiler. I do find it unfortunate that
a lint can't be used here (as it's kinda what it wants), however...

This matter of timing is related to the idea of using a lint for shadowing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants