Skip to content

Commit 8bab2ac

Browse files
committed
Polishings in dotc, core, ast, parsing
1 parent 9f2a801 commit 8bab2ac

21 files changed

+158
-89
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,8 @@ object desugar {
18101810
case ext: ExtMethods =>
18111811
Block(List(ext), Literal(Constant(())).withSpan(ext.span))
18121812
case CapturingTypeTree(refs, parent) =>
1813+
// convert `{refs} T` to `T @retains refs`
1814+
// `{refs}-> T` to `-> (T @retainsByName refs)`
18131815
def annotate(annotName: TypeName, tp: Tree) =
18141816
Annotated(tp, New(scalaDot(annotName), List(refs)))
18151817
parent match

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,11 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
195195
case arg => arg.typeOpt.widen.isRepeatedParam
196196
}
197197

198+
/** Is tree a type tree of the form `=> T` or (under -Ycc) `{refs}-> T`? */
198199
def isByNameType(tree: Tree)(using Context): Boolean =
199200
stripByNameType(tree) ne tree
200201

202+
/** Strip `=> T` to `T` and (under -Ycc) `{refs}-> T` to `T` */
201203
def stripByNameType(tree: Tree)(using Context): Tree = unsplice(tree) match
202204
case ByNameTypeTree(t1) => t1
203205
case untpd.CapturingTypeTree(_, parent) =>
@@ -398,10 +400,16 @@ trait UntypedTreeInfo extends TreeInfo[Untyped] { self: Trees.Instance[Untyped]
398400
}
399401
}
400402

403+
/** Under -Ycc: A builder and extractor for `=> T`, which is an alias for `{*}-> T`.
404+
* Only trees of the form `=> T` are matched; trees written directly as `{*}-> T`
405+
* are ignored by the extractor.
406+
*/
401407
object ImpureByNameTypeTree:
408+
402409
def apply(tp: ByNameTypeTree)(using Context): untpd.CapturingTypeTree =
403410
untpd.CapturingTypeTree(
404411
Ident(nme.CAPTURE_ROOT).withSpan(tp.span.startPos) :: Nil, tp)
412+
405413
def unapply(tp: Tree)(using Context): Option[ByNameTypeTree] = tp match
406414
case untpd.CapturingTypeTree(id @ Ident(nme.CAPTURE_ROOT) :: Nil, bntp: ByNameTypeTree)
407415
if id.span == bntp.span.startPos => Some(bntp)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
165165
ta.assignType(untpd.Inlined(call, bindings, expansion), bindings, expansion)
166166

167167
def TypeTree(tp: Type, inferred: Boolean = false)(using Context): TypeTree =
168-
(if inferred then new InferredTypeTree() else untpd.TypeTree()).withType(tp)
168+
(if inferred then untpd.InferredTypeTree() else untpd.TypeTree()).withType(tp)
169169

170170
def SingletonTypeTree(ref: Tree)(using Context): SingletonTypeTree =
171171
ta.assignType(untpd.SingletonTypeTree(ref), ref)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
217217

218218
case class Infix()(implicit @constructorOnly src: SourceFile) extends Mod(Flags.Infix)
219219

220+
/** Used under -Ycc to mark impure function types `A => B` in `FunctionWithMods` */
220221
case class Impure()(implicit @constructorOnly src: SourceFile) extends Mod(Flags.Impure)
221222
}
222223

@@ -395,6 +396,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
395396
def JavaSeqLiteral(elems: List[Tree], elemtpt: Tree)(implicit src: SourceFile): JavaSeqLiteral = new JavaSeqLiteral(elems, elemtpt)
396397
def Inlined(call: tpd.Tree, bindings: List[MemberDef], expansion: Tree)(implicit src: SourceFile): Inlined = new Inlined(call, bindings, expansion)
397398
def TypeTree()(implicit src: SourceFile): TypeTree = new TypeTree()
399+
def InferredTypeTree()(implicit src: SourceFile): TypeTree = new InferredTypeTree()
398400
def SingletonTypeTree(ref: Tree)(implicit src: SourceFile): SingletonTypeTree = new SingletonTypeTree(ref)
399401
def RefinedTypeTree(tpt: Tree, refinements: List[Tree])(implicit src: SourceFile): RefinedTypeTree = new RefinedTypeTree(tpt, refinements)
400402
def AppliedTypeTree(tpt: Tree, args: List[Tree])(implicit src: SourceFile): AppliedTypeTree = new AppliedTypeTree(tpt, args)

compiler/src/dotty/tools/dotc/cc/CaptureOps.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ extension (tp: Type)
5151
case None => ann.tree.putAttachment(BoxedType, BoxedTypeCache())
5252
case _ =>
5353
ann.tree.attachment(BoxedType)(tp)
54+
case tp: RealTypeBounds =>
55+
tp.derivedTypeBounds(tp.lo.boxed, tp.hi.boxed)
5456
case _ =>
5557
tp
5658

@@ -84,7 +86,7 @@ extension (tp: Type)
8486

8587
/** Under -Ycc, map regular function type to impure function type
8688
*/
87-
def adaptFunctionType(using Context): Type = tp match
89+
def adaptFunctionTypeUnderCC(using Context): Type = tp match
8890
case AppliedType(fn, args)
8991
if ctx.settings.Ycc.value && defn.isFunctionClass(fn.typeSymbol) =>
9092
val fname = fn.typeSymbol.name

compiler/src/dotty/tools/dotc/cc/CaptureSet.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,12 @@ object CaptureSet:
575575
def mapRefs(xs: Refs, tm: TypeMap, variance: Int)(using Context): CaptureSet =
576576
mapRefs(xs, extrapolateCaptureRef(_, tm, variance))
577577

578+
/** Return true iff
579+
* - arg1 is a TypeBounds >: CL T <: CH T of two capturing types with equal parents.
580+
* - arg2 is a capturing type CA U
581+
* - CH <: CA <: CL
582+
* In other words, we can unify CL, CH and CA.
583+
*/
578584
def subCapturesRange(arg1: TypeBounds, arg2: Type)(using Context): Boolean = arg1 match
579585
case TypeBounds(CapturingType(lo, loRefs), CapturingType(hi, hiRefs)) if lo =:= hi =>
580586
given VarState = VarState()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ object Flags {
314314
/** A Scala 2x super accessor / an unpickled Scala 2.x class */
315315
val (SuperParamAliasOrScala2x @ _, SuperParamAlias @ _, Scala2x @ _) = newFlags(26, "<super-param-alias>", "<scala-2.x>")
316316

317-
/** A parameter with a default value / an impure untpd.Function type */
317+
/** A parameter with a default value / an impure untpd.FunctionWithMods type */
318318
val (_, HasDefault @ _, Impure @ _) = newFlags(27, "<hasdefault>", "<{*}>")
319319

320320
/** An extension method, or a collective extension instance */
@@ -405,7 +405,7 @@ object Flags {
405405
val (_, _, ChildrenQueried @ _) = newFlags(56, "<children-queried>")
406406

407407
/** A module variable (Scala 2.x only)
408-
* (re-used as a capture checking flag in CheckCaptures)
408+
* (re-used as a flag for private parameter accessors in Recheck)
409409
*/
410410
val (_, Scala2ModuleVar @ _, _) = newFlags(57, "<modulevar>")
411411

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,23 +192,31 @@ object NamerOps:
192192
modcls.registeredCompanion = cls
193193

194194
/** For secondary constructors, make it known in the context that their type parameters
195-
* are aliases of the class type parameters. This is done by (ab?)-using GADT constraints.
196-
* See pos/i941.scala
195+
* are aliases of the class type parameters.
196+
* @return if `sym` is a secondary constructor, a fresh context that
197+
* contains GADT constraints linking the type parameters.
197198
*/
198199
def linkConstructorParams(sym: Symbol)(using Context): Context =
199200
if sym.isConstructor && !sym.isPrimaryConstructor then
200201
sym.rawParamss match
201202
case (tparams @ (tparam :: _)) :: _ if tparam.isType =>
202203
val rhsCtx = ctx.fresh.setFreshGADTBounds
203-
rhsCtx.gadt.addToConstraint(tparams)
204-
tparams.lazyZip(sym.owner.typeParams).foreach { (psym, tparam) =>
205-
val tr = tparam.typeRef
206-
rhsCtx.gadt.addBound(psym, tr, isUpper = false)
207-
rhsCtx.gadt.addBound(psym, tr, isUpper = true)
208-
}
204+
linkConstructorParams(sym, tparams, rhsCtx)
209205
rhsCtx
210206
case _ =>
211207
ctx
212208
else ctx
213209

210+
/** For secondary constructor `sym`, make it known in the given context `rhsCtx`
211+
* that their type parameters are aliases of the class type parameters. This is done
212+
* by (ab?)-using GADT constraints. See pos/i941.scala.
213+
*/
214+
def linkConstructorParams(sym: Symbol, tparams: List[Symbol], rhsCtx: Context)(using Context): Unit =
215+
rhsCtx.gadt.addToConstraint(tparams)
216+
tparams.lazyZip(sym.owner.typeParams).foreach { (psym, tparam) =>
217+
val tr = tparam.typeRef
218+
rhsCtx.gadt.addBound(psym, tr, isUpper = false)
219+
rhsCtx.gadt.addBound(psym, tr, isUpper = true)
220+
}
221+
214222
end NamerOps

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ object StdNames {
282282
// ----- Term names -----------------------------------------
283283

284284
// Compiler-internal
285-
val ANYname: N = "<anyname>"
286285
val CAPTURE_ROOT: N = "*"
287286
val CONSTRUCTOR: N = "<init>"
288287
val STATIC_CONSTRUCTOR: N = "<clinit>"
@@ -350,7 +349,6 @@ object StdNames {
350349
val AppliedTypeTree: N = "AppliedTypeTree"
351350
val ArrayAnnotArg: N = "ArrayAnnotArg"
352351
val CAP: N = "CAP"
353-
val ClassManifestFactory: N = "ClassManifestFactory"
354352
val Constant: N = "Constant"
355353
val ConstantType: N = "ConstantType"
356354
val Eql: N = "Eql"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ object Substituters:
182182

183183
final class SubstSymMap(from: List[Symbol], to: List[Symbol])(using Context) extends DeepTypeMap, BiTypeMap {
184184
def apply(tp: Type): Type = substSym(tp, from, to, this)(using mapCtx)
185-
def inverse(tp: Type) = tp.substSym(to, from)
185+
def inverse(tp: Type) = tp.substSym(to, from) // implicitly requires that `to` contains no duplicates.
186186
}
187187

188188
final class SubstThisMap(from: ClassSymbol, to: Type)(using Context) extends DeepTypeMap {

0 commit comments

Comments
 (0)