File tree 3 files changed +44
-4
lines changed
compiler/src/dotty/tools/dotc/core
3 files changed +44
-4
lines changed Original file line number Diff line number Diff line change @@ -295,11 +295,24 @@ trait ConstraintHandling {
295
295
end legalBound
296
296
297
297
protected def addOneBound (param : TypeParamRef , rawBound : Type , isUpper : Boolean )(using Context ): Boolean =
298
+
299
+ // Replace top-level occurrences of `param` in `bound` by `Nothing`
300
+ def sanitize (bound : Type ): Type =
301
+ if bound.stripped eq param then defn.NothingType
302
+ else bound match
303
+ case bound : AndOrType =>
304
+ bound.derivedAndOrType(sanitize(bound.tp1), sanitize(bound.tp2))
305
+ case _ =>
306
+ bound
307
+
298
308
if ! constraint.contains(param) then true
299
- else if ! isUpper && param.occursIn(rawBound) then
300
- // We don't allow recursive lower bounds when defining a type,
301
- // so we shouldn't allow them as constraints either.
302
- false
309
+ else if ! isUpper && param.occursIn(rawBound.widen) then
310
+ val rawBound1 = sanitize(rawBound.widenDealias)
311
+ if param.occursIn(rawBound1) then
312
+ // We don't allow recursive lower bounds when defining a type,
313
+ // so we shouldn't allow them as constraints either.
314
+ false
315
+ else addOneBound(param, rawBound1, isUpper)
303
316
else
304
317
305
318
// Narrow one of the bounds of type parameter `param`
Original file line number Diff line number Diff line change
1
+ -- [E007] Type Mismatch Error: tests/neg/i21535.scala:7:4 --------------------------------------------------------------
2
+ 3 | (if (true) then
3
+ 4 | new A(66)
4
+ 5 | else
5
+ 6 | m1()
6
+ 7 | ).m2(p1 = p); // error
7
+ | ^
8
+ | Found: (Int | Short) @uncheckedVariance
9
+ | Required: Int & Short
10
+ |
11
+ | longer explanation available when compiling with `-explain`
Original file line number Diff line number Diff line change
1
+ def test () = {
2
+ val p = 10 .toShort
3
+ (if (true ) then
4
+ new A (66 )
5
+ else
6
+ m1()
7
+ ).m2(p1 = p); // error
8
+
9
+ }
10
+
11
+ def m1 (): A [Short ] = new A (10 )
12
+
13
+ class A [D ](var f : D ) {
14
+
15
+ def m2 (p1 : D = f, p2 : D = f): Unit = {}
16
+ }
You can’t perform that action at this time.
0 commit comments