@@ -8,7 +8,7 @@ import Symbols.*, StdNames.*, Trees.*, ContextOps.*
88import Decorators .*
99import Annotations .Annotation
1010import NameKinds .{UniqueName , ContextBoundParamName , ContextFunctionParamName , DefaultGetterName , WildcardParamName }
11- import typer .{Namer , Checking }
11+ import typer .{Namer , Checking , ErrorReporting }
1212import util .{Property , SourceFile , SourcePosition , SrcPos , Chars }
1313import config .{Feature , Config }
1414import config .Feature .{sourceVersion , migrateTo3 , enabled }
@@ -214,9 +214,10 @@ object desugar {
214214 def valDef (vdef0 : ValDef )(using Context ): Tree =
215215 val vdef @ ValDef (_, tpt, rhs) = vdef0
216216 val valName = normalizeName(vdef, tpt).asTermName
217+ val tpt1 = qualifiedType(tpt, valName)
217218 var mods1 = vdef.mods
218219
219- val vdef1 = cpy.ValDef (vdef)(name = valName).withMods(mods1)
220+ val vdef1 = cpy.ValDef (vdef)(name = valName, tpt = tpt1 ).withMods(mods1)
220221
221222 if isSetterNeeded(vdef) then
222223 val setterParam = makeSyntheticParameter(tpt = SetterParamTree ().watching(vdef))
@@ -2279,6 +2280,10 @@ object desugar {
22792280 case PatDef (mods, pats, tpt, rhs) =>
22802281 val pats1 = if (tpt.isEmpty) pats else pats map (Typed (_, tpt))
22812282 flatTree(pats1 map (makePatDef(tree, mods, _, rhs)))
2283+ case QualifiedTypeTree (parent, None , qualifier) =>
2284+ ErrorReporting .errorTree(parent, em " missing parameter name in qualified type " , tree.srcPos)
2285+ case QualifiedTypeTree (parent, Some (paramName), qualifier) =>
2286+ qualifiedType(parent, paramName, qualifier, tree.span)
22822287 case ext : ExtMethods =>
22832288 Block (List (ext), syntheticUnitLiteral.withSpan(ext.span))
22842289 case f : FunctionWithMods if f.hasErasedParams => makeFunctionWithValDefs(f, pt)
@@ -2457,4 +2462,23 @@ object desugar {
24572462 collect(tree)
24582463 buf.toList
24592464 }
2465+
2466+ /** If `tree` is a `QualifiedTypeTree`, then desugars it using `paramName` as
2467+ * the qualified parameter name. Otherwise, returns `tree` unchanged.
2468+ */
2469+ def qualifiedType (tree : Tree , paramName : TermName )(using Context ): Tree = tree match
2470+ case QualifiedTypeTree (parent, None , qualifier) => qualifiedType(parent, paramName, qualifier, tree.span)
2471+ case _ => tree
2472+
2473+ /** Returns the annotated type used to represent the qualified type with the
2474+ * given components:
2475+ * `parent @qualified[parent]((paramName: parent) => qualifier)`.
2476+ */
2477+ def qualifiedType (parent : Tree , paramName : TermName , qualifier : Tree , span : Span )(using Context ): Tree =
2478+ val param = makeParameter(paramName, parent, EmptyModifiers ) // paramName: parent
2479+ val predicate = WildcardFunction (List (param), qualifier) // (paramName: parent) => qualifier
2480+ val qualifiedAnnot = scalaAnnotationDot(nme.qualified)
2481+ val annot = Apply (TypeApply (qualifiedAnnot, List (parent)), predicate).withSpan(span) // @qualified[parent](predicate)
2482+ Annotated (parent, annot).withSpan(span) // parent @qualified[parent](predicate)
2483+
24602484}
0 commit comments