Skip to content

Commit 4f28d8a

Browse files
committed
Address review comments
1 parent b2f0791 commit 4f28d8a

File tree

10 files changed

+46
-49
lines changed

10 files changed

+46
-49
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ object desugar {
234234
* of the same method.
235235
* @param evidenceFlags The flags to use for evidence definitions
236236
* @param freshName A function to generate fresh names for evidence definitions
237-
* @param allParams If `tdef` is a type paramter, all parameters of the owning method,
237+
* @param allParamss If `tdef` is a type paramter, all parameters of the owning method,
238238
* otherwise the empty list.
239239
*/
240240
private def desugarContextBounds(
@@ -246,29 +246,31 @@ object desugar {
246246

247247
val evidenceNames = mutable.ListBuffer[TermName]()
248248

249-
def desugarRhs(rhs: Tree): Tree = rhs match
250-
case ContextBounds(tbounds, cxbounds) =>
249+
def desugarRHS(rhs: Tree): Tree = rhs match
250+
case ContextBounds(tbounds, ctxbounds) =>
251251
val isMember = evidenceFlags.isAllOf(DeferredGivenFlags)
252-
for bound <- cxbounds do
252+
for bound <- ctxbounds do
253253
val evidenceName = bound match
254254
case ContextBoundTypeTree(_, _, ownName) if !ownName.isEmpty =>
255255
ownName // if there is an explicitly given name, use it.
256-
case _ if Config.nameSingleContextBounds && !isMember
257-
&& cxbounds.tail.isEmpty && Feature.enabled(Feature.modularity) =>
258-
tdef.name.toTermName
259256
case _ =>
260-
freshName(bound)
257+
if Config.nameSingleContextBounds
258+
&& !isMember
259+
&& ctxbounds.tail.isEmpty
260+
&& Feature.enabled(Feature.modularity)
261+
then tdef.name.toTermName
262+
else freshName(bound)
261263
evidenceNames += evidenceName
262264
val evidenceParam = ValDef(evidenceName, bound, EmptyTree).withFlags(evidenceFlags)
263265
evidenceParam.pushAttachment(ContextBoundParam, ())
264266
evidenceBuf += evidenceParam
265267
tbounds
266268
case LambdaTypeTree(tparams, body) =>
267-
cpy.LambdaTypeTree(rhs)(tparams, desugarRhs(body))
269+
cpy.LambdaTypeTree(rhs)(tparams, desugarRHS(body))
268270
case _ =>
269271
rhs
270272

271-
val tdef1 = cpy.TypeDef(tdef)(rhs = desugarRhs(tdef.rhs))
273+
val tdef1 = cpy.TypeDef(tdef)(rhs = desugarRHS(tdef.rhs))
272274
// Under x.modularity, if there was a context bound, and `tdef`s name as a term name is
273275
// neither a name of an existing parameter nor a name of generated evidence for
274276
// the same method, add a WitnessAnnotation with all generated evidence names to `tdef`.
@@ -695,10 +697,10 @@ object desugar {
695697
case _ => false
696698
}
697699

698-
def isRepeated(tree: Tree): Boolean = stripByNameType(tree) match {
700+
/** Is this a repeated argument x* (using a spread operator)? */
701+
def isRepeated(tree: Tree): Boolean = stripByNameType(tree) match
699702
case PostfixOp(_, Ident(tpnme.raw.STAR)) => true
700703
case _ => false
701-
}
702704

703705
def appliedRef(tycon: Tree, tparams: List[TypeDef] = constrTparams, widenHK: Boolean = false) = {
704706
val targs = for (tparam <- tparams) yield {
@@ -1218,7 +1220,7 @@ object desugar {
12181220

12191221
/** Extract a synthesized given name from a type tree. This is used for
12201222
* both anonymous givens and (under x.modularity) deferred givens.
1221-
* @param followArgs If true include argument types in the name
1223+
* @param followArgs if true include argument types in the name
12221224
*/
12231225
private class NameExtractor(followArgs: Boolean) extends UntypedTreeAccumulator[String] {
12241226
private def extractArgs(args: List[Tree])(using Context): String =

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -394,22 +394,16 @@ trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] =>
394394
))
395395

396396
def unapply(tree: Tree)(using Context): Option[List[TermName]] =
397-
def isWitnessNames(tp: Type) = tp match
398-
case tp: TypeRef =>
399-
tp.name == tpnme.WitnessNames && tp.symbol == defn.WitnessNamesAnnot
400-
case _ =>
401-
false
402397
unsplice(tree) match
403-
case Apply(
404-
Select(New(tpt: tpd.TypeTree), nme.CONSTRUCTOR),
405-
SeqLiteral(elems, _) :: Nil
406-
) if isWitnessNames(tpt.tpe) =>
407-
Some:
408-
elems.map:
409-
case Literal(Constant(str: String)) =>
410-
ContextBoundParamName.unmangle(str.toTermName.asSimpleName)
411-
case _ =>
412-
None
398+
case Apply(Select(New(tpt: tpd.TypeTree), nme.CONSTRUCTOR), SeqLiteral(elems, _) :: Nil) =>
399+
tpt.tpe match
400+
case tp: TypeRef if tp.name == tpnme.WitnessNames && tp.symbol == defn.WitnessNamesAnnot =>
401+
Some:
402+
elems.map:
403+
case Literal(Constant(str: String)) =>
404+
ContextBoundParamName.unmangle(str.toTermName.asSimpleName)
405+
case _ => None
406+
case _ => None
413407
end WitnessNamesAnnot
414408
}
415409

compiler/src/dotty/tools/dotc/core/Flags.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ object Flags {
573573
val DeferredOrLazyOrMethod: FlagSet = Deferred | Lazy | Method
574574
val DeferredOrTermParamOrAccessor: FlagSet = Deferred | ParamAccessor | TermParam // term symbols without right-hand sides
575575
val DeferredOrTypeParam: FlagSet = Deferred | TypeParam // type symbols without right-hand sides
576-
val DeferredGivenFlags = Deferred | Given | HasDefault
576+
val DeferredGivenFlags: FlagSet = Deferred | Given | HasDefault
577577
val EnumValue: FlagSet = Enum | StableRealizable // A Scala enum value
578578
val FinalOrInline: FlagSet = Final | Inline
579579
val FinalOrModuleClass: FlagSet = Final | ModuleClass // A module class or a final class

compiler/src/dotty/tools/dotc/core/NamerOps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ object NamerOps:
262262
* bound that defines a set of witnesses with names `witnessNames`.
263263
*
264264
* @param params If `tsym` is a type parameter, a list of parameter symbols
265-
* that include all witnesses, otherwise the empty list.
265+
* that includes all witnesses, otherwise the empty list.
266266
*
267267
* The context-bound companion has as name the name of `tsym` translated to
268268
* a term name. We create a synthetic val of the form

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,7 @@ object Types extends TypeUtils {
16551655
*
16561656
* P { ... type T = / += / -= U ... } # T
16571657
*
1658-
* to just U. Analogously, `P { val x: S} # x` is reduced tp `S` if `S`
1658+
* to just U. Analogously, `P { val x: S} # x` is reduced to `S` if `S`
16591659
* is a singleton type.
16601660
*
16611661
* Does not perform the reduction if the resulting type would contain
@@ -5050,14 +5050,14 @@ object Types extends TypeUtils {
50505050
* or if it has as an upper bound a precise TypeVar.
50515051
*/
50525052
def isPrecise(using Context) =
5053-
precise
5054-
|| {
5055-
val constr = ctx.typerState.constraint
5056-
constr.upper(origin).exists: tparam =>
5057-
constr.typeVarOfParam(tparam) match
5058-
case tvar: TypeVar => tvar.precise
5059-
case _ => false
5060-
}
5053+
precise || hasPreciseUpperBound
5054+
5055+
private def hasPreciseUpperBound(using Context) =
5056+
val constr = ctx.typerState.constraint
5057+
constr.upper(origin).exists: tparam =>
5058+
constr.typeVarOfParam(tparam) match
5059+
case tvar: TypeVar => tvar.precise
5060+
case _ => false
50615061

50625062
/** The policy used for widening singletons or unions when instantiating
50635063
* this variable in the current context.

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3552,9 +3552,10 @@ object Parsers {
35523552
!impliedMods.is(Given)
35533553
|| startParamTokens.contains(in.token)
35543554
|| isIdent
3555-
&& (in.name == nme.inline
3556-
|| in.name == nme.tracked && in.featureEnabled(Feature.modularity)
3557-
|| in.lookahead.isColon)
3555+
&& (in.name == nme.inline // inline starts a name binding
3556+
|| in.name == nme.tracked // tracked starts a name binding under x.modularity
3557+
&& in.featureEnabled(Feature.modularity)
3558+
|| in.lookahead.isColon) // a following `:` starts a name binding
35583559
(mods, paramsAreNamed)
35593560
val params =
35603561
if paramsAreNamed then commaSeparated(() => param())

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,7 @@ class Namer { typer: Typer =>
18601860
for case WitnessNamesAnnot(ws) <- tdef.mods.annotations do
18611861
witnessNamesOfParam(tdef) = ws
18621862

1863-
/** Is each name in `wnames` defined spmewhere in the longest prefix of all `params`
1863+
/** Is each name in `wnames` defined somewhere in the longest prefix of all `params`
18641864
* that have been typed ahead (i.e. that carry the TypedAhead attachment)?
18651865
*/
18661866
def allParamsSeen(wnames: List[TermName], params: List[MemberDef]) =
@@ -1919,7 +1919,7 @@ class Namer { typer: Typer =>
19191919
&& param.hasAttachment(ContextBoundParam)
19201920
&& sym.info.memberNames(abstractTypeNameFilter).nonEmpty
19211921

1922-
/** Set every context bound evidence parameter of a class to be tracked,
1922+
/** Under x.modularity, set every context bound evidence parameter of a class to be tracked,
19231923
* provided it has a type that has an abstract type member. Reset private and local flags
19241924
* so that the parameter becomes a `val`.
19251925
*/

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
888888
case (tp1: TermRef, tp2: TermRef) =>
889889
if tp1.info.isSingleton && (tp1 frozen_=:= tp2) then 1
890890
else compare(tp1, tp2, preferGeneral = false)
891-
case (tp1: TermRef, _) => 1 // should not happen, but prefer TermRefs over othersver others
891+
case (tp1: TermRef, _) => 1 // should not happen, but prefer TermRefs over others
892892
case (_, tp2: TermRef) => -1
893893
case _ => 0
894894

@@ -4588,7 +4588,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
45884588
cpy.Ident(qual)(qual.symbol.name.sourceModuleName.toTypeName)
45894589
case _ =>
45904590
errorTree(tree, em"cannot convert from $tree to an instance creation expression")
4591-
val tycon = ctorResultType.underlyingClassRef(refinementOK = true)
4591+
val tycon = ctorResultType.underlyingClassRef(refinementOK = Feature.enabled(modularity))
45924592
typed(
45934593
untpd.Select(
45944594
untpd.New(untpd.TypedSplice(tpt.withType(tycon))),

docs/_docs/internals/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ MatchType ::= InfixType `match` <<< TypeCaseClauses >>>
191191
InfixType ::= RefinedType {id [nl] RefinedType} InfixOp(t1, op, t2)
192192
RefinedType ::= AnnotType {[nl] Refinement} RefinedTypeTree(t, ds)
193193
AnnotType ::= SimpleType {Annotation} Annotated(t, annot)
194-
AnnotType1 ::= SimpleType1 {Annotation} Annotated(t, annot)
194+
AnnotType1 ::= SimpleType1 {Annotation} Annotated(t, annot)
195195
196196
SimpleType ::= SimpleLiteral SingletonTypeTree(l)
197197
| ‘?’ TypeBounds

library/src/scala/annotation/internal/WitnessNames.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ package internal
1111
*
1212
* 2. During Namer or Unpickling, when encountering a type declaration A with
1313
* a WitnessNames(n_1, ... , n_k) annotation, create a CB companion `val A` with
14-
* rtype `<context-bound-companion>`[ref_1 | ... | ref_k] where ref_i is a TermRef
14+
* type `<context-bound-companion>`[ref_1 | ... | ref_k] where ref_i is a TermRef
1515
* with the same prefix as A and name n_i. Except, don't do this if the type in
1616
* question is a type parameter and there is already a term parameter with name A
1717
* defined for the same method.
@@ -20,7 +20,7 @@ package internal
2020
*
2121
* type `<context-bound-companion>`[-Refs]
2222
*
23-
* The context bound companion's variance is negative, so that unons in the
23+
* The context bound companion's variance is negative, so that unions in the
2424
* arguments are joined when encountering multiple definfitions and forming a glb.
2525
*
2626
* 3. Add a special case for typing a selection A.m on a value A of type

0 commit comments

Comments
 (0)