@@ -443,13 +443,13 @@ object desugar {
443
443
private def toDefParam (tparam : TypeDef , keepAnnotations : Boolean ): TypeDef = {
444
444
var mods = tparam.rawMods
445
445
if (! keepAnnotations) mods = mods.withAnnotations(Nil )
446
- tparam.withMods(mods & ( EmptyFlags | Sealed ) | Param )
446
+ tparam.withMods(mods & EmptyFlags | Param )
447
447
}
448
448
private def toDefParam (vparam : ValDef , keepAnnotations : Boolean , keepDefault : Boolean ): ValDef = {
449
449
var mods = vparam.rawMods
450
450
if (! keepAnnotations) mods = mods.withAnnotations(Nil )
451
451
val hasDefault = if keepDefault then HasDefault else EmptyFlags
452
- vparam.withMods(mods & (GivenOrImplicit | Erased | hasDefault) | Param )
452
+ vparam.withMods(mods & (GivenOrImplicit | Erased | hasDefault | Tracked ) | Param )
453
453
}
454
454
455
455
def mkApply (fn : Tree , paramss : List [ParamClause ])(using Context ): Tree =
@@ -535,7 +535,7 @@ object desugar {
535
535
// but not on the constructor parameters. The reverse is true for
536
536
// annotations on class _value_ parameters.
537
537
val constrTparams = impliedTparams.map(toDefParam(_, keepAnnotations = false ))
538
- val constrVparamss =
538
+ def defVparamss =
539
539
if (originalVparamss.isEmpty) { // ensure parameter list is non-empty
540
540
if (isCaseClass)
541
541
report.error(CaseClassMissingParamList (cdef), namePos)
@@ -546,6 +546,10 @@ object desugar {
546
546
ListOfNil
547
547
}
548
548
else originalVparamss.nestedMap(toDefParam(_, keepAnnotations = true , keepDefault = true ))
549
+ val constrVparamss = defVparamss
550
+ // defVparamss also needed as separate tree nodes in implicitWrappers below.
551
+ // Need to be separate because they are `watch`ed in addParamRefinements.
552
+ // See parsercombinators-givens.scala for a test case.
549
553
val derivedTparams =
550
554
constrTparams.zipWithConserve(impliedTparams)((tparam, impliedParam) =>
551
555
derivedTypeParam(tparam).withAnnotations(impliedParam.mods.annotations))
@@ -623,6 +627,11 @@ object desugar {
623
627
case _ => false
624
628
}
625
629
630
+ def isRepeated (tree : Tree ): Boolean = stripByNameType(tree) match {
631
+ case PostfixOp (_, Ident (tpnme.raw.STAR )) => true
632
+ case _ => false
633
+ }
634
+
626
635
def appliedRef (tycon : Tree , tparams : List [TypeDef ] = constrTparams, widenHK : Boolean = false ) = {
627
636
val targs = for (tparam <- tparams) yield {
628
637
val targ = refOfDef(tparam)
@@ -639,10 +648,13 @@ object desugar {
639
648
appliedTypeTree(tycon, targs)
640
649
}
641
650
642
- def isRepeated (tree : Tree ): Boolean = stripByNameType(tree) match {
643
- case PostfixOp (_, Ident (tpnme.raw.STAR )) => true
644
- case _ => false
645
- }
651
+ def addParamRefinements (core : Tree , paramss : List [List [ValDef ]]): Tree =
652
+ val refinements =
653
+ for params <- paramss; param <- params; if param.mods.is(Tracked ) yield
654
+ ValDef (param.name, SingletonTypeTree (TermRefTree ().watching(param)), EmptyTree )
655
+ .withSpan(param.span)
656
+ if refinements.isEmpty then core
657
+ else RefinedTypeTree (core, refinements).showing(i " refined result: $result" , Printers .desugar)
646
658
647
659
// a reference to the class type bound by `cdef`, with type parameters coming from the constructor
648
660
val classTypeRef = appliedRef(classTycon)
@@ -863,18 +875,17 @@ object desugar {
863
875
Nil
864
876
}
865
877
else {
866
- val defParamss = constrVparamss match {
878
+ val defParamss = defVparamss match
867
879
case Nil :: paramss =>
868
880
paramss // drop leading () that got inserted by class
869
881
// TODO: drop this once we do not silently insert empty class parameters anymore
870
882
case paramss => paramss
871
- }
872
883
val finalFlag = if ctx.settings.YcompileScala2Library .value then EmptyFlags else Final
873
884
// implicit wrapper is typechecked in same scope as constructor, so
874
885
// we can reuse the constructor parameters; no derived params are needed.
875
886
DefDef (
876
887
className.toTermName, joinParams(constrTparams, defParamss),
877
- classTypeRef, creatorExpr)
888
+ addParamRefinements( classTypeRef, defParamss) , creatorExpr)
878
889
.withMods(companionMods | mods.flags.toTermFlags & (GivenOrImplicit | Inline ) | finalFlag)
879
890
.withSpan(cdef.span) :: Nil
880
891
}
@@ -903,7 +914,9 @@ object desugar {
903
914
}
904
915
if mods.isAllOf(Given | Inline | Transparent ) then
905
916
report.error(" inline given instances cannot be trasparent" , cdef)
906
- val classMods = if mods.is(Given ) then mods &~ (Inline | Transparent ) | Synthetic else mods
917
+ var classMods = if mods.is(Given ) then mods &~ (Inline | Transparent ) | Synthetic else mods
918
+ if vparamAccessors.exists(_.mods.is(Tracked )) then
919
+ classMods |= Dependent
907
920
cpy.TypeDef (cdef : TypeDef )(
908
921
name = className,
909
922
rhs = cpy.Template (impl)(constr, parents1, clsDerived, self1,
0 commit comments