Skip to content

By name match selector #4999

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
allanrenucci opened this issue Aug 24, 2018 · 1 comment
Closed

By name match selector #4999

allanrenucci opened this issue Aug 24, 2018 · 1 comment

Comments

@allanrenucci
Copy link
Contributor

trait Foo
final class Bar extends Foo

class Test {
  def test(xs: => Foo) = xs match {
    case xs: Bar => 1
    case _       => 2
  }
}
-- Error: tests/allan/Test.scala:104:9 -----------------------------------------
104 |    case xs: Bar => 1
    |         ^
    |this case is unreachable since trait Function0 and class Bar are unrelated

However, this is not an issue with the exhaustivity checker. It fails Ycheck:

checking tests/allan/Test.scala after phase MegaPhase{pruneErasedDefs, vcInlineMethods, seqLiterals, intercepted, getters, elimByName, collectNullableFields, elimOuterSelect, augmentScala2Traits, resolveSuper, primitiveForwarders, functionXXLForwarders, arrayConstructors}
exception while typing case val x1: (() => Foo)(xs) = xs.apply() of class class dotty.tools.dotc.ast.Trees$ValDef # 777
exception while typing {
  case val x1: (() => Foo)(xs) = xs.apply()
  if x1.$isInstanceOf$[Bar] then 
    {
      case val x2: Bar = x1.asInstanceOf[Bar]
      case val xs: Bar = x2
      1
    }
   else 2
} of class class dotty.tools.dotc.ast.Trees$Block # 778
...
Exception in thread "main" java.lang.AssertionError: assertion failed: found:    Foo
required: (() => Foo)(xs)

The type we get for case val x1 after ElimByName ((() => Foo)(xs)) is clearly wrong. Should be Foo

@Blaisorblade
Copy link
Contributor

Is it normal that the type of the scrutinee xs (in PatternMatcher) is TermRef(NoPrefix, xs)/ xs.type? That doesn't look very useful. Or maybe it is — it'd be bad for xs definition but it's fine for a mention to it.

x1's is given by TermRef(NoPrefix, xs).widenExpr, which won't strip the ExprType, but either widenTermRefExpr or (more conservative) widenIfUnstable will manage.

@allanrenucci allanrenucci added this to the 0.10 Tech Preview milestone Sep 4, 2018
allanrenucci added a commit that referenced this issue Sep 5, 2018
Fix #4999: allow pattern matching against by-name arguments
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

2 participants