Skip to content

DuplicateBind error triggered by same variable name in alternative pattern #1612

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
ShaneDelmore opened this issue Oct 18, 2016 · 8 comments
Closed

Comments

@ShaneDelmore
Copy link
Contributor

I believe the issue is in Typer.transform. Here is an example snippet and output:

object Test {
  def g(p:(Int,Int)) = p match {
    case (10,n) | (n,10) => println(n) 
    case _ => println("nope")
  }
}

On compile this error is returned:

-- [E005] Naming Error: test.scala -------------------------------------------------------------------------------------
9 |    case (10,n) | (n,10) => println(n)
  |                   ^
  |                   duplicate pattern variable: `n`

longer explanation available when compiling with `-explain`
@ShaneDelmore
Copy link
Contributor Author

Tagging @DarkDimius and @liufengyun at @felixmulder request.

@odersky
Copy link
Contributor

odersky commented Oct 19, 2016

There's clearly an error here, but not the one that's flagged. scalac says:

error: illegal variable in pattern alternative

@ShaneDelmore
Copy link
Contributor Author

@odersky This is actually how I found this bug, I was attempting to improve the error message for "illegal variable in pattern alternative" and found I could not trigger the error message in Dotty as I could in Scala.

@odersky
Copy link
Contributor

odersky commented Oct 25, 2016

@ShaneDelmore You want to give it a try to add the error message? I believe it should be in Typer. The thing to watch out for are Bind nodes that bind a name other than _ inside Alternative nodes.
Maybe add a Mode bit that we are inside an alternative that gets tested in the Bind node?

@ShaneDelmore
Copy link
Contributor Author

I will take a look at it this weekend and see if I can make any headway on it.

@ShaneDelmore
Copy link
Contributor Author

I started looking into this bug last weekend and think I should be able to come up with a fix. Besides the error message, is there any reason not to just allow multiple variable bindings if they exist in every alternative with the same name and same type?

@felixmulder
Copy link
Contributor

This sounds like a judgement call for @odersky, the spec doesn't specifically prohibit this. It might make the desugaring inside PatternMatcher harder - unclear.

@odersky
Copy link
Contributor

odersky commented Nov 1, 2017

Duplicate of #3332.

@odersky odersky closed this as completed Nov 1, 2017
odersky added a commit to dotty-staging/dotty that referenced this issue Nov 1, 2017
odersky added a commit to dotty-staging/dotty that referenced this issue Nov 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants