Skip to content

Commit f7c46af

Browse files
committed
Harden resultTypeApprox
1 parent 64e36f1 commit f7c46af

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,12 @@ object Types {
142142
/** Is this type a value type? */
143143
final def isValueType: Boolean = this.isInstanceOf[ValueType]
144144

145-
/** Is the is value type or type lambda? */
145+
/** Is this a value type or a type lambda? */
146146
final def isValueTypeOrLambda: Boolean = isValueType || this.isInstanceOf[TypeLambda]
147147

148+
/** Is this a value type or a wildcard? */
149+
final def isValueTypeOrWildcard: Boolean = isValueType || this.isInstanceOf[WildcardType]
150+
148151
/** Does this type denote a stable reference (i.e. singleton type)? */
149152
final def isStable(implicit ctx: Context): Boolean = stripTypeVar match {
150153
case tp: TermRef => tp.termSymbol.isStable && tp.prefix.isStable || tp.info.isStable
@@ -1524,11 +1527,8 @@ object Types {
15241527
/** A marker trait for types that can be types of values or prototypes of value types */
15251528
trait ValueTypeOrProto extends TermType
15261529

1527-
/** A marker trait for types that can be types of values or wildcards */
1528-
trait ValueTypeOrWildcard extends TermType
1529-
15301530
/** A marker trait for types that can be types of values or that are higher-kinded */
1531-
trait ValueType extends ValueTypeOrProto with ValueTypeOrWildcard
1531+
trait ValueType extends ValueTypeOrProto
15321532

15331533
/** A marker trait for types that are guaranteed to contain only a
15341534
* single non-null value (they might contain null in addition).
@@ -2480,8 +2480,8 @@ object Types {
24802480

24812481
object AndType {
24822482
def apply(tp1: Type, tp2: Type)(implicit ctx: Context): AndType = {
2483-
assert(tp1.isInstanceOf[ValueTypeOrWildcard] &&
2484-
tp2.isInstanceOf[ValueTypeOrWildcard], i"$tp1 & $tp2 / " + s"$tp1 & $tp2")
2483+
assert(tp1.isValueTypeOrWildcard &&
2484+
tp2.isValueTypeOrWildcard, i"$tp1 & $tp2 / " + s"$tp1 & $tp2")
24852485
unchecked(tp1, tp2)
24862486
}
24872487

@@ -2525,8 +2525,8 @@ object Types {
25252525
myBaseClasses
25262526
}
25272527

2528-
assert(tp1.isInstanceOf[ValueTypeOrWildcard] &&
2529-
tp2.isInstanceOf[ValueTypeOrWildcard], s"$tp1 $tp2")
2528+
assert(tp1.isValueTypeOrWildcard &&
2529+
tp2.isValueTypeOrWildcard, s"$tp1 $tp2")
25302530

25312531
private[this] var myJoin: Type = _
25322532
private[this] var myJoinPeriod: Period = Nowhere
@@ -3757,7 +3757,7 @@ object Types {
37573757
object TryDynamicCallType extends FlexType
37583758

37593759
/** Wildcard type, possibly with bounds */
3760-
abstract case class WildcardType(optBounds: Type) extends CachedGroundType with ValueTypeOrWildcard {
3760+
abstract case class WildcardType(optBounds: Type) extends CachedGroundType with TermType {
37613761
def derivedWildcardType(optBounds: Type)(implicit ctx: Context) =
37623762
if (optBounds eq this.optBounds) this
37633763
else if (!optBounds.exists) WildcardType

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,16 +472,18 @@ object ProtoTypes {
472472
}
473473

474474
/** Create a new TypeVar that represents a dependent method parameter singleton */
475-
def newDepTypeVar(tp: Type)(implicit ctx: Context): TypeVar =
475+
def newDepTypeVar(tp: Type)(implicit ctx: Context): TypeVar = {
476476
newTypeVar(TypeBounds.upper(AndType(tp.widenExpr, defn.SingletonClass.typeRef)))
477-
477+
}
478478
/** The result type of `mt`, where all references to parameters of `mt` are
479479
* replaced by either wildcards (if typevarsMissContext) or TypeParamRefs.
480480
*/
481481
def resultTypeApprox(mt: MethodType)(implicit ctx: Context): Type =
482482
if (mt.isResultDependent) {
483483
def replacement(tp: Type) =
484-
if (ctx.mode.is(Mode.TypevarsMissContext)) WildcardType else newDepTypeVar(tp)
484+
if (ctx.mode.is(Mode.TypevarsMissContext) ||
485+
!tp.widenExpr.isValueTypeOrWildcard) WildcardType
486+
else newDepTypeVar(tp)
485487
mt.resultType.substParams(mt, mt.paramInfos.map(replacement))
486488
}
487489
else mt.resultType

tests/neg/parser-stability-17.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
trait x0[] { x0: x0 => }
2-
class x0[x1] extends x0[x0 x0] x2 x0
1+
trait x0[] { x0: x0 => } // error // error
2+
class x0[x1] extends x0[x0 x0] x2 x0 // error // error

tests/neg/parser-stability-19.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object x0 {
2+
case class x0[] // error // error
3+
def x0( ) ] // error // error
4+
def x0 ( x0:x0 ):x0.type = x1 x0 // error // error // error

0 commit comments

Comments
 (0)