@@ -4250,34 +4250,38 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4250
4250
else formals1
4251
4251
implicitArgs(formals2, argIndex + 1 , pt)
4252
4252
4253
+ // try to constrain type before implicit search. See #18763
4253
4254
def tryConstrainType (pt1 : Type ): Boolean = {
4255
+ // subst dummyArg into FunProto so that we don't have to search for nested implicit
4256
+ val tm = new TypeMap :
4257
+ def apply (t : Type ): Type = t match
4258
+ case fp@ FunProto (args, resType) =>
4259
+ fp.derivedFunProto(
4260
+ args.map(arg =>
4261
+ if (arg.isInstanceOf [untpd.TypedSplice ]) arg
4262
+ else dummyArg(arg.typeOpt).withSpan(arg.span)
4263
+ ),
4264
+ mapOver(resType)
4265
+ )
4266
+ case _ =>
4267
+ mapOver(t)
4254
4268
val ownedVars = ctx.typerState.ownedVars
4255
- val qualifying = (ownedVars -- locked).toList
4256
- if (! formal.isGround && (pt1 `ne` pt) && (pt1 ne sharpenedPt) && (ownedVars ne locked) && ! ownedVars.isEmpty && qualifying.nonEmpty) {
4269
+ def qualifying = (ownedVars -- locked).toList
4270
+ val resultAlreadyConstrained = pt1.isInstanceOf [MethodOrPoly ]
4271
+ if ! formal.isGround
4272
+ && (pt1 `ne` pt)
4273
+ && (pt1 ne sharpenedPt)
4274
+ && (ownedVars ne locked)
4275
+ && ! ownedVars.isEmpty
4276
+ && qualifying.nonEmpty
4277
+ && ! resultAlreadyConstrained then
4257
4278
val approxRes = wildApprox(pt1.resultType)
4258
- val tm = new TypeMap :
4259
- def apply (t : Type ) = t match
4260
- case fp@ FunProto (args, resType) =>
4261
- fp.derivedFunProto(
4262
- args.map(arg =>
4263
- if (arg.isInstanceOf [untpd.TypedSplice ]) arg
4264
- else dummyArg(arg.typeOpt).withSpan(arg.span)
4265
- ),
4266
- mapOver(resType)
4267
- )
4268
- case _ =>
4269
- mapOver(t)
4270
4279
val stripedApproxRes = tm(approxRes)
4271
- if ( ! stripedApproxRes.containsWildcardTypes) {
4280
+ if ! stripedApproxRes.containsWildcardTypes then
4272
4281
if ctx.typerState.isCommittable then
4273
- NoViewsAllowed .constrainResult(tree.symbol, wtp.resultType, stripedApproxRes)
4274
- else constrainResult(tree.symbol, wtp.resultType, stripedApproxRes)
4275
- } else {
4276
- false
4277
- }
4278
- } else {
4279
- false
4280
- }
4282
+ return NoViewsAllowed .constrainResult(tree.symbol, wtp.resultType, stripedApproxRes)
4283
+ else return constrainResult(tree.symbol, wtp.resultType, stripedApproxRes)
4284
+ false
4281
4285
}
4282
4286
4283
4287
val pt1 = pt.deepenProtoTrans
0 commit comments