From 2bd0e2d3d276b2d21042737c9cf2d083c1cd6b77 Mon Sep 17 00:00:00 2001 From: Rocco Mathijn Andela Date: Wed, 29 Jan 2025 11:43:10 +0100 Subject: [PATCH 1/2] Warn on bad extensions of aliases (#22362) Fixes https://github.com/scala/scala3/issues/22233 --- compiler/src/dotty/tools/dotc/typer/RefChecks.scala | 2 +- tests/{pos => warn}/ext-override.scala | 2 +- tests/warn/i16743.check | 7 +++++++ tests/warn/i16743.scala | 2 +- tests/warn/i22233.scala | 1 + 5 files changed, 11 insertions(+), 3 deletions(-) rename tests/{pos => warn}/ext-override.scala (89%) create mode 100644 tests/warn/i22233.scala diff --git a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala index 87768e8a2026..bb58bb05fadf 100644 --- a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala +++ b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala @@ -1089,7 +1089,7 @@ object RefChecks { } } .exists - if !target.typeSymbol.denot.isAliasType && !target.typeSymbol.denot.isOpaqueAlias && hidden + if !target.typeSymbol.isOpaqueAlias && hidden then report.warning(ExtensionNullifiedByMember(sym, target.typeSymbol), sym.srcPos) end checkExtensionMethods diff --git a/tests/pos/ext-override.scala b/tests/warn/ext-override.scala similarity index 89% rename from tests/pos/ext-override.scala rename to tests/warn/ext-override.scala index d08439e13c9a..7c082695cbaa 100644 --- a/tests/pos/ext-override.scala +++ b/tests/warn/ext-override.scala @@ -7,6 +7,6 @@ trait Foo[T]: class Bla: def hi: String = "hi" object Bla: - given Foo[Bla] with + given Foo[Bla]: extension (x: Bla) def hi: String = x.hi diff --git a/tests/warn/i16743.check b/tests/warn/i16743.check index 6fa1f2c83357..9fdf80e71f2b 100644 --- a/tests/warn/i16743.check +++ b/tests/warn/i16743.check @@ -1,3 +1,10 @@ +-- [E194] Potential Issue Warning: tests/warn/i16743.scala:90:8 -------------------------------------------------------- +90 | def length() = 42 // warn This extension method will be shadowed by .length() on String. + | ^ + | Extension method length will never be selected from type String + | because String already has a member with the same name and compatible parameter types. + | + | longer explanation available when compiling with `-explain` -- [E194] Potential Issue Warning: tests/warn/i16743.scala:30:6 -------------------------------------------------------- 30 | def t = 27 // warn | ^ diff --git a/tests/warn/i16743.scala b/tests/warn/i16743.scala index 4c9c99cf30d0..e8860aeabaae 100644 --- a/tests/warn/i16743.scala +++ b/tests/warn/i16743.scala @@ -87,7 +87,7 @@ class Depends: object Depending: extension (using depends: Depends)(x: depends.Thing) def y = 42 - def length() = 42 // nowarn see Quote above + def length() = 42 // warn This extension method will be shadowed by .length() on String. def f(using d: Depends) = d.thing.y def g(using d: Depends) = d.thing.length() diff --git a/tests/warn/i22233.scala b/tests/warn/i22233.scala new file mode 100644 index 000000000000..08caea1c25fb --- /dev/null +++ b/tests/warn/i22233.scala @@ -0,0 +1 @@ +extension (s: String) def length = 42 // warn From 68e6ec2d1737189f056697e3711d469060546f42 Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Mon, 21 Apr 2025 13:52:55 +0200 Subject: [PATCH 2/2] Warn on bad extensions of aliases (#22362) Fixes https://github.com/scala/scala3/issues/22233 [Cherry-picked 5fe57c299b68c52f4596e707904513bffa14848f][modified]