@@ -8,7 +8,7 @@ import Symbols.*, StdNames.*, Trees.*, ContextOps.*
8
8
import Decorators .*
9
9
import Annotations .Annotation
10
10
import NameKinds .{UniqueName , ContextBoundParamName , ContextFunctionParamName , DefaultGetterName , WildcardParamName }
11
- import typer .{Namer , Checking }
11
+ import typer .{Namer , Checking , ErrorReporting }
12
12
import util .{Property , SourceFile , SourcePosition , SrcPos , Chars }
13
13
import config .{Feature , Config }
14
14
import config .Feature .{sourceVersion , migrateTo3 , enabled , betterForsEnabled }
@@ -199,9 +199,10 @@ object desugar {
199
199
def valDef (vdef0 : ValDef )(using Context ): Tree =
200
200
val vdef @ ValDef (_, tpt, rhs) = vdef0
201
201
val valName = normalizeName(vdef, tpt).asTermName
202
+ val tpt1 = qualifiedType(tpt, valName)
202
203
var mods1 = vdef.mods
203
204
204
- val vdef1 = cpy.ValDef (vdef)(name = valName).withMods(mods1)
205
+ val vdef1 = cpy.ValDef (vdef)(name = valName, tpt = tpt1 ).withMods(mods1)
205
206
206
207
if isSetterNeeded(vdef) then
207
208
val setterParam = makeSyntheticParameter(tpt = SetterParamTree ().watching(vdef))
@@ -2145,6 +2146,10 @@ object desugar {
2145
2146
case PatDef (mods, pats, tpt, rhs) =>
2146
2147
val pats1 = if (tpt.isEmpty) pats else pats map (Typed (_, tpt))
2147
2148
flatTree(pats1 map (makePatDef(tree, mods, _, rhs)))
2149
+ case QualifiedTypeTree (parent, None , qualifier) =>
2150
+ ErrorReporting .errorTree(parent, em " missing parameter name in qualified type " , tree.srcPos)
2151
+ case QualifiedTypeTree (parent, Some (paramName), qualifier) =>
2152
+ qualifiedType(parent, paramName, qualifier, tree.span)
2148
2153
case ext : ExtMethods =>
2149
2154
Block (List (ext), syntheticUnitLiteral.withSpan(ext.span))
2150
2155
case f : FunctionWithMods if f.hasErasedParams => makeFunctionWithValDefs(f, pt)
@@ -2323,4 +2328,23 @@ object desugar {
2323
2328
collect(tree)
2324
2329
buf.toList
2325
2330
}
2331
+
2332
+ /** If `tree` is a `QualifiedTypeTree`, then desugars it using `paramName` as
2333
+ * the qualified paramater name. Otherwise, returns `tree` unchanged.
2334
+ */
2335
+ def qualifiedType (tree : Tree , paramName : TermName )(using Context ): Tree = tree match
2336
+ case QualifiedTypeTree (parent, None , qualifier) => qualifiedType(parent, paramName, qualifier, tree.span)
2337
+ case _ => tree
2338
+
2339
+ /** Returns the annotated type used to represent the qualified type with the
2340
+ * given components:
2341
+ * `parent @qualified[parent]((paramName: parent) => qualifier)`.
2342
+ */
2343
+ def qualifiedType (parent : Tree , paramName : TermName , qualifier : Tree , span : Span )(using Context ): Tree =
2344
+ val param = makeParameter(paramName, parent, EmptyModifiers ) // paramName: parent
2345
+ val predicate = WildcardFunction (List (param), qualifier) // (paramName: parent) => qualifier
2346
+ val qualifiedAnnot = scalaAnnotationDot(nme.qualified)
2347
+ val annot = Apply (TypeApply (qualifiedAnnot, List (parent)), predicate).withSpan(span) // @qualified[parent](predicate)
2348
+ Annotated (parent, annot).withSpan(span) // parent @qualified[parent](predicate)
2349
+
2326
2350
}
0 commit comments