Skip to content

Fix #3083: Check that all named parts of locally inferred types are d… #3749

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 5 commits into from
Jan 21, 2018
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
17 changes: 17 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,22 @@ trait Checking {
checkNoForwardDependencies(vparams1)
case Nil =>
}

/** Check that all named type that form part of this type have a denotation.
* Called on inferred (result) types of ValDefs and DefDefs.
* This could fail for types where the member was originally available as part
* of the self type, yet is no longer visible once the `this` has been replaced
* by some other prefix. See neg/i3083.scala
*/
def checkMembersOK(tp: Type, pos: Position)(implicit ctx: Context): Type = {
val check: Type => Unit = {
case ref: NamedType if !ref.denot.exists =>
ctx.error(em"$ref is not defined in inferred type $tp", pos)
case _ =>
}
tp.foreachPart(check, stopAtStatic = true)
tp
}
}

trait NoChecking extends Checking {
Expand All @@ -738,4 +754,5 @@ trait NoChecking extends Checking {
override def checkTraitInheritance(parentSym: Symbol, cls: ClassSymbol, pos: Position)(implicit ctx: Context) = ()
override def checkCaseInheritance(parentSym: Symbol, caseCls: ClassSymbol, pos: Position)(implicit ctx: Context) = ()
override def checkNoForwardDependencies(vparams: List[ValDef])(implicit ctx: Context): Unit = ()
override def checkMembersOK(tp: Type, pos: Position)(implicit ctx: Context): Type = tp
}
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ class Namer { typer: Typer =>
case _: untpd.DerivedTypeTree =>
WildcardType
case TypeTree() =>
inferredType
checkMembersOK(inferredType, mdef.pos)
case DependentTypeTree(tpFun) =>
tpFun(paramss.head)
case TypedSplice(tpt: TypeTree) if !isFullyDefined(tpt.tpe, ForceDegree.none) =>
Expand Down
32 changes: 32 additions & 0 deletions tests/neg/i3083.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
object Main {

trait Literal {
type F[T]
def num(i: Int): F[Int]
}

trait Addition { self: Literal =>
def add(l: F[Int], r: F[Int]): F[Int]
}

def expression(adder: Addition) = {
import adder._
add(num(1), num(2)) // error // error (not found: num)
}
}

object Minimized {
trait Literal {
type F[T]
}

trait Addition { self: Literal =>
def foo: F[Int]
}

object Main {
def expression(adder: Addition) = { // error: adder.F is not defined in inferred type
adder.foo
}
}
}
15 changes: 15 additions & 0 deletions tests/pos/i3083.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
object Minimized {
trait Literal {
type F[T]
}

trait Addition { self: Literal =>
def foo: F[Int]
}

object Main {
def expression(adder: Addition & Literal) = { // error: adder.F is not defined in inferred type
adder.foo
}
}
}