From eb5c3e81304eee5ce75c2796bd4b7b16d2d1b19b Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Mon, 16 Sep 2024 10:29:51 +0100 Subject: [PATCH] Avoid cyclic errors forcing default arg types --- compiler/src/dotty/tools/dotc/typer/Applications.scala | 4 ++++ tests/neg/19414-desugared.check | 3 +-- tests/neg/19414.check | 3 +-- tests/neg/given-ambiguous-default-2.check | 8 ++++---- tests/pos/i21568.scala | 6 ++++++ 5 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 tests/pos/i21568.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala index 11a95ce23f93..992f283154ca 100644 --- a/compiler/src/dotty/tools/dotc/typer/Applications.scala +++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala @@ -696,6 +696,10 @@ trait Applications extends Compatibility { fail(MissingArgument(methodType.paramNames(n), methString)) def tryDefault(n: Int, args1: List[Arg]): Unit = { + if !success then + missingArg(n) // fail fast before forcing the default arg tpe, to avoid cyclic errors + return + val sym = methRef.symbol val testOnly = this.isInstanceOf[TestApplication[?]] diff --git a/tests/neg/19414-desugared.check b/tests/neg/19414-desugared.check index c21806e16c2c..cc51ee471553 100644 --- a/tests/neg/19414-desugared.check +++ b/tests/neg/19414-desugared.check @@ -8,7 +8,6 @@ | writer = | /* ambiguous: both given instance given_Writer_JsValue and given instance given_Writer_JsObject match type Writer[B] */ | summon[Writer[B]] - | , - | this.given_BodySerializer_B$default$2[B]) + | ) | |But both given instance given_Writer_JsValue and given instance given_Writer_JsObject match type Writer[B]. diff --git a/tests/neg/19414.check b/tests/neg/19414.check index 6804546df037..016e3942c825 100644 --- a/tests/neg/19414.check +++ b/tests/neg/19414.check @@ -8,7 +8,6 @@ | evidence$1 = | /* ambiguous: both given instance given_Writer_JsValue and given instance given_Writer_JsObject match type Writer[B] */ | summon[Writer[B]] - | , - | this.given_BodySerializer_B$default$2[B]) + | ) | |But both given instance given_Writer_JsValue and given instance given_Writer_JsObject match type Writer[B]. diff --git a/tests/neg/given-ambiguous-default-2.check b/tests/neg/given-ambiguous-default-2.check index cbe8b972a389..4d473a301340 100644 --- a/tests/neg/given-ambiguous-default-2.check +++ b/tests/neg/given-ambiguous-default-2.check @@ -1,9 +1,9 @@ -- [E172] Type Error: tests/neg/given-ambiguous-default-2.scala:18:23 -------------------------------------------------- 18 |def f: Unit = summon[C] // error: Ambiguous given instances | ^ - |No best given instance of type C was found for parameter x of method summon in object Predef. - |I found: + | No best given instance of type C was found for parameter x of method summon in object Predef. + | I found: | - | given_C(a = /* ambiguous: both given instance a1 and given instance a2 match type A */summon[A], this.given_C$default$2) + | given_C(a = /* ambiguous: both given instance a1 and given instance a2 match type A */summon[A]) | - |But both given instance a1 and given instance a2 match type A. + | But both given instance a1 and given instance a2 match type A. diff --git a/tests/pos/i21568.scala b/tests/pos/i21568.scala new file mode 100644 index 000000000000..87184956ea79 --- /dev/null +++ b/tests/pos/i21568.scala @@ -0,0 +1,6 @@ +class Lang(name: String) +object Lang { + val Default = Lang("") + def apply(language: String): Lang = ??? + def apply(maybeLang: Option[String], default: Lang = Default): Lang = ??? +}