From e8136b724e74c8c3661a92f2489b52a83b4aed22 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 22 Oct 2024 10:01:21 +0100 Subject: [PATCH] Avoid orphan param from default arg --- compiler/src/dotty/tools/dotc/typer/Namer.scala | 5 +++++ tests/pos/i21558.orig.scala | 10 ++++++++++ tests/pos/i21558.scala | 8 ++++++++ 3 files changed, 23 insertions(+) create mode 100644 tests/pos/i21558.orig.scala create mode 100644 tests/pos/i21558.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Namer.scala b/compiler/src/dotty/tools/dotc/typer/Namer.scala index 6167db62fbe0..b1b94288bcbf 100644 --- a/compiler/src/dotty/tools/dotc/typer/Namer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Namer.scala @@ -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 diff --git a/tests/pos/i21558.orig.scala b/tests/pos/i21558.orig.scala new file mode 100644 index 000000000000..3a955920a1c7 --- /dev/null +++ b/tests/pos/i21558.orig.scala @@ -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 diff --git a/tests/pos/i21558.scala b/tests/pos/i21558.scala new file mode 100644 index 000000000000..80168992cea9 --- /dev/null +++ b/tests/pos/i21558.scala @@ -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