Skip to content

Commit d2521d1

Browse files
committed
More words for misused using
1 parent 5af9339 commit d2521d1

File tree

7 files changed

+273
-8
lines changed

7 files changed

+273
-8
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3731,6 +3731,8 @@ object Parsers {
37313731

37323732
def addParamMod(mod: () => Mod) = impliedMods = addMod(impliedMods, atSpan(in.skipToken()) { mod() })
37333733

3734+
def paramModAdvice = "It is a keyword only at the beginning of a parameter clause."
3735+
37343736
def paramMods() =
37353737
if in.token == IMPLICIT then
37363738
report.errorOrMigrationWarning(
@@ -3748,6 +3750,8 @@ object Parsers {
37483750
if initialMods.is(Given) then
37493751
syntaxError(em"`using` is already implied here, should not be given explicitly", in.offset)
37503752
addParamMod(() => Mod.Given())
3753+
if in.isColon then
3754+
syntaxErrorOrIncomplete(ExpectedTokenButFoundSoftKeyword(IDENTIFIER, COLONop, nme.using, paramModAdvice))
37513755

37523756
def param(): ValDef = {
37533757
val start = in.offset
@@ -3774,8 +3778,20 @@ object Parsers {
37743778
mods |= Param
37753779
}
37763780
atSpan(start, nameStart) {
3777-
val name = ident()
3778-
acceptColon()
3781+
val name = ident() match
3782+
case nme.using if !in.isColon =>
3783+
val msg = ExpectedTokenButFoundSoftKeyword(expected = COLONop, found = in.token, nme.using, paramModAdvice)
3784+
val span = Span(in.offset, in.offset + (if in.name != null then in.name.show.length else 0))
3785+
val pickOne =
3786+
if in.token == IDENTIFIER then
3787+
while in.isSoftModifierInParamModifierPosition do ident() // skip to intended name, discard mods
3788+
ident()
3789+
else nme.using
3790+
syntaxErrorOrIncomplete(msg, span)
3791+
pickOne
3792+
case name =>
3793+
acceptColon()
3794+
name
37793795
if (in.token == ARROW && paramOwner.isClass && !mods.is(Local))
37803796
syntaxError(VarValParametersMayNotBeCallByName(name, mods.is(Mutable)))
37813797
// needed?, it's checked later anyway
@@ -3811,7 +3827,7 @@ object Parsers {
38113827
syntaxError(em"`using` expected")
38123828
Nil
38133829
else
3814-
val clause =
3830+
val clause = {
38153831
if paramOwner == ParamOwner.ExtensionPrefix
38163832
&& !isIdent(nme.using) && !isIdent(nme.erased)
38173833
then
@@ -3841,7 +3857,8 @@ object Parsers {
38413857
else contextTypes(paramOwner, numLeadParams, impliedMods)
38423858
params match
38433859
case Nil => Nil
3844-
case (h :: t) => h.withAddedFlags(firstParamMod.flags) :: t
3860+
case h :: t => h.withAddedFlags(firstParamMod.flags) :: t
3861+
}
38453862
checkVarArgsRules(clause)
38463863
clause
38473864
}

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ extends NotFoundMsg(MissingIdentID) {
302302
|imported from elsewhere.
303303
|
304304
|Possible reasons why no matching declaration was found:
305-
| - The declaration or the use is mis-spelt.
305+
| - The declaration or the use is misspelled.
306306
| - An import is missing.
307307
| - The declaration exists but refers to a type in a context where a term is expected, or vice-versa."""
308308
}
@@ -1272,6 +1272,17 @@ extends SyntaxMsg(ExpectedTokenButFoundID) {
12721272
""
12731273
}
12741274

1275+
class ExpectedTokenButFoundSoftKeyword(expected: Token, found: Token, soft: Name, advice: String = "")(using Context)
1276+
extends SyntaxMsg(ExpectedTokenButFoundID):
1277+
def addendum = if !advice.isEmpty then s"\n$advice" else advice
1278+
def msg(using Context) =
1279+
val expectedText = if Tokens.isIdentifier(expected) then "an identifier" else Tokens.showToken(expected)
1280+
val what = if Tokens.isIdentifier(found) || expected == Tokens.COLONop then "an identifier" else "the soft keyword"
1281+
s"""$expectedText expected, but ${Tokens.showToken(found)} found
1282+
|The soft keyword `$soft` was taken as $what in this context.$addendum""".stripMargin
1283+
def explain(using Context) = s"The soft keyword `$soft` has special meaning only in certain contexts."
1284+
end ExpectedTokenButFoundSoftKeyword
1285+
12751286
class MixedLeftAndRightAssociativeOps(op1: Name, op2: Name, op2LeftAssoc: Boolean)(using Context)
12761287
extends SyntaxMsg(MixedLeftAndRightAssociativeOpsID) {
12771288
def msg(using Context) =

compiler/test/dotty/tools/vulpix/ParallelTesting.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,12 @@ trait ParallelTesting extends RunnerOrchestration:
948948

949949
Option {
950950
if actualErrors == 0 then s"\nNo errors found when compiling neg test $testSource"
951-
else if expectedErrors == 0 then s"\nNo errors expected/defined in $testSource -- use // error or // nopos-error"
951+
else if expectedErrors == 0 then
952+
s"""|No expected errors marked in $testSource -- use // error or // nopos-error
953+
|actual error count: $actualErrors
954+
|${unexpected.mkString("Unexpected errors:\n", "\n", "")}
955+
|$showErrors
956+
|""".stripMargin.trim.linesIterator.mkString("\n", "\n", "")
952957
else if expectedErrors != actualErrors then
953958
s"""|Wrong number of errors encountered when compiling $testSource
954959
|expected: $expectedErrors, actual: $actualErrors

tests/neg/22550.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
| imported from elsewhere.
3333
|
3434
| Possible reasons why no matching declaration was found:
35-
| - The declaration or the use is mis-spelt.
35+
| - The declaration or the use is misspelled.
3636
| - An import is missing.
3737
| - The declaration exists but refers to a type in a context where a term is expected, or vice-versa.
3838
---------------------------------------------------------------------------------------------------------------------

tests/neg/22550b.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
| imported from elsewhere.
3333
|
3434
| Possible reasons why no matching declaration was found:
35-
| - The declaration or the use is mis-spelt.
35+
| - The declaration or the use is misspelled.
3636
| - An import is missing.
3737
| - The declaration exists but refers to a type in a context where a term is expected, or vice-versa.
3838
--------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)