Skip to content

Commit 37e5010

Browse files
committed
Interpret transparent for traits
1 parent 0dab4d5 commit 37e5010

File tree

7 files changed

+23
-21
lines changed

7 files changed

+23
-21
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -286,35 +286,35 @@ trait ConstraintHandling {
286286
}
287287
}
288288

289-
/** If `tp` is an intersection such that some operands are mixin trait instances
290-
* and others are not, replace as many mixin trait instances as possible with Any
289+
/** If `tp` is an intersection such that some operands are transparent trait instances
290+
* and others are not, replace as many transparent trait instances as possible with Any
291291
* as long as the result is still a subtype of `bound`. But fall back to the
292292
* original type if the resulting widened type is a supertype of all dropped
293-
* types (since in this case the type was not a true intersection of mixin traits
293+
* types (since in this case the type was not a true intersection of transparent traits
294294
* and other types to start with).
295295
*/
296-
def dropMixinTraits(tp: Type, bound: Type)(using Context): Type =
296+
def dropTransparentTraits(tp: Type, bound: Type)(using Context): Type =
297297
var kept: Set[Type] = Set() // types to keep since otherwise bound would not fit
298298
var dropped: List[Type] = List() // the types dropped so far, last one on top
299299

300-
def dropOneMixinTrait(tp: Type): Type =
300+
def dropOneTransparentTrait(tp: Type): Type =
301301
val tpd = tp.dealias
302-
if tpd.typeSymbol.isMixinTrait && !tpd.isLambdaSub && !kept.contains(tpd) then
302+
if tpd.typeSymbol.isTransparentTrait && !tpd.isLambdaSub && !kept.contains(tpd) then
303303
dropped = tpd :: dropped
304304
defn.AnyType
305305
else tpd match
306306
case AndType(tp1, tp2) =>
307-
val tp1w = dropOneMixinTrait(tp1)
307+
val tp1w = dropOneTransparentTrait(tp1)
308308
if tp1w ne tp1 then tp1w & tp2
309309
else
310-
val tp2w = dropOneMixinTrait(tp2)
310+
val tp2w = dropOneTransparentTrait(tp2)
311311
if tp2w ne tp2 then tp1 & tp2w
312312
else tpd
313313
case _ =>
314314
tp
315315

316316
def recur(tp: Type): Type =
317-
val tpw = dropOneMixinTrait(tp)
317+
val tpw = dropOneTransparentTrait(tp)
318318
if tpw eq tp then tp
319319
else if tpw <:< bound then recur(tpw)
320320
else
@@ -324,15 +324,15 @@ trait ConstraintHandling {
324324

325325
val tpw = recur(tp)
326326
if (tpw eq tp) || dropped.forall(_ frozen_<:< tpw) then tp else tpw
327-
end dropMixinTraits
327+
end dropTransparentTraits
328328

329329
/** Widen inferred type `inst` with upper `bound`, according to the following rules:
330330
* 1. If `inst` is a singleton type, or a union containing some singleton types,
331331
* widen (all) the singleton type(s), provided the result is a subtype of `bound`
332332
* (i.e. `inst.widenSingletons <:< bound` succeeds with satisfiable constraint)
333333
* 2. If `inst` is a union type, approximate the union type from above by an intersection
334334
* of all common base types, provided the result is a subtype of `bound`.
335-
* 3. drop mixin traits from intersections (see @dropMixinTraits)
335+
* 3. drop transparent traits from intersections (see @dropTransparentTraits)
336336
*
337337
* Don't do these widenings if `bound` is a subtype of `scala.Singleton`.
338338
* Also, if the result of these widenings is a TypeRef to a module class,
@@ -357,7 +357,7 @@ trait ConstraintHandling {
357357

358358
val wideInst =
359359
if isSingleton(bound) then inst
360-
else dropMixinTraits(widenOr(widenSingle(inst)), bound)
360+
else dropTransparentTraits(widenOr(widenSingle(inst)), bound)
361361
wideInst match
362362
case wideInst: TypeRef if wideInst.symbol.is(Module) =>
363363
TermRef(wideInst.prefix, wideInst.symbol.sourceModule)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,5 @@ object Flags {
579579
val SyntheticParam: FlagSet = Synthetic | Param
580580
val SyntheticTermParam: FlagSet = Synthetic | TermParam
581581
val SyntheticTypeParam: FlagSet = Synthetic | TypeParam
582+
val TransparentTrait: FlagSet = Trait | Transparent
582583
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,9 +1109,10 @@ object SymDenotations {
11091109
final def isEffectivelySealed(using Context): Boolean =
11101110
isOneOf(FinalOrSealed) || isClass && !isOneOf(EffectivelyOpenFlags)
11111111

1112-
final def isMixinTrait(using Context): Boolean =
1113-
isClass
1114-
&& (hasAnnotation(defn.MixinAnnot) || defn.assumedMixinTraits.contains(symbol.asClass))
1112+
final def isTransparentTrait(using Context): Boolean =
1113+
isAllOf(TransparentTrait)
1114+
|| isClass
1115+
&& (hasAnnotation(defn.MixinAnnot) || defn.assumedMixinTraits.contains(symbol.asClass))
11151116

11161117
/** The class containing this denotation which has the given effective name. */
11171118
final def enclosingClassNamed(name: Name)(using Context): Symbol = {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2674,8 +2674,8 @@ object TypeComparer {
26742674
def widenInferred(inst: Type, bound: Type)(using Context): Type =
26752675
comparing(_.widenInferred(inst, bound))
26762676

2677-
def dropMixinTraits(tp: Type, bound: Type)(using Context): Type =
2678-
comparing(_.dropMixinTraits(tp, bound))
2677+
def dropTransparentTraits(tp: Type, bound: Type)(using Context): Type =
2678+
comparing(_.dropTransparentTraits(tp, bound))
26792679

26802680
def constrainPatternType(pat: Type, scrut: Type)(using Context): Boolean =
26812681
comparing(_.constrainPatternType(pat, scrut))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ trait Applications extends Compatibility {
11351135
&& tree.tpe.classSymbol.isEnumCase
11361136
&& tree.tpe.widen.isValueType
11371137
then
1138-
val widened = TypeComparer.dropMixinTraits(
1138+
val widened = TypeComparer.dropTransparentTraits(
11391139
tree.tpe.parents.reduceLeft(TypeComparer.andType(_, _)),
11401140
pt)
11411141
if widened <:< pt then Typed(tree, TypeTree(widened))

tests/neg-custom-args/fatal-warnings/supertraits.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import annotation.mixin
2-
@mixin sealed trait TA
3-
@mixin sealed trait TB
2+
transparent sealed trait TA
3+
transparent sealed trait TB
44
trait S
55
case object a extends S, TA, TB
66
case object b extends S, TA, TB

tests/neg/supertraits.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import annotation.mixin
2-
@mixin trait S
2+
transparent trait S
33
trait A
44
class B extends A, S
55
class C extends A, S

0 commit comments

Comments
 (0)