Skip to content

Commit

Permalink
Avoid orphan param from default arg
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Oct 22, 2024
1 parent 2fc299b commit e8136b7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2130,6 +2130,11 @@ class Namer { typer: Typer =>
val pt = inherited.orElse(expectedDefaultArgType).orElse(fallbackProto).widenExpr
val tp = typedAheadRhs(pt).tpe
if (defaultTp eq pt) && (tp frozen_<:< defaultTp) then
// See i21558, the default argument new A(1.0) is of type A[?T]
// With an uninterpolated, invariant ?T type variable.
// So before we return the default getter parameter type (A[? <: Double])
// we want to force ?T to instantiate, so it's poly is removed from the constraint
isFullyDefined(tp, ForceDegree.all)
// When possible, widen to the default getter parameter type to permit a
// larger choice of overrides (see `default-getter.scala`).
// For justification on the use of `@uncheckedVariance`, see
Expand Down
10 changes: 10 additions & 0 deletions tests/pos/i21558.orig.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Base
class A[T <: Float](val f: T) extends Base

def test() = {
m1(new A(m2()));

}

def m1(x: Base) = {}
def m2(p: A[? <: Float] = new A(1.0f)): Int = 1
8 changes: 8 additions & 0 deletions tests/pos/i21558.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Base
class A[T <: Double](val f: T) extends Base

class Test:
def test() = m1(new A(m2()))

def m1(x: Base): Unit = {}
def m2(p: A[? <: Double] = new A(1.0)): Int = 2

0 comments on commit e8136b7

Please sign in to comment.