Skip to content

Commit 6b089ed

Browse files
committed
Avoid more constraints in result if trying views
1 parent b31e865 commit 6b089ed

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4473,12 +4473,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
44734473
def argHasDefault = hasDefaultParams && !defaultArg.isEmpty
44744474

44754475
def canProfitFromMoreConstraints =
4476+
!ctx.mode.is(Mode.ImplicitExploration)
4477+
&& {
44764478
arg.tpe.isInstanceOf[AmbiguousImplicits]
44774479
// Ambiguity could be decided by more constraints
44784480
|| !isFullyDefined(formal, ForceDegree.none) && !argHasDefault
44794481
// More context might constrain type variables which could make implicit scope larger.
44804482
// But in this case we should search with additional arguments typed only if there
44814483
// is no default argument.
4484+
}
44824485

44834486
// Try to constrain the result using `pt1`, but back out if a BadTyperStateAssertion
44844487
// is thrown. TODO Find out why the bad typer state arises and prevent it. The try-catch
@@ -4585,10 +4588,12 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
45854588
issueErrors(tree, args, failureType)
45864589
else
45874590
val app = cpy.Apply(tree)(untpd.TypedSplice(tree), namedArgs)
4588-
// old-style implicit needs to be marked using so that implicits are searched
4589-
val needsUsing = wtp.isImplicitMethod || wtp.match
4591+
// avoid warning if method is old-style implicit that context bounds will be contextual
4592+
val needsUsing = wtp.isContextualMethod || wtp.match
45904593
case MethodType(ContextBoundParamName(_) :: _) => sourceVersion.isAtLeast(`3.4`)
45914594
case _ => false
4595+
// it is benign to always set apply kind; typedApply may redundantly search missing implicits
4596+
// because it doesn't distinguish adaptNoArgs from explicit application
45924597
if needsUsing then app.setApplyKind(ApplyKind.Using)
45934598
typr.println(i"try with default implicit args $app")
45944599
// If the retyped tree still has an error type and is an `Apply`

tests/warn/i19506.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//> using options -source 3.4
2+
3+
trait Reader[T]
4+
def read[T: Reader](s: String, trace: Boolean = false)(implicit i: Int = 42): T = ???
5+
6+
// if apply kind is not using when retrying to insert the default arg, it will warn:
7+
// Context bounds will map to context parameters.
8+
// A `using` clause is needed to pass explicit arguments to them.
9+
// The method to invoke (read) might have been compiled under 3.3,
10+
// or (as above) the contextual parameter may have been merged with an existing implicit param list.
11+
def Test =
12+
implicit def reader: Reader[Object] = new Reader[Object]{}
13+
read[Object]("") // no warn

tests/warn/i22439.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@main def test() =
2+
given Int = 42
3+
4+
locally:
5+
given String = "y"
6+
def ff(implicit i: Int, s: String = "x") = s * i
7+
def gg(using i: Int, s: String = "x") = s * i
8+
ff(using i = 10) // warn uses default arg
9+
gg(using i = 10) // warn
10+
ff(i = 10) // warn should use using
11+
gg(i = 10) // remarkably, augmentString(gg(given_Int, given_String)).apply(i = 10)

0 commit comments

Comments
 (0)