Skip to content

"Unreachable pattern" if matched variant is not imported #19100

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

Closed
vhbit opened this issue Nov 19, 2014 · 6 comments
Closed

"Unreachable pattern" if matched variant is not imported #19100

vhbit opened this issue Nov 19, 2014 · 6 comments

Comments

@vhbit
Copy link
Contributor

vhbit commented Nov 19, 2014

enum Foo {
  Bar,
  Baz
}

impl Foo {
   fn foo(&self) {
      match self {
         &Bar => println!("bar"),
         &Baz => println!("baz"),
      }
   }
}

It reports unreachable pattern:

test.rs:12:9: 12:13 error: unreachable pattern [E0001]
test.rs:12         &Baz => println!("baz"), 
                   ^~~~
test.rs:12:9: 12:13 help: pass `--explain E0001` to see a detailed explanation

while actually it should report unknown identifier

@0xc0170
Copy link

0xc0170 commented Nov 19, 2014

I can confirm this with the latest rust nightly (rustc 0.13.0-nightly (336349c 2014-11-17 20:37:19 +0000)

In zinc there are some enums and then match in the code, which did not reflect the latest changes in namespaced enums, thus reported as unreachable pattern, but once namespace was added, it built without errors.

@vhbit
Copy link
Contributor Author

vhbit commented Nov 19, 2014

I've actually seen it long before namespaced enums reform (so it is not related to it) in macros, but didn't bother reporting as though it was just a problem with macros.

@mahkoh
Copy link
Contributor

mahkoh commented Nov 19, 2014

This is working as expected.

     match self {
         &Bar => println!("bar"),
      }

is equivalent to

{
    let &Bar = self;
    println!("bar");
}

@vhbit
Copy link
Contributor Author

vhbit commented Nov 19, 2014

@mahkoh aha, it seems to be actually explanation why it happens - in example you've provided it also shows a warning "unused variable Bar". So looks like Bar and Baz are treated as "bind a variable Bar to value of self". In my sample it means that indeed, Baz arm is unreachable.

@klutzy
Copy link
Contributor

klutzy commented Nov 19, 2014

Yes, Bar is not imported so it's working as wildcard variable. See also #10402 and #10304.

@ghost
Copy link

ghost commented Nov 19, 2014

Closing as a dupe of #10402.

@ghost ghost closed this as completed Nov 19, 2014
This issue was closed.
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

No branches or pull requests

4 participants