diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index bc2ac0aa20a9..474b6c4a6030 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -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 = @@ -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), "*") @@ -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), @@ -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()) } diff --git a/tests/neg/18031.check b/tests/neg/18031.check new file mode 100644 index 000000000000..47d3e784ade1 --- /dev/null +++ b/tests/neg/18031.check @@ -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. diff --git a/tests/neg/18031.scala b/tests/neg/18031.scala new file mode 100644 index 000000000000..39b39a03d003 --- /dev/null +++ b/tests/neg/18031.scala @@ -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