Skip to content

Commit 2e17a6c

Browse files
authored
Merge pull request #5623 from dotty-staging/fix-#5556
Fix #5556: Check SAM types for realizability
2 parents 0d00e2c + 17cada8 commit 2e17a6c

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,7 @@ object Types {
641641
val rinfo = tp.refinedInfo
642642
if (name.isTypeName && !pinfo.isInstanceOf[ClassInfo]) { // simplified case that runs more efficiently
643643
val jointInfo =
644-
if (rinfo.isTypeAlias) rinfo
645-
else if (pinfo.isTypeAlias) pinfo
646-
else if (ctx.base.pendingMemberSearches.contains(name)) pinfo safe_& rinfo
644+
if (ctx.base.pendingMemberSearches.contains(name)) pinfo safe_& rinfo
647645
else pinfo recoverable_& rinfo
648646
pdenot.asSingleDenotation.derivedSingleDenotation(pdenot.symbol, jointInfo)
649647
} else {

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
268268
case tree: New if isCheckable(tree) =>
269269
Checking.checkInstantiable(tree.tpe, tree.pos)
270270
super.transform(tree)
271+
case tree: Closure if !tree.tpt.isEmpty =>
272+
Checking.checkRealizable(tree.tpt.tpe, tree.pos, "SAM type")
273+
super.transform(tree)
271274
case tree @ Annotated(annotated, annot) =>
272275
cpy.Annotated(tree)(transform(annotated), transformAnnot(annot))
273276
case tree: AppliedTypeTree =>

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ object Checking {
136136
}
137137

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

145145
/** A type map which checks that the only cycles in a type are F-bounds

tests/neg/i5556.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
trait SAM {
2+
type T >: Int
3+
def apply(x: Int): Int
4+
def t: T = 1
5+
}
6+
7+
object O{
8+
def main(a:Array[String])={
9+
val fn: SAM {type T = String} = (i:Int) => i // error: SAM{T = String} is not a legal SAM type
10+
def cce: String = fn.t
11+
println(cce)
12+
}
13+
}

0 commit comments

Comments
 (0)