Skip to content

Commit 58cbdbc

Browse files
committed
Refactor some descriptions to be more reusable
1 parent 6896fe3 commit 58cbdbc

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ object Capabilities:
5858
trait RootCapability extends Capability:
5959
val rootId = nextRootId
6060
nextRootId += 1
61+
def descr(using Context): String
6162

6263
/** The base trait of all capabilties represented as types */
6364
trait CoreCapability extends TypeProxy, Capability:
@@ -120,6 +121,7 @@ object Capabilities:
120121
*/
121122
@sharable // We override below all operations that access internal capability state
122123
object GlobalCap extends RootCapability:
124+
def descr(using Context) = "the universal root capability"
123125
override val maybe = Maybe(this)
124126
override val readOnly = ReadOnly(this)
125127
override def reach = unsupported("cap.reach")
@@ -143,6 +145,14 @@ object Capabilities:
143145
case that: FreshCap => this eq that
144146
case _ => false
145147

148+
def descr(using Context) =
149+
val originStr = origin match
150+
case Origin.InDecl(sym) if sym.exists =>
151+
origin.explanation
152+
case _ =>
153+
i" created in ${hiddenSet.owner.sanitizedDescription}${origin.explanation}"
154+
i"a fresh root capability$originStr"
155+
146156
object FreshCap:
147157
def apply(origin: Origin)(using Context): FreshCap | GlobalCap.type =
148158
if ccConfig.useSepChecks then FreshCap(ctx.owner, origin)
@@ -225,6 +235,9 @@ object Capabilities:
225235
rcap.myOrigin = primary
226236
primary.variants += rcap
227237
rcap
238+
239+
def descr(using Context) =
240+
i"a root capability associated with the result type of $binder"
228241
end ResultCap
229242

230243
/** A trait for references in CaptureSets. These can be NamedTypes, ThisTypes or ParamRefs,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ class SymUtils:
118118

119119
def isGenericProduct(using Context): Boolean = whyNotGenericProduct.isEmpty
120120

121+
def sanitizedDescription(using Context): String =
122+
if self.isConstructor then
123+
i"constructor of ${self.owner.sanitizedDescription}"
124+
else if self.isAnonymousFunction then
125+
i"anonymous function of type ${self.info}"
126+
else if self.name.toString.contains('$') then
127+
self.owner.sanitizedDescription
128+
else
129+
self.show
130+
121131
/** Is this an old style implicit conversion?
122132
* @param directOnly only consider explicitly written methods
123133
* @param forImplicitClassOnly only consider methods generated from implicit classes

compiler/src/dotty/tools/dotc/reporting/Message.scala

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ object Message:
5353
case None => false
5454
end Disambiguation
5555

56-
private type Recorded = Symbol | ParamRef | SkolemType | Capability
56+
private type Recorded = Symbol | ParamRef | SkolemType | RootCapability
5757

5858
private case class SeenKey(str: String, isType: Boolean)
5959

@@ -183,31 +183,11 @@ object Message:
183183
s"is a ${ctx.printer.kindString(sym)}${sym.showExtendedLocation}${addendum("bounds", info)}"
184184
case tp: SkolemType =>
185185
s"is an unknown value of type ${tp.widen.show}"
186-
case ref: Capability =>
186+
case ref: RootCapability =>
187187
val relation =
188188
if List("^", "=>", "?=>").exists(key.startsWith) then "refers to"
189189
else "is"
190-
def ownerStr(owner: Symbol): String =
191-
if owner.isConstructor then
192-
i"constructor of ${ownerStr(owner.owner)}"
193-
else if owner.isAnonymousFunction then
194-
i"anonymous function of type ${owner.info}"
195-
else if owner.name.toString.contains('$') then
196-
ownerStr(owner.owner)
197-
else
198-
owner.show
199-
val descr =
200-
ref match
201-
case GlobalCap => "the universal root capability"
202-
case ref: FreshCap =>
203-
val descr = ref.origin match
204-
case origin @ Origin.InDecl(sym) if sym.exists =>
205-
origin.explanation
206-
case origin =>
207-
i" created in ${ownerStr(ref.hiddenSet.owner)}${origin.explanation}"
208-
i"a fresh root capability$descr"
209-
case ResultCap(binder) => i"a root capability associated with the result type of $binder"
210-
s"$relation $descr"
190+
s"$relation ${ref.descr}"
211191
end explanation
212192

213193
/** Produce a where clause with explanations for recorded iterms.
@@ -274,14 +254,16 @@ object Message:
274254
override def toTextCapturing(parent: Type, refs: GeneralCaptureSet, boxText: Text) = refs match
275255
case refs: CaptureSet
276256
if isUniversalCaptureSet(refs) && !defn.isFunctionType(parent) && !printDebug && seen.isActive =>
277-
boxText ~ toTextLocal(parent) ~ seen.record("^", isType = true, refs.elems.nth(0))
257+
boxText
258+
~ toTextLocal(parent)
259+
~ seen.record("^", isType = true, refs.elems.nth(0).asInstanceOf[RootCapability])
278260
case _ =>
279261
super.toTextCapturing(parent, refs, boxText)
280262

281263
override def funMiddleText(isContextual: Boolean, isPure: Boolean, refs: GeneralCaptureSet | Null): Text =
282264
refs match
283265
case refs: CaptureSet if isUniversalCaptureSet(refs) && seen.isActive =>
284-
seen.record(arrow(isContextual, isPure = false), isType = true, refs.elems.nth(0))
266+
seen.record(arrow(isContextual, isPure = false), isType = true, refs.elems.nth(0).asInstanceOf[RootCapability])
285267
case _ =>
286268
super.funMiddleText(isContextual, isPure, refs)
287269

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import language.experimental.captureChecking
2+
trait Ref
3+
def swap(x1: Ref^, x2: Ref^): Unit = ()
4+
def foo(a: Ref^)[X](op: (z: Ref^) -> X^{z}): X^{a} = op(a)
5+
def test1(a: Ref^): Unit =
6+
def bad(x: Ref^)(y: Ref^{a}): Unit = swap(x, y)
7+
val t1 = bad
8+
def t2[X] = foo(a)[X]
9+
val t3 = t2[(y: Ref^{a}) -> Unit](t1)
10+
t3(a) // boom

0 commit comments

Comments
 (0)