Skip to content

Fix #2514 Addendum #2754

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

Merged
merged 3 commits into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
// Note: the <: Type constraint looks necessary (and is needed to make the file compile in dotc).
// But Scalac accepts the program happily without it. Need to find out why.

def unsplice[T >: Untyped](tree: Trees.Tree[T]): Trees.Tree[T] = tree.asInstanceOf[untpd.Tree] match {
case untpd.TypedSplice(tree1) => tree1.asInstanceOf[Trees.Tree[T]]
case _ => tree
}
def unsplice(tree: Trees.Tree[T]): Trees.Tree[T] = tree

def isDeclarationOrTypeDef(tree: Tree): Boolean = unsplice(tree) match {
case DefDef(_, _, _, _, EmptyTree)
Expand Down Expand Up @@ -116,7 +113,7 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
case _ => false
}

def isSuperSelection(tree: untpd.Tree) = unsplice(tree) match {
def isSuperSelection(tree: Tree) = unsplice(tree) match {
case Select(Super(_, _), _) => true
case _ => false
}
Expand All @@ -129,7 +126,7 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
}

/** Is tree a variable pattern? */
def isVarPattern(pat: untpd.Tree): Boolean = unsplice(pat) match {
def isVarPattern(pat: Tree): Boolean = unsplice(pat) match {
case x: BackquotedIdent => false
case x: Ident => x.name.isVariableName
case _ => false
Expand Down Expand Up @@ -160,7 +157,7 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
def isLeftAssoc(operator: Name) = !operator.isEmpty && (operator.toSimpleName.last != ':')

/** can this type be a type pattern? */
def mayBeTypePat(tree: untpd.Tree): Boolean = unsplice(tree) match {
def mayBeTypePat(tree: Tree): Boolean = unsplice(tree) match {
case AndTypeTree(tpt1, tpt2) => mayBeTypePat(tpt1) || mayBeTypePat(tpt2)
case OrTypeTree(tpt1, tpt2) => mayBeTypePat(tpt1) || mayBeTypePat(tpt2)
case RefinedTypeTree(tpt, refinements) => mayBeTypePat(tpt) || refinements.exists(_.isInstanceOf[Bind])
Expand Down Expand Up @@ -253,6 +250,13 @@ trait UntypedTreeInfo extends TreeInfo[Untyped] { self: Trees.Instance[Untyped]
import TreeInfo._
import untpd._

/** The underlying tree when stripping any TypedSplice or Parens nodes */
override def unsplice(tree: Tree): Tree = tree match {
case TypedSplice(tree1) => tree1
case Parens(tree1) => unsplice(tree1)
case _ => tree
}

/** True iff definition is a val or def with no right-hand-side, or it
* is an abstract typoe declaration
*/
Expand Down
8 changes: 6 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,12 @@ object ErrorReporting {

def patternConstrStr(tree: Tree): String = ???

def typeMismatch(tree: Tree, pt: Type, implicitFailure: SearchFailure = NoImplicitMatches): Tree =
errorTree(tree, typeMismatchMsg(normalize(tree.tpe, pt), pt, implicitFailure.postscript))
def typeMismatch(tree: Tree, pt: Type, implicitFailure: SearchFailure = NoImplicitMatches): Tree = {
val normTp = normalize(tree.tpe, pt)
val treeTp = if (normTp <:< pt) tree.tpe else normTp
// use normalized type if that also shows an error, original type otherwise
errorTree(tree, typeMismatchMsg(treeTp, pt, implicitFailure.postscript))
}

/** A subtype log explaining why `found` does not conform to `expected` */
def whyNoMatchStr(found: Type, expected: Type) = {
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
if (ctx.mode is Mode.Pattern) {
if (name == nme.WILDCARD)
return tree.withType(pt)
if (isVarPattern(tree) && name.isTermName)
if (untpd.isVarPattern(tree) && name.isTermName)
return typed(desugar.patternVar(tree), pt)
}

Expand Down Expand Up @@ -496,7 +496,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
* (x: T) to (x @ (w: T)). This is either `_` or `_*`.
*/
def cases(ifPat: => Tree, ifExpr: => Tree, wildName: TermName) = tree.expr match {
case id: untpd.Ident if (ctx.mode is Mode.Pattern) && isVarPattern(id) =>
case id: untpd.Ident if (ctx.mode is Mode.Pattern) && untpd.isVarPattern(id) =>
if (id.name == nme.WILDCARD || id.name == nme.WILDCARD_STAR) ifPat
else {
import untpd._
Expand Down Expand Up @@ -1114,7 +1114,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
def typedArg(arg: untpd.Tree, tparam: ParamInfo) = {
val (desugaredArg, argPt) =
if (ctx.mode is Mode.Pattern)
(if (isVarPattern(arg)) desugar.patternVar(arg) else arg, tparam.paramInfo)
(if (untpd.isVarPattern(arg)) desugar.patternVar(arg) else arg, tparam.paramInfo)
else
(arg, WildcardType)
if (tpt1.symbol.isClass)
Expand Down
10 changes: 10 additions & 0 deletions tests/neg/i2514a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object Foo {
def foo(): Int = {
val f: implicit Int => Int = implicit (x: Int) => 2 * x
f(2)
}

val f = implicit (x: Int) => x

(implicit (x: Int) => x): (implicit Int => Int) // error: no implicit argument found
}