@@ -19,7 +19,7 @@ import StdNames._
19
19
import util .Positions ._
20
20
import Constants ._
21
21
import ScriptParsers ._
22
- import annotation .switch
22
+ import scala . annotation .{ tailrec , switch }
23
23
import util .DotClass
24
24
import rewrite .Rewrites .patch
25
25
@@ -648,12 +648,19 @@ object Parsers {
648
648
}
649
649
650
650
/* ------------- TYPES ------------------------------------------------------ */
651
+ /** Same as [[typ ]], but emits a syntax error if it returns a wildcard.
652
+ */
653
+ def toplevelTyp (): Tree = {
654
+ val t = typ()
655
+ for (wildcardPos <- findWildcardType(t)) syntaxError(" unbound wildcard type" , wildcardPos)
656
+ t
657
+ }
651
658
652
- /** Type ::= FunArgTypes `=>' Type
659
+ /** Type ::= FunArgTypes `=>' Type
653
660
* | HkTypeParamClause `->' Type
654
- * | InfixType
661
+ * | InfixType
655
662
* FunArgTypes ::= InfixType
656
- * | `(' [ FunArgType {`,' FunArgType } ] `)'
663
+ * | `(' [ FunArgType {`,' FunArgType } ] `)'
657
664
*/
658
665
def typ (): Tree = {
659
666
val start = in.offset
@@ -737,6 +744,7 @@ object Parsers {
737
744
* | StableId
738
745
* | Path `.' type
739
746
* | `(' ArgTypes `)'
747
+ * | `_' TypeBounds
740
748
* | Refinement
741
749
* | Literal
742
750
*/
@@ -746,6 +754,10 @@ object Parsers {
746
754
else if (in.token == LBRACE )
747
755
atPos(in.offset) { RefinedTypeTree (EmptyTree , refinement()) }
748
756
else if (isSimpleLiteral) { SingletonTypeTree (literal()) }
757
+ else if (in.token == USCORE ) {
758
+ val start = in.skipToken()
759
+ typeBounds().withPos(Position (start, in.offset, start))
760
+ }
749
761
else path(thisOK = false , handleSingletonType) match {
750
762
case r @ SingletonTypeTree (_) => r
751
763
case r => convertToTypeId(r)
@@ -770,25 +782,16 @@ object Parsers {
770
782
atPos(t.pos.start, id.pos.start) { SelectFromTypeTree (t, id.name) }
771
783
}
772
784
773
- /** ArgType ::= Type | `_' TypeBounds
774
- */
775
- val argType = () =>
776
- if (in.token == USCORE ) {
777
- val start = in.skipToken()
778
- typeBounds().withPos(Position (start, in.offset, start))
779
- }
780
- else typ()
781
-
782
- /** NamedTypeArg ::= id `=' ArgType
785
+ /** NamedTypeArg ::= id `=' Type
783
786
*/
784
787
val namedTypeArg = () => {
785
788
val name = ident()
786
789
accept(EQUALS )
787
- NamedArg (name.toTypeName, argType ())
790
+ NamedArg (name.toTypeName, typ ())
788
791
}
789
792
790
- /** ArgTypes ::= ArgType {`,' ArgType }
791
- * NamedTypeArg {`,' NamedTypeArg}
793
+ /** ArgTypes ::= Type {`,' Type }
794
+ * | NamedTypeArg {`,' NamedTypeArg}
792
795
*/
793
796
def argTypes (namedOK : Boolean = false ) = {
794
797
def otherArgs (first : Tree , arg : () => Tree ): List [Tree ] = {
@@ -801,22 +804,22 @@ object Parsers {
801
804
first :: rest
802
805
}
803
806
if (namedOK && in.token == IDENTIFIER )
804
- argType () match {
807
+ typ () match {
805
808
case Ident (name) if in.token == EQUALS =>
806
809
in.nextToken()
807
- otherArgs(NamedArg (name, argType ()), namedTypeArg)
810
+ otherArgs(NamedArg (name, typ ()), namedTypeArg)
808
811
case firstArg =>
809
812
if (in.token == EQUALS ) println(s " ??? $firstArg" )
810
- otherArgs(firstArg, argType )
813
+ otherArgs(firstArg, typ )
811
814
}
812
- else commaSeparated(argType )
815
+ else commaSeparated(typ )
813
816
}
814
817
815
- /** FunArgType ::= ArgType | `=>' ArgType
818
+ /** FunArgType ::= Type | `=>' Type
816
819
*/
817
820
val funArgType = () =>
818
- if (in.token == ARROW ) atPos(in.skipToken()) { ByNameTypeTree (argType ()) }
819
- else argType ()
821
+ if (in.token == ARROW ) atPos(in.skipToken()) { ByNameTypeTree (typ ()) }
822
+ else typ ()
820
823
821
824
/** ParamType ::= [`=>'] ParamValueType
822
825
*/
@@ -827,14 +830,14 @@ object Parsers {
827
830
/** ParamValueType ::= Type [`*']
828
831
*/
829
832
def paramValueType (): Tree = {
830
- val t = typ ()
833
+ val t = toplevelTyp ()
831
834
if (isIdent(nme.raw.STAR )) {
832
835
in.nextToken()
833
836
atPos(t.pos.start) { PostfixOp (t, nme.raw.STAR ) }
834
837
} else t
835
838
}
836
839
837
- /** TypeArgs ::= `[' ArgType {`,' ArgType } `]'
840
+ /** TypeArgs ::= `[' Type {`,' Type } `]'
838
841
* NamedTypeArgs ::= `[' NamedTypeArg {`,' NamedTypeArg} `]'
839
842
*/
840
843
def typeArgs (namedOK : Boolean = false ): List [Tree ] = inBrackets(argTypes(namedOK))
@@ -849,7 +852,7 @@ object Parsers {
849
852
atPos(in.offset) { TypeBoundsTree (bound(SUPERTYPE ), bound(SUBTYPE )) }
850
853
851
854
private def bound (tok : Int ): Tree =
852
- if (in.token == tok) { in.nextToken(); typ () }
855
+ if (in.token == tok) { in.nextToken(); toplevelTyp () }
853
856
else EmptyTree
854
857
855
858
/** TypeParamBounds ::= TypeBounds {`<%' Type} {`:' Type}
@@ -863,26 +866,37 @@ object Parsers {
863
866
def contextBounds (pname : TypeName ): List [Tree ] = in.token match {
864
867
case COLON =>
865
868
atPos(in.skipToken) {
866
- AppliedTypeTree (typ (), Ident (pname))
869
+ AppliedTypeTree (toplevelTyp (), Ident (pname))
867
870
} :: contextBounds(pname)
868
871
case VIEWBOUND =>
869
872
deprecationWarning(" view bounds `<%' are deprecated, use a context bound `:' instead" )
870
873
atPos(in.skipToken) {
871
- Function (Ident (pname) :: Nil , typ ())
874
+ Function (Ident (pname) :: Nil , toplevelTyp ())
872
875
} :: contextBounds(pname)
873
876
case _ =>
874
877
Nil
875
878
}
876
879
877
880
def typedOpt (): Tree =
878
- if (in.token == COLON ) { in.nextToken(); typ () }
881
+ if (in.token == COLON ) { in.nextToken(); toplevelTyp () }
879
882
else TypeTree ()
880
883
881
884
def typeDependingOn (location : Location .Value ): Tree =
882
885
if (location == Location .InParens ) typ()
883
886
else if (location == Location .InPattern ) refinedType()
884
887
else infixType()
885
888
889
+ /** Checks whether `t` is a wildcard type.
890
+ * If it is, returns the [[Position ]] where the wildcard occurs.
891
+ */
892
+ @ tailrec
893
+ private final def findWildcardType (t : Tree ): Option [Position ] = t match {
894
+ case TypeBoundsTree (_, _) => Some (t.pos)
895
+ case Parens (t1) => findWildcardType(t1)
896
+ case Annotated (_, t1) => findWildcardType(t1)
897
+ case _ => None
898
+ }
899
+
886
900
/* ----------- EXPRESSIONS ------------------------------------------------ */
887
901
888
902
/** EqualsExpr ::= `=' Expr
0 commit comments