From 6db1423d8783e745b60b80b3b4ef54668b9d6318 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 21 Feb 2024 17:51:37 +0000 Subject: [PATCH 1/6] Test case for REPL bad symbolic reference --- .../test/dotty/tools/repl/ReplCompilerTests.scala | 14 ++++++++++++++ compiler/test/dotty/tools/repl/ReplTest.scala | 4 ++++ .../test/dotty/tools/repl/TabcompleteTests.scala | 4 ---- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala index 26092b73f107..8c29c8e03b33 100644 --- a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala +++ b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala @@ -409,6 +409,20 @@ class ReplCompilerTests extends ReplTest: @Test def `i13097 expect template after colon` = contextually: assert(ParseResult.isIncomplete("class C:")) + @Test def i15562: Unit = initially { + val s1 = run("List(1, 2).filter(_ % 2 == 0).foreach(println)") + assertEquals("2", storedOutput().trim) + s1 + } andThen { s1 ?=> + val comp = tabComplete("List(1, 2).filter(_ % 2 == 0).fore") + assertEquals(List("foreach"), comp.distinct) + s1 + } andThen { + val s2 = run("List(1, 2).filter(_ % 2 == 0).foreach(println)") + assertEquals("2", storedOutput().trim) + s2 + } + object ReplCompilerTests: private val pattern = Pattern.compile("\\r[\\n]?|\\n"); diff --git a/compiler/test/dotty/tools/repl/ReplTest.scala b/compiler/test/dotty/tools/repl/ReplTest.scala index ae0d0c666a9c..bc70f68e45fe 100644 --- a/compiler/test/dotty/tools/repl/ReplTest.scala +++ b/compiler/test/dotty/tools/repl/ReplTest.scala @@ -40,6 +40,10 @@ extends ReplDriver(options, new PrintStream(out, true, StandardCharsets.UTF_8.na def contextually[A](op: Context ?=> A): A = op(using initialState.context) + /** Returns the `(, )`*/ + def tabComplete(src: String)(implicit state: State): List[String] = + completions(src.length, src, state).map(_.value).sorted + extension [A](state: State) infix def andThen(op: State ?=> A): A = op(using state) diff --git a/compiler/test/dotty/tools/repl/TabcompleteTests.scala b/compiler/test/dotty/tools/repl/TabcompleteTests.scala index 0bce525e1469..e4c3a2557e7d 100644 --- a/compiler/test/dotty/tools/repl/TabcompleteTests.scala +++ b/compiler/test/dotty/tools/repl/TabcompleteTests.scala @@ -8,10 +8,6 @@ import org.junit.Test /** These tests test input that has proved problematic */ class TabcompleteTests extends ReplTest { - /** Returns the `(, )`*/ - private def tabComplete(src: String)(implicit state: State): List[String] = - completions(src.length, src, state).map(_.value).sorted - @Test def tabCompleteList = initially { val comp = tabComplete("List.r") assertEquals(List("range"), comp.distinct) From c2c78d7312ba882a1c7be3c1369e753282ed7bd9 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 21 Feb 2024 22:08:06 +0000 Subject: [PATCH 2/6] Add a test case which still fails --- .../test/dotty/tools/repl/ReplCompilerTests.scala | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala index 8c29c8e03b33..cfae36f394af 100644 --- a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala +++ b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala @@ -423,6 +423,20 @@ class ReplCompilerTests extends ReplTest: s2 } + @Test def i15562b: Unit = initially { + val s1 = run("List(1, 2).filter(_ % 2 == 0).foreach(println)") + assertEquals("2", storedOutput().trim) + s1 + } andThen { s1 ?=> + val comp = tabComplete("val x = false + true; List(1, 2).filter(_ % 2 == 0).fore") + assertEquals(List("foreach"), comp.distinct) + s1 + } andThen { + val s2 = run("List(1, 2).filter(_ % 2 == 0).foreach(println)") + assertEquals("2", storedOutput().trim) + s2 + } + object ReplCompilerTests: private val pattern = Pattern.compile("\\r[\\n]?|\\n"); From 4615a9c4ef576405ab61680a3f2ba9a11c0b33fc Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Fri, 23 Feb 2024 17:22:37 +0000 Subject: [PATCH 3/6] Avoid losing the symbols denotation on update --- compiler/src/dotty/tools/dotc/core/Symbols.scala | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/Symbols.scala b/compiler/src/dotty/tools/dotc/core/Symbols.scala index 32a2da8b46b6..619ec92ae3e7 100644 --- a/compiler/src/dotty/tools/dotc/core/Symbols.scala +++ b/compiler/src/dotty/tools/dotc/core/Symbols.scala @@ -112,16 +112,23 @@ object Symbols extends SymUtils { private def computeDenot(lastd: SymDenotation)(using Context): SymDenotation = { util.Stats.record("Symbol.computeDenot") val now = ctx.period + val prev = checkedPeriod checkedPeriod = now - if (lastd.validFor contains now) lastd else recomputeDenot(lastd) + if lastd.validFor.contains(now) then + lastd + else + val newd = recomputeDenot(lastd) + if newd.exists then + lastDenot = newd + else + checkedPeriod = prev + newd } /** Overridden in NoSymbol */ protected def recomputeDenot(lastd: SymDenotation)(using Context): SymDenotation = { util.Stats.record("Symbol.recomputeDenot") - val newd = lastd.current.asInstanceOf[SymDenotation] - lastDenot = newd - newd + lastd.current.asSymDenotation } /** The original denotation of this symbol, without forcing anything */ From e79b2e99800d2d06696e04192a63ce0e6f22bff8 Mon Sep 17 00:00:00 2001 From: odersky Date: Mon, 26 Feb 2024 13:59:52 +0100 Subject: [PATCH 4/6] Some tweaks to denotation updates - Be more specific when we go into the special case of not updating checkedPeriod - Always update lastDenotation. - Keep computeDenot small, move work to recomputeDenot --- .../src/dotty/tools/dotc/core/Symbols.scala | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/Symbols.scala b/compiler/src/dotty/tools/dotc/core/Symbols.scala index 619ec92ae3e7..1cd4d5f86d05 100644 --- a/compiler/src/dotty/tools/dotc/core/Symbols.scala +++ b/compiler/src/dotty/tools/dotc/core/Symbols.scala @@ -110,25 +110,28 @@ object Symbols extends SymUtils { } private def computeDenot(lastd: SymDenotation)(using Context): SymDenotation = { + // Written that way do that it comes in at 32 bytes and is therefore inlineable for + // the JIT (reputedly, cutoff is at 35 bytes) util.Stats.record("Symbol.computeDenot") val now = ctx.period - val prev = checkedPeriod checkedPeriod = now - if lastd.validFor.contains(now) then - lastd - else - val newd = recomputeDenot(lastd) - if newd.exists then - lastDenot = newd - else - checkedPeriod = prev - newd + if lastd.validFor.contains(now) then lastd else recomputeDenot(lastd) } /** Overridden in NoSymbol */ protected def recomputeDenot(lastd: SymDenotation)(using Context): SymDenotation = { util.Stats.record("Symbol.recomputeDenot") - lastd.current.asSymDenotation + val newd = lastd.current.asInstanceOf[SymDenotation] + lastDenot = newd + if !newd.exists && lastd.initial.validFor.firstPhaseId > ctx.phaseId then + // We are trying to bring forward a symbol that is defined only at a later phase + // (typically, a nested Java class, invisible before erasure). + // In that case, keep the checked period to the previous validity, which + // means we will try another bring forward when the symbol is referenced + // at a later phase. Otherwise we'd get stuck on NoDenotation here. + // See #15562 and test i15562b in ReplCompilerTests + checkedPeriod = lastd.initial.validFor + newd } /** The original denotation of this symbol, without forcing anything */ @@ -798,7 +801,7 @@ object Symbols extends SymUtils { cls: ClassSymbol, name: TermName = nme.WILDCARD, selfInfo: Type = NoType)(using Context): TermSymbol = - newSymbol(cls, name, SelfSymFlags, selfInfo orElse cls.classInfo.selfType, coord = cls.coord) + newSymbol(cls, name, SelfSymFlags, selfInfo.orElse(cls.classInfo.selfType), coord = cls.coord) /** Create new type parameters with given owner, names, and flags. * @param boundsFn A function that, given type refs to the newly created @@ -965,7 +968,7 @@ object Symbols extends SymUtils { */ def getPackageClassIfDefined(path: PreName)(using Context): Symbol = staticRef(path.toTypeName, isPackage = true, generateStubs = false) - .disambiguate(_ is PackageClass).symbol + .disambiguate(_.is(PackageClass)).symbol def requiredModule(path: PreName)(using Context): TermSymbol = { val name = path.toTermName From 5631d76f247c3d02e21eeab135b7917f30f21394 Mon Sep 17 00:00:00 2001 From: odersky Date: Mon, 26 Feb 2024 15:58:30 +0100 Subject: [PATCH 5/6] Avoid setting lastDenot to NoDenotation in the forward reference scenario This brings back an element of the original solution. --- compiler/src/dotty/tools/dotc/core/Symbols.scala | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/Symbols.scala b/compiler/src/dotty/tools/dotc/core/Symbols.scala index 1cd4d5f86d05..54b97fe1664e 100644 --- a/compiler/src/dotty/tools/dotc/core/Symbols.scala +++ b/compiler/src/dotty/tools/dotc/core/Symbols.scala @@ -122,15 +122,16 @@ object Symbols extends SymUtils { protected def recomputeDenot(lastd: SymDenotation)(using Context): SymDenotation = { util.Stats.record("Symbol.recomputeDenot") val newd = lastd.current.asInstanceOf[SymDenotation] - lastDenot = newd - if !newd.exists && lastd.initial.validFor.firstPhaseId > ctx.phaseId then + if newd.exists || lastd.initial.validFor.firstPhaseId <= ctx.phaseId then + lastDenot = newd + else // We are trying to bring forward a symbol that is defined only at a later phase // (typically, a nested Java class, invisible before erasure). - // In that case, keep the checked period to the previous validity, which - // means we will try another bring forward when the symbol is referenced - // at a later phase. Otherwise we'd get stuck on NoDenotation here. + // In that case, keep lastDenot as it was and set the checked period to lastDenot's + // previous validity, which means we will try another bring forward when the symbol + // is referenced at a later phase. Otherwise we'd get stuck on NoDenotation here. // See #15562 and test i15562b in ReplCompilerTests - checkedPeriod = lastd.initial.validFor + checkedPeriod = lastd.validFor newd } From 46c50e7ba25554f2ba7f9a422fb9109d5387348e Mon Sep 17 00:00:00 2001 From: odersky Date: Mon, 26 Feb 2024 18:07:42 +0100 Subject: [PATCH 6/6] Update compiler/src/dotty/tools/dotc/core/Symbols.scala --- compiler/src/dotty/tools/dotc/core/Symbols.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/core/Symbols.scala b/compiler/src/dotty/tools/dotc/core/Symbols.scala index 54b97fe1664e..78c736649605 100644 --- a/compiler/src/dotty/tools/dotc/core/Symbols.scala +++ b/compiler/src/dotty/tools/dotc/core/Symbols.scala @@ -110,7 +110,7 @@ object Symbols extends SymUtils { } private def computeDenot(lastd: SymDenotation)(using Context): SymDenotation = { - // Written that way do that it comes in at 32 bytes and is therefore inlineable for + // Written that way so that it comes in at 32 bytes and is therefore inlineable for // the JIT (reputedly, cutoff is at 35 bytes) util.Stats.record("Symbol.computeDenot") val now = ctx.period