Skip to content

Commit b5bc514

Browse files
authored
Merge pull request swiftlang#39192 from hamishknight/a-range-of-arguments
[CS] Store argument list mappings on solutions
2 parents e7594f2 + ddff9c4 commit b5bc514

File tree

6 files changed

+68
-24
lines changed

6 files changed

+68
-24
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,11 @@ class Solution {
11981198
/// A map from argument expressions to their applied property wrapper expressions.
11991199
llvm::MapVector<ASTNode, SmallVector<AppliedPropertyWrapper, 2>> appliedPropertyWrappers;
12001200

1201+
/// A mapping from the constraint locators for references to various
1202+
/// names (e.g., member references, normal name references, possible
1203+
/// constructions) to the argument lists for the call to that locator.
1204+
llvm::MapVector<ConstraintLocator *, ArgumentList *> argumentLists;
1205+
12011206
/// Record a new argument matching choice for given locator that maps a
12021207
/// single argument to a single parameter.
12031208
void recordSingleArgMatchingChoice(ConstraintLocator *locator);
@@ -1332,6 +1337,10 @@ class Solution {
13321337
/// expression type map.
13331338
bool isStaticallyDerivedMetatype(Expr *E) const;
13341339

1340+
/// Retrieve the argument list that is associated with a call at the given
1341+
/// locator.
1342+
ArgumentList *getArgumentList(ConstraintLocator *locator) const;
1343+
13351344
SWIFT_DEBUG_DUMP;
13361345

13371346
/// Dump this solution.
@@ -2406,6 +2415,11 @@ class ConstraintSystem {
24062415
/// Cache of the effects any closures visited.
24072416
llvm::SmallDenseMap<ClosureExpr *, FunctionType::ExtInfo, 4> closureEffectsCache;
24082417

2418+
/// A mapping from the constraint locators for references to various
2419+
/// names (e.g., member references, normal name references, possible
2420+
/// constructions) to the argument lists for the call to that locator.
2421+
llvm::MapVector<ConstraintLocator *, ArgumentList *> ArgumentLists;
2422+
24092423
public:
24102424
/// A map from argument expressions to their applied property wrapper expressions.
24112425
llvm::SmallMapVector<ASTNode, SmallVector<AppliedPropertyWrapper, 2>, 4> appliedPropertyWrappers;
@@ -2784,20 +2798,18 @@ class ConstraintSystem {
27842798
/// we're exploring.
27852799
SolverState *solverState = nullptr;
27862800

2787-
/// A mapping from the constraint locators for references to various
2788-
/// names (e.g., member references, normal name references, possible
2789-
/// constructions) to the argument lists for the call to that locator.
2790-
llvm::DenseMap<ConstraintLocator *, ArgumentList *> ArgumentLists;
2791-
27922801
/// Form a locator that can be used to retrieve argument information cached in
27932802
/// the constraint system for the callee described by the anchor of the
27942803
/// passed locator.
27952804
ConstraintLocator *getArgumentInfoLocator(ConstraintLocator *locator);
27962805

2797-
/// Retrieve the argument list that is associated with a member
2798-
/// reference at the given locator.
2806+
/// Retrieve the argument list that is associated with a call at the given
2807+
/// locator.
27992808
ArgumentList *getArgumentList(ConstraintLocator *locator);
28002809

2810+
/// Associate an argument list with a call at a given locator.
2811+
void associateArgumentList(ConstraintLocator *locator, ArgumentList *args);
2812+
28012813
Optional<SelectedOverload>
28022814
findSelectedOverloadFor(ConstraintLocator *locator) const {
28032815
auto result = ResolvedOverloads.find(locator);
@@ -2896,6 +2908,9 @@ class ConstraintSystem {
28962908
/// The length of \c ImplicitValueConversions.
28972909
unsigned numImplicitValueConversions;
28982910

2911+
/// The length of \c ArgumentLists.
2912+
unsigned numArgumentLists;
2913+
28992914
/// The previous score.
29002915
Score PreviousScore;
29012916

lib/AST/ArgumentList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ ArgumentList *ArgumentList::createImplicit(ASTContext &ctx, SourceLoc lParenLoc,
116116
ArgumentList *ArgumentList::createImplicit(ASTContext &ctx,
117117
ArrayRef<Argument> args,
118118
AllocationArena arena) {
119-
return createImplicit(ctx, SourceLoc(), args, SourceLoc());
119+
return createImplicit(ctx, SourceLoc(), args, SourceLoc(), arena);
120120
}
121121

122122
ArgumentList *ArgumentList::forImplicitSingle(ASTContext &ctx, Identifier label,

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Expr *FailureDiagnostic::findParentExpr(const Expr *subExpr) const {
112112

113113
ArgumentList *
114114
FailureDiagnostic::getArgumentListFor(ConstraintLocator *locator) const {
115-
return getConstraintSystem().getArgumentList(locator);
115+
return S.getArgumentList(locator);
116116
}
117117

118118
Expr *FailureDiagnostic::getBaseExprFor(const Expr *anchor) const {

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ namespace {
917917
CS.getConstraintLocator(locator,
918918
ConstraintLocator::FunctionResult);
919919

920-
associateArgumentList(memberLocator, argList);
920+
CS.associateArgumentList(memberLocator, argList);
921921

922922
Type outputTy;
923923

@@ -1174,7 +1174,7 @@ namespace {
11741174

11751175
Type visitObjectLiteralExpr(ObjectLiteralExpr *expr) {
11761176
auto *exprLoc = CS.getConstraintLocator(expr);
1177-
associateArgumentList(exprLoc, expr->getArgs());
1177+
CS.associateArgumentList(exprLoc, expr->getArgs());
11781178

11791179
// If the expression has already been assigned a type; just use that type.
11801180
if (expr->getType())
@@ -2687,7 +2687,7 @@ namespace {
26872687
Type visitApplyExpr(ApplyExpr *expr) {
26882688
auto fnExpr = expr->getFn();
26892689

2690-
associateArgumentList(CS.getConstraintLocator(expr), expr->getArgs());
2690+
CS.associateArgumentList(CS.getConstraintLocator(expr), expr->getArgs());
26912691

26922692
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(fnExpr)) {
26932693
auto typeOperation = getTypeOperation(UDE, CS.getASTContext());
@@ -3442,11 +3442,6 @@ namespace {
34423442
}
34433443
llvm_unreachable("unhandled operation");
34443444
}
3445-
3446-
void associateArgumentList(ConstraintLocator *locator, ArgumentList *args) {
3447-
assert(locator && locator->getAnchor());
3448-
CS.ArgumentLists[CS.getArgumentInfoLocator(locator)] = args;
3449-
}
34503445
};
34513446

34523447
class ConstraintWalker : public ASTWalker {

lib/Sema/CSSolver.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ Solution ConstraintSystem::finalize() {
199199
solution.ImplicitValueConversions.push_back(valueConversion);
200200
}
201201

202+
// Remember argument lists.
203+
for (const auto &argListMapping : ArgumentLists) {
204+
solution.argumentLists.insert(argListMapping);
205+
}
206+
202207
return solution;
203208
}
204209

@@ -297,6 +302,11 @@ void ConstraintSystem::applySolution(const Solution &solution) {
297302
ImplicitValueConversions.push_back(valueConversion);
298303
}
299304

305+
// Register the argument lists.
306+
for (auto &argListMapping : solution.argumentLists) {
307+
ArgumentLists.insert(argListMapping);
308+
}
309+
300310
// Register any fixes produced along this path.
301311
Fixes.append(solution.Fixes.begin(), solution.Fixes.end());
302312
}
@@ -510,6 +520,7 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
510520
numSolutionApplicationTargets = cs.solutionApplicationTargets.size();
511521
numCaseLabelItems = cs.caseLabelItems.size();
512522
numImplicitValueConversions = cs.ImplicitValueConversions.size();
523+
numArgumentLists = cs.ArgumentLists.size();
513524

514525
PreviousScore = cs.CurrentScore;
515526

@@ -619,6 +630,9 @@ ConstraintSystem::SolverScope::~SolverScope() {
619630
// Remove any implicit value conversions.
620631
truncate(cs.ImplicitValueConversions, numImplicitValueConversions);
621632

633+
// Remove any argument lists no longer in scope.
634+
truncate(cs.ArgumentLists, numArgumentLists);
635+
622636
// Reset the previous score.
623637
cs.CurrentScore = PreviousScore;
624638

lib/Sema/ConstraintSystem.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3713,10 +3713,11 @@ static bool diagnoseAmbiguity(
37133713
/*isApplication=*/false, decl->getDescriptiveKind(),
37143714
name.isSpecial(), name.getBaseName());
37153715
} else {
3716-
bool isApplication =
3717-
llvm::any_of(cs.ArgumentLists, [&](const auto &pair) {
3716+
bool isApplication = llvm::any_of(solutions, [&](const auto &S) {
3717+
return llvm::any_of(S.argumentLists, [&](const auto &pair) {
37183718
return pair.first->getAnchor() == commonAnchor;
37193719
});
3720+
});
37203721

37213722
DE.diagnose(getLoc(commonAnchor),
37223723
diag::no_overloads_match_exactly_in_call, isApplication,
@@ -3757,7 +3758,7 @@ static bool diagnoseAmbiguity(
37573758

37583759
if (fn->getNumParams() == 1) {
37593760
auto *argList =
3760-
cs.getArgumentList(solution.Fixes.front()->getLocator());
3761+
solution.getArgumentList(solution.Fixes.front()->getLocator());
37613762
assert(argList);
37623763

37633764
const auto &param = fn->getParams()[0];
@@ -4668,6 +4669,27 @@ ArgumentList *ConstraintSystem::getArgumentList(ConstraintLocator *locator) {
46684669
return nullptr;
46694670
}
46704671

4672+
void ConstraintSystem::associateArgumentList(ConstraintLocator *locator,
4673+
ArgumentList *args) {
4674+
assert(locator && locator->getAnchor());
4675+
auto *argInfoLoc = getArgumentInfoLocator(locator);
4676+
auto inserted = ArgumentLists.insert({argInfoLoc, args}).second;
4677+
assert(inserted && "Multiple argument lists at locator?");
4678+
(void)inserted;
4679+
}
4680+
4681+
ArgumentList *Solution::getArgumentList(ConstraintLocator *locator) const {
4682+
if (!locator)
4683+
return nullptr;
4684+
4685+
if (auto *infoLocator = constraintSystem->getArgumentInfoLocator(locator)) {
4686+
auto known = argumentLists.find(infoLocator);
4687+
if (known != argumentLists.end())
4688+
return known->second;
4689+
}
4690+
return nullptr;
4691+
}
4692+
46714693
/// Given an apply expr, returns true if it is expected to have a direct callee
46724694
/// overload, resolvable using `getChoiceFor`. Otherwise, returns false.
46734695
static bool shouldHaveDirectCalleeOverload(const CallExpr *callExpr) {
@@ -4725,12 +4747,10 @@ Type Solution::resolveInterfaceType(Type type) const {
47254747

47264748
Optional<FunctionArgApplyInfo>
47274749
Solution::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
4728-
auto &cs = getConstraintSystem();
4729-
47304750
// It's only valid to use `&` in argument positions, but we need
47314751
// to figure out exactly where it was used.
47324752
if (auto *argExpr = getAsExpr<InOutExpr>(locator->getAnchor())) {
4733-
auto *argLoc = cs.getArgumentLocator(argExpr);
4753+
auto *argLoc = getConstraintSystem().getArgumentLocator(argExpr);
47344754
assert(argLoc && "Incorrect use of `inout` expression");
47354755
locator = argLoc;
47364756
}
@@ -4763,7 +4783,7 @@ Solution::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
47634783
if (!argExpr)
47644784
return None;
47654785

4766-
auto *argList = cs.getArgumentList(argLocator);
4786+
auto *argList = getArgumentList(argLocator);
47674787
if (!argList)
47684788
return None;
47694789

0 commit comments

Comments
 (0)