Skip to content

Fix #5556: Check SAM types for realizability #5623

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 1 commit into from
Dec 17, 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
4 changes: 1 addition & 3 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,7 @@ object Types {
val rinfo = tp.refinedInfo
if (name.isTypeName && !pinfo.isInstanceOf[ClassInfo]) { // simplified case that runs more efficiently
val jointInfo =
if (rinfo.isTypeAlias) rinfo
else if (pinfo.isTypeAlias) pinfo
else if (ctx.base.pendingMemberSearches.contains(name)) pinfo safe_& rinfo
if (ctx.base.pendingMemberSearches.contains(name)) pinfo safe_& rinfo
else pinfo recoverable_& rinfo
pdenot.asSingleDenotation.derivedSingleDenotation(pdenot.symbol, jointInfo)
} else {
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
case tree: New if isCheckable(tree) =>
Checking.checkInstantiable(tree.tpe, tree.pos)
super.transform(tree)
case tree: Closure if !tree.tpt.isEmpty =>
Checking.checkRealizable(tree.tpt.tpe, tree.pos, "SAM type")
super.transform(tree)
case tree @ Annotated(annotated, annot) =>
cpy.Annotated(tree)(transform(annotated), transformAnnot(annot))
case tree: AppliedTypeTree =>
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ object Checking {
}

/** Check that type `tp` is realizable. */
def checkRealizable(tp: Type, pos: Position)(implicit ctx: Context): Unit = {
def checkRealizable(tp: Type, pos: Position, what: String = "path")(implicit ctx: Context): Unit = {
val rstatus = realizability(tp)
if (rstatus ne Realizable)
ctx.errorOrMigrationWarning(em"$tp is not a legal path\nsince it${rstatus.msg}", pos)
ctx.errorOrMigrationWarning(em"$tp is not a legal $what\nsince it${rstatus.msg}", pos)
}

/** A type map which checks that the only cycles in a type are F-bounds
Expand Down
13 changes: 13 additions & 0 deletions tests/neg/i5556.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
trait SAM {
type T >: Int
def apply(x: Int): Int
def t: T = 1
}

object O{
def main(a:Array[String])={
val fn: SAM {type T = String} = (i:Int) => i // error: SAM{T = String} is not a legal SAM type
def cce: String = fn.t
println(cce)
}
}