Skip to content

Commit 5b0a357

Browse files
Jezisek-EmatiqKordyjan
authored andcommitted
Fix compile error message in wildcard exports
closes #18031 Co-authored-by: Matt Bovel <[email protected]> Co-authored-by: Cyrille Lavigne <[email protected]> [Cherry-picked a553539]
1 parent 6577292 commit 5b0a357

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
@@ -3389,7 +3389,7 @@ object Parsers {
33893389
*/
33903390
def importOrExportClause(leading: Token, mkTree: ImportConstr): List[Tree] = {
33913391
val offset = accept(leading)
3392-
commaSeparated(importExpr(mkTree)) match {
3392+
commaSeparated(importExpr(leading, mkTree)) match {
33933393
case t :: rest =>
33943394
// The first import should start at the start offset of the keyword.
33953395
val firstPos =
@@ -3444,13 +3444,18 @@ object Parsers {
34443444
* NamedSelector ::= id [‘as’ (id | ‘_’)]
34453445
* WildCardSelector ::= ‘*' | ‘given’ [InfixType]
34463446
*/
3447-
def importExpr(mkTree: ImportConstr): () => Tree =
3447+
def importExpr(leading: Token, mkTree: ImportConstr): () => Tree =
3448+
3449+
def exprName =
3450+
(leading: @unchecked) match
3451+
case EXPORT => "export"
3452+
case IMPORT => "import"
34483453

34493454
/** ‘*' | ‘_' */
34503455
def wildcardSelector() =
34513456
if in.token == USCORE && sourceVersion.isAtLeast(future) then
34523457
report.errorOrMigrationWarning(
3453-
em"`_` is no longer supported for a wildcard import; use `*` instead${rewriteNotice(`future-migration`)}",
3458+
em"`_` is no longer supported for a wildcard $exprName; use `*` instead${rewriteNotice(`future-migration`)}",
34543459
in.sourcePos(),
34553460
from = future)
34563461
patch(source, Span(in.offset, in.offset + 1), "*")
@@ -3469,7 +3474,7 @@ object Parsers {
34693474
if in.token == ARROW || isIdent(nme.as) then
34703475
if in.token == ARROW && sourceVersion.isAtLeast(future) then
34713476
report.errorOrMigrationWarning(
3472-
em"The import renaming `a => b` is no longer supported ; use `a as b` instead${rewriteNotice(`future-migration`)}",
3477+
em"The $exprName renaming `a => b` is no longer supported ; use `a as b` instead${rewriteNotice(`future-migration`)}",
34733478
in.sourcePos(),
34743479
from = future)
34753480
patch(source, Span(in.offset, in.offset + 2),
@@ -3489,7 +3494,7 @@ object Parsers {
34893494
case _ =>
34903495
if isIdent(nme.raw.STAR) then wildcardSelector()
34913496
else
3492-
if !idOK then syntaxError(em"named imports cannot follow wildcard imports")
3497+
if !idOK then syntaxError(em"named ${exprName}s cannot follow wildcard ${exprName}s")
34933498
namedSelector(termIdent())
34943499
}
34953500

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)