Skip to content

Commit 3b91996

Browse files
authored
Make this.type nullable again (unless under -Yexplicit-nulls). (#18399)
2 parents 7d95abc + b296048 commit 3b91996

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,9 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
955955
// of null and the value denoted by p (i.e., the value v for which v eq p). [Otherwise,] the type
956956
// denotes the set consisting of only the value denoted by p.
957957
!ctx.explicitNulls && isNullable(tp.underlying) && tp.isStable
958+
case tp: ThisType =>
959+
// Same as above; this.type is also a singleton type in spec language
960+
!ctx.explicitNulls && isNullable(tp.underlying)
958961
case tp: RefinedOrRecType => isNullable(tp.parent)
959962
case tp: AppliedType => isNullable(tp.tycon)
960963
case AndType(tp1, tp2) => isNullable(tp1) && isNullable(tp2)

tests/explicit-nulls/neg/i17467.check

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@
2929
| must be more specific than (c1 : Null)
3030
|
3131
| longer explanation available when compiling with `-explain`
32+
-- [E007] Type Mismatch Error: tests/explicit-nulls/neg/i17467.scala:18:24 ---------------------------------------------
33+
18 | def me: this.type = null // error
34+
| ^^^^
35+
| Found: Null
36+
| Required: (Bar.this : Test.Bar)
37+
| Note that implicit conversions were not tried because the result of an implicit conversion
38+
| must be more specific than (Bar.this : Test.Bar)
39+
|
40+
| longer explanation available when compiling with `-explain`

tests/explicit-nulls/neg/i17467.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ object Test:
1313
val c1: Null = null
1414
val c2: c1.type = null // error
1515
end test
16+
17+
class Bar:
18+
def me: this.type = null // error
1619
end Test

tests/neg/i17467.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@
5353
| must be more specific than (g1 : AnyRef)
5454
|
5555
| longer explanation available when compiling with `-explain`
56-
-- [E007] Type Mismatch Error: tests/neg/i17467.scala:33:24 ------------------------------------------------------------
57-
33 | def me: this.type = null // error
56+
-- [E007] Type Mismatch Error: tests/neg/i17467.scala:36:24 ------------------------------------------------------------
57+
36 | def me: this.type = null // error
5858
| ^^^^
5959
| Found: Null
60-
| Required: (Bar.this : Test.Bar)
60+
| Required: (Baz.this : Test.Baz)
6161
| Note that implicit conversions were not tried because the result of an implicit conversion
62-
| must be more specific than (Bar.this : Test.Bar)
62+
| must be more specific than (Baz.this : Test.Baz)
6363
|
6464
| longer explanation available when compiling with `-explain`

tests/neg/i17467.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@ object Test:
3030
object Mod
3131

3232
class Bar:
33+
def me: this.type = null
34+
35+
class Baz(val x: String) extends AnyVal:
3336
def me: this.type = null // error
3437
end Test

0 commit comments

Comments
 (0)