@@ -2803,46 +2803,59 @@ self =>
28032803 * }}}
28042804 */
28052805 def importSelectors (): List [ImportSelector ] = {
2806- val selectors0 = inBracesOrNil(commaSeparated(importSelector()))
2807-
2808- // Treat an import of `*, given` or `given, *` as if it was an import of `*`
2809- // since the former in Scala 3 has the same semantics as the latter in Scala 2.
2810- val selectors =
2811- if (currentRun.isScala3 && selectors0.exists(_.isWildcard))
2812- selectors0.filterNot(sel => sel.name == nme.`given` && sel.rename == sel.name)
2813- else
2814- selectors0
2815-
2816- for (t <- selectors.init if t.isWildcard) syntaxError(t.namePos, " Wildcard import must be in last position" )
2817- selectors
2806+ def isWilder (sel : ImportSelector ) = sel.isWildcard || currentRun.isScala3 && ! sel.isRename && sel.name == nme.`given`
2807+ // error on duplicate target names, import x.{a=>z, b=>z}, and fix import x.{given, *} to x._
2808+ def checkSelectors (xs : List [ImportSelector ]): List [ImportSelector ] = xs match {
2809+ case h :: t =>
2810+ // wildcards must come last, and for -Xsource:3, take trailing given, * (or *, given) as _
2811+ if (isWilder(h)) {
2812+ if (t.exists(! isWilder(_)))
2813+ syntaxError(h.namePos, " wildcard import must be in last position" )
2814+ xs.filter(_.isWildcard) match {
2815+ case ws @ (_ :: Nil ) => ws
2816+ case Nil =>
2817+ syntaxError(h.namePos, " given requires a wildcard selector" )
2818+ ImportSelector .wildAt(h.namePos) :: Nil
2819+ case ws @ (w :: _) =>
2820+ syntaxError(w.namePos, " duplicate wildcard selector" )
2821+ w :: Nil
2822+ }
2823+ }
2824+ else {
2825+ if (! h.isMask)
2826+ t.find(_.rename == h.rename).foreach { duplicate =>
2827+ val msg =
2828+ if (h.isRename || duplicate.isRename) s " ${h.rename} is an ambiguous name on import "
2829+ else s " ${h.rename} is imported twice "
2830+ syntaxError(h.namePos, msg)
2831+ }
2832+ h :: checkSelectors(t)
2833+ }
2834+ case _ => Nil
2835+ }
2836+ checkSelectors(inBracesOrNil(commaSeparated(importSelector())))
28182837 }
28192838
28202839 def wildcardOrIdent () =
2821- if (in.token == USCORE || currentRun.isScala3 && isRawStar) { in.nextToken() ; nme.WILDCARD }
2840+ if (in.token == USCORE || currentRun.isScala3 && isRawStar) { in.nextToken(); nme.WILDCARD }
28222841 else ident()
28232842
28242843 /** {{{
28252844 * ImportSelector ::= Id [`=>` Id | `=>` `_`]
28262845 * }}}
28272846 */
28282847 def importSelector (): ImportSelector = {
2829- val start = in.offset
2830- val bbq = in.token == BACKQUOTED_IDENT
2831- val name = wildcardOrIdent()
2832- var renameOffset = - 1
2833-
2834- val rename =
2848+ val start = in.offset
2849+ val bbq = in.token == BACKQUOTED_IDENT
2850+ val name = wildcardOrIdent()
2851+ val (rename, renameOffset) =
28352852 if (in.token == ARROW || (currentRun.isScala3 && isRawIdent && in.name == nme.as)) {
28362853 in.nextToken()
2837- renameOffset = in.offset
2838- if (name == nme.WILDCARD && ! bbq) syntaxError(renameOffset, " Wildcard import cannot be renamed" )
2839- wildcardOrIdent()
2840- }
2841- else if (name == nme.WILDCARD && ! bbq) null
2842- else {
2843- renameOffset = start
2844- name
2854+ if (name == nme.WILDCARD && ! bbq) syntaxError(in.offset, " Wildcard import cannot be renamed" )
2855+ (wildcardOrIdent(), in.offset)
28452856 }
2857+ else if (name == nme.WILDCARD && ! bbq) (null , - 1 )
2858+ else (name, start)
28462859
28472860 ImportSelector (name, start, rename, renameOffset)
28482861 }
0 commit comments