Skip to content

Commit 6590783

Browse files
author
som-snytt
authored
Merge pull request scala#10815 from som-snytt/unused/parser
Reduce unused parameters in parser
2 parents 3524e27 + 5d5880e commit 6590783

File tree

1 file changed

+35
-43
lines changed

1 file changed

+35
-43
lines changed

src/compiler/scala/tools/nsc/ast/parser/Parsers.scala

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ self =>
218218

219219
override def blockExpr(): Tree = skipBraces(EmptyTree)
220220

221-
override def templateBody(isPre: Boolean) = skipBraces((noSelfType, EmptyTree.asList))
221+
override def templateBody() = skipBraces((noSelfType, EmptyTree.asList))
222222
}
223223

224224
class UnitParser(override val unit: global.CompilationUnit, patches: List[BracePatch]) extends SourceFileParser(unit.source) { uself =>
@@ -617,22 +617,18 @@ self =>
617617
and
618618
}
619619

620-
// warn under -Xsource:3
621-
def migrationWarning(offset: Offset, msg: String, since: String, actions: List[CodeAction] = Nil): Unit =
622-
if (currentRun.isScala3)
623-
warning(offset, msg, WarningCategory.Scala3Migration, actions)
624-
625-
// warn under -Xsource:3, otherwise deprecation
626-
def hardMigrationWarning(offset: Offset, msg: String, since: String, actions: List[CodeAction] = Nil): Unit =
620+
// warn under -Xsource:3; otherwise if since is nonEmpty, issue a deprecation
621+
def migrationWarning(offset: Offset, msg: String, since: String = "", actions: List[CodeAction] = Nil): Unit =
627622
if (currentRun.isScala3) warning(offset, msg, WarningCategory.Scala3Migration, actions)
628-
else deprecationWarning(offset, msg, since, actions)
623+
else if (!since.isEmpty) deprecationWarning(offset, msg, since, actions)
629624

630625
// deprecation or migration under -Xsource:3, with different messages
631-
def hardMigrationWarning(offset: Offset, depr: => String, migr: => String, since: String, actions: String => List[CodeAction]): Unit =
632-
if (currentRun.isScala3) warning(offset, migr, WarningCategory.Scala3Migration, actions(migr))
633-
else deprecationWarning(offset, depr, since, actions(depr))
634-
def hardMigrationWarning(offset: Offset, depr: => String, migr: => String, since: String): Unit =
635-
hardMigrationWarning(offset, depr, migr, since, _ => Nil)
626+
def migrationWarning(offset: Offset, depr: => String, migr: => String, since: String, actions: String => List[CodeAction]): Unit = {
627+
val msg = if (currentRun.isScala3) migr else depr
628+
migrationWarning(offset, msg, since, actions(msg))
629+
}
630+
def migrationWarning(offset: Offset, depr: => String, migr: => String, since: String): Unit =
631+
migrationWarning(offset, depr, migr, since, (_: String) => Nil)
636632

637633
def expectedMsgTemplate(exp: String, fnd: String) = s"$exp expected but $fnd found."
638634
def expectedMsg(token: Token): String =
@@ -819,7 +815,7 @@ self =>
819815
def actions =
820816
if (tree.pos.isRange) runReporting.codeAction("add parentheses", tree.pos, s"(${unit.source.sourceAt(tree.pos)})", msg)
821817
else Nil
822-
migrationWarning(tree.pos.point, wrn, "2.13.11", actions)
818+
migrationWarning(tree.pos.point, wrn, /*since="2.13.11",*/ actions = actions)
823819
List(convertToParam(tree))
824820
case _ => List(convertToParam(tree))
825821
}
@@ -1037,8 +1033,7 @@ self =>
10371033
val fun = s"${CodeAction.maybeWrapInParens(qual)}.${unit.source.sourceAt(operatorPos.withEnd(rhs.pos.start))}".trim
10381034
val fix = s"$fun${CodeAction.wrapInParens(unit.source.sourceAt(rhs.pos))}"
10391035
val msg = "type application is not allowed for infix operators"
1040-
migrationWarning(offset, msg, "2.13.11",
1041-
runReporting.codeAction("use selection", pos, fix, msg))
1036+
migrationWarning(offset, msg, /*since="2.13.11",*/ actions = runReporting.codeAction("use selection", pos, fix, msg))
10421037
}
10431038
atPos(pos)(makeBinop(isExpr, lhs, operator, rhs, operatorPos, targs))
10441039
}
@@ -2114,7 +2109,7 @@ self =>
21142109
def msg(what: String, instead: String): String = s"`val` keyword in for comprehension is $what: $instead"
21152110
if (hasEq) {
21162111
val without = "instead, bind the value without `val`"
2117-
hardMigrationWarning(in.offset, msg("deprecated", without), msg("unsupported", without), "2.10.0", actions)
2112+
migrationWarning(in.offset, msg("deprecated", without), msg("unsupported", without), since="2.10.0", actions=actions(_))
21182113
} else {
21192114
val m = msg("unsupported", "just remove `val`")
21202115
syntaxError(in.offset, m, actions(m))
@@ -2669,7 +2664,7 @@ self =>
26692664
val pname: TypeName =
26702665
if (in.token == USCORE) {
26712666
if (!isAbstractOwner)
2672-
hardMigrationWarning(in.offset, "Top-level wildcard is not allowed", "2.13.7")
2667+
migrationWarning(in.offset, "Top-level wildcard is not allowed", since = "2.13.7")
26732668
in.nextToken()
26742669
freshTypeName("_$$")
26752670
}
@@ -2682,7 +2677,7 @@ self =>
26822677
def msg(what: String) = s"""view bounds are $what; use an implicit parameter instead.
26832678
| example: instead of `def f[A <% Int](a: A)` use `def f[A](a: A)(implicit ev: A => Int)`""".stripMargin
26842679
while (in.token == VIEWBOUND) {
2685-
hardMigrationWarning(in.offset, msg("deprecated"), msg("unsupported"), "2.12.0")
2680+
migrationWarning(in.offset, msg("deprecated"), msg("unsupported"), since = "2.12.0")
26862681
contextBoundBuf += atPos(in.skipToken())(makeFunctionTypeTree(List(Ident(pname)), typ()))
26872682
}
26882683
while (in.token == COLON) {
@@ -3021,18 +3016,17 @@ self =>
30213016
if (in.token == THIS) {
30223017
def missingEquals() = {
30233018
val msg = "procedure syntax is deprecated for constructors: add `=`, as in method definition"
3024-
hardMigrationWarning(in.lastOffset, msg, "2.13.2",
3025-
runReporting.codeAction("replace procedure syntax", o2p(in.lastOffset), " =", msg))
3019+
migrationWarning(in.lastOffset, msg, since = "2.13.2", actions = runReporting.codeAction("replace procedure syntax", o2p(in.lastOffset), " =", msg))
30263020
}
30273021
atPos(start, in.skipToken()) {
3028-
val vparamss = paramClauses(nme.CONSTRUCTOR, classContextBounds map (_.duplicate), ofCaseClass = false)
3022+
val vparamss = paramClauses(nme.CONSTRUCTOR, classContextBounds.map(_.duplicate), ofCaseClass = false)
30293023
newLineOptWhenFollowedBy(LBRACE)
30303024
val rhs =
30313025
if (in.token == LBRACE) {
3032-
missingEquals(); atPos(in.offset) { constrBlock(vparamss) }
3026+
missingEquals(); atPos(in.offset) { constrBlock() }
30333027
}
30343028
else {
3035-
accept(EQUALS) ; atPos(in.offset) { constrExpr(vparamss) }
3029+
accept(EQUALS) ; atPos(in.offset) { constrExpr() }
30363030
}
30373031
DefDef(mods, nme.CONSTRUCTOR, List(), vparamss, TypeTree(), rhs)
30383032
}
@@ -3065,13 +3059,13 @@ self =>
30653059
val rhs =
30663060
if (isStatSep || in.token == RBRACE) {
30673061
if (restype.isEmpty) {
3068-
hardMigrationWarning(in.lastOffset, msg("deprecated", ": Unit"), msg("unsupported", ": Unit"), "2.13.0", declActions)
3062+
migrationWarning(in.lastOffset, msg("deprecated", ": Unit"), msg("unsupported", ": Unit"), since = "2.13.0", actions = declActions)
30693063
restype = scalaUnitConstr
30703064
}
30713065
newmods |= Flags.DEFERRED
30723066
EmptyTree
30733067
} else if (restype.isEmpty && in.token == LBRACE) {
3074-
hardMigrationWarning(in.offset, msg("deprecated", ": Unit ="), msg("unsupported", ": Unit ="), "2.13.0", defnActions)
3068+
migrationWarning(in.offset, msg("deprecated", ": Unit ="), msg("unsupported", ": Unit ="), since = "2.13.0", actions = defnActions)
30753069
restype = scalaUnitConstr
30763070
blockExpr()
30773071
} else {
@@ -3093,7 +3087,7 @@ self =>
30933087
val o = nameOffset + name.decode.length
30943088
runReporting.codeAction("remove ()", r2p(o, o, o + 2), "", msg, expected = Some(("()", unit)))
30953089
}
3096-
def warnNilary() = hardMigrationWarning(nameOffset, unaryMsg("deprecated"), unaryMsg("unsupported"), "2.13.4", action)
3090+
def warnNilary() = migrationWarning(nameOffset, unaryMsg("deprecated"), unaryMsg("unsupported"), since = "2.13.4", actions = action)
30973091
vparamss match {
30983092
case List(List()) => warnNilary()
30993093
case List(List(), x :: xs) if x.mods.isImplicit => warnNilary()
@@ -3111,15 +3105,15 @@ self =>
31113105
* | ConstrBlock
31123106
* }}}
31133107
*/
3114-
def constrExpr(vparamss: List[List[ValDef]]): Tree =
3115-
if (in.token == LBRACE) constrBlock(vparamss)
3116-
else Block(selfInvocation(vparamss) :: Nil, literalUnit)
3108+
def constrExpr(): Tree =
3109+
if (in.token == LBRACE) constrBlock()
3110+
else Block(selfInvocation() :: Nil, literalUnit)
31173111

31183112
/** {{{
31193113
* SelfInvocation ::= this ArgumentExprs {ArgumentExprs}
31203114
* }}}
31213115
*/
3122-
def selfInvocation(vparamss: List[List[ValDef]]): Tree =
3116+
def selfInvocation(): Tree =
31233117
atPos(accept(THIS)) {
31243118
newLineOptWhenFollowedBy(LBRACE)
31253119
var t = Apply(Ident(nme.CONSTRUCTOR), argumentExprs())
@@ -3135,9 +3129,9 @@ self =>
31353129
* ConstrBlock ::= `{` SelfInvocation {semi BlockStat} `}`
31363130
* }}}
31373131
*/
3138-
def constrBlock(vparamss: List[List[ValDef]]): Tree =
3132+
def constrBlock(): Tree =
31393133
atPos(in.skipToken()) {
3140-
val stats = selfInvocation(vparamss) :: {
3134+
val stats = selfInvocation() :: {
31413135
if (isStatSep) { in.nextToken(); blockStatSeq() }
31423136
else Nil
31433137
}
@@ -3224,7 +3218,7 @@ self =>
32243218
val nameOffset = in.offset
32253219
val name = identForType()
32263220
if (currentRun.isScala3 && in.token == LBRACKET && isAfterLineEnd)
3227-
hardMigrationWarning(in.offset, "type parameters should not follow newline", "2.13.7")
3221+
migrationWarning(in.offset, "type parameters should not follow newline", since = "2.13.7")
32283222
def orStart(p: Offset) = if (name == tpnme.ERROR) start else p
32293223
val namePos = NamePos(r2p(orStart(nameOffset), orStart(nameOffset)))
32303224
atPos(start, orStart(nameOffset)) {
@@ -3328,12 +3322,12 @@ self =>
33283322
if (in.token == LBRACE) {
33293323
val braceOffset = in.offset
33303324
// @S: pre template body cannot stub like post body can!
3331-
val (self, body) = templateBody(isPre = true)
3325+
val (self, body) = templateBody()
33323326
if (in.token == WITH && (self eq noSelfType)) {
33333327
val advice =
33343328
if (currentRun.isScala3) "use trait parameters instead."
33353329
else "they will be replaced by trait parameters in 3.0, see the migration guide on avoiding var/val in traits."
3336-
hardMigrationWarning(braceOffset, s"early initializers are deprecated; $advice", "2.13.0")
3330+
migrationWarning(braceOffset, s"early initializers are deprecated; $advice", since = "2.13.0")
33373331
val earlyDefs: List[Tree] = body.map(ensureEarlyDef).filter(_.nonEmpty)
33383332
in.nextToken()
33393333
val parents = templateParents()
@@ -3354,7 +3348,7 @@ self =>
33543348
copyValDef(vdef)(mods = mods | Flags.PRESUPER)
33553349
case tdef @ TypeDef(mods, name, tparams, rhs) =>
33563350
def msg(what: String): String = s"early type members are $what: move them to the regular body; the semantics are the same"
3357-
hardMigrationWarning(tdef.pos.point, msg("deprecated"), msg("unsupported"), "2.11.0")
3351+
migrationWarning(tdef.pos.point, msg("deprecated"), msg("unsupported"), since = "2.11.0")
33583352
treeCopy.TypeDef(tdef, mods | Flags.PRESUPER, name, tparams, rhs)
33593353
case docdef @ DocDef(comm, rhs) =>
33603354
treeCopy.DocDef(docdef, comm, rhs)
@@ -3410,16 +3404,15 @@ self =>
34103404
/** {{{
34113405
* TemplateBody ::= [nl] `{` TemplateStatSeq `}`
34123406
* }}}
3413-
* @param isPre specifies whether in early initializer (true) or not (false)
34143407
*/
3415-
def templateBody(isPre: Boolean) = inBraces(templateStatSeq(isPre)) match {
3408+
def templateBody() = inBraces(templateStatSeq()) match {
34163409
case (selfTypeVal, Nil) => (selfTypeVal, EmptyTree.asList)
34173410
case result => result
34183411
}
34193412
def templateBodyOpt(parenMeansSyntaxError: Boolean): (ValDef, List[Tree]) = {
34203413
newLineOptWhenFollowedBy(LBRACE)
34213414
if (in.token == LBRACE) {
3422-
templateBody(isPre = false)
3415+
templateBody()
34233416
} else {
34243417
if (in.token == LPAREN) {
34253418
if (parenMeansSyntaxError) syntaxError(s"traits or objects may not have parameters", skipIt = true)
@@ -3482,9 +3475,8 @@ self =>
34823475
/** {{{
34833476
* TemplateStatSeq ::= [id [`:` Type] `=>`] TemplateStats
34843477
* }}}
3485-
* @param isPre specifies whether in early initializer (true) or not (false)
34863478
*/
3487-
def templateStatSeq(isPre : Boolean): (ValDef, List[Tree]) = {
3479+
def templateStatSeq(): (ValDef, List[Tree]) = {
34883480
var self: ValDef = noSelfType
34893481
var firstOpt: Option[Tree] = None
34903482
if (isExprIntro) checkNoEscapingPlaceholders {

0 commit comments

Comments
 (0)