Skip to content

Commit

Permalink
Fix compile error message in wildcard exports
Browse files Browse the repository at this point in the history
closes #18031

Co-authored-by: Matt Bovel <[email protected]>
Co-authored-by: Cyrille Lavigne <[email protected]>

[Cherry-picked a553539]
  • Loading branch information
Jezisek-Ematiq authored and Kordyjan committed Nov 28, 2023
1 parent 798dc2d commit f6aaf2a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
15 changes: 10 additions & 5 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3389,7 +3389,7 @@ object Parsers {
*/
def importOrExportClause(leading: Token, mkTree: ImportConstr): List[Tree] = {
val offset = accept(leading)
commaSeparated(importExpr(mkTree)) match {
commaSeparated(importExpr(leading, mkTree)) match {
case t :: rest =>
// The first import should start at the start offset of the keyword.
val firstPos =
Expand Down Expand Up @@ -3444,13 +3444,18 @@ object Parsers {
* NamedSelector ::= id [‘as’ (id | ‘_’)]
* WildCardSelector ::= ‘*' | ‘given’ [InfixType]
*/
def importExpr(mkTree: ImportConstr): () => Tree =
def importExpr(leading: Token, mkTree: ImportConstr): () => Tree =

def exprName =
(leading: @unchecked) match
case EXPORT => "export"
case IMPORT => "import"

/** ‘*' | ‘_' */
def wildcardSelector() =
if in.token == USCORE && sourceVersion.isAtLeast(future) then
report.errorOrMigrationWarning(
em"`_` is no longer supported for a wildcard import; use `*` instead${rewriteNotice(`future-migration`)}",
em"`_` is no longer supported for a wildcard $exprName; use `*` instead${rewriteNotice(`future-migration`)}",
in.sourcePos(),
from = future)
patch(source, Span(in.offset, in.offset + 1), "*")
Expand All @@ -3469,7 +3474,7 @@ object Parsers {
if in.token == ARROW || isIdent(nme.as) then
if in.token == ARROW && sourceVersion.isAtLeast(future) then
report.errorOrMigrationWarning(
em"The import renaming `a => b` is no longer supported ; use `a as b` instead${rewriteNotice(`future-migration`)}",
em"The $exprName renaming `a => b` is no longer supported ; use `a as b` instead${rewriteNotice(`future-migration`)}",
in.sourcePos(),
from = future)
patch(source, Span(in.offset, in.offset + 2),
Expand All @@ -3489,7 +3494,7 @@ object Parsers {
case _ =>
if isIdent(nme.raw.STAR) then wildcardSelector()
else
if !idOK then syntaxError(em"named imports cannot follow wildcard imports")
if !idOK then syntaxError(em"named ${exprName}s cannot follow wildcard ${exprName}s")
namedSelector(termIdent())
}

Expand Down
28 changes: 28 additions & 0 deletions tests/neg/18031.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- Error: tests/neg/18031.scala:8:15 -----------------------------------------------------------------------------------
8 | export A.{*, x as _} // error
| ^
| named exports cannot follow wildcard exports
-- Error: tests/neg/18031.scala:11:15 ----------------------------------------------------------------------------------
11 | import A.{*, x as _} // error
| ^
| named imports cannot follow wildcard imports
-- Error: tests/neg/18031.scala:15:14 ----------------------------------------------------------------------------------
15 | export A.{x => blah} // error
| ^
| The export renaming `a => b` is no longer supported ; use `a as b` instead
| This construct can be rewritten automatically under -rewrite -source future-migration.
-- Error: tests/neg/18031.scala:18:14 ----------------------------------------------------------------------------------
18 | import A.{x => blah} // error
| ^
| The import renaming `a => b` is no longer supported ; use `a as b` instead
| This construct can be rewritten automatically under -rewrite -source future-migration.
-- Error: tests/neg/18031.scala:22:11 ----------------------------------------------------------------------------------
22 | export A._ // error
| ^
| `_` is no longer supported for a wildcard export; use `*` instead
| This construct can be rewritten automatically under -rewrite -source future-migration.
-- Error: tests/neg/18031.scala:25:11 ----------------------------------------------------------------------------------
25 | import A._ // error
| ^
| `_` is no longer supported for a wildcard import; use `*` instead
| This construct can be rewritten automatically under -rewrite -source future-migration.
25 changes: 25 additions & 0 deletions tests/neg/18031.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// scalac: -source:future

object A:
val x, y, z = 0


object B:
export A.{*, x as _} // error

object C:
import A.{*, x as _} // error


object D:
export A.{x => blah} // error

object E:
import A.{x => blah} // error


object F:
export A._ // error

object G:
import A._ // error

0 comments on commit f6aaf2a

Please sign in to comment.