From 07aac72be44d9f53640da90a2cbecdb56502a2bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ku=C4=8Dera?= Date: Tue, 11 Jul 2023 19:19:32 +0200 Subject: [PATCH] Fix compile error message in wildcard exports closes https://github.com/lampepfl/dotty/issues/18031 Co-authored-by: Matt Bovel Co-authored-by: Cyrille Lavigne --- .../dotty/tools/dotc/parsing/Parsers.scala | 15 ++++++---- tests/neg/18031.check | 28 +++++++++++++++++++ tests/neg/18031.scala | 25 +++++++++++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 tests/neg/18031.check create mode 100644 tests/neg/18031.scala diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index cec7a72f3e8e..71c3fd9c43bd 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -3419,7 +3419,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 = @@ -3474,13 +3474,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), "*") @@ -3499,7 +3504,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), @@ -3519,7 +3524,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