Skip to content

Commit

Permalink
Revert "Compensate loss of transitivity"
Browse files Browse the repository at this point in the history
This reverts commit 7c4bd67
See scala#21344 (comment)

Fixes scala#21320
  • Loading branch information
EugeneFlesselle committed Aug 9, 2024
1 parent c09154a commit 58f3407
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
18 changes: 3 additions & 15 deletions compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,8 @@ trait Implicits:
def disambiguate(alt1: SearchResult, alt2: SearchSuccess) = alt1 match
case alt1: SearchSuccess =>
var diff = compareAlternatives(alt1, alt2, disambiguate = true)
assert(diff <= 0 || isWarnPriorityChangeVersion)
// diff > 0 candidates should already have been eliminated in `rank`
if diff == 0 && alt1.ref =:= alt2.ref then
diff = 1 // See i12951 for a test where this happens
else if diff == 0 && alt2.isExtension then
Expand Down Expand Up @@ -1636,21 +1638,7 @@ trait Implicits:
validateOrdering(ord)
throw ex

val sorted = sort(eligible)
val res = sorted match
case first :: rest =>
val firstIsImplicit = first.ref.symbol.is(Implicit)
if rest.exists(_.ref.symbol.is(Implicit) != firstIsImplicit) then
// Mixture of implicits and givens
// Rank implicits first, then, if there is a given that it better than the best implicit(s)
// switch over to givens.
val (sortedImplicits, sortedGivens) = sorted.partition(_.ref.symbol.is(Implicit))
val implicitResult = rank(sortedImplicits, NoMatchingImplicitsFailure, Nil)
rank(sortedGivens, implicitResult, Nil)
else
rank(sorted, NoMatchingImplicitsFailure, Nil)
case _ =>
NoMatchingImplicitsFailure
val res = rank(sort(eligible), NoMatchingImplicitsFailure, Nil)

// Issue all priority change warnings that can affect the result
val shownWarnings = priorityChangeWarnings.toList.collect:
Expand Down
File renamed without changes.
73 changes: 73 additions & 0 deletions tests/pos/i21320b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import scala.deriving.*
import scala.compiletime.*

trait ConfigMonoid[T]:
def zero: T
def orElse(main: T, defaults: T): T

object ConfigMonoid:
given option[T]: ConfigMonoid[Option[T]] = ???

inline def zeroTuple[C <: Tuple]: Tuple =
inline erasedValue[C] match
case _: EmptyTuple => EmptyTuple
case _: (t *: ts) =>
summonInline[ConfigMonoid[t]].zero *: zeroTuple[ts]

inline def valueTuple[C <: Tuple, T](index: Int, main: T, defaults: T): Tuple =
inline erasedValue[C] match
case _: EmptyTuple => EmptyTuple
case _: (t *: ts) =>
def get(v: T) = v.asInstanceOf[Product].productElement(index).asInstanceOf[t]
summonInline[ConfigMonoid[t]].orElse(get(main), get(defaults)) *: valueTuple[ts, T](
index + 1,
main,
defaults
)

inline given derive[T](using m: Mirror.ProductOf[T]): ConfigMonoid[T] =
new ConfigMonoid[T]:
def zero: T = m.fromProduct(zeroTuple[m.MirroredElemTypes])
def orElse(main: T, defaults: T): T = m.fromProduct(valueTuple[m.MirroredElemTypes, T](0, main, defaults))



final case class PublishOptions(
v1: Option[String] = None,
v2: Option[String] = None,
v3: Option[String] = None,
v4: Option[String] = None,
v5: Option[String] = None,
v6: Option[String] = None,
v7: Option[String] = None,
v8: Option[String] = None,
v9: Option[String] = None,
ci: PublishContextualOptions = PublishContextualOptions(),
)
object PublishOptions:
implicit val monoid: ConfigMonoid[PublishOptions] = ConfigMonoid.derive

final case class PublishContextualOptions(
v1: Option[String] = None,
v2: Option[String] = None,
v3: Option[String] = None,
v4: Option[String] = None,
v5: Option[String] = None,
v6: Option[String] = None,
v7: Option[String] = None,
v8: Option[String] = None,
v9: Option[String] = None,
v10: Option[String] = None,
v11: Option[String] = None,
v12: Option[String] = None,
v13: Option[String] = None,
v14: Option[String] = None,
v15: Option[String] = None,
v16: Option[String] = None,
v17: Option[String] = None,
v18: Option[String] = None,
v19: Option[String] = None,
v20: Option[String] = None
)
object PublishContextualOptions:
implicit val monoid: ConfigMonoid[PublishContextualOptions] = ConfigMonoid.derive // was crash

0 comments on commit 58f3407

Please sign in to comment.