@@ -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
@@ -2124,8 +2131,13 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
2124
2131
if (thisSym.isMutable)
2125
2132
mutableLocalVars += thisSym
2126
2133
2134
+ /* The `thisLocalIdent` must be nullable. Even though we initially
2135
+ * assign it to `this`, which is non-nullable, tail-recursive calls
2136
+ * may reassign it to a different value, which in general will be
2137
+ * nullable.
2138
+ */
2127
2139
val thisLocalIdent = encodeLocalSym(thisSym)
2128
- val thisLocalType = currentThisType
2140
+ val thisLocalType = currentThisTypeNullable
2129
2141
2130
2142
val genRhs = {
2131
2143
/* #3267 In default methods, scalac will type its _$this
@@ -2222,7 +2234,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
2222
2234
* @param tree
2223
2235
* The tree to adapt.
2224
2236
* @param tpe
2225
- * The target type, which must be either `AnyType` or `ClassType(_) `.
2237
+ * The target type, which must be either `AnyType` or `ClassType`.
2226
2238
*/
2227
2239
private def forceAdapt (tree : js.Tree , tpe : jstpe.Type ): js.Tree = {
2228
2240
if (tree.tpe == tpe || tpe == jstpe.AnyType ) {
@@ -2670,7 +2682,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
2670
2682
js.This ()(currentThisType)
2671
2683
} { thisLocalIdent =>
2672
2684
// .copy() to get the correct position
2673
- js.VarRef (thisLocalIdent.copy())(currentThisType )
2685
+ js.VarRef (thisLocalIdent.copy())(currentThisTypeNullable )
2674
2686
}
2675
2687
}
2676
2688
@@ -3333,7 +3345,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
3333
3345
val isTailJumpThisLocalVar = formalArgSym.name == nme.THIS
3334
3346
3335
3347
val tpe =
3336
- if (isTailJumpThisLocalVar) currentThisType
3348
+ if (isTailJumpThisLocalVar) currentThisTypeNullable
3337
3349
else toIRType(formalArgSym.tpe)
3338
3350
3339
3351
val fixedActualArg =
@@ -3561,7 +3573,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
3561
3573
// The Scala type system prevents x.isInstanceOf[Null] and ...[Nothing]
3562
3574
assert(sym != NullClass && sym != NothingClass ,
3563
3575
s " Found a .isInstanceOf[ $sym] at $pos" )
3564
- js.IsInstanceOf (value, toIRType(to))
3576
+ js.IsInstanceOf (value, toIRType(to).toNonNullable )
3565
3577
}
3566
3578
}
3567
3579
@@ -6334,7 +6346,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
6334
6346
}
6335
6347
val className = encodeClassName(currentClassSym).withSuffix(suffix)
6336
6348
6337
- val classType = jstpe.ClassType (className, nullable = true )
6349
+ val thisType = jstpe.ClassType (className, nullable = false )
6338
6350
6339
6351
// val f: Any
6340
6352
val fFieldIdent = js.FieldIdent (FieldName (className, SimpleFieldName (" f" )))
@@ -6353,10 +6365,10 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
6353
6365
jstpe.NoType ,
6354
6366
Some (js.Block (List (
6355
6367
js.Assign (
6356
- js.Select (js.This ()(classType ), fFieldIdent)(jstpe.AnyType ),
6368
+ js.Select (js.This ()(thisType ), fFieldIdent)(jstpe.AnyType ),
6357
6369
fParamDef.ref),
6358
6370
js.ApplyStatically (js.ApplyFlags .empty.withConstructor(true ),
6359
- js.This ()(classType ),
6371
+ js.This ()(thisType ),
6360
6372
ir.Names .ObjectClass ,
6361
6373
js.MethodIdent (ir.Names .NoArgConstructorName ),
6362
6374
Nil )(jstpe.NoType )))))(
@@ -6404,7 +6416,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
6404
6416
}.map((ensureBoxed _).tupled)
6405
6417
6406
6418
val call = js.JSFunctionApply (
6407
- js.Select (js.This ()(classType ), fFieldIdent)(jstpe.AnyType ),
6419
+ js.Select (js.This ()(thisType ), fFieldIdent)(jstpe.AnyType ),
6408
6420
actualParams)
6409
6421
6410
6422
val body = fromAny(call, enteringPhase(currentRun.posterasurePhase) {
0 commit comments