Skip to content

REPL - fix collective extensions #13375

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

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 12 additions & 8 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3647,12 +3647,12 @@ object Parsers {
in.nextToken()
val methods =
if isDefIntro(modifierTokens) then
extMethod(nparams) :: Nil
extMethodOpt(nparams).toList
else
in.observeIndented()
newLineOptWhenFollowedBy(LBRACE)
if in.isNestedStart then inDefScopeBraces(extMethods(nparams))
else { syntaxError("Extension without extension methods"); Nil }
else { syntaxErrorOrIncomplete("Extension without extension methods"); Nil }
val result = atSpan(start)(ExtMethods(joinParams(tparams, leadParamss.toList), methods))
val comment = in.getDocComment(start)
if comment.isDefined then
Expand All @@ -3663,21 +3663,25 @@ object Parsers {

/** ExtMethod ::= {Annotation [nl]} {Modifier} ‘def’ DefDef
*/
def extMethod(numLeadParams: Int): DefDef =
def extMethodOpt(numLeadParams: Int): Option[DefDef] =
val start = in.offset
val mods = defAnnotsMods(modifierTokens)
accept(DEF)
defDefOrDcl(start, mods, numLeadParams)

if in.token == DEF then
accept(DEF)
Some(defDefOrDcl(start, mods, numLeadParams))
else
None

/** ExtMethods ::= ExtMethod | [nl] ‘{’ ExtMethod {semi ExtMethod ‘}’
*/
def extMethods(numLeadParams: Int): List[DefDef] = checkNoEscapingPlaceholders {
val meths = new ListBuffer[DefDef]
while
meths += extMethod(numLeadParams)
val m = extMethodOpt(numLeadParams)
m.foreach(meths += _)
statSepOrEnd(meths, what = "extension method")
do ()
if meths.isEmpty then syntaxError("`def` expected")
if meths.isEmpty then syntaxErrorOrIncomplete("`def` expected")
meths.toList
}

Expand Down
6 changes: 6 additions & 0 deletions compiler/test/dotty/tools/repl/ReplCompilerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ class ReplCompilerTests extends ReplTest {
assertFalse(ParseResult.isIncomplete("_ + 1")) // was: assertThrows[NullPointerException]
}

@Test def i9374: Unit = fromInitialState { state =>
given Context = state.context
assert(ParseResult.isIncomplete("extension (x: String)"))
assert(ParseResult.isIncomplete("extension (x: String) {"))
}

@Test def testSingletonPrint = fromInitialState { implicit state =>
run("""val a = "hello"; val x: a.type = a""")
assertMultiLineEquals("val a: String = hello\nval x: a.type = hello", storedOutput().trim)
Expand Down