From 8544c301609d528263e5cd0867d98e8c62565792 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 6 Jan 2021 15:51:51 +0100 Subject: [PATCH] Avoid NoDenotation.owner in some situations Fixes #9328 --- .../tools/dotc/core/SymDenotations.scala | 4 ++-- .../dotty/tools/dotc/typer/Implicits.scala | 17 ++++++++------- tests/neg/i9328.scala | 21 +++++++++++++++++++ 3 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 tests/neg/i9328.scala diff --git a/compiler/src/dotty/tools/dotc/core/SymDenotations.scala b/compiler/src/dotty/tools/dotc/core/SymDenotations.scala index 05eb9c435fb7..cad0d282bc16 100644 --- a/compiler/src/dotty/tools/dotc/core/SymDenotations.scala +++ b/compiler/src/dotty/tools/dotc/core/SymDenotations.scala @@ -1393,10 +1393,10 @@ object SymDenotations { def thisType(using Context): Type = NoPrefix def typeRef(using Context): TypeRef = - TypeRef(owner.thisType, symbol) + TypeRef(maybeOwner.thisType, symbol) def termRef(using Context): TermRef = - TermRef(owner.thisType, symbol) + TermRef(maybeOwner.thisType, symbol) /** The typeRef applied to its own type parameters */ def appliedRef(using Context): Type = diff --git a/compiler/src/dotty/tools/dotc/typer/Implicits.scala b/compiler/src/dotty/tools/dotc/typer/Implicits.scala index 761d550afc96..063d92221166 100644 --- a/compiler/src/dotty/tools/dotc/typer/Implicits.scala +++ b/compiler/src/dotty/tools/dotc/typer/Implicits.scala @@ -1657,14 +1657,15 @@ sealed class TermRefSet(using Context): def += (ref: TermRef): Unit = val pre = ref.prefix - val sym = ref.symbol.asTerm - elems.get(sym) match - case null => - elems.put(sym, pre) - case prefix: Type => - if !(prefix =:= pre) then elems.put(sym, pre :: prefix :: Nil) - case prefixes: List[Type] => - if !prefixes.exists(_ =:= pre) then elems.put(sym, pre :: prefixes) + if ref.symbol.exists then + val sym = ref.symbol.asTerm + elems.get(sym) match + case null => + elems.put(sym, pre) + case prefix: Type => + if !(prefix =:= pre) then elems.put(sym, pre :: prefix :: Nil) + case prefixes: List[Type] => + if !prefixes.exists(_ =:= pre) then elems.put(sym, pre :: prefixes) def ++= (that: TermRefSet): Unit = if !that.isEmpty then that.foreach(+=) diff --git a/tests/neg/i9328.scala b/tests/neg/i9328.scala new file mode 100644 index 000000000000..c4b5e0cf19e2 --- /dev/null +++ b/tests/neg/i9328.scala @@ -0,0 +1,21 @@ +// fuzz test to avoid NoSymbol.owner +type Id[T] = T match { + case _ => T +} + +class Foo2[T <: Id[T]] // error // error + +object Foo { // error + object Foo { } + Foo { } +} +implicit class Foo(a: Float) // error +case class Foo() + +case class Bar( +} { ; // error +object Bar { // error + class Foo(a: Int) extends AnyVal + Foo() +} +type Bar // error \ No newline at end of file