Skip to content

Fix #1503: be more careful where to insert apply #1522

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 2 commits into from
Sep 18, 2016
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
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ object desugar {
Apply(Select(left, op), args)
} else {
val x = ctx.freshName().toTermName
Block(
new InfixOpBlock(
ValDef(x, TypeTree(), left).withMods(synthetic),
Apply(Select(right, op), Ident(x)))
}
Expand Down
10 changes: 10 additions & 0 deletions src/dotty/tools/dotc/ast/untpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
class PolyTypeDef(name: TypeName, override val tparams: List[TypeDef], rhs: Tree)
extends TypeDef(name, rhs)

/** A block arising from a right-associative infix operation, where, e.g.
*
* a +: b
*
* is expanded to
*
* { val x = a; b.+:(x) }
*/
class InfixOpBlock(leftOperand: Tree, rightOp: Tree) extends Block(leftOperand :: Nil, rightOp)

// ----- TypeTrees that refer to other tree's symbols -------------------

/** A type tree that gets its type from some other tree's symbol. Enters the
Expand Down
5 changes: 4 additions & 1 deletion src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -927,14 +927,17 @@ object Types {
def narrow(implicit ctx: Context): TermRef =
TermRef(NoPrefix, ctx.newSkolem(this))

/** Useful for diagnsotics: The underlying type if this type is a type proxy,
/** Useful for diagnostics: The underlying type if this type is a type proxy,
* otherwise NoType
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: weird indent doesn't match next row

*/
def underlyingIfProxy(implicit ctx: Context) = this match {
case this1: TypeProxy => this1.underlying
case _ => NoType
}

/** If this is a FunProto or PolyProto, WildcardType, otherwise this. */
def notApplied: Type = this

// ----- Normalizing typerefs over refined types ----------------------------

/** If this normalizes* to a refinement type that has a refinement for `name` (which might be followed
Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>

/** Overridden in ReTyper to handle primitive operations that can be generated after erasure */
protected def handleUnexpectedFunType(tree: untpd.Apply, fun: Tree)(implicit ctx: Context): Tree =
throw new Error(s"unexpected type.\n fun = $fun,\n methPart(fun) = ${methPart(fun)},\n methPart(fun).tpe = ${methPart(fun).tpe},\n tpe = ${fun.tpe}")
throw new Error(i"unexpected type.\n fun = $fun,\n methPart(fun) = ${methPart(fun)},\n methPart(fun).tpe = ${methPart(fun).tpe},\n tpe = ${fun.tpe}")

def typedNamedArgs(args: List[untpd.Tree])(implicit ctx: Context) =
for (arg @ NamedArg(id, argtpt) <- args) yield {
Expand Down
4 changes: 4 additions & 0 deletions src/dotty/tools/dotc/typer/ProtoTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ object ProtoTypes {
if ((args eq this.args) && (resultType eq this.resultType) && (typer eq this.typer)) this
else new FunProto(args, resultType, typer)

override def notApplied = WildcardType

/** Forget the types of any arguments that have been typed producing a constraint in a
* typer state that is not yet committed into the one of the current context `ctx`.
* This is necessary to avoid "orphan" PolyParams that are referred to from
Expand Down Expand Up @@ -319,6 +321,8 @@ object ProtoTypes {
if ((targs eq this.targs) && (resType eq this.resType)) this
else PolyProto(targs, resType)

override def notApplied = WildcardType

def map(tm: TypeMap)(implicit ctx: Context): PolyProto =
derivedPolyProto(targs mapConserve tm, tm(resultType))

Expand Down
21 changes: 13 additions & 8 deletions src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,13 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
def typedBlock(tree: untpd.Block, pt: Type)(implicit ctx: Context) = track("typedBlock") {
val exprCtx = index(tree.stats)
val stats1 = typedStats(tree.stats, ctx.owner)
val expr1 = typedExpr(tree.expr, pt)(exprCtx)
val ept =
if (tree.isInstanceOf[untpd.InfixOpBlock])
// Right-binding infix operations are expanded to InfixBlocks, which may be followed by arguments.
// Example: `(a /: bs)(op)` expands to `{ val x = a; bs./:(x) } (op)` where `{...}` is an InfixBlock.
pt
else pt.notApplied
val expr1 = typedExpr(tree.expr, ept)(exprCtx)
ensureNoLocalRefs(
assignType(cpy.Block(tree)(stats1, expr1), stats1, expr1), pt, localSyms(stats1))
}
Expand Down Expand Up @@ -619,8 +625,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit

def typedIf(tree: untpd.If, pt: Type)(implicit ctx: Context) = track("typedIf") {
val cond1 = typed(tree.cond, defn.BooleanType)
val thenp1 = typed(tree.thenp, pt)
val elsep1 = typed(tree.elsep orElse (untpd.unitLiteral withPos tree.pos), pt)
val thenp1 = typed(tree.thenp, pt.notApplied)
val elsep1 = typed(tree.elsep orElse (untpd.unitLiteral withPos tree.pos), pt.notApplied)
val thenp2 :: elsep2 :: Nil = harmonize(thenp1 :: elsep1 :: Nil)
assignType(cpy.If(tree)(cond1, thenp2, elsep2), thenp2, elsep2)
}
Expand Down Expand Up @@ -793,7 +799,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
val selType = widenForMatchSelector(
fullyDefinedType(sel1.tpe, "pattern selector", tree.pos))

val cases1 = typedCases(tree.cases, selType, pt)
val cases1 = typedCases(tree.cases, selType, pt.notApplied)
val cases2 = harmonize(cases1).asInstanceOf[List[CaseDef]]
assignType(cpy.Match(tree)(sel1, cases2), cases2)
}
Expand Down Expand Up @@ -920,8 +926,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
}

def typedTry(tree: untpd.Try, pt: Type)(implicit ctx: Context): Try = track("typedTry") {
val expr1 = typed(tree.expr, pt)
val cases1 = typedCases(tree.cases, defn.ThrowableType, pt)
val expr1 = typed(tree.expr, pt.notApplied)
val cases1 = typedCases(tree.cases, defn.ThrowableType, pt.notApplied)
val finalizer1 = typed(tree.finalizer, defn.UnitType)
val expr2 :: cases2x = harmonize(expr1 :: cases1)
val cases2 = cases2x.asInstanceOf[List[CaseDef]]
Expand Down Expand Up @@ -1535,8 +1541,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
}

/** If this tree is a select node `qual.name`, try to insert an implicit conversion
* `c` around `qual` so that `c(qual).name` conforms to `pt`. If that fails
* return `tree` itself.
* `c` around `qual` so that `c(qual).name` conforms to `pt`.
*/
def tryInsertImplicitOnQualifier(tree: Tree, pt: Type)(implicit ctx: Context): Option[Tree] = ctx.traceIndented(i"try insert impl on qualifier $tree $pt") {
tree match {
Expand Down
14 changes: 14 additions & 0 deletions tests/neg/i1503.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
object Test {

val cond = true
def foo1() = println("hi")
def bar1() = println("there")

def foo2(x: Int) = println("hi")
def bar2(x: Int) = println("there")

def main(args: Array[String]) = {
(if (cond) foo1 else bar1)() // error: Unit does not take parameters
(if (cond) foo2 else bar2)(22) // error: missing arguments // error: missing arguments
}
}
5 changes: 5 additions & 0 deletions tests/run/i1503.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
hello
hi
33
hi
hi
38 changes: 38 additions & 0 deletions tests/run/i1503.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
object Test {

def test1() =
(new Function0[Unit] {
def apply() = println("hello")
})()

val cond = true
val foo = () => println("hi")
val bar = () => println("there")

val baz = (x: Int) => println(x)

def test2() =
(if (cond) foo else bar)()

def test2a() =
(if (cond) baz else baz)(33)

def test3() =
(try foo
catch { case ex: Exception => bar }
finally ())()

def test4() =
(cond match {
case true => foo
case false => bar
})()

def main(args: Array[String]) = {
test1()
test2()
test2a()
test3()
test4()
}
}