File tree 2 files changed +37
-7
lines changed
compiler/src/dotty/tools/dotc/typer
2 files changed +37
-7
lines changed Original file line number Diff line number Diff line change @@ -1975,13 +1975,19 @@ trait Applications extends Compatibility {
1975
1975
// alternatives are the same after following ExprTypes, pick one of them
1976
1976
// (prefer the one that is not a method, but that's arbitrary).
1977
1977
if alt1.widenExpr =:= alt2 then - 1 else 1
1978
- else ownerScore match
1979
- case 1 => if winsType1 || ! winsType2 then 1 else 0
1980
- case - 1 => if winsType2 || ! winsType1 then - 1 else 0
1981
- case 0 =>
1982
- if winsType1 != winsType2 then if winsType1 then 1 else - 1
1983
- else if alt1.symbol == alt2.symbol then comparePrefixes
1984
- else 0
1978
+ else
1979
+ // For implicit resolution, take ownerscore as more significant than type resolution
1980
+ // Reason: People use owner hierarchies to explicitly prioritize, we should not
1981
+ // break that by changing implicit priority of types. On the other hand we do want
1982
+ // to comparePrefixes if there is a draw; StringFormaterTest breaks if we don't do that.
1983
+ def drawOrOwner = if preferGeneral then ownerScore else 0
1984
+ ownerScore match
1985
+ case 1 => if winsType1 || ! winsType2 then 1 else drawOrOwner
1986
+ case - 1 => if winsType2 || ! winsType1 then - 1 else drawOrOwner
1987
+ case 0 =>
1988
+ if winsType1 != winsType2 then if winsType1 then 1 else - 1
1989
+ else if alt1.symbol == alt2.symbol then comparePrefixes
1990
+ else 0
1985
1991
end compareWithTypes
1986
1992
1987
1993
if alt1.symbol.is(ConstructorProxy ) && ! alt2.symbol.is(ConstructorProxy ) then - 1
Original file line number Diff line number Diff line change
1
+ /* These tests show various mechanisms available for implicit prioritization.
2
+ */
3
+ import language .`3.6`
4
+
5
+ class A // The type for which we infer terms below
6
+ class B extends A
7
+
8
+ /* First, two schemes that require a pre-planned architecture for how and
9
+ * where given instances are defined.
10
+ *
11
+ * Traditional scheme: prioritize with location in class hierarchy
12
+ */
13
+ class LowPriorityImplicits :
14
+ given g1 : A ()
15
+
16
+ object NormalImplicits extends LowPriorityImplicits :
17
+ given g2 : B ()
18
+
19
+ def test1 =
20
+ import NormalImplicits .given
21
+ val x = summon[A ]
22
+ val _: B = x
23
+ val y = summon[B ]
24
+ val _: B = y
You can’t perform that action at this time.
0 commit comments