Skip to content

Commit 7e4f876

Browse files
author
ematejska
authored
Merge pull request #7059 from DougGregor/track-access-dc-in-constraints-3.1
[3.1] Track the actual DC of a member access in the constraint system.
2 parents 2f7ab8a + 10b222d commit 7e4f876

File tree

9 files changed

+243
-118
lines changed

9 files changed

+243
-118
lines changed

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7243,7 +7243,7 @@ Expr *TypeChecker::callWitness(Expr *base, DeclContext *dc,
72437243
// Form a reference to the witness itself.
72447244
Type openedFullType, openedType;
72457245
std::tie(openedFullType, openedType)
7246-
= cs.getTypeOfMemberReference(base->getType(), witness,
7246+
= cs.getTypeOfMemberReference(base->getType(), witness, dc,
72477247
/*isTypeReference=*/false,
72487248
/*isDynamicResult=*/false,
72497249
FunctionRefKind::DoubleApply,

lib/Sema/CSGen.cpp

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,8 @@ namespace {
10191019
namespace {
10201020
class ConstraintGenerator : public ExprVisitor<ConstraintGenerator, Type> {
10211021
ConstraintSystem &CS;
1022+
DeclContext *CurDC;
1023+
SmallVector<DeclContext*, 4> DCStack;
10221024

10231025
/// \brief Add constraints for a reference to a named member of the given
10241026
/// base type, and return the type of such a reference.
@@ -1031,7 +1033,7 @@ namespace {
10311033
auto tv = CS.createTypeVariable(
10321034
CS.getConstraintLocator(expr, ConstraintLocator::Member),
10331035
TVO_CanBindToLValue);
1034-
CS.addValueMemberConstraint(baseTy, name, tv, functionRefKind,
1036+
CS.addValueMemberConstraint(baseTy, name, tv, CurDC, functionRefKind,
10351037
CS.getConstraintLocator(expr, ConstraintLocator::Member));
10361038
return tv;
10371039
}
@@ -1055,7 +1057,7 @@ namespace {
10551057
OverloadChoice choice(CS.getType(base), decl, /*isSpecialized=*/false,
10561058
functionRefKind);
10571059
auto locator = CS.getConstraintLocator(expr, ConstraintLocator::Member);
1058-
CS.addBindOverloadConstraint(tv, choice, locator);
1060+
CS.addBindOverloadConstraint(tv, choice, locator, CurDC);
10591061
return tv;
10601062
}
10611063

@@ -1153,10 +1155,11 @@ namespace {
11531155
if (decl) {
11541156
OverloadChoice choice(baseTy, decl, /*isSpecialized=*/false,
11551157
FunctionRefKind::DoubleApply);
1156-
CS.addBindOverloadConstraint(fnTy, choice, subscriptMemberLocator);
1158+
CS.addBindOverloadConstraint(fnTy, choice, subscriptMemberLocator,
1159+
CurDC);
11571160
} else {
11581161
CS.addValueMemberConstraint(baseTy, Context.Id_subscript,
1159-
fnTy, FunctionRefKind::DoubleApply,
1162+
fnTy, CurDC, FunctionRefKind::DoubleApply,
11601163
subscriptMemberLocator);
11611164
}
11621165

@@ -1168,10 +1171,26 @@ namespace {
11681171
}
11691172

11701173
public:
1171-
ConstraintGenerator(ConstraintSystem &CS) : CS(CS) { }
1172-
virtual ~ConstraintGenerator() = default;
1174+
ConstraintGenerator(ConstraintSystem &CS) : CS(CS), CurDC(CS.DC) { }
1175+
virtual ~ConstraintGenerator() {
1176+
// We really ought to have this assertion:
1177+
// assert(DCStack.empty() && CurDC == CS.DC);
1178+
// Unfortunately, ASTWalker is really bad at letting us establish
1179+
// invariants like this because walkToExprPost isn't called if
1180+
// something early-aborts the walk.
1181+
}
11731182

11741183
ConstraintSystem &getConstraintSystem() const { return CS; }
1184+
1185+
void enterClosure(ClosureExpr *closure) {
1186+
DCStack.push_back(CurDC);
1187+
CurDC = closure;
1188+
}
1189+
1190+
void exitClosure(ClosureExpr *closure) {
1191+
assert(CurDC == closure);
1192+
CurDC = DCStack.pop_back_val();
1193+
}
11751194

11761195
virtual Type visitErrorExpr(ErrorExpr *E) {
11771196
// FIXME: Can we do anything with error expressions at this point?
@@ -1238,7 +1257,7 @@ namespace {
12381257
segmentTyV, locator);
12391258

12401259
DeclName segmentName(C, C.Id_init, { C.Id_stringInterpolationSegment });
1241-
CS.addValueMemberConstraint(tvMeta, segmentName, methodTy,
1260+
CS.addValueMemberConstraint(tvMeta, segmentName, methodTy, CurDC,
12421261
FunctionRefKind::DoubleApply, locator);
12431262
}
12441263

@@ -1347,7 +1366,8 @@ namespace {
13471366
CS.resolveOverload(locator, tv,
13481367
OverloadChoice(Type(), E->getDecl(),
13491368
E->isSpecialized(),
1350-
E->getFunctionRefKind()));
1369+
E->getFunctionRefKind()),
1370+
CurDC);
13511371

13521372
if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
13531373
if (VD->getInterfaceType() &&
@@ -1427,7 +1447,7 @@ namespace {
14271447
return nullptr;
14281448

14291449
// Record this overload set.
1430-
CS.addOverloadSet(tv, choices, locator);
1450+
CS.addOverloadSet(tv, choices, CurDC, locator);
14311451
return tv;
14321452
}
14331453

@@ -1473,7 +1493,7 @@ namespace {
14731493
// member, i.e., an enum case or a static variable.
14741494
auto baseMetaTy = MetatypeType::get(baseTy);
14751495
CS.addUnresolvedValueMemberConstraint(baseMetaTy, expr->getName(),
1476-
memberTy, functionRefKind,
1496+
memberTy, CurDC, functionRefKind,
14771497
memberLocator);
14781498

14791499
// If there is an argument, apply it.
@@ -1534,7 +1554,7 @@ namespace {
15341554
/*options=*/0);
15351555
auto methodTy = FunctionType::get(argsTy, resultTy);
15361556
CS.addValueMemberConstraint(baseTy, expr->getName(),
1537-
methodTy, expr->getFunctionRefKind(),
1557+
methodTy, CurDC, expr->getFunctionRefKind(),
15381558
CS.getConstraintLocator(expr, ConstraintLocator::ConstructorMember));
15391559

15401560
// The result of the expression is the partial application of the
@@ -2887,6 +2907,8 @@ namespace {
28872907
if (auto closure = dyn_cast<ClosureExpr>(expr)) {
28882908
auto &CS = CG.getConstraintSystem();
28892909
if (closure->hasSingleExpressionBody()) {
2910+
CG.enterClosure(closure);
2911+
28902912
// Visit the closure itself, which produces a function type.
28912913
auto funcTy = CG.visit(expr)->castTo<FunctionType>();
28922914
CS.setType(expr, funcTy);
@@ -2912,6 +2934,8 @@ namespace {
29122934
Expr *walkToExprPost(Expr *expr) override {
29132935
if (auto closure = dyn_cast<ClosureExpr>(expr)) {
29142936
if (closure->hasSingleExpressionBody()) {
2937+
CG.exitClosure(closure);
2938+
29152939
auto &CS = CG.getConstraintSystem();
29162940
Type closureTy = CS.getType(closure);
29172941

@@ -3311,7 +3335,7 @@ swift::resolveValueMember(DeclContext &DC, Type BaseTy, DeclName Name) {
33113335
return Result;
33123336
ConstraintLocator *Locator = CS.getConstraintLocator(nullptr);
33133337
TypeVariableType *TV = CS.createTypeVariable(Locator, TVO_CanBindToLValue);
3314-
CS.addOverloadSet(TV, LookupResult.ViableCandidates, Locator);
3338+
CS.addOverloadSet(TV, LookupResult.ViableCandidates, &DC, Locator);
33153339
Optional<Solution> OpSolution = CS.solveSingle();
33163340
if (!OpSolution.hasValue())
33173341
return Result;

lib/Sema/CSSimplify.cpp

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,8 +1258,6 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
12581258
// Obviously, this must not happen at the top level, or the
12591259
// algorithm would not terminate.
12601260
addUnsolvedConstraint(Constraint::create(*this, kind, type1, type2,
1261-
DeclName(),
1262-
FunctionRefKind::Compound,
12631261
getConstraintLocator(locator)));
12641262
return SolutionKind::Solved;
12651263
}
@@ -2182,6 +2180,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
21822180
ConstraintSystem::SolutionKind
21832181
ConstraintSystem::simplifyConstructionConstraint(
21842182
Type valueType, FunctionType *fnType, TypeMatchOptions flags,
2183+
DeclContext *useDC,
21852184
FunctionRefKind functionRefKind, ConstraintLocator *locator) {
21862185

21872186
// Desugar the value type.
@@ -2256,9 +2255,9 @@ ConstraintSystem::simplifyConstructionConstraint(
22562255
}
22572256

22582257
NameLookupOptions lookupOptions = defaultConstructorLookupOptions;
2259-
if (isa<AbstractFunctionDecl>(DC))
2258+
if (isa<AbstractFunctionDecl>(useDC))
22602259
lookupOptions |= NameLookupFlags::KnownPrivate;
2261-
auto ctors = TC.lookupConstructors(DC, valueType, lookupOptions);
2260+
auto ctors = TC.lookupConstructors(useDC, valueType, lookupOptions);
22622261
if (!ctors)
22632262
return SolutionKind::Error;
22642263

@@ -2275,7 +2274,8 @@ ConstraintSystem::simplifyConstructionConstraint(
22752274
// variable T. T2 is the result type provided via the construction
22762275
// constraint itself.
22772276
addValueMemberConstraint(MetatypeType::get(valueType, TC.Context), name,
2278-
FunctionType::get(tv, resultType), functionRefKind,
2277+
FunctionType::get(tv, resultType),
2278+
useDC, functionRefKind,
22792279
getConstraintLocator(
22802280
fnLocator,
22812281
ConstraintLocator::ConstructorMember));
@@ -2317,7 +2317,6 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
23172317
if (flags.contains(TMF_GenerateConstraints)) {
23182318
addUnsolvedConstraint(
23192319
Constraint::create(*this, kind, type, protocol->getDeclaredType(),
2320-
DeclName(), FunctionRefKind::Compound,
23212320
getConstraintLocator(locator)));
23222321
return SolutionKind::Solved;
23232322
}
@@ -2410,8 +2409,7 @@ ConstraintSystem::simplifyCheckedCastConstraint(
24102409
if (flags.contains(TMF_GenerateConstraints)) {
24112410
addUnsolvedConstraint(
24122411
Constraint::create(*this, ConstraintKind::CheckedCast, fromType,
2413-
toType, DeclName(), FunctionRefKind::Compound,
2414-
getConstraintLocator(locator)));
2412+
toType, getConstraintLocator(locator)));
24152413
return SolutionKind::Solved;
24162414
}
24172415

@@ -2541,8 +2539,7 @@ ConstraintSystem::simplifyOptionalObjectConstraint(
25412539
if (flags.contains(TMF_GenerateConstraints)) {
25422540
addUnsolvedConstraint(
25432541
Constraint::create(*this, ConstraintKind::OptionalObject, optLValueTy,
2544-
second, DeclName(), FunctionRefKind::Compound,
2545-
getConstraintLocator(locator)));
2542+
second, getConstraintLocator(locator)));
25462543
return SolutionKind::Solved;
25472544
}
25482545

@@ -3110,6 +3107,7 @@ ConstraintSystem::SolutionKind
31103107
ConstraintSystem::simplifyMemberConstraint(ConstraintKind kind,
31113108
Type baseTy, DeclName member,
31123109
Type memberTy,
3110+
DeclContext *useDC,
31133111
FunctionRefKind functionRefKind,
31143112
TypeMatchOptions flags,
31153113
ConstraintLocatorBuilder locatorB) {
@@ -3141,8 +3139,8 @@ ConstraintSystem::simplifyMemberConstraint(ConstraintKind kind,
31413139
// If requested, generate a constraint.
31423140
if (flags.contains(TMF_GenerateConstraints)) {
31433141
addUnsolvedConstraint(
3144-
Constraint::create(*this, kind, baseTy, memberTy, member,
3145-
functionRefKind, locator));
3142+
Constraint::createMember(*this, kind, baseTy, memberTy, member, useDC,
3143+
functionRefKind, locator));
31463144
return SolutionKind::Solved;
31473145
}
31483146

@@ -3158,7 +3156,7 @@ ConstraintSystem::simplifyMemberConstraint(ConstraintKind kind,
31583156

31593157
// If we found viable candidates, then we're done!
31603158
if (!result.ViableCandidates.empty()) {
3161-
addOverloadSet(memberTy, result.ViableCandidates, locator,
3159+
addOverloadSet(memberTy, result.ViableCandidates, useDC, locator,
31623160
result.getFavoredChoice());
31633161

31643162
return SolutionKind::Solved;
@@ -3196,7 +3194,7 @@ ConstraintSystem::simplifyMemberConstraint(ConstraintKind kind,
31963194

31973195
// Look through one level of optional.
31983196
addValueMemberConstraint(baseObjTy->getOptionalObjectType(),
3199-
member, memberTy, functionRefKind, locator);
3197+
member, memberTy, useDC, functionRefKind, locator);
32003198
return SolutionKind::Solved;
32013199
}
32023200
return SolutionKind::Error;
@@ -3213,7 +3211,6 @@ ConstraintSystem::simplifyDefaultableConstraint(
32133211
if (flags.contains(TMF_GenerateConstraints)) {
32143212
addUnsolvedConstraint(
32153213
Constraint::create(*this, ConstraintKind::Defaultable, first, second,
3216-
DeclName(), FunctionRefKind::Compound,
32173214
getConstraintLocator(locator)));
32183215
return SolutionKind::Solved;
32193216
}
@@ -3238,7 +3235,6 @@ ConstraintSystem::simplifyDynamicTypeOfConstraint(
32383235
if (flags.contains(TMF_GenerateConstraints)) {
32393236
addUnsolvedConstraint(
32403237
Constraint::create(*this, ConstraintKind::DynamicTypeOf, type1, type2,
3241-
DeclName(), FunctionRefKind::Compound,
32423238
getConstraintLocator(locator)));
32433239
return SolutionKind::Solved;
32443240
}
@@ -3300,8 +3296,7 @@ ConstraintSystem::simplifyBridgingConstraint(Type type1,
33003296
if (flags.contains(TMF_GenerateConstraints)) {
33013297
addUnsolvedConstraint(
33023298
Constraint::create(*this, ConstraintKind::BridgingConversion, type1,
3303-
type2, DeclName(), FunctionRefKind::Compound,
3304-
getConstraintLocator(locator)));
3299+
type2, getConstraintLocator(locator)));
33053300
return SolutionKind::Solved;
33063301
}
33073302

@@ -3527,9 +3522,7 @@ ConstraintSystem::simplifyEscapableFunctionOfConstraint(
35273522
if (flags.contains(TMF_GenerateConstraints)) {
35283523
addUnsolvedConstraint(
35293524
Constraint::create(*this, ConstraintKind::EscapableFunctionOf,
3530-
type1, type2,
3531-
DeclName(), FunctionRefKind::Compound,
3532-
getConstraintLocator(locator)));
3525+
type1, type2, getConstraintLocator(locator)));
35333526
return SolutionKind::Solved;
35343527
}
35353528

@@ -3602,8 +3595,7 @@ ConstraintSystem::simplifyApplicableFnConstraint(
36023595
if (flags.contains(TMF_GenerateConstraints)) {
36033596
addUnsolvedConstraint(
36043597
Constraint::create(*this, ConstraintKind::ApplicableFunction, type1,
3605-
type2, DeclName(), FunctionRefKind::Compound,
3606-
getConstraintLocator(locator)));
3598+
type2, getConstraintLocator(locator)));
36073599
return SolutionKind::Solved;
36083600
}
36093601

@@ -3673,6 +3665,7 @@ ConstraintSystem::simplifyApplicableFnConstraint(
36733665

36743666
// Construct the instance from the input arguments.
36753667
return simplifyConstructionConstraint(instance2, func1, subflags,
3668+
/*FIXME?*/ DC,
36763669
FunctionRefKind::SingleApply,
36773670
getConstraintLocator(outerLocator));
36783671
}
@@ -3889,18 +3882,12 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
38893882
if (flags.contains(TMF_GenerateConstraints)) {
38903883
auto int8Con = Constraint::create(*this, ConstraintKind::Bind,
38913884
baseType2, TC.getInt8Type(DC),
3892-
DeclName(),
3893-
FunctionRefKind::Compound,
38943885
getConstraintLocator(locator));
38953886
auto uint8Con = Constraint::create(*this, ConstraintKind::Bind,
38963887
baseType2, TC.getUInt8Type(DC),
3897-
DeclName(),
3898-
FunctionRefKind::Compound,
38993888
getConstraintLocator(locator));
39003889
auto voidCon = Constraint::create(*this, ConstraintKind::Bind,
39013890
baseType2, TC.Context.TheEmptyTupleType,
3902-
DeclName(),
3903-
FunctionRefKind::Compound,
39043891
getConstraintLocator(locator));
39053892

39063893
Constraint *disjunctionChoices[] = {int8Con, uint8Con, voidCon};
@@ -4229,8 +4216,7 @@ void ConstraintSystem::addConstraint(ConstraintKind kind, Type first,
42294216
case SolutionKind::Error:
42304217
// Add a failing constraint, if needed.
42314218
if (shouldAddNewFailingConstraint()) {
4232-
auto c = Constraint::create(*this, kind, first, second, DeclName(),
4233-
FunctionRefKind::Compound,
4219+
auto c = Constraint::create(*this, kind, first, second,
42344220
getConstraintLocator(locator));
42354221
if (isFavored) c->setFavored();
42364222
addNewFailingConstraint(c);
@@ -4256,9 +4242,7 @@ void ConstraintSystem::addExplicitConversionConstraint(
42564242
// Coercion (the common case).
42574243
Constraint *coerceConstraint =
42584244
Constraint::create(*this, ConstraintKind::Conversion,
4259-
fromType, toType, DeclName(),
4260-
FunctionRefKind::Compound,
4261-
locatorPtr);
4245+
fromType, toType, locatorPtr);
42624246
coerceConstraint->setFavored();
42634247
constraints.push_back(coerceConstraint);
42644248

@@ -4267,9 +4251,7 @@ void ConstraintSystem::addExplicitConversionConstraint(
42674251
// The source type can be explicitly converted to the destination type.
42684252
Constraint *bridgingConstraint =
42694253
Constraint::create(*this, ConstraintKind::BridgingConversion,
4270-
fromType, toType, DeclName(),
4271-
FunctionRefKind::Compound,
4272-
locatorPtr);
4254+
fromType, toType, locatorPtr);
42734255
constraints.push_back(bridgingConstraint);
42744256
}
42754257

@@ -4347,7 +4329,8 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
43474329

43484330
case ConstraintKind::BindOverload:
43494331
resolveOverload(constraint.getLocator(), constraint.getFirstType(),
4350-
constraint.getOverloadChoice());
4332+
constraint.getOverloadChoice(),
4333+
constraint.getOverloadUseDC());
43514334
return SolutionKind::Solved;
43524335

43534336
case ConstraintKind::ConformsTo:
@@ -4391,6 +4374,7 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
43914374
constraint.getFirstType(),
43924375
constraint.getMember(),
43934376
constraint.getSecondType(),
4377+
constraint.getMemberUseDC(),
43944378
constraint.getFunctionRefKind(),
43954379
TMF_GenerateConstraints,
43964380
constraint.getLocator());

0 commit comments

Comments
 (0)