From 989ff1a08644b3163061d4a020561c68b98327a4 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 2 Aug 2023 11:33:25 +0100 Subject: [PATCH 1/2] Disallow overloading from breaking stable patterns [Cherry-picked 85d656a81ad0fd3b0ea51f68b5d5a3059baad70e] --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 4 +++- tests/pos/i18247.scala | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i18247.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 237f48d7bd96..9647bdf40978 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -668,7 +668,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer if ctx.mode.is(Mode.Pattern) && !tree.isType && !pt.isInstanceOf[ApplyingProto] - && !tree.tpe.isStable + && !tree.tpe.match + case tp: NamedType => tp.denot.hasAltWith(_.symbol.namedType.isStable) + case tp => tp.isStable && !isWildcardArg(tree) then report.error(StableIdentPattern(tree, pt), tree.srcPos) diff --git a/tests/pos/i18247.scala b/tests/pos/i18247.scala new file mode 100644 index 000000000000..b3805f5a7d02 --- /dev/null +++ b/tests/pos/i18247.scala @@ -0,0 +1,10 @@ +sealed trait Op +object Op { + case object `==` extends Op +} + +def t1(a: Op): true = { + a match { + case Op.`==` => true // was: won't compile + } +} From c43d3877501c458c79fbe20dbed47631b67ff593 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 2 Aug 2023 16:33:16 +0100 Subject: [PATCH 2/2] Refine how we check for stability In particular, must continue to use the type's prefix, rather than the denotation's prefix. That way, e.g. in tests/neg/i3812b.scala, referencing value class'\''s value, will fail on the non-stable value class prefix, rather than the denotation'\''s prefix, which is NoSymbol. [Cherry-picked e6012a0801c9ce864232249c7ce2d7acd66596f5] --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 9647bdf40978..34940d2f5277 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -669,7 +669,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer && !tree.isType && !pt.isInstanceOf[ApplyingProto] && !tree.tpe.match - case tp: NamedType => tp.denot.hasAltWith(_.symbol.namedType.isStable) + case tp: NamedType => tp.denot.hasAltWith(_.symbol.isStableMember && tp.prefix.isStable || tp.info.isStable) case tp => tp.isStable && !isWildcardArg(tree) then