Skip to content

Commit 436c7f2

Browse files
authored
Merge pull request #41988 from xedin/csapply-fix-use-of-incorrect-dc
[CSApply] Use declaration context associated with rewriter
2 parents 3f37fc0 + be08bac commit 436c7f2

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

lib/Sema/CSApply.cpp

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ namespace {
513513

514514
return SubstitutionMap::get(sig,
515515
QueryTypeSubstitutionMap{subs},
516-
LookUpConformanceInModule(cs.DC->getParentModule()));
516+
LookUpConformanceInModule(dc->getParentModule()));
517517
}
518518

519519
/// Determine whether the given reference is to a method on
@@ -532,7 +532,7 @@ namespace {
532532
// Determine the declaration selected for this overloaded reference.
533533
auto &ctx = cs.getASTContext();
534534

535-
auto semantics = decl->getAccessSemanticsFromContext(cs.DC,
535+
auto semantics = decl->getAccessSemanticsFromContext(dc,
536536
/*isAccessOnSelf*/false);
537537

538538
// If this is a member of a nominal type, build a reference to the
@@ -552,7 +552,7 @@ namespace {
552552
// the protocol requirement with Self == the concrete type, and SILGen
553553
// (or later) can devirtualize as appropriate.
554554
auto conformance =
555-
TypeChecker::conformsToProtocol(baseTy, proto, cs.DC->getParentModule());
555+
TypeChecker::conformsToProtocol(baseTy, proto, dc->getParentModule());
556556
if (conformance.isConcrete()) {
557557
if (auto witness = conformance.getConcrete()->getWitnessDecl(decl)) {
558558
bool isMemberOperator = witness->getDeclContext()->isTypeContext();
@@ -859,7 +859,7 @@ namespace {
859859
resultTy = cs.getType(result);
860860
if (resultTy->hasOpenedExistentialWithRoot(record.Archetype)) {
861861
Type erasedTy = resultTy->typeEraseOpenedArchetypesWithRoot(
862-
record.Archetype, cs.DC);
862+
record.Archetype, dc);
863863
auto range = result->getSourceRange();
864864
result = coerceToType(result, erasedTy, locator);
865865
// FIXME: Implement missing tuple-to-tuple conversion
@@ -1288,7 +1288,7 @@ namespace {
12881288
// reference.
12891289
if (auto *TD = dyn_cast<TypeDecl>(member)) {
12901290
Type refType = simplifyType(openedType);
1291-
auto ref = TypeExpr::createForDecl(memberLoc, TD, cs.DC);
1291+
auto ref = TypeExpr::createForDecl(memberLoc, TD, dc);
12921292
cs.setType(ref, refType);
12931293
auto *result = new (context) DotSyntaxBaseIgnoredExpr(
12941294
base, dotLoc, ref, refType);
@@ -1354,7 +1354,7 @@ namespace {
13541354
// Erase opened existentials from the type of the thunk; we're
13551355
// going to open the existential inside the thunk's body.
13561356
containerTy = containerTy->typeEraseOpenedArchetypesWithRoot(
1357-
knownOpened->second, cs.DC);
1357+
knownOpened->second, dc);
13581358
selfTy = containerTy;
13591359
}
13601360
}
@@ -1414,7 +1414,7 @@ namespace {
14141414
// existential.
14151415
if (openedExistential) {
14161416
refType = refType->typeEraseOpenedArchetypesWithRoot(
1417-
baseTy->castTo<OpenedArchetypeType>(), cs.DC);
1417+
baseTy->castTo<OpenedArchetypeType>(), dc);
14181418
}
14191419

14201420
cs.setType(ref, refType);
@@ -1555,7 +1555,7 @@ namespace {
15551555
if (knownOpened != solution.OpenedExistentialTypes.end()) {
15561556
curryThunkTy = curryThunkTy
15571557
->typeEraseOpenedArchetypesWithRoot(
1558-
knownOpened->second, cs.DC)
1558+
knownOpened->second, dc)
15591559
->castTo<FunctionType>();
15601560
}
15611561

@@ -1569,7 +1569,7 @@ namespace {
15691569
SourceLoc(),
15701570
/*argument label*/ SourceLoc(), Identifier(),
15711571
/*parameter name*/ SourceLoc(), context.Id_self,
1572-
cs.DC);
1572+
dc);
15731573

15741574
auto selfParamTy = selfParam.getPlainType();
15751575
bool isLValue = selfParam.isInOut();
@@ -2212,7 +2212,7 @@ namespace {
22122212
auto bridgedToObjectiveCConformance
22132213
= TypeChecker::conformsToProtocol(valueType,
22142214
bridgedProto,
2215-
cs.DC->getParentModule());
2215+
dc->getParentModule());
22162216

22172217
FuncDecl *fn = nullptr;
22182218

@@ -2473,7 +2473,7 @@ namespace {
24732473
ProtocolDecl *protocol = TypeChecker::getProtocol(
24742474
ctx, expr->getLoc(), KnownProtocolKind::ExpressibleByStringLiteral);
24752475

2476-
if (!TypeChecker::conformsToProtocol(type, protocol, cs.DC->getParentModule())) {
2476+
if (!TypeChecker::conformsToProtocol(type, protocol, dc->getParentModule())) {
24772477
// If the type does not conform to ExpressibleByStringLiteral, it should
24782478
// be ExpressibleByExtendedGraphemeClusterLiteral.
24792479
protocol = TypeChecker::getProtocol(
@@ -2482,7 +2482,7 @@ namespace {
24822482
isStringLiteral = false;
24832483
isGraphemeClusterLiteral = true;
24842484
}
2485-
if (!TypeChecker::conformsToProtocol(type, protocol, cs.DC->getParentModule())) {
2485+
if (!TypeChecker::conformsToProtocol(type, protocol, dc->getParentModule())) {
24862486
// ... or it should be ExpressibleByUnicodeScalarLiteral.
24872487
protocol = TypeChecker::getProtocol(
24882488
cs.getASTContext(), expr->getLoc(),
@@ -2597,7 +2597,7 @@ namespace {
25972597
assert(proto && "Missing string interpolation protocol?");
25982598

25992599
auto conformance =
2600-
TypeChecker::conformsToProtocol(type, proto, cs.DC->getParentModule());
2600+
TypeChecker::conformsToProtocol(type, proto, dc->getParentModule());
26012601
assert(conformance && "string interpolation type conforms to protocol");
26022602

26032603
DeclName constrName(ctx, DeclBaseName::createConstructor(), argLabels);
@@ -2711,7 +2711,7 @@ namespace {
27112711
assert(proto && "Missing object literal protocol?");
27122712
auto conformance =
27132713
TypeChecker::conformsToProtocol(conformingType, proto,
2714-
cs.DC->getParentModule());
2714+
dc->getParentModule());
27152715
assert(conformance && "object literal type conforms to protocol");
27162716

27172717
auto constrName = TypeChecker::getObjectLiteralConstructorName(ctx, expr);
@@ -2931,7 +2931,7 @@ namespace {
29312931
diagnoseBadInitRef = false;
29322932
// Make sure the reference to 'self' occurs within an initializer.
29332933
if (!dyn_cast_or_null<ConstructorDecl>(
2934-
cs.DC->getInnermostMethodContext())) {
2934+
dc->getInnermostMethodContext())) {
29352935
if (!SuppressDiagnostics)
29362936
de.diagnose(dotLoc, diag::init_delegation_outside_initializer);
29372937
return nullptr;
@@ -2944,7 +2944,7 @@ namespace {
29442944
if (diagnoseBadInitRef) {
29452945
// Determine whether 'super' would have made sense as a base.
29462946
bool hasSuper = false;
2947-
if (auto func = cs.DC->getInnermostMethodContext()) {
2947+
if (auto func = dc->getInnermostMethodContext()) {
29482948
if (auto classDecl = func->getDeclContext()->getSelfClassDecl()) {
29492949
hasSuper = classDecl->hasSuperclass();
29502950
}
@@ -2984,7 +2984,7 @@ namespace {
29842984
};
29852985

29862986
auto result =
2987-
TypeChecker::lookupUnqualified(cs.DC, UDE->getName(), UDE->getLoc());
2987+
TypeChecker::lookupUnqualified(dc, UDE->getName(), UDE->getLoc());
29882988
assert(result && "names can't just disappear");
29892989
// These should all come from the same place.
29902990
auto exampleInner = result.front();
@@ -3066,7 +3066,7 @@ namespace {
30663066
auto &ctx = cs.getASTContext();
30673067
auto baseMetaTy = baseTy->getAs<MetatypeType>();
30683068
auto baseInstTy = (baseMetaTy ? baseMetaTy->getInstanceType() : baseTy);
3069-
auto classTy = ctx.getBridgedToObjC(cs.DC, baseInstTy);
3069+
auto classTy = ctx.getBridgedToObjC(dc, baseInstTy);
30703070

30713071
if (baseMetaTy) {
30723072
// FIXME: We're dropping side effects in the base here!
@@ -3328,7 +3328,7 @@ namespace {
33283328

33293329
auto conformance =
33303330
TypeChecker::conformsToProtocol(arrayTy, arrayProto,
3331-
cs.DC->getParentModule());
3331+
dc->getParentModule());
33323332
assert(conformance && "Type does not conform to protocol?");
33333333

33343334
DeclName name(ctx, DeclBaseName::createConstructor(),
@@ -3373,7 +3373,7 @@ namespace {
33733373

33743374
auto conformance =
33753375
TypeChecker::conformsToProtocol(dictionaryTy, dictionaryProto,
3376-
cs.DC->getParentModule());
3376+
dc->getParentModule());
33773377
if (conformance.isInvalid())
33783378
return nullptr;
33793379

@@ -3498,7 +3498,7 @@ namespace {
34983498
// initializer.
34993499
Expr *unwrappedSubExpr = expr->getSubExpr()->getSemanticsProvidingExpr();
35003500
Type valueTy = cs.getType(unwrappedSubExpr)->getOptionalObjectType();
3501-
auto inCtor = cast<ConstructorDecl>(cs.DC->getInnermostMethodContext());
3501+
auto inCtor = cast<ConstructorDecl>(dc->getInnermostMethodContext());
35023502
if (valueTy && !inCtor->isFailable()) {
35033503
bool isChaining;
35043504
auto *otherCtorRef = expr->getCalledConstructor(isChaining);
@@ -3588,7 +3588,7 @@ namespace {
35883588
SuppressDiagnostics ? CheckedCastContextKind::None
35893589
: CheckedCastContextKind::IsExpr;
35903590
auto castKind = TypeChecker::typeCheckCheckedCast(
3591-
fromType, toType, castContextKind, cs.DC, expr->getLoc(), sub,
3591+
fromType, toType, castContextKind, dc, expr->getLoc(), sub,
35923592
castTypeRepr->getSourceRange());
35933593

35943594
switch (castKind) {
@@ -4302,7 +4302,7 @@ namespace {
43024302
Expr *callExpr = CallExpr::createImplicit(ctx, fnRef, argList);
43034303

43044304
auto resultTy = TypeChecker::typeCheckExpression(
4305-
callExpr, cs.DC, /*contextualInfo=*/{valueType, CTP_CannotFail});
4305+
callExpr, dc, /*contextualInfo=*/{valueType, CTP_CannotFail});
43064306
assert(resultTy && "Conversion cannot fail!");
43074307
(void)resultTy;
43084308

@@ -4470,8 +4470,8 @@ namespace {
44704470
// Check that we requested a property getter or setter.
44714471
switch (E->getSelectorKind()) {
44724472
case ObjCSelectorExpr::Method: {
4473-
bool isSettable = var->isSettable(cs.DC) &&
4474-
var->isSetterAccessibleFrom(cs.DC);
4473+
bool isSettable = var->isSettable(dc) &&
4474+
var->isSetterAccessibleFrom(dc);
44754475
auto primaryDiag =
44764476
de.diagnose(E->getLoc(), diag::expr_selector_expected_method,
44774477
isSettable, var->getName());
@@ -4512,15 +4512,15 @@ namespace {
45124512

45134513
case ObjCSelectorExpr::Setter:
45144514
// Make sure we actually have a setter.
4515-
if (!var->isSettable(cs.DC)) {
4515+
if (!var->isSettable(dc)) {
45164516
de.diagnose(E->getLoc(), diag::expr_selector_property_not_settable,
45174517
var->getDescriptiveKind(), var->getName());
45184518
de.diagnose(var, diag::decl_declared_here, var->getName());
45194519
return E;
45204520
}
45214521

45224522
// Make sure the setter is accessible.
4523-
if (!var->isSetterAccessibleFrom(cs.DC)) {
4523+
if (!var->isSetterAccessibleFrom(dc)) {
45244524
de.diagnose(E->getLoc(),
45254525
diag::expr_selector_property_setter_inaccessible,
45264526
var->getDescriptiveKind(), var->getName());
@@ -4802,7 +4802,7 @@ namespace {
48024802
FunctionType::get({FunctionType::Param(baseTy)}, leafTy, closureInfo);
48034803
auto closure = new (ctx)
48044804
AutoClosureExpr(/*set body later*/nullptr, leafTy,
4805-
discriminator, cs.DC);
4805+
discriminator, dc);
48064806
auto param = new (ctx) ParamDecl(
48074807
SourceLoc(),
48084808
/*argument label*/ SourceLoc(), Identifier(),
@@ -4821,7 +4821,7 @@ namespace {
48214821
closureTy, outerClosureInfo);
48224822
auto outerClosure = new (ctx)
48234823
AutoClosureExpr(/*set body later*/nullptr, closureTy,
4824-
discriminator, cs.DC);
4824+
discriminator, dc);
48254825
auto outerParam =
48264826
new (ctx) ParamDecl(SourceLoc(),
48274827
/*argument label*/ SourceLoc(), Identifier(),
@@ -4989,7 +4989,7 @@ namespace {
49894989
// with all of the generic parameters resolved.
49904990
auto hashableConformance =
49914991
TypeChecker::conformsToProtocol(indexType, hashable,
4992-
cs.DC->getParentModule());
4992+
dc->getParentModule());
49934993
assert(hashableConformance);
49944994

49954995
conformances.push_back(hashableConformance);
@@ -5270,7 +5270,7 @@ Expr *ExprRewriter::coerceSuperclass(Expr *expr, Type toType) {
52705270
// concrete superclass.
52715271
auto fromArchetype =
52725272
OpenedArchetypeType::getAny(fromType->getCanonicalType(),
5273-
cs.DC->getGenericSignatureOfContext());
5273+
dc->getGenericSignatureOfContext());
52745274

52755275
auto *archetypeVal = cs.cacheType(new (ctx) OpaqueValueExpr(
52765276
expr->getSourceRange(), fromArchetype));
@@ -6023,7 +6023,8 @@ static Expr *buildElementConversion(ExprRewriter &rewriter,
60236023
auto &cs = rewriter.getConstraintSystem();
60246024
if (bridged &&
60256025
TypeChecker::typeCheckCheckedCast(srcType, destType,
6026-
CheckedCastContextKind::None, cs.DC,
6026+
CheckedCastContextKind::None,
6027+
rewriter.dc,
60276028
SourceLoc(), nullptr, SourceRange())
60286029
!= CheckedCastKind::Coercion) {
60296030
if (auto conversion =
@@ -6279,7 +6280,7 @@ Expr *ExprRewriter::coerceExistential(Expr *expr, Type toType,
62796280

62806281
auto conformances =
62816282
collectExistentialConformances(fromInstanceType, toInstanceType,
6282-
cs.DC->getParentModule());
6283+
dc->getParentModule());
62836284

62846285
// Use the requirements of any parameterized protocols to build out fake
62856286
// argument conversions that can be used to infer opaque types.
@@ -6336,7 +6337,7 @@ Expr *ExprRewriter::coerceExistential(Expr *expr, Type toType,
63366337
// For existential-to-existential coercions, open the source existential.
63376338
if (fromType->isAnyExistentialType()) {
63386339
fromType = OpenedArchetypeType::getAny(fromType->getCanonicalType(),
6339-
cs.DC->getGenericSignatureOfContext());
6340+
dc->getGenericSignatureOfContext());
63406341

63416342
auto *archetypeVal = cs.cacheType(
63426343
new (ctx) OpaqueValueExpr(expr->getSourceRange(), fromType));
@@ -6500,7 +6501,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
65006501
auto hashable = ctx.getProtocol(KnownProtocolKind::Hashable);
65016502
auto conformance =
65026503
TypeChecker::conformsToProtocol(
6503-
cs.getType(expr), hashable, cs.DC->getParentModule());
6504+
cs.getType(expr), hashable, dc->getParentModule());
65046505
assert(conformance && "must conform to Hashable");
65056506

65066507
return cs.cacheType(
@@ -6759,7 +6760,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
67596760
// leave an explicit escape to noescape cast here such that SILGen can skip
67606761
// the cast and emit a code for the escaping function.
67616762
bool isInDefaultArgumentContext = false;
6762-
if (auto initalizerCtx = dyn_cast<Initializer>(cs.DC))
6763+
if (auto initalizerCtx = dyn_cast<Initializer>(dc))
67636764
isInDefaultArgumentContext = (initalizerCtx->getInitializerKind() ==
67646765
InitializerKind::DefaultArgument);
67656766
auto toEI = toFunc->getExtInfo();
@@ -7209,7 +7210,7 @@ Expr *ExprRewriter::convertLiteralInPlace(
72097210
// initialize via the builtin protocol.
72107211
if (builtinProtocol) {
72117212
auto builtinConformance = TypeChecker::conformsToProtocol(
7212-
type, builtinProtocol, cs.DC->getParentModule());
7213+
type, builtinProtocol, dc->getParentModule());
72137214
if (builtinConformance) {
72147215
// Find the witness that we'll use to initialize the type via a builtin
72157216
// literal.
@@ -7233,7 +7234,7 @@ Expr *ExprRewriter::convertLiteralInPlace(
72337234
// This literal type must conform to the (non-builtin) protocol.
72347235
assert(protocol && "requirements should have stopped recursion");
72357236
auto conformance = TypeChecker::conformsToProtocol(type, protocol,
7236-
cs.DC->getParentModule());
7237+
dc->getParentModule());
72377238
assert(conformance && "must conform to literal protocol");
72387239

72397240
// Dig out the literal type and perform a builtin literal conversion to it.
@@ -7358,7 +7359,7 @@ std::pair<Expr *, ArgumentList *> ExprRewriter::buildDynamicCallable(
73587359
ctx.getProtocol(KnownProtocolKind::ExpressibleByDictionaryLiteral);
73597360
auto conformance =
73607361
TypeChecker::conformsToProtocol(argumentType, dictLitProto,
7361-
cs.DC->getParentModule());
7362+
dc->getParentModule());
73627363
auto keyType = conformance.getTypeWitnessByName(argumentType, ctx.Id_Key);
73637364
auto valueType =
73647365
conformance.getTypeWitnessByName(argumentType, ctx.Id_Value);
@@ -8478,14 +8479,16 @@ static Optional<SolutionApplicationTarget> applySolutionToForEachStmt(
84788479

84798480
resultTarget.setExpr(sequence);
84808481

8482+
auto *dc = target.getDeclContext();
8483+
84818484
// Get the conformance of the sequence type to the Sequence protocol.
84828485
auto stmt = forEachStmtInfo.stmt;
84838486
auto sequenceProto = TypeChecker::getProtocol(
84848487
cs.getASTContext(), stmt->getForLoc(),
84858488
stmt->getAwaitLoc().isValid() ?
84868489
KnownProtocolKind::AsyncSequence : KnownProtocolKind::Sequence);
84878490
auto sequenceConformance = TypeChecker::conformsToProtocol(
8488-
forEachStmtInfo.sequenceType, sequenceProto, cs.DC->getParentModule());
8491+
forEachStmtInfo.sequenceType, sequenceProto, dc->getParentModule());
84898492
assert(!sequenceConformance.isInvalid() &&
84908493
"Couldn't find sequence conformance");
84918494

@@ -8503,7 +8506,6 @@ static Optional<SolutionApplicationTarget> applySolutionToForEachStmt(
85038506
}
85048507

85058508
// Apply the solution to the filtering condition, if there is one.
8506-
auto dc = target.getDeclContext();
85078509
if (forEachStmtInfo.whereExpr) {
85088510
auto *boolDecl = dc->getASTContext().getBoolDecl();
85098511
assert(boolDecl);

0 commit comments

Comments
 (0)