Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make this.type nullable again (unless under -Yexplicit-nulls). #18399

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading