Skip to content

Backport "Fix compile error message in wildcard exports" to LTS #19063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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