Skip to content

Commit

Permalink
Make this.type nullable again (unless under -Yexplicit-nulls). (#18399
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sjrd authored Aug 14, 2023
2 parents 7d95abc + b296048 commit 3b91996
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,9 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
// of null and the value denoted by p (i.e., the value v for which v eq p). [Otherwise,] the type
// denotes the set consisting of only the value denoted by p.
!ctx.explicitNulls && isNullable(tp.underlying) && tp.isStable
case tp: ThisType =>
// Same as above; this.type is also a singleton type in spec language
!ctx.explicitNulls && isNullable(tp.underlying)
case tp: RefinedOrRecType => isNullable(tp.parent)
case tp: AppliedType => isNullable(tp.tycon)
case AndType(tp1, tp2) => isNullable(tp1) && isNullable(tp2)
Expand Down
9 changes: 9 additions & 0 deletions tests/explicit-nulls/neg/i17467.check
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,12 @@
| must be more specific than (c1 : Null)
|
| longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: tests/explicit-nulls/neg/i17467.scala:18:24 ---------------------------------------------
18 | def me: this.type = null // error
| ^^^^
| Found: Null
| Required: (Bar.this : Test.Bar)
| Note that implicit conversions were not tried because the result of an implicit conversion
| must be more specific than (Bar.this : Test.Bar)
|
| longer explanation available when compiling with `-explain`
3 changes: 3 additions & 0 deletions tests/explicit-nulls/neg/i17467.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ object Test:
val c1: Null = null
val c2: c1.type = null // error
end test

class Bar:
def me: this.type = null // error
end Test
8 changes: 4 additions & 4 deletions tests/neg/i17467.check
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@
| must be more specific than (g1 : AnyRef)
|
| longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: tests/neg/i17467.scala:33:24 ------------------------------------------------------------
33 | def me: this.type = null // error
-- [E007] Type Mismatch Error: tests/neg/i17467.scala:36:24 ------------------------------------------------------------
36 | def me: this.type = null // error
| ^^^^
| Found: Null
| Required: (Bar.this : Test.Bar)
| Required: (Baz.this : Test.Baz)
| Note that implicit conversions were not tried because the result of an implicit conversion
| must be more specific than (Bar.this : Test.Bar)
| must be more specific than (Baz.this : Test.Baz)
|
| longer explanation available when compiling with `-explain`
3 changes: 3 additions & 0 deletions tests/neg/i17467.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ object Test:
object Mod

class Bar:
def me: this.type = null

class Baz(val x: String) extends AnyVal:
def me: this.type = null // error
end Test

0 comments on commit 3b91996

Please sign in to comment.