Skip to content

Commit 2bd0e2d

Browse files
RoccoMathijntgodzik
authored andcommitted
Warn on bad extensions of aliases (scala#22362)
Fixes scala#22233
1 parent 16aaa27 commit 2bd0e2d

File tree

5 files changed

+11
-3
lines changed

5 files changed

+11
-3
lines changed

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ object RefChecks {
10891089
}
10901090
}
10911091
.exists
1092-
if !target.typeSymbol.denot.isAliasType && !target.typeSymbol.denot.isOpaqueAlias && hidden
1092+
if !target.typeSymbol.isOpaqueAlias && hidden
10931093
then report.warning(ExtensionNullifiedByMember(sym, target.typeSymbol), sym.srcPos)
10941094
end checkExtensionMethods
10951095

tests/pos/ext-override.scala renamed to tests/warn/ext-override.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ trait Foo[T]:
77
class Bla:
88
def hi: String = "hi"
99
object Bla:
10-
given Foo[Bla] with
10+
given Foo[Bla]:
1111
extension (x: Bla)
1212
def hi: String = x.hi

tests/warn/i16743.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
-- [E194] Potential Issue Warning: tests/warn/i16743.scala:90:8 --------------------------------------------------------
2+
90 | def length() = 42 // warn This extension method will be shadowed by .length() on String.
3+
| ^
4+
| Extension method length will never be selected from type String
5+
| because String already has a member with the same name and compatible parameter types.
6+
|
7+
| longer explanation available when compiling with `-explain`
18
-- [E194] Potential Issue Warning: tests/warn/i16743.scala:30:6 --------------------------------------------------------
29
30 | def t = 27 // warn
310
| ^

tests/warn/i16743.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Depends:
8787
object Depending:
8888
extension (using depends: Depends)(x: depends.Thing)
8989
def y = 42
90-
def length() = 42 // nowarn see Quote above
90+
def length() = 42 // warn This extension method will be shadowed by .length() on String.
9191
def f(using d: Depends) = d.thing.y
9292
def g(using d: Depends) = d.thing.length()
9393

tests/warn/i22233.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extension (s: String) def length = 42 // warn

0 commit comments

Comments
 (0)