Skip to content

Commit 64929b4

Browse files
committed
improve error message for rename the same import twice
1 parent dfab5e5 commit 64929b4

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public enum ErrorMessageID {
129129
JavaSymbolIsNotAValueID,
130130
DoubleDeclarationID,
131131
MatchCaseOnlyNullWarningID,
132+
ImportRenamedTwiceID,
132133
;
133134

134135
public int errorNumber() {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,4 +2105,11 @@ object messages {
21052105
}
21062106
val explanation = ""
21072107
}
2108+
2109+
case class ImportRenamedTwice(ident: untpd.Ident)(implicit ctx: Context) extends Message(ImportRenamedTwiceID) {
2110+
val kind = "Syntax"
2111+
val msg: String = s"${ident.show} is renamed twice on the same import line."
2112+
val explanation: String = ""
2113+
}
2114+
21082115
}

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@ package dotty.tools.dotc
22
package transform
33

44
import dotty.tools.dotc.ast.{Trees, tpd, untpd}
5-
import scala.collection.{ mutable, immutable }
6-
import ValueClasses._
7-
import scala.annotation.tailrec
5+
import scala.collection.mutable
86
import core._
9-
import typer.ErrorReporting._
107
import typer.Checking
11-
import Types._, Contexts._, Constants._, Names._, NameOps._, Flags._, DenotTransformers._
12-
import SymDenotations._, Symbols._, StdNames._, Annotations._, Trees._, Scopes._, Denotations._
13-
import util.Positions._
8+
import Types._, Contexts._, Names._, Flags._, DenotTransformers._
9+
import SymDenotations._, StdNames._, Annotations._, Trees._
1410
import Decorators._
15-
import config.Printers.typr
16-
import Symbols._, TypeUtils._, SymUtils._
17-
import reporting.diagnostic.messages.{NotAMember, SuperCallsNotAllowedInline}
11+
import Symbols._, SymUtils._
12+
import reporting.diagnostic.messages.{ImportRenamedTwice, NotAMember, SuperCallsNotAllowedInline}
1813

1914
object PostTyper {
2015
val name = "posttyper"
@@ -293,7 +288,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
293288
if (name != nme.WILDCARD && !exprTpe.member(name).exists && !exprTpe.member(name.toTypeName).exists)
294289
ctx.error(NotAMember(exprTpe, name, "value"), ident.pos)
295290
if (seen(ident.name))
296-
ctx.error(s"${ident.show} is renamed twice", ident.pos)
291+
ctx.error(ImportRenamedTwice(ident), ident.pos)
297292
seen += ident.name
298293
}
299294
selectors.foreach {

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import dotty.tools.dotc.core.Contexts.Context
66
import dotty.tools.dotc.core.Types.WildcardType
77
import dotty.tools.dotc.parsing.Tokens
88
import dotty.tools.dotc.reporting.diagnostic.messages._
9+
import dotty.tools.dotc.transform.PostTyper
910
import org.junit.Assert._
1011
import org.junit.Test
1112

@@ -1348,4 +1349,16 @@ class ErrorMessagesTests extends ErrorMessagesTest {
13481349
val (msg @ FunctionTypeNeedsNonEmptyParameterList(_, _)) :: Nil = messages
13491350
assertEquals(msg.mods, "erased implicit")
13501351
}
1352+
1353+
@Test def renameImportTwice =
1354+
checkMessagesAfter(PostTyper.name) {
1355+
"""
1356+
|import java.lang.{Integer => Foo, Integer => Baz}
1357+
""".stripMargin
1358+
}.expect { (ictx, messages) =>
1359+
implicit val ctx: Context = ictx
1360+
assertMessageCount(1, messages)
1361+
val (msg @ ImportRenamedTwice(ident)) :: Nil = messages
1362+
assertEquals(ident.show, "Integer")
1363+
}
13511364
}

0 commit comments

Comments
 (0)