From 991b02056d87bd75691f4cc910bbbb8365fee148 Mon Sep 17 00:00:00 2001 From: odersky Date: Thu, 7 Apr 2022 22:22:56 +0200 Subject: [PATCH 1/2] Don't try conversions or extensions on Java companion objects Fixes #12537 --- compiler/src/dotty/tools/dotc/transform/SymUtils.scala | 2 ++ compiler/src/dotty/tools/dotc/typer/Checking.scala | 2 +- compiler/src/dotty/tools/dotc/typer/Typer.scala | 2 ++ tests/neg/i12537.scala | 2 ++ 4 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 tests/neg/i12537.scala diff --git a/compiler/src/dotty/tools/dotc/transform/SymUtils.scala b/compiler/src/dotty/tools/dotc/transform/SymUtils.scala index c7f9c762e440..7694a7cc240a 100644 --- a/compiler/src/dotty/tools/dotc/transform/SymUtils.scala +++ b/compiler/src/dotty/tools/dotc/transform/SymUtils.scala @@ -64,6 +64,8 @@ object SymUtils: def isSuperAccessor(using Context): Boolean = self.name.is(SuperAccessorName) + def isNoValue(using Context): Boolean = self.is(Package) || self.isAllOf(JavaModule) + /** Is this a type or term parameter or a term parameter accessor? */ def isParamOrAccessor(using Context): Boolean = self.is(Param) || self.is(ParamAccessor) diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index 0525ce805d2e..6e8fa8b9c9d8 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -713,7 +713,7 @@ object Checking { def checkValue(tree: Tree)(using Context): Unit = val sym = tree.tpe.termSymbol - if sym.is(Flags.Package) || sym.isAllOf(Flags.JavaModule) && !ctx.isJava then + if sym.isNoValue && !ctx.isJava then report.error(JavaSymbolIsNotAValue(sym), tree.srcPos) def checkValue(tree: Tree, proto: Type)(using Context): tree.type = diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index a85b2574e59c..ac8d6152812e 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -3245,6 +3245,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer rememberSearchFailure(qual, SearchFailure(qual.withType(NestedFailure(ex.toMessage, selectionProto)))) + if qual.symbol.isNoValue then return EmptyTree + // try an extension method in scope try val nestedCtx = ctx.fresh.setNewTyperState() diff --git a/tests/neg/i12537.scala b/tests/neg/i12537.scala new file mode 100644 index 000000000000..f6c6347bf47e --- /dev/null +++ b/tests/neg/i12537.scala @@ -0,0 +1,2 @@ +extension [T](x: T) def ext: T = x +def foo = String.ext // error From 06ab7948a29bd9b32f597d4b8952de4d15aaa7e0 Mon Sep 17 00:00:00 2001 From: odersky Date: Thu, 7 Apr 2022 23:20:44 +0200 Subject: [PATCH 2/2] Update fuzz test --- tests/neg/i7750.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/neg/i7750.scala b/tests/neg/i7750.scala index 6b29a6e68640..bdbe3b8f33bc 100644 --- a/tests/neg/i7750.scala +++ b/tests/neg/i7750.scala @@ -1,3 +1,3 @@ -package object A extends runtime.A { +package object A extends runtime.A { // error implicit def a( : ) = 1() // error // error // error } \ No newline at end of file