Skip to content

Commit c4bebff

Browse files
committed
WIP
1 parent 054c53d commit c4bebff

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,8 @@ object Types {
671671
val rinfo = tp.refinedInfo
672672
if (name.isTypeName && !pinfo.isInstanceOf[ClassInfo]) { // simplified case that runs more efficiently
673673
val jointInfo =
674-
if (ctx.base.pendingMemberSearches.contains(name)) pinfo safe_& rinfo
674+
if rinfo.isInstanceOf[TypeAlias] then rinfo
675+
else if (ctx.base.pendingMemberSearches.contains(name)) pinfo safe_& rinfo
675676
else pinfo recoverable_& rinfo
676677
pdenot.asSingleDenotation.derivedSingleDenotation(pdenot.symbol, jointInfo)
677678
}

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

+28
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,34 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
340340
else
341341
Checking.checkAppliedType(tree)
342342
super.transform(tree)
343+
case tree: RefinedTypeTree =>
344+
//println(i"checking $tree: ${tree.tpe}")
345+
def checkRefinements(tp: Type): Unit = tp match
346+
case RefinedType(parent, name, info) =>
347+
info match
348+
case info: TypeAlias =>
349+
val mbr = parent.member(name)
350+
mbr.info match
351+
case bounds: TypeBounds if !bounds.contains(info) =>
352+
//println(i"falling back to checking F-bounded ${tree.tpe}")
353+
val site = tree.tpe.narrow
354+
val narrowMbr = mbr.asSeenFrom(site)
355+
val owner = mbr.symbol.maybeOwner
356+
val narrowInfo = info.asSeenFrom(site, owner)
357+
val ok = narrowMbr.info match
358+
case bounds: TypeBounds => bounds.contains(narrowInfo)
359+
case _ => false
360+
//println(i"fall back ${narrowMbr.info} $narrowInfo")
361+
if !ok then
362+
report.error(
363+
em"""type alias type $name$info
364+
|out of bounds: $bounds / ${bounds.toString}""", tree.sourcePos)
365+
case _ =>
366+
case _ =>
367+
checkRefinements(parent)
368+
case _ =>
369+
checkRefinements(tree.tpe)
370+
super.transform(tree)
343371
case SingletonTypeTree(ref) =>
344372
Checking.checkRealizable(ref.tpe, ref.posd)
345373
super.transform(tree)

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

+12
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ object Checking {
123123
withMode(Mode.AllowLambdaWildcardApply)(checkValidIfApply)
124124
}
125125

126+
/** Check refined type for well-formedness. This means
127+
* - all arguments are within their corresponding bounds
128+
* - if type is a higher-kinded application with wildcard arguments,
129+
* check that it or one of its supertypes can be reduced to a normal application.
130+
* Unreducible applications correspond to general existentials, and we
131+
* cannot handle those.
132+
* @param tree The applied type tree to check
133+
* @param tpt If `tree` is synthesized from a type in a TypeTree,
134+
* the original TypeTree, or EmptyTree otherwise.
135+
def checkRefinedType(tp: RefinedType)
136+
*/
137+
126138
/** Check all applied type trees in inferred type `tpt` for well-formedness */
127139
def checkAppliedTypesIn(tpt: TypeTree)(using Context): Unit =
128140
val checker = new TypeTraverser:

tests/neg/i6225.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
object O1 { // error: cannot be instantiated
1+
object O1 {
22
type A[X] = X
33
opaque type T = A // error: opaque type alias must be fully applied
44
}
55

66
object O2 {
77
opaque type A[X] = X
8-
object A { // error: cannot be instantiated
8+
object A {
99
opaque type T = A // error: opaque type alias must be fully applied
1010
}
1111
}

tests/pending/pos/i9346.scala

-9
This file was deleted.

0 commit comments

Comments
 (0)