From 4f9d7c81e4fdbfaa0b6b57e8497849bd8394abd9 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 24 Dec 2020 11:13:47 +0100 Subject: [PATCH 1/2] Avoid NPE when duplicating symbols Fixes #10542 --- .../src/dotty/tools/dotc/core/Symbols.scala | 6 ++-- tests/pos/i10542.scala | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/pos/i10542.scala diff --git a/compiler/src/dotty/tools/dotc/core/Symbols.scala b/compiler/src/dotty/tools/dotc/core/Symbols.scala index a9baa4d65802..b1939261084f 100644 --- a/compiler/src/dotty/tools/dotc/core/Symbols.scala +++ b/compiler/src/dotty/tools/dotc/core/Symbols.scala @@ -835,8 +835,10 @@ object Symbols { info = completer, privateWithin = ttmap1.mapOwner(odenot.privateWithin), // since this refers to outer symbols, need not include copies (from->to) in ownermap here. annotations = odenot.annotations) - copy.registeredCompanion = - copy.registeredCompanion.subst(originals, copies) + copy.denot match + case cd: ClassDenotation => + cd.registeredCompanion = cd.unforcedRegisteredCompanion.subst(originals, copies) + case _ => } copies.foreach(_.ensureCompleted()) // avoid memory leak diff --git a/tests/pos/i10542.scala b/tests/pos/i10542.scala new file mode 100644 index 000000000000..21e0e2a50748 --- /dev/null +++ b/tests/pos/i10542.scala @@ -0,0 +1,29 @@ +package test1: + + trait Foo { + inline def foo[A](t: => A): Unit = () + } + + object Bar extends Foo + + object Test { + Bar.foo { + sealed trait T1 + case object S1 extends T1 + } + } +/* +package test2: + + trait Foo { + inline def foo[A](t: => A): Unit = () + } + + object Bar extends Foo + + object Test { + Bar.foo { + trait T1 + val array = Array(new T1 {}) + } + }*/ \ No newline at end of file From 20a4652c5ea0198050d21a6ef1d06c8ae6be4408 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 24 Dec 2020 11:27:05 +0100 Subject: [PATCH 2/2] Properly map type constants in TreeTypeMap --- .../src/dotty/tools/dotc/ast/TreeTypeMap.scala | 4 +++- tests/pos/i10542.scala | 17 +++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/ast/TreeTypeMap.scala b/compiler/src/dotty/tools/dotc/ast/TreeTypeMap.scala index 6bbf253d3b80..a3c1484d4e62 100644 --- a/compiler/src/dotty/tools/dotc/ast/TreeTypeMap.scala +++ b/compiler/src/dotty/tools/dotc/ast/TreeTypeMap.scala @@ -4,7 +4,7 @@ package ast import core._ import Types._, Contexts._ -import Symbols._, Annotations._, Trees._, Symbols._ +import Symbols._, Annotations._, Trees._, Symbols._, Constants.Constant import Decorators._ import dotty.tools.dotc.transform.SymUtils._ import core.tasty.TreePickler.Hole @@ -124,6 +124,8 @@ class TreeTypeMap( cpy.Labeled(labeled)(bind1, expr1) case Hole(isTermHole, n, args) => Hole(isTermHole, n, args.mapConserve(transform)).withSpan(tree.span).withType(mapType(tree.tpe)) + case lit @ Literal(Constant(tpe: Type)) => + cpy.Literal(lit)(Constant(mapType(tpe))) case tree1 => super.transform(tree1) } diff --git a/tests/pos/i10542.scala b/tests/pos/i10542.scala index 21e0e2a50748..f6c759ef4769 100644 --- a/tests/pos/i10542.scala +++ b/tests/pos/i10542.scala @@ -1,4 +1,4 @@ -package test1: +package test_10542: trait Foo { inline def foo[A](t: => A): Unit = () @@ -12,8 +12,8 @@ package test1: case object S1 extends T1 } } -/* -package test2: + +package test_10540: trait Foo { inline def foo[A](t: => A): Unit = () @@ -26,4 +26,13 @@ package test2: trait T1 val array = Array(new T1 {}) } - }*/ \ No newline at end of file + } + +package test_9655: + + inline def foo[T](inline body: T): T = body + + def test = foo { + sealed trait Status + object Active extends Status + }