Skip to content

Commit 7309868

Browse files
authored
[CS] Pass SelectedOverload in a couple of places (#28649)
[CS] Pass SelectedOverload in a couple of places
2 parents b28df6c + 54bfa59 commit 7309868

File tree

3 files changed

+51
-82
lines changed

3 files changed

+51
-82
lines changed

lib/Sema/CSApply.cpp

+50-75
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,13 @@ namespace {
384384

385385
public:
386386
/// Build a reference to the given declaration.
387-
Expr *buildDeclRef(OverloadChoice choice, DeclNameLoc loc, Type openedType,
387+
Expr *buildDeclRef(SelectedOverload overload, DeclNameLoc loc,
388388
ConstraintLocatorBuilder locator, bool implicit,
389-
FunctionRefKind functionRefKind,
390389
AccessSemantics semantics) {
390+
auto choice = overload.choice;
391391
assert(choice.getKind() != OverloadChoiceKind::DeclViaDynamic);
392392
auto *decl = choice.getDecl();
393+
auto fullType = simplifyType(overload.openedFullType);
393394

394395
// Determine the declaration selected for this overloaded reference.
395396
auto &ctx = cs.getASTContext();
@@ -399,10 +400,7 @@ namespace {
399400
if (decl->getDeclContext()->isTypeContext() && isa<FuncDecl>(decl)) {
400401
assert(cast<FuncDecl>(decl)->isOperator() && "Must be an operator");
401402

402-
auto openedFnType = openedType->castTo<FunctionType>();
403-
auto simplifiedFnType
404-
= simplifyType(openedFnType)->castTo<FunctionType>();
405-
auto baseTy = getBaseType(simplifiedFnType);
403+
auto baseTy = getBaseType(fullType->castTo<FunctionType>());
406404

407405
// Handle operator requirements found in protocols.
408406
if (auto proto = dyn_cast<ProtocolDecl>(decl->getDeclContext())) {
@@ -424,7 +422,10 @@ namespace {
424422
// FIXME: the hop through 'getDecl()' is because
425423
// SpecializedProtocolConformance doesn't substitute into
426424
// witnesses' ConcreteDeclRefs.
427-
Type expectedFnType = simplifiedFnType->getResult();
425+
Type expectedFnType = simplifyType(overload.openedType);
426+
assert(expectedFnType->isEqual(
427+
fullType->castTo<AnyFunctionType>()->getResult()) &&
428+
"Cannot handle adjustments made to the opened type");
428429
Expr *refExpr;
429430
if (witness->getDeclContext()->isTypeContext()) {
430431
Expr *base =
@@ -435,7 +436,7 @@ namespace {
435436
} else {
436437
auto declRefExpr = new (ctx) DeclRefExpr(witness, loc,
437438
/*Implicit=*/false);
438-
declRefExpr->setFunctionRefKind(functionRefKind);
439+
declRefExpr->setFunctionRefKind(choice.getFunctionRefKind());
439440
refExpr = declRefExpr;
440441
}
441442

@@ -464,26 +465,22 @@ namespace {
464465
TypeExpr::createImplicitHack(loc.getBaseNameLoc(), baseTy, ctx);
465466
cs.cacheExprTypes(base);
466467

467-
return buildMemberRef(base, openedType, SourceLoc(), choice, loc,
468-
openedFnType->getResult(), locator, locator,
469-
implicit, functionRefKind, semantics);
468+
return buildMemberRef(base, SourceLoc(), overload, loc, locator,
469+
locator, implicit, semantics);
470470
}
471471

472-
auto type = solution.simplifyType(openedType);
473-
474472
if (isa<TypeDecl>(decl) && !isa<ModuleDecl>(decl)) {
475473
auto typeExpr = TypeExpr::createImplicitHack(
476-
loc.getBaseNameLoc(), type->getMetatypeInstanceType(),
477-
ctx);
474+
loc.getBaseNameLoc(), fullType->getMetatypeInstanceType(), ctx);
478475
cs.cacheType(typeExpr);
479476
return typeExpr;
480477
}
481478

482479
auto ref = resolveConcreteDeclRef(decl, locator);
483480
auto declRefExpr =
484-
new (ctx) DeclRefExpr(ref, loc, implicit, semantics, type);
481+
new (ctx) DeclRefExpr(ref, loc, implicit, semantics, fullType);
485482
cs.cacheType(declRefExpr);
486-
declRefExpr->setFunctionRefKind(functionRefKind);
483+
declRefExpr->setFunctionRefKind(choice.getFunctionRefKind());
487484
return forceUnwrapIfExpected(declRefExpr, choice, locator);
488485
}
489486

@@ -734,12 +731,15 @@ namespace {
734731
}
735732

736733
/// Build a new member reference with the given base and member.
737-
Expr *buildMemberRef(Expr *base, Type openedFullType, SourceLoc dotLoc,
738-
OverloadChoice choice, DeclNameLoc memberLoc,
739-
Type openedType, ConstraintLocatorBuilder locator,
734+
Expr *buildMemberRef(Expr *base, SourceLoc dotLoc,
735+
SelectedOverload overload, DeclNameLoc memberLoc,
736+
ConstraintLocatorBuilder locator,
740737
ConstraintLocatorBuilder memberLocator, bool Implicit,
741-
FunctionRefKind functionRefKind,
742738
AccessSemantics semantics) {
739+
auto choice = overload.choice;
740+
auto openedType = overload.openedType;
741+
auto openedFullType = overload.openedFullType;
742+
743743
ValueDecl *member = choice.getDecl();
744744

745745
auto &context = cs.getASTContext();
@@ -770,7 +770,7 @@ namespace {
770770
"Direct property access doesn't make sense for this");
771771
auto ref = new (context) DeclRefExpr(memberRef, memberLoc, Implicit);
772772
cs.setType(ref, refTy);
773-
ref->setFunctionRefKind(functionRefKind);
773+
ref->setFunctionRefKind(choice.getFunctionRefKind());
774774
auto *DSBI = cs.cacheType(new (context) DotSyntaxBaseIgnoredExpr(
775775
base, dotLoc, ref, cs.getType(ref)));
776776
return forceUnwrapIfExpected(DSBI, choice, memberLocator);
@@ -969,7 +969,7 @@ namespace {
969969
// Handle all other references.
970970
auto declRefExpr = new (context) DeclRefExpr(memberRef, memberLoc,
971971
Implicit, semantics);
972-
declRefExpr->setFunctionRefKind(functionRefKind);
972+
declRefExpr->setFunctionRefKind(choice.getFunctionRefKind());
973973
cs.setType(declRefExpr, refTy);
974974
Expr *ref = declRefExpr;
975975

@@ -1005,9 +1005,6 @@ namespace {
10051005

10061006
return finishApply(apply, memberRef, openedType, locator);
10071007
}
1008-
1009-
/// Describes either a type or the name of a type to be resolved.
1010-
using TypeOrName = llvm::PointerUnion<Identifier, Type>;
10111008

10121009
/// Convert the given literal expression via a protocol pair.
10131010
///
@@ -2273,10 +2270,8 @@ namespace {
22732270
return expr;
22742271
}
22752272

2276-
return buildDeclRef(selected->choice, expr->getNameLoc(),
2277-
selected->openedFullType, locator, expr->isImplicit(),
2278-
expr->getFunctionRefKind(),
2279-
expr->getAccessSemantics());
2273+
return buildDeclRef(*selected, expr->getNameLoc(), locator,
2274+
expr->isImplicit(), expr->getAccessSemantics());
22802275
}
22812276

22822277
Expr *visitSuperRefExpr(SuperRefExpr *expr) {
@@ -2304,12 +2299,9 @@ namespace {
23042299
// Determine the declaration selected for this overloaded reference.
23052300
auto locator = cs.getConstraintLocator(expr);
23062301
auto selected = solution.getOverloadChoice(locator);
2307-
auto choice = selected.choice;
23082302

2309-
return buildDeclRef(choice, expr->getNameLoc(), selected.openedFullType,
2310-
locator, expr->isImplicit(),
2311-
choice.getFunctionRefKind(),
2312-
AccessSemantics::Ordinary);
2303+
return buildDeclRef(selected, expr->getNameLoc(), locator,
2304+
expr->isImplicit(), AccessSemantics::Ordinary);
23132305
}
23142306

23152307
Expr *visitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *expr) {
@@ -2328,10 +2320,9 @@ namespace {
23282320
ConstraintLocator::Member);
23292321
auto selected = solution.getOverloadChoice(memberLocator);
23302322
return buildMemberRef(
2331-
expr->getBase(), selected.openedFullType, expr->getDotLoc(),
2332-
selected.choice, expr->getNameLoc(), selected.openedType,
2323+
expr->getBase(), expr->getDotLoc(), selected, expr->getNameLoc(),
23332324
cs.getConstraintLocator(expr), memberLocator, expr->isImplicit(),
2334-
selected.choice.getFunctionRefKind(), expr->getAccessSemantics());
2325+
expr->getAccessSemantics());
23352326
}
23362327

23372328
Expr *visitDynamicMemberRefExpr(DynamicMemberRefExpr *expr) {
@@ -2373,10 +2364,8 @@ namespace {
23732364
// Build the member reference.
23742365
auto *exprLoc = cs.getConstraintLocator(expr);
23752366
auto result = buildMemberRef(
2376-
base, selected.openedFullType, expr->getDotLoc(), selected.choice,
2377-
expr->getNameLoc(), selected.openedType,
2378-
exprLoc, memberLocator, expr->isImplicit(),
2379-
selected.choice.getFunctionRefKind(), AccessSemantics::Ordinary);
2367+
base, expr->getDotLoc(), selected, expr->getNameLoc(), exprLoc,
2368+
memberLocator, expr->isImplicit(), AccessSemantics::Ordinary);
23802369
if (!result)
23812370
return nullptr;
23822371

@@ -2515,18 +2504,17 @@ namespace {
25152504
Expr *applyCtorRefExpr(Expr *expr, Expr *base, SourceLoc dotLoc,
25162505
DeclNameLoc nameLoc, bool implicit,
25172506
ConstraintLocator *ctorLocator,
2518-
OverloadChoice choice,
2519-
FunctionRefKind functionRefKind, Type openedType) {
2507+
SelectedOverload overload) {
2508+
auto choice = overload.choice;
25202509
assert(choice.getKind() != OverloadChoiceKind::DeclViaDynamic);
25212510
auto *ctor = cast<ConstructorDecl>(choice.getDecl());
25222511

25232512
// If the subexpression is a metatype, build a direct reference to the
25242513
// constructor.
25252514
if (cs.getType(base)->is<AnyMetatypeType>()) {
25262515
return buildMemberRef(
2527-
base, openedType, dotLoc, choice, nameLoc, cs.getType(expr),
2528-
ConstraintLocatorBuilder(cs.getConstraintLocator(expr)),
2529-
ctorLocator, implicit, functionRefKind, AccessSemantics::Ordinary);
2516+
base, dotLoc, overload, nameLoc, cs.getConstraintLocator(expr),
2517+
ctorLocator, implicit, AccessSemantics::Ordinary);
25302518
}
25312519

25322520
// The subexpression must be either 'self' or 'super'.
@@ -2570,8 +2558,9 @@ namespace {
25702558

25712559
// Build a partial application of the delegated initializer.
25722560
auto callee = resolveConcreteDeclRef(ctor, ctorLocator);
2573-
Expr *ctorRef = buildOtherConstructorRef(openedType, callee, base,
2574-
nameLoc, ctorLocator, implicit);
2561+
Expr *ctorRef = buildOtherConstructorRef(overload.openedFullType, callee,
2562+
base, nameLoc, ctorLocator,
2563+
implicit);
25752564
auto *call = new (cs.getASTContext()) DotSyntaxCallExpr(ctorRef, dotLoc,
25762565
base);
25772566

@@ -2587,10 +2576,8 @@ namespace {
25872576
expr,
25882577
ConstraintLocator::ConstructorMember);
25892578
if (auto selected = solution.getOverloadChoiceIfAvailable(ctorLocator)) {
2590-
auto choice = selected->choice;
25912579
return applyCtorRefExpr(
2592-
expr, base, dotLoc, nameLoc, implicit, ctorLocator, choice,
2593-
choice.getFunctionRefKind(), selected->openedFullType);
2580+
expr, base, dotLoc, nameLoc, implicit, ctorLocator, *selected);
25942581
}
25952582

25962583
// Determine the declaration selected for this overloaded reference.
@@ -2618,9 +2605,7 @@ namespace {
26182605
cs.diagnoseDeprecatedConditionalConformanceOuterAccess(
26192606
UDE, selected.choice.getDecl());
26202607

2621-
return buildDeclRef(selected.choice, nameLoc, selected.openedFullType,
2622-
memberLocator, implicit,
2623-
selected.choice.getFunctionRefKind(),
2608+
return buildDeclRef(selected, nameLoc, memberLocator, implicit,
26242609
AccessSemantics::Ordinary);
26252610
}
26262611

@@ -2651,11 +2636,9 @@ namespace {
26512636
case OverloadChoiceKind::Decl:
26522637
case OverloadChoiceKind::DeclViaUnwrappedOptional:
26532638
case OverloadChoiceKind::DeclViaDynamic:
2654-
return buildMemberRef(base, selected.openedFullType, dotLoc,
2655-
selected.choice, nameLoc, selected.openedType,
2639+
return buildMemberRef(base, dotLoc, selected, nameLoc,
26562640
cs.getConstraintLocator(expr), memberLocator,
2657-
implicit, selected.choice.getFunctionRefKind(),
2658-
AccessSemantics::Ordinary);
2641+
implicit, AccessSemantics::Ordinary);
26592642

26602643
case OverloadChoiceKind::TupleIndex: {
26612644
Type toType = simplifyType(cs.getType(expr));
@@ -6485,12 +6468,10 @@ static Expr *buildCallAsFunctionMethodRef(
64856468
ExprRewriter &rewriter, ApplyExpr *apply, SelectedOverload selected,
64866469
ConstraintLocatorBuilder applyFunctionLoc) {
64876470
auto *fn = apply->getFn();
6488-
auto choice = selected.choice;
64896471
// Create direct reference to `callAsFunction` method.
64906472
auto *declRef = rewriter.buildMemberRef(
6491-
fn, selected.openedFullType, /*dotLoc*/ SourceLoc(), choice,
6492-
DeclNameLoc(fn->getEndLoc()), selected.openedType, applyFunctionLoc,
6493-
applyFunctionLoc, /*implicit*/ true, choice.getFunctionRefKind(),
6473+
fn, /*dotLoc*/ SourceLoc(), selected, DeclNameLoc(fn->getEndLoc()),
6474+
applyFunctionLoc, applyFunctionLoc, /*implicit*/ true,
64946475
AccessSemantics::Ordinary);
64956476
if (!declRef)
64966477
return nullptr;
@@ -6523,12 +6504,9 @@ ExprRewriter::finishApplyDynamicCallable(ApplyExpr *apply,
65236504
bool useKwargsMethod = argumentLabel == ctx.Id_withKeywordArguments;
65246505

65256506
// Construct expression referencing the `dynamicallyCall` method.
6526-
auto member = buildMemberRef(fn, selected.openedFullType,
6527-
SourceLoc(), selected.choice,
6528-
DeclNameLoc(method->getNameLoc()),
6529-
selected.openedType, loc, loc, /*implicit*/ true,
6530-
selected.choice.getFunctionRefKind(),
6531-
AccessSemantics::Ordinary);
6507+
auto member = buildMemberRef(fn, SourceLoc(), selected,
6508+
DeclNameLoc(method->getNameLoc()), loc, loc,
6509+
/*implicit*/ true, AccessSemantics::Ordinary);
65326510

65336511
// Construct argument to the method (either an array or dictionary
65346512
// expression).
@@ -6883,12 +6861,9 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, ConcreteDeclRef callee,
68836861
// Consider the constructor decl reference expr 'implicit', but the
68846862
// constructor call expr itself has the apply's 'implicitness'.
68856863
auto ctorRef = resolveConcreteDeclRef(choice.getDecl(), ctorLocator);
6886-
Expr *declRef = buildMemberRef(fn, selected->openedFullType,
6887-
/*dotLoc=*/SourceLoc(), choice,
6888-
DeclNameLoc(fn->getEndLoc()),
6889-
selected->openedType, locator, ctorLocator,
6890-
/*Implicit=*/true,
6891-
choice.getFunctionRefKind(),
6864+
Expr *declRef = buildMemberRef(fn, /*dotLoc=*/SourceLoc(), *selected,
6865+
DeclNameLoc(fn->getEndLoc()), locator,
6866+
ctorLocator, /*Implicit=*/true,
68926867
AccessSemantics::Ordinary);
68936868
if (!declRef)
68946869
return nullptr;

lib/Sema/ConstraintSystem.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -2357,6 +2357,7 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
23572357
auto overload = SelectedOverload{choice, openedFullType, refType, boundType};
23582358
auto result = ResolvedOverloads.insert({locator, overload});
23592359
assert(result.second && "Already resolved this overload?");
2360+
(void)result;
23602361

23612362
// In some cases we already created the appropriate bind constraints.
23622363
if (!bindConstraintCreated) {

lib/Sema/ConstraintSystem.h

-7
Original file line numberDiff line numberDiff line change
@@ -939,13 +939,6 @@ class SolutionDiff {
939939
explicit SolutionDiff(ArrayRef<Solution> solutions);
940940
};
941941

942-
/// Identifies a specific conversion from
943-
struct SpecificConstraint {
944-
CanType First;
945-
CanType Second;
946-
ConstraintKind Kind;
947-
};
948-
949942
/// An intrusive, doubly-linked list of constraints.
950943
using ConstraintList = llvm::ilist<Constraint>;
951944

0 commit comments

Comments
 (0)