diff --git a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala index e763f6c7a303..de40ac7232b7 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala @@ -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) diff --git a/tests/explicit-nulls/neg/i17467.check b/tests/explicit-nulls/neg/i17467.check index 3b5e556ba36d..2dcf20952e7c 100644 --- a/tests/explicit-nulls/neg/i17467.check +++ b/tests/explicit-nulls/neg/i17467.check @@ -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` diff --git a/tests/explicit-nulls/neg/i17467.scala b/tests/explicit-nulls/neg/i17467.scala index 2e96252f6759..3b66afcc3606 100644 --- a/tests/explicit-nulls/neg/i17467.scala +++ b/tests/explicit-nulls/neg/i17467.scala @@ -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 diff --git a/tests/neg/i17467.check b/tests/neg/i17467.check index 26ea170d7af5..a274a519f69a 100644 --- a/tests/neg/i17467.check +++ b/tests/neg/i17467.check @@ -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` diff --git a/tests/neg/i17467.scala b/tests/neg/i17467.scala index f527b6981b41..f8023e74742f 100644 --- a/tests/neg/i17467.scala +++ b/tests/neg/i17467.scala @@ -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