Skip to content

Commit 7e7f350

Browse files
committed
Merge branch 'master' of https://github.com/lampepfl/dotty into fix_13503_attempt2
2 parents 1b66487 + ffaedb5 commit 7e7f350

File tree

61 files changed

+865
-351
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+865
-351
lines changed

compiler/src/dotty/tools/dotc/CompilationUnit.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ class CompilationUnit protected (val source: SourceFile) {
5656
*/
5757
var needsQuotePickling: Boolean = false
5858

59-
/** A structure containing a temporary map for generating inline accessors */
60-
val inlineAccessors: InlineAccessors = new InlineAccessors
61-
6259
var suspended: Boolean = false
6360
var suspendedAtInliningPhase: Boolean = false
6461

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
113113
case _ => 0
114114
}
115115

116-
/** The (last) list of arguments of an application */
117-
def arguments(tree: Tree): List[Tree] = unsplice(tree) match {
118-
case Apply(_, args) => args
119-
case TypeApply(fn, _) => arguments(fn)
120-
case Block(_, expr) => arguments(expr)
116+
/** All term arguments of an application in a single flattened list */
117+
def allArguments(tree: Tree): List[Tree] = unsplice(tree) match {
118+
case Apply(fn, args) => allArguments(fn) ::: args
119+
case TypeApply(fn, _) => allArguments(fn)
120+
case Block(_, expr) => allArguments(expr)
121121
case _ => Nil
122122
}
123123

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

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ import StdNames._
77
import dotty.tools.dotc.ast.tpd
88
import scala.util.Try
99
import util.Spans.Span
10+
import printing.{Showable, Printer}
11+
import printing.Texts.Text
12+
import annotation.internal.sharable
1013

1114
object Annotations {
1215

1316
def annotClass(tree: Tree)(using Context) =
1417
if (tree.symbol.isConstructor) tree.symbol.owner
1518
else tree.tpe.typeSymbol
1619

17-
abstract class Annotation {
20+
abstract class Annotation extends Showable {
1821
def tree(using Context): Tree
1922

2023
def symbol(using Context): Symbol = annotClass(tree)
@@ -26,7 +29,8 @@ object Annotations {
2629
def derivedAnnotation(tree: Tree)(using Context): Annotation =
2730
if (tree eq this.tree) this else Annotation(tree)
2831

29-
def arguments(using Context): List[Tree] = ast.tpd.arguments(tree)
32+
/** All arguments to this annotation in a single flat list */
33+
def arguments(using Context): List[Tree] = ast.tpd.allArguments(tree)
3034

3135
def argument(i: Int)(using Context): Option[Tree] = {
3236
val args = arguments
@@ -44,15 +48,48 @@ object Annotations {
4448
/** The tree evaluation has finished. */
4549
def isEvaluated: Boolean = true
4650

51+
/** Normally, type map over all tree nodes of this annotation, but can
52+
* be overridden. Returns EmptyAnnotation if type type map produces a range
53+
* type, since ranges cannot be types of trees.
54+
*/
55+
def mapWith(tm: TypeMap)(using Context) =
56+
val args = arguments
57+
if args.isEmpty then this
58+
else
59+
val findDiff = new TreeAccumulator[Type]:
60+
def apply(x: Type, tree: Tree)(using Context): Type =
61+
if tm.isRange(x) then x
62+
else
63+
val tp1 = tm(tree.tpe)
64+
foldOver(if tp1 =:= tree.tpe then x else tp1, tree)
65+
val diff = findDiff(NoType, args)
66+
if tm.isRange(diff) then EmptyAnnotation
67+
else if diff.exists then derivedAnnotation(tm.mapOver(tree))
68+
else this
69+
70+
/** Does this annotation refer to a parameter of `tl`? */
71+
def refersToParamOf(tl: TermLambda)(using Context): Boolean =
72+
val args = arguments
73+
if args.isEmpty then false
74+
else tree.existsSubTree {
75+
case id: Ident => id.tpe match
76+
case TermParamRef(tl1, _) => tl eq tl1
77+
case _ => false
78+
case _ => false
79+
}
80+
81+
/** A string representation of the annotation. Overridden in BodyAnnotation.
82+
*/
83+
def toText(printer: Printer): Text = printer.annotText(this)
84+
4785
def ensureCompleted(using Context): Unit = tree
4886

4987
def sameAnnotation(that: Annotation)(using Context): Boolean =
5088
symbol == that.symbol && tree.sameTree(that.tree)
5189
}
5290

53-
case class ConcreteAnnotation(t: Tree) extends Annotation {
91+
case class ConcreteAnnotation(t: Tree) extends Annotation:
5492
def tree(using Context): Tree = t
55-
}
5693

5794
abstract class LazyAnnotation extends Annotation {
5895
protected var mySym: Symbol | (Context ?=> Symbol)
@@ -98,6 +135,7 @@ object Annotations {
98135
if (tree eq this.tree) this else ConcreteBodyAnnotation(tree)
99136
override def arguments(using Context): List[Tree] = Nil
100137
override def ensureCompleted(using Context): Unit = ()
138+
override def toText(printer: Printer): Text = "@Body"
101139
}
102140

103141
class ConcreteBodyAnnotation(body: Tree) extends BodyAnnotation {
@@ -194,6 +232,8 @@ object Annotations {
194232
apply(defn.SourceFileAnnot, Literal(Constant(path)))
195233
}
196234

235+
@sharable val EmptyAnnotation = Annotation(EmptyTree)
236+
197237
def ThrowsAnnotation(cls: ClassSymbol)(using Context): Annotation = {
198238
val tref = cls.typeRef
199239
Annotation(defn.ThrowsAnnot.typeRef.appliedTo(tref), Ident(tref))

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,13 @@ object TypeOps:
164164
// with Nulls (which have no base classes). Under -Yexplicit-nulls, we take
165165
// corrective steps, so no widening is wanted.
166166
simplify(l, theMap) | simplify(r, theMap)
167-
case AnnotatedType(parent, annot)
168-
if annot.symbol == defn.UncheckedVarianceAnnot && !ctx.mode.is(Mode.Type) && !theMap.isInstanceOf[SimplifyKeepUnchecked] =>
169-
simplify(parent, theMap)
167+
case tp @ AnnotatedType(parent, annot) =>
168+
val parent1 = simplify(parent, theMap)
169+
if annot.symbol == defn.UncheckedVarianceAnnot
170+
&& !ctx.mode.is(Mode.Type)
171+
&& !theMap.isInstanceOf[SimplifyKeepUnchecked]
172+
then parent1
173+
else tp.derivedAnnotatedType(parent1, annot)
170174
case _: MatchType =>
171175
val normed = tp.tryNormalize
172176
if (normed.exists) normed else mapOver

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3601,6 +3601,9 @@ object Types {
36013601
case tp: AppliedType => tp.fold(status, compute(_, _, theAcc))
36023602
case tp: TypeVar if !tp.isInstantiated => combine(status, Provisional)
36033603
case tp: TermParamRef if tp.binder eq thisLambdaType => TrueDeps
3604+
case AnnotatedType(parent, ann) =>
3605+
if ann.refersToParamOf(thisLambdaType) then TrueDeps
3606+
else compute(status, parent, theAcc)
36043607
case _: ThisType | _: BoundType | NoPrefix => status
36053608
case _ =>
36063609
(if theAcc != null then theAcc else DepAcc()).foldOver(status, tp)
@@ -3653,8 +3656,10 @@ object Types {
36533656
if (isResultDependent) {
36543657
val dropDependencies = new ApproximatingTypeMap {
36553658
def apply(tp: Type) = tp match {
3656-
case tp @ TermParamRef(thisLambdaType, _) =>
3659+
case tp @ TermParamRef(`thisLambdaType`, _) =>
36573660
range(defn.NothingType, atVariance(1)(apply(tp.underlying)))
3661+
case AnnotatedType(parent, ann) if ann.refersToParamOf(thisLambdaType) =>
3662+
mapOver(parent)
36583663
case _ => mapOver(tp)
36593664
}
36603665
}
@@ -4104,18 +4109,17 @@ object Types {
41044109

41054110
override def underlying(using Context): Type = tycon
41064111

4107-
override def superType(using Context): Type = {
4108-
if (ctx.period != validSuper) {
4109-
cachedSuper = tycon match {
4112+
override def superType(using Context): Type =
4113+
if ctx.period != validSuper then
4114+
validSuper = if (tycon.isProvisional) Nowhere else ctx.period
4115+
cachedSuper = tycon match
41104116
case tycon: HKTypeLambda => defn.AnyType
41114117
case tycon: TypeRef if tycon.symbol.isClass => tycon
4112-
case tycon: TypeProxy => tycon.superType.applyIfParameterized(args)
4118+
case tycon: TypeProxy =>
4119+
if isMatchAlias then validSuper = Nowhere
4120+
tycon.superType.applyIfParameterized(args).normalized
41134121
case _ => defn.AnyType
4114-
}
4115-
validSuper = if (tycon.isProvisional) Nowhere else ctx.period
4116-
}
41174122
cachedSuper
4118-
}
41194123

41204124
override def translucentSuperType(using Context): Type = tycon match {
41214125
case tycon: TypeRef if tycon.symbol.isOpaqueAlias =>
@@ -5379,6 +5383,8 @@ object Types {
53795383
variance = saved
53805384
derivedLambdaType(tp)(ptypes1, this(restpe))
53815385

5386+
def isRange(tp: Type): Boolean = tp.isInstanceOf[Range]
5387+
53825388
/** Map this function over given type */
53835389
def mapOver(tp: Type): Type = {
53845390
record(s"TypeMap mapOver ${getClass}")
@@ -5422,8 +5428,9 @@ object Types {
54225428

54235429
case tp @ AnnotatedType(underlying, annot) =>
54245430
val underlying1 = this(underlying)
5425-
if (underlying1 eq underlying) tp
5426-
else derivedAnnotatedType(tp, underlying1, mapOver(annot))
5431+
val annot1 = annot.mapWith(this)
5432+
if annot1 eq EmptyAnnotation then underlying1
5433+
else derivedAnnotatedType(tp, underlying1, annot1)
54275434

54285435
case _: ThisType
54295436
| _: BoundType
@@ -5495,9 +5502,6 @@ object Types {
54955502
else newScopeWith(elems1: _*)
54965503
}
54975504

5498-
def mapOver(annot: Annotation): Annotation =
5499-
annot.derivedAnnotation(mapOver(annot.tree))
5500-
55015505
def mapOver(tree: Tree): Tree = treeTypeMap(tree)
55025506

55035507
/** Can be overridden. By default, only the prefix is mapped. */
@@ -5544,8 +5548,6 @@ object Types {
55445548

55455549
protected def emptyRange = range(defn.NothingType, defn.AnyType)
55465550

5547-
protected def isRange(tp: Type): Boolean = tp.isInstanceOf[Range]
5548-
55495551
protected def lower(tp: Type): Type = tp match {
55505552
case tp: Range => tp.lo
55515553
case _ => tp

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,10 @@ class PlainPrinter(_ctx: Context) extends Printer {
404404
case tp: ExprType =>
405405
// parameterless methods require special treatment, see #11201
406406
(if (isParameter) ": => " else ": ") ~ toTextGlobal(tp.widenExpr)
407+
case tp: PolyType =>
408+
"[" ~ paramsText(tp) ~ "]"
409+
~ (Str(": ") provided !tp.resultType.isInstanceOf[MethodType])
410+
~ toTextGlobal(tp.resultType)
407411
case tp =>
408412
": " ~ toTextGlobal(tp)
409413
}
@@ -539,7 +543,10 @@ class PlainPrinter(_ctx: Context) extends Printer {
539543
case _ => literalText(String.valueOf(const.value))
540544
}
541545

542-
def toText(annot: Annotation): Text = s"@${annot.symbol.name}" // for now
546+
/** Usual target for `Annotation#toText`, overridden in RefinedPrinter */
547+
def annotText(annot: Annotation): Text = s"@${annot.symbol.name}"
548+
549+
def toText(annot: Annotation): Text = annot.toText(this)
543550

544551
def toText(param: LambdaParam): Text =
545552
varianceSign(param.paramVariance)
@@ -570,7 +577,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
570577
Text()
571578

572579
nodeName ~ "(" ~ elems ~ tpSuffix ~ ")" ~ (Str(tree.sourcePos.toString) provided printDebug)
573-
}.close // todo: override in refined printer
580+
}.close
574581

575582
def toText(pos: SourcePosition): Text =
576583
if (!pos.exists) "<no position>"

compiler/src/dotty/tools/dotc/printing/Printer.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ abstract class Printer {
119119
/** A description of sym's location */
120120
def extendedLocationText(sym: Symbol): Text
121121

122+
/** Textual description of regular annotation in terms of its tree */
123+
def annotText(annot: Annotation): Text
124+
122125
/** Textual representation of denotation */
123126
def toText(denot: Denotation): Text
124127

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import typer.ProtoTypes._
2121
import Trees._
2222
import TypeApplications._
2323
import Decorators._
24-
import NameKinds.WildcardParamName
24+
import NameKinds.{WildcardParamName, DefaultGetterName}
2525
import util.Chars.isOperatorPart
2626
import transform.TypeUtils._
2727
import transform.SymUtils._
@@ -607,7 +607,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
607607
case tree: Template =>
608608
toTextTemplate(tree)
609609
case Annotated(arg, annot) =>
610-
toTextLocal(arg) ~~ annotText(annot)
610+
toTextLocal(arg) ~~ annotText(annot.symbol.enclosingClass, annot)
611611
case EmptyTree =>
612612
"<empty>"
613613
case TypedSplice(t) =>
@@ -964,14 +964,22 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
964964
keywordStr("package ") ~ toTextPackageId(tree.pid) ~ bodyText
965965
}
966966

967+
/** Textual representation of an instance creation expression without the leading `new` */
967968
protected def constrText(tree: untpd.Tree): Text = toTextLocal(tree).stripPrefix(keywordStr("new ")) // DD
968969

969-
protected def annotText(tree: untpd.Tree): Text = "@" ~ constrText(tree) // DD
970-
971-
override def annotsText(sym: Symbol): Text =
972-
Text(sym.annotations.map(ann =>
973-
if ann.symbol == defn.BodyAnnot then Str(simpleNameString(ann.symbol))
974-
else annotText(ann.tree)))
970+
protected def annotText(sym: Symbol, tree: untpd.Tree): Text =
971+
def recur(t: untpd.Tree): Text = t match
972+
case Apply(fn, Nil) => recur(fn)
973+
case Apply(fn, args) =>
974+
val explicitArgs = args.filterNot(_.symbol.name.is(DefaultGetterName))
975+
recur(fn) ~ "(" ~ toTextGlobal(explicitArgs, ", ") ~ ")"
976+
case TypeApply(fn, args) => recur(fn) ~ "[" ~ toTextGlobal(args, ", ") ~ "]"
977+
case Select(qual, nme.CONSTRUCTOR) => recur(qual)
978+
case New(tpt) => recur(tpt)
979+
case _ =>
980+
val annotSym = sym.orElse(tree.symbol.enclosingClass)
981+
s"@${if annotSym.exists then annotSym.name.toString else t.show}"
982+
recur(tree)
975983

976984
protected def modText(mods: untpd.Modifiers, sym: Symbol, kw: String, isType: Boolean): Text = { // DD
977985
val suppressKw = if (enclDefIsClass) mods.isAllOf(LocalParam) else mods.is(Param)
@@ -984,12 +992,16 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
984992
if (rawFlags.is(Param)) flagMask = flagMask &~ Given &~ Erased
985993
val flags = rawFlags & flagMask
986994
var flagsText = toTextFlags(sym, flags)
987-
val annotations =
988-
if (sym.exists) sym.annotations.filterNot(ann => dropAnnotForModText(ann.symbol)).map(_.tree)
989-
else mods.annotations.filterNot(tree => dropAnnotForModText(tree.symbol))
990-
Text(annotations.map(annotText), " ") ~~ flagsText ~~ (Str(kw) provided !suppressKw)
995+
val annotTexts =
996+
if sym.exists then
997+
sym.annotations.filterNot(ann => dropAnnotForModText(ann.symbol)).map(toText)
998+
else
999+
mods.annotations.filterNot(tree => dropAnnotForModText(tree.symbol)).map(annotText(NoSymbol, _))
1000+
Text(annotTexts, " ") ~~ flagsText ~~ (Str(kw) provided !suppressKw)
9911001
}
9921002

1003+
override def annotText(annot: Annotation): Text = annotText(annot.symbol, annot.tree)
1004+
9931005
def optText(name: Name)(encl: Text => Text): Text =
9941006
if (name.isEmpty) "" else encl(toText(name))
9951007

compiler/src/dotty/tools/dotc/transform/AccessProxies.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@ abstract class AccessProxies {
3131
*/
3232
protected def passReceiverAsArg(accessorName: Name)(using Context): Boolean = false
3333

34-
/** The accessor definitions that need to be added to class `cls`
35-
* As a side-effect, this method removes entries from the `accessedBy` map.
36-
* So a second call of the same method will yield the empty list.
37-
*/
34+
/** The accessor definitions that need to be added to class `cls` */
3835
private def accessorDefs(cls: Symbol)(using Context): Iterator[DefDef] =
39-
for (accessor <- cls.info.decls.iterator; accessed <- accessedBy.remove(accessor).toOption) yield
36+
for accessor <- cls.info.decls.iterator; accessed <- accessedBy.get(accessor) yield
4037
DefDef(accessor.asTerm, prefss => {
4138
def numTypeParams = accessed.info match {
4239
case info: PolyType => info.paramNames.length

compiler/src/dotty/tools/dotc/transform/Inlining.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import SymUtils._
1515
import NameKinds._
1616
import dotty.tools.dotc.ast.tpd
1717
import typer.Implicits.SearchFailureType
18+
import typer.PrepareInlineable
1819

1920
import scala.collection.mutable
2021
import dotty.tools.dotc.core.Annotations._

0 commit comments

Comments
 (0)