From a94c8e1874e283f73208a222bb17a8dbf6182d04 Mon Sep 17 00:00:00 2001 From: rmathijn Date: Mon, 13 Jan 2025 19:09:37 +0100 Subject: [PATCH 1/4] Warn on bad extensions of effectively final aliases --- compiler/src/dotty/tools/dotc/typer/RefChecks.scala | 3 ++- tests/warn/i16743.check | 7 +++++++ tests/warn/i16743.scala | 2 +- tests/warn/i22233.check | 7 +++++++ tests/warn/i22233.scala | 1 + 5 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 tests/warn/i22233.check 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 dcdabaf3a72d..3e8a84f58480 100644 --- a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala +++ b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala @@ -1190,7 +1190,8 @@ object RefChecks { } } .exists - if !target.typeSymbol.denot.isAliasType && !target.typeSymbol.denot.isOpaqueAlias && hidden + if !target.typeSymbol.denot.isOpaqueAlias // Opaque types hide original members so extension is not nullified by the original member + && hidden then report.warning(ExtensionNullifiedByMember(sym, target.typeSymbol), sym.srcPos) end checkExtensionMethods 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.check b/tests/warn/i22233.check new file mode 100644 index 000000000000..2771101e1ae2 --- /dev/null +++ b/tests/warn/i22233.check @@ -0,0 +1,7 @@ +-- [E194] Potential Issue Warning: tests/warn/i22233.scala:1:26 -------------------------------------------------------- +1 |extension (s: String) def length = 42 // warn + | ^ + | 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` 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 527ef94b761de5a356200427dd4402d533b99627 Mon Sep 17 00:00:00 2001 From: rmathijn Date: Mon, 13 Jan 2025 19:21:33 +0100 Subject: [PATCH 2/4] Signed CLA From 2685ba99f98c9bd4f991a339131c5ce826f9611b Mon Sep 17 00:00:00 2001 From: rmathijn Date: Tue, 28 Jan 2025 21:04:52 +0100 Subject: [PATCH 3/4] pr remarks --- compiler/src/dotty/tools/dotc/typer/RefChecks.scala | 3 +-- tests/warn/i22233.check | 7 ------- tests/{pos => warns}/ext-override.scala | 2 ++ 3 files changed, 3 insertions(+), 9 deletions(-) delete mode 100644 tests/warn/i22233.check rename tests/{pos => warns}/ext-override.scala (93%) diff --git a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala index 3e8a84f58480..5c6e7e20ccd1 100644 --- a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala +++ b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala @@ -1190,8 +1190,7 @@ object RefChecks { } } .exists - if !target.typeSymbol.denot.isOpaqueAlias // Opaque types hide original members so extension is not nullified by the original member - && hidden + if !target.typeSymbol.isOpaqueAlias && hidden then report.warning(ExtensionNullifiedByMember(sym, target.typeSymbol), sym.srcPos) end checkExtensionMethods diff --git a/tests/warn/i22233.check b/tests/warn/i22233.check deleted file mode 100644 index 2771101e1ae2..000000000000 --- a/tests/warn/i22233.check +++ /dev/null @@ -1,7 +0,0 @@ --- [E194] Potential Issue Warning: tests/warn/i22233.scala:1:26 -------------------------------------------------------- -1 |extension (s: String) def length = 42 // warn - | ^ - | 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` diff --git a/tests/pos/ext-override.scala b/tests/warns/ext-override.scala similarity index 93% rename from tests/pos/ext-override.scala rename to tests/warns/ext-override.scala index 7c082695cbaa..df573fbe7662 100644 --- a/tests/pos/ext-override.scala +++ b/tests/warns/ext-override.scala @@ -1,3 +1,5 @@ +package warns + //> using options -Xfatal-warnings trait Foo[T]: From 804778479153a36ccdf1bea95128535de7743eae Mon Sep 17 00:00:00 2001 From: rmathijn Date: Wed, 29 Jan 2025 09:52:41 +0100 Subject: [PATCH 4/4] pr remarks --- tests/{warns => warn}/ext-override.scala | 2 -- 1 file changed, 2 deletions(-) rename tests/{warns => warn}/ext-override.scala (93%) diff --git a/tests/warns/ext-override.scala b/tests/warn/ext-override.scala similarity index 93% rename from tests/warns/ext-override.scala rename to tests/warn/ext-override.scala index df573fbe7662..7c082695cbaa 100644 --- a/tests/warns/ext-override.scala +++ b/tests/warn/ext-override.scala @@ -1,5 +1,3 @@ -package warns - //> using options -Xfatal-warnings trait Foo[T]: