Skip to content

Commit a553539

Browse files
Jezisek-EmatiqmbovelCyrille Lavigne
committed
Fix compile error message in wildcard exports
closes #18031 Co-authored-by: Matt Bovel <[email protected]> Co-authored-by: Cyrille Lavigne <[email protected]>
1 parent d0403b6 commit a553539

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3419,7 +3419,7 @@ object Parsers {
34193419
*/
34203420
def importOrExportClause(leading: Token, mkTree: ImportConstr): List[Tree] = {
34213421
val offset = accept(leading)
3422-
commaSeparated(importExpr(mkTree)) match {
3422+
commaSeparated(importExpr(leading, mkTree)) match {
34233423
case t :: rest =>
34243424
// The first import should start at the start offset of the keyword.
34253425
val firstPos =
@@ -3474,13 +3474,18 @@ object Parsers {
34743474
* NamedSelector ::= id [‘as’ (id | ‘_’)]
34753475
* WildCardSelector ::= ‘*' | ‘given’ [InfixType]
34763476
*/
3477-
def importExpr(mkTree: ImportConstr): () => Tree =
3477+
def importExpr(leading: Token, mkTree: ImportConstr): () => Tree =
3478+
3479+
def exprName =
3480+
(leading: @unchecked) match
3481+
case EXPORT => "export"
3482+
case IMPORT => "import"
34783483

34793484
/** ‘*' | ‘_' */
34803485
def wildcardSelector() =
34813486
if in.token == USCORE && sourceVersion.isAtLeast(future) then
34823487
report.errorOrMigrationWarning(
3483-
em"`_` is no longer supported for a wildcard import; use `*` instead${rewriteNotice(`future-migration`)}",
3488+
em"`_` is no longer supported for a wildcard $exprName; use `*` instead${rewriteNotice(`future-migration`)}",
34843489
in.sourcePos(),
34853490
from = future)
34863491
patch(source, Span(in.offset, in.offset + 1), "*")
@@ -3499,7 +3504,7 @@ object Parsers {
34993504
if in.token == ARROW || isIdent(nme.as) then
35003505
if in.token == ARROW && sourceVersion.isAtLeast(future) then
35013506
report.errorOrMigrationWarning(
3502-
em"The import renaming `a => b` is no longer supported ; use `a as b` instead${rewriteNotice(`future-migration`)}",
3507+
em"The $exprName renaming `a => b` is no longer supported ; use `a as b` instead${rewriteNotice(`future-migration`)}",
35033508
in.sourcePos(),
35043509
from = future)
35053510
patch(source, Span(in.offset, in.offset + 2),
@@ -3519,7 +3524,7 @@ object Parsers {
35193524
case _ =>
35203525
if isIdent(nme.raw.STAR) then wildcardSelector()
35213526
else
3522-
if !idOK then syntaxError(em"named imports cannot follow wildcard imports")
3527+
if !idOK then syntaxError(em"named ${exprName}s cannot follow wildcard ${exprName}s")
35233528
namedSelector(termIdent())
35243529
}
35253530

tests/neg/18031.check

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-- Error: tests/neg/18031.scala:8:15 -----------------------------------------------------------------------------------
2+
8 | export A.{*, x as _} // error
3+
| ^
4+
| named exports cannot follow wildcard exports
5+
-- Error: tests/neg/18031.scala:11:15 ----------------------------------------------------------------------------------
6+
11 | import A.{*, x as _} // error
7+
| ^
8+
| named imports cannot follow wildcard imports
9+
-- Error: tests/neg/18031.scala:15:14 ----------------------------------------------------------------------------------
10+
15 | export A.{x => blah} // error
11+
| ^
12+
| The export renaming `a => b` is no longer supported ; use `a as b` instead
13+
| This construct can be rewritten automatically under -rewrite -source future-migration.
14+
-- Error: tests/neg/18031.scala:18:14 ----------------------------------------------------------------------------------
15+
18 | import A.{x => blah} // error
16+
| ^
17+
| The import renaming `a => b` is no longer supported ; use `a as b` instead
18+
| This construct can be rewritten automatically under -rewrite -source future-migration.
19+
-- Error: tests/neg/18031.scala:22:11 ----------------------------------------------------------------------------------
20+
22 | export A._ // error
21+
| ^
22+
| `_` is no longer supported for a wildcard export; use `*` instead
23+
| This construct can be rewritten automatically under -rewrite -source future-migration.
24+
-- Error: tests/neg/18031.scala:25:11 ----------------------------------------------------------------------------------
25+
25 | import A._ // error
26+
| ^
27+
| `_` is no longer supported for a wildcard import; use `*` instead
28+
| This construct can be rewritten automatically under -rewrite -source future-migration.

tests/neg/18031.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// scalac: -source:future
2+
3+
object A:
4+
val x, y, z = 0
5+
6+
7+
object B:
8+
export A.{*, x as _} // error
9+
10+
object C:
11+
import A.{*, x as _} // error
12+
13+
14+
object D:
15+
export A.{x => blah} // error
16+
17+
object E:
18+
import A.{x => blah} // error
19+
20+
21+
object F:
22+
export A._ // error
23+
24+
object G:
25+
import A._ // error

0 commit comments

Comments
 (0)