@@ -152,12 +152,19 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
152
152
private val fieldsMutatedInCurrentClass = new ScopedVar [mutable.Set [Name ]]
153
153
private val generatedSAMWrapperCount = new ScopedVar [VarBox [Int ]]
154
154
155
+ def currentThisTypeNullable : jstpe.Type =
156
+ encodeClassType(currentClassSym)
157
+
155
158
def currentThisType : jstpe.Type = {
156
- encodeClassType(currentClassSym) match {
159
+ currentThisTypeNullable match {
157
160
case tpe @ jstpe.ClassType (cls, _) =>
158
- jstpe.BoxedClassToPrimType .getOrElse(cls, tpe)
159
- case tpe =>
161
+ jstpe.BoxedClassToPrimType .getOrElse(cls, tpe.toNonNullable)
162
+ case tpe @ jstpe.AnyType =>
163
+ // We are in a JS class, in which even `this` is nullable
160
164
tpe
165
+ case tpe =>
166
+ throw new AssertionError (
167
+ s " Unexpected IR this type $tpe for class ${currentClassSym.get}" )
161
168
}
162
169
}
163
170
@@ -2125,7 +2132,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
2125
2132
mutableLocalVars += thisSym
2126
2133
2127
2134
val thisLocalIdent = encodeLocalSym(thisSym)
2128
- val thisLocalType = currentThisType
2135
+ val thisLocalType = currentThisTypeNullable
2129
2136
2130
2137
val genRhs = {
2131
2138
/* #3267 In default methods, scalac will type its _$this
@@ -2222,7 +2229,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
2222
2229
* @param tree
2223
2230
* The tree to adapt.
2224
2231
* @param tpe
2225
- * The target type, which must be either `AnyType` or `ClassType(_) `.
2232
+ * The target type, which must be either `AnyType` or `ClassType`.
2226
2233
*/
2227
2234
private def forceAdapt (tree : js.Tree , tpe : jstpe.Type ): js.Tree = {
2228
2235
if (tree.tpe == tpe || tpe == jstpe.AnyType ) {
@@ -2670,7 +2677,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
2670
2677
js.This ()(currentThisType)
2671
2678
} { thisLocalIdent =>
2672
2679
// .copy() to get the correct position
2673
- js.VarRef (thisLocalIdent.copy())(currentThisType )
2680
+ js.VarRef (thisLocalIdent.copy())(currentThisTypeNullable )
2674
2681
}
2675
2682
}
2676
2683
@@ -3333,7 +3340,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
3333
3340
val isTailJumpThisLocalVar = formalArgSym.name == nme.THIS
3334
3341
3335
3342
val tpe =
3336
- if (isTailJumpThisLocalVar) currentThisType
3343
+ if (isTailJumpThisLocalVar) currentThisTypeNullable
3337
3344
else toIRType(formalArgSym.tpe)
3338
3345
3339
3346
val fixedActualArg =
@@ -3561,7 +3568,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
3561
3568
// The Scala type system prevents x.isInstanceOf[Null] and ...[Nothing]
3562
3569
assert(sym != NullClass && sym != NothingClass ,
3563
3570
s " Found a .isInstanceOf[ $sym] at $pos" )
3564
- js.IsInstanceOf (value, toIRType(to))
3571
+ js.IsInstanceOf (value, toIRType(to).toNonNullable )
3565
3572
}
3566
3573
}
3567
3574
@@ -6334,7 +6341,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
6334
6341
}
6335
6342
val className = encodeClassName(currentClassSym).withSuffix(suffix)
6336
6343
6337
- val classType = jstpe.ClassType (className)
6344
+ val thisType = jstpe.ClassType (className, nullable = false )
6338
6345
6339
6346
// val f: Any
6340
6347
val fFieldIdent = js.FieldIdent (FieldName (className, SimpleFieldName (" f" )))
@@ -6353,10 +6360,10 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
6353
6360
jstpe.NoType ,
6354
6361
Some (js.Block (List (
6355
6362
js.Assign (
6356
- js.Select (js.This ()(classType ), fFieldIdent)(jstpe.AnyType ),
6363
+ js.Select (js.This ()(thisType ), fFieldIdent)(jstpe.AnyType ),
6357
6364
fParamDef.ref),
6358
6365
js.ApplyStatically (js.ApplyFlags .empty.withConstructor(true ),
6359
- js.This ()(classType ),
6366
+ js.This ()(thisType ),
6360
6367
ir.Names .ObjectClass ,
6361
6368
js.MethodIdent (ir.Names .NoArgConstructorName ),
6362
6369
Nil )(jstpe.NoType )))))(
@@ -6404,7 +6411,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
6404
6411
}.map((ensureBoxed _).tupled)
6405
6412
6406
6413
val call = js.JSFunctionApply (
6407
- js.Select (js.This ()(classType ), fFieldIdent)(jstpe.AnyType ),
6414
+ js.Select (js.This ()(thisType ), fFieldIdent)(jstpe.AnyType ),
6408
6415
actualParams)
6409
6416
6410
6417
val body = fromAny(call, enteringPhase(currentRun.posterasurePhase) {
0 commit comments