Skip to content

Commit 1f52e1e

Browse files
committed
Better disambiguation to avoid MergeErrors
If there's a MergeError we now disambiguate following symbol preference. This is needed to make new collections compile. Also, better error messages for those cases where a MergeError is still emitted.
1 parent 1211fc0 commit 1f52e1e

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -332,14 +332,20 @@ object Denotations {
332332
}
333333

334334
/** Handle merge conflict by throwing a `MergeError` exception */
335-
private def mergeConflict(tp1: Type, tp2: Type)(implicit ctx: Context): Type = {
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]"
336337
def showType(tp: Type) = tp match {
337338
case ClassInfo(_, cls, _, _, _) => cls.showLocated
338339
case bounds: TypeBounds => i"type bounds $bounds"
339340
case _ => tp.show
340341
}
341-
if (true) throw new MergeError(s"cannot merge ${showType(tp1)} with ${showType(tp2)}", tp1, tp2)
342-
else throw new Error(s"cannot merge ${showType(tp1)} with ${showType(tp2)}") // flip condition for debugging
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
343349
}
344350

345351
/** Merge parameter names of lambda types. If names in corresponding positions match, keep them,
@@ -389,13 +395,13 @@ object Denotations {
389395
tp2 match {
390396
case tp2: TypeBounds => if (safeIntersection) tp1 safe_& tp2 else tp1 & tp2
391397
case tp2: ClassInfo if tp1 contains tp2 => tp2
392-
case _ => mergeConflict(tp1, tp2)
398+
case _ => mergeConflict(tp1, tp2, that)
393399
}
394400
case tp1: ClassInfo =>
395401
tp2 match {
396402
case tp2: ClassInfo if tp1.cls eq tp2.cls => tp1.derivedClassInfo(tp1.prefix & tp2.prefix)
397403
case tp2: TypeBounds if tp2 contains tp1 => tp1
398-
case _ => mergeConflict(tp1, tp2)
404+
case _ => mergeConflict(tp1, tp2, that)
399405
}
400406
case tp1: MethodOrPoly =>
401407
tp2 match {
@@ -420,9 +426,9 @@ object Denotations {
420426
tp1.derivedLambdaType(
421427
mergeParamNames(tp1, tp2), tp1.paramInfos,
422428
infoMeet(tp1.resultType, tp2.resultType.subst(tp2, tp1)))
423-
else mergeConflict(tp1, tp2)
429+
else mergeConflict(tp1, tp2, that)
424430
case _ =>
425-
mergeConflict(tp1, tp2)
431+
mergeConflict(tp1, tp2, that)
426432
}
427433
case _ =>
428434
tp1 & tp2
@@ -523,7 +529,9 @@ object Denotations {
523529
try infoMeet(info1, info2)
524530
catch {
525531
case ex: MergeError =>
526-
if (pre.widen.classSymbol.is(Scala2x) || ctx.scala2Mode)
532+
if (preferSym(sym2, sym1)) info2
533+
else if (preferSym(sym1, sym2)) info1
534+
else if (pre.widen.classSymbol.is(Scala2x) || ctx.scala2Mode)
527535
info1 // follow Scala2 linearization -
528536
// compare with way merge is performed in SymDenotation#computeMembersNamed
529537
else
@@ -560,13 +568,13 @@ object Denotations {
560568
tp2 match {
561569
case tp2: TypeBounds => tp1 | tp2
562570
case tp2: ClassInfo if tp1 contains tp2 => tp1
563-
case _ => mergeConflict(tp1, tp2)
571+
case _ => mergeConflict(tp1, tp2, that)
564572
}
565573
case tp1: ClassInfo =>
566574
tp2 match {
567575
case tp2: ClassInfo if tp1.cls eq tp2.cls => tp1.derivedClassInfo(tp1.prefix | tp2.prefix)
568576
case tp2: TypeBounds if tp2 contains tp1 => tp2
569-
case _ => mergeConflict(tp1, tp2)
577+
case _ => mergeConflict(tp1, tp2, that)
570578
}
571579
case tp1: MethodOrPoly =>
572580
tp2 match {
@@ -577,7 +585,7 @@ object Denotations {
577585
mergeParamNames(tp1, tp2), tp1.paramInfos,
578586
tp1.resultType | tp2.resultType.subst(tp2, tp1))
579587
case _ =>
580-
mergeConflict(tp1, tp2)
588+
mergeConflict(tp1, tp2, that)
581589
}
582590
case _ =>
583591
tp1 | tp2

0 commit comments

Comments
 (0)