Skip to content

Backport "Fix #9879: allow top-level opaque type definitions in REPL" to 3.3 LTS #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4377,7 +4377,7 @@ object Parsers {
* | Expr1
* |
*/
def blockStatSeq(): List[Tree] = checkNoEscapingPlaceholders {
def blockStatSeq(outermost: Boolean = false): List[Tree] = checkNoEscapingPlaceholders {
val stats = new ListBuffer[Tree]
while
var empty = false
Expand All @@ -4389,7 +4389,11 @@ object Parsers {
stats += closure(in.offset, Location.InBlock, modifiers(BitSet(IMPLICIT)))
else if isIdent(nme.extension) && followingIsExtension() then
stats += extension()
else if isDefIntro(localModifierTokens, excludedSoftModifiers = Set(nme.`opaque`)) then
else if isDefIntro(localModifierTokens,
excludedSoftModifiers =
// Allow opaque definitions at outermost level in REPL.
if outermost && ctx.mode.is(Mode.Interactive)
then Set.empty else Set(nme.`opaque`)) then
stats +++= localDef(in.offset)
else
empty = true
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/repl/ParseResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ object ParseResult {

private def parseStats(using Context): List[untpd.Tree] = {
val parser = new Parser(ctx.source)
val stats = parser.blockStatSeq()
val stats = parser.blockStatSeq(outermost = true)
parser.accept(Tokens.EOF)
stats
}
Expand Down
28 changes: 28 additions & 0 deletions compiler/test/dotty/tools/repl/ReplCompilerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,34 @@ class ReplCompilerTests extends ReplTest:
assertTrue(last, last.startsWith("val res0: tpolecat.type = null"))
assertTrue(last, last.endsWith("""// result of "res0.toString" is null"""))

@Test def `i9879`: Unit = initially:
run {
"""|opaque type A = Int; def getA: A = 0
|object Wrapper { opaque type A = Int; def getA: A = 1 }
|val x = getA
|val y = Wrapper.getA""".stripMargin
}
val expected = List(
"def getA: A",
"// defined object Wrapper",
"val x: A = 0",
"val y: Wrapper.A = 1"
)
assertEquals(expected, lines())

@Test def `i9879b`: Unit = initially:
run {
"""|def test =
| type A = Int
| opaque type B = String
| object Wrapper { opaque type C = Int }
| ()""".stripMargin
}
val all = lines()
assertEquals(6, all.length)
assertTrue(all.head.startsWith("-- [E103] Syntax Error"))
assertTrue(all.exists(_.trim().startsWith("| Illegal start of statement: this modifier is not allowed here")))

object ReplCompilerTests:

private val pattern = Pattern.compile("\\r[\\n]?|\\n");
Expand Down