Skip to content

Commit d977e78

Browse files
oderskyBlaisorblade
authored andcommitted
MergeError refactorings
1 parent 4828fe5 commit d977e78

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

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

+12-32
Original file line numberDiff line numberDiff line change
@@ -332,21 +332,8 @@ object Denotations {
332332
}
333333

334334
/** Handle merge conflict by throwing a `MergeError` exception */
335-
private def mergeConflict(tp1: Type, tp2: Type, that: Denotation)(implicit ctx: Context): Type = {
336-
def showSymbol(sym: Symbol): String = if (sym.exists) sym.showLocated else "[unknown]"
337-
def showType(tp: Type) = tp match {
338-
case ClassInfo(_, cls, _, _, _) => cls.showLocated
339-
case bounds: TypeBounds => i"type bounds $bounds"
340-
case _ => tp.show
341-
}
342-
val msg =
343-
s"""cannot merge
344-
| ${showSymbol(this.symbol)} of type ${showType(tp1)} and
345-
| ${showSymbol(that.symbol)} of type ${showType(tp2)}
346-
"""
347-
if (true) throw new MergeError(msg, tp1, tp2)
348-
else throw new Error(msg) // flip condition for debugging
349-
}
335+
private def mergeConflict(tp1: Type, tp2: Type, that: Denotation)(implicit ctx: Context): Type =
336+
throw new MergeError(this.symbol, that.symbol, tp1, tp2, NoPrefix)
350337

351338
/** Merge parameter names of lambda types. If names in corresponding positions match, keep them,
352339
* otherwise generate new synthetic names.
@@ -537,8 +524,7 @@ object Denotations {
537524
else if (pre.widen.classSymbol.is(Scala2x) || ctx.scala2Mode)
538525
info1 // follow Scala2 linearization -
539526
// compare with way merge is performed in SymDenotation#computeMembersNamed
540-
else
541-
throw new MergeError(s"${ex.getMessage} as members of type ${pre.show}", ex.tp1, ex.tp2)
527+
else throw new MergeError(ex.sym1, ex.sym2, ex.tp1, ex.tp2, pre)
542528
}
543529
new JointRefDenotation(sym, jointInfo, denot1.validFor & denot2.validFor)
544530
}
@@ -1136,21 +1122,15 @@ object Denotations {
11361122
def doubleDefError(denot1: Denotation, denot2: Denotation, pre: Type = NoPrefix)(implicit ctx: Context): Nothing = {
11371123
val sym1 = denot1.symbol
11381124
val sym2 = denot2.symbol
1139-
def fromWhere = if (pre == NoPrefix) "" else i"\nwhen seen as members of $pre"
1140-
val msg =
1141-
if (denot1.isTerm)
1142-
i"""cannot merge
1143-
| $sym1: ${sym1.info} and
1144-
| $sym2: ${sym2.info};
1145-
|they are both defined in ${sym1.owner} but have matching signatures
1146-
| ${denot1.info} and
1147-
| ${denot2.info}$fromWhere"""
1148-
else
1149-
i"""cannot merge
1150-
| $sym1 ${denot1.info}
1151-
| $sym2 ${denot2.info}
1152-
|they are conflicting definitions$fromWhere"""
1153-
throw new MergeError(msg, denot2.info, denot2.info)
1125+
if (denot1.isTerm)
1126+
throw new MergeError(sym1, sym2, sym1.info, sym2.info, pre) {
1127+
override def addendum(implicit ctx: Context) =
1128+
i"""
1129+
|they are both defined in ${sym1.owner} but have matching signatures
1130+
| ${denot1.info} and
1131+
| ${denot2.info}${super.addendum}"""
1132+
}
1133+
else throw new MergeError(sym1, sym2, denot1.info, denot2.info, pre)
11541134
}
11551135

11561136
// --- Overloaded denotations and predenotations -------------------------------------------------

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,24 @@ object CyclicReference {
123123
}
124124
}
125125

126-
class MergeError(msg: String, val tp1: Type, val tp2: Type) extends TypeError(msg)
126+
class MergeError(val sym1: Symbol, val sym2: Symbol, val tp1: Type, val tp2: Type, prefix: Type) extends TypeError {
127+
128+
private def showSymbol(sym: Symbol)(implicit ctx: Context): String =
129+
if (sym.exists) sym.showLocated else "[unknown]"
130+
131+
private def showType(tp: Type)(implicit ctx: Context) = tp match {
132+
case ClassInfo(_, cls, _, _, _) => cls.showLocated
133+
case _ => tp.show
134+
}
135+
136+
protected def addendum(implicit ctx: Context) =
137+
if (prefix `eq` NoPrefix) "" else i"\nas members of type $prefix"
138+
139+
override def toMessage(implicit ctx: Context): Message = {
140+
if (ctx.debug) printStackTrace()
141+
i"""cannot merge
142+
| ${showSymbol(sym1)} of type ${showType(tp1)} and
143+
| ${showSymbol(sym2)} of type ${showType(tp2)}$addendum
144+
"""
145+
}
146+
}

0 commit comments

Comments
 (0)