Skip to content

Commit e2f8006

Browse files
authored
Merge pull request #75011 from hamishknight/context-is-key
[Sema] Enforce PatternTypeRequest returns contextual type
2 parents 156aef5 + 18531b6 commit e2f8006

13 files changed

+45
-60
lines changed

lib/Sema/CodeSynthesisDistributedActor.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ static VarDecl *addImplicitDistributedActorIDProperty(
100100
propDecl->copyFormalAccessFrom(nominal, /*sourceIsParentContext*/ true);
101101
propDecl->setInterfaceType(propertyType);
102102

103-
Pattern *propPat = NamedPattern::createImplicit(C, propDecl, propertyType);
104-
propPat = TypedPattern::createImplicit(C, propPat, propertyType);
105-
propPat->setType(propertyType);
103+
auto propContextTy = nominal->mapTypeIntoContext(propertyType);
104+
105+
Pattern *propPat = NamedPattern::createImplicit(C, propDecl, propContextTy);
106+
propPat = TypedPattern::createImplicit(C, propPat, propContextTy);
106107

107108
PatternBindingDecl *pbDecl = PatternBindingDecl::createImplicit(
108109
C, StaticSpellingKind::None, propPat, /*InitExpr*/ nullptr,
@@ -150,9 +151,10 @@ static VarDecl *addImplicitDistributedActorActorSystemProperty(
150151
propDecl->copyFormalAccessFrom(nominal, /*sourceIsParentContext*/ true);
151152
propDecl->setInterfaceType(propertyType);
152153

153-
Pattern *propPat = NamedPattern::createImplicit(C, propDecl, propertyType);
154-
propPat = TypedPattern::createImplicit(C, propPat, propertyType);
155-
propPat->setType(propertyType);
154+
auto propContextTy = nominal->mapTypeIntoContext(propertyType);
155+
156+
Pattern *propPat = NamedPattern::createImplicit(C, propDecl, propContextTy);
157+
propPat = TypedPattern::createImplicit(C, propPat, propContextTy);
156158

157159
PatternBindingDecl *pbDecl = PatternBindingDecl::createImplicit(
158160
C, StaticSpellingKind::None, propPat, /*InitExpr*/ nullptr,

lib/Sema/DerivedConformanceActor.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ static ValueDecl *deriveActor_unownedExecutor(DerivedConformance &derived) {
140140

141141
auto propertyPair = derived.declareDerivedProperty(
142142
DerivedConformance::SynthesizedIntroducer::Var, ctx.Id_unownedExecutor,
143-
executorType, executorType,
144-
/*static*/ false, /*final*/ false);
143+
executorType, /*static*/ false, /*final*/ false);
145144
auto property = propertyPair.first;
146145
property->setSynthesized(true);
147146
property->getAttrs().add(new (ctx) SemanticsAttr(SEMANTICS_DEFAULT_ACTOR,
@@ -164,8 +163,7 @@ static ValueDecl *deriveActor_unownedExecutor(DerivedConformance &derived) {
164163
AvailabilityInference::applyInferredAvailableAttrs(
165164
property, asAvailableAs, ctx);
166165

167-
auto getter =
168-
derived.addGetterToReadOnlyDerivedProperty(property, executorType);
166+
auto getter = derived.addGetterToReadOnlyDerivedProperty(property);
169167
getter->setBodySynthesizer(deriveBodyActor_unownedExecutor);
170168

171169
derived.addMembersToConformanceContext(

lib/Sema/DerivedConformanceAdditiveArithmetic.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,22 +292,19 @@ deriveBodyAdditiveArithmetic_zero(AbstractFunctionDecl *funcDecl, void *) {
292292
static ValueDecl *deriveAdditiveArithmetic_zero(DerivedConformance &derived) {
293293
auto &C = derived.Context;
294294
auto *nominal = derived.Nominal;
295-
auto *parentDC = derived.getConformanceContext();
296295

297296
auto returnInterfaceTy = nominal->getDeclaredInterfaceType();
298-
auto returnTy = parentDC->mapTypeIntoContext(returnInterfaceTy);
299297

300298
// Create property declaration.
301299
VarDecl *propDecl;
302300
PatternBindingDecl *pbDecl;
303301
std::tie(propDecl, pbDecl) = derived.declareDerivedProperty(
304302
DerivedConformance::SynthesizedIntroducer::Var, C.Id_zero,
305-
returnInterfaceTy, returnTy, /*isStatic*/ true,
306-
/*isFinal*/ true);
303+
returnInterfaceTy,
304+
/*isStatic*/ true, /*isFinal*/ true);
307305

308306
// Create property getter.
309-
auto *getterDecl =
310-
derived.addGetterToReadOnlyDerivedProperty(propDecl, returnTy);
307+
auto *getterDecl = derived.addGetterToReadOnlyDerivedProperty(propDecl);
311308
getterDecl->setBodySynthesizer(deriveBodyAdditiveArithmetic_zero, nullptr);
312309

313310
derived.addMembersToConformanceContext({propDecl, pbDecl});

lib/Sema/DerivedConformanceCaseIterable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ ValueDecl *DerivedConformance::deriveCaseIterable(ValueDecl *requirement) {
101101
VarDecl *propDecl;
102102
PatternBindingDecl *pbDecl;
103103
std::tie(propDecl, pbDecl) = declareDerivedProperty(
104-
SynthesizedIntroducer::Var, Context.Id_allCases, returnTy, returnTy,
104+
SynthesizedIntroducer::Var, Context.Id_allCases, returnTy,
105105
/*isStatic=*/true, /*isFinal=*/true);
106106

107107
propDecl->getAttrs().add(
108108
new (C) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));
109109

110110
// Define the getter.
111-
auto *getterDecl = addGetterToReadOnlyDerivedProperty(propDecl, returnTy);
111+
auto *getterDecl = addGetterToReadOnlyDerivedProperty(propDecl);
112112

113113
getterDecl->setBodySynthesizer(&deriveCaseIterable_enum_getter);
114114

lib/Sema/DerivedConformanceCodingKey.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,11 @@ static ValueDecl *deriveProperty(DerivedConformance &derived, Type type,
163163
VarDecl *propDecl;
164164
PatternBindingDecl *pbDecl;
165165
std::tie(propDecl, pbDecl) = derived.declareDerivedProperty(
166-
DerivedConformance::SynthesizedIntroducer::Var, name, type, type,
166+
DerivedConformance::SynthesizedIntroducer::Var, name, type,
167167
/*isStatic=*/false, /*isFinal=*/false);
168168

169169
// Define the getter.
170-
auto *getterDecl = derived.addGetterToReadOnlyDerivedProperty(
171-
propDecl, type);
170+
auto *getterDecl = derived.addGetterToReadOnlyDerivedProperty(propDecl);
172171

173172
// Synthesize the body.
174173
synthesizer(getterDecl);

lib/Sema/DerivedConformanceDifferentiable.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -413,14 +413,16 @@ getOrSynthesizeTangentVectorStruct(DerivedConformance &derived, Identifier id) {
413413
// causes the type checker to not guarantee the order of these members.
414414
auto memberContextualType =
415415
parentDC->mapTypeIntoContext(member->getValueInterfaceType());
416-
auto memberTanType =
416+
auto memberTanInterfaceType =
417417
getTangentVectorInterfaceType(memberContextualType, parentDC);
418-
tangentProperty->setInterfaceType(memberTanType);
418+
tangentProperty->setInterfaceType(memberTanInterfaceType);
419+
auto memberTanContextType =
420+
parentDC->mapTypeIntoContext(memberTanInterfaceType);
419421
Pattern *memberPattern =
420-
NamedPattern::createImplicit(C, tangentProperty, memberTanType);
422+
NamedPattern::createImplicit(C, tangentProperty, memberTanContextType);
421423
memberPattern =
422-
TypedPattern::createImplicit(C, memberPattern, memberTanType);
423-
memberPattern->setType(memberTanType);
424+
TypedPattern::createImplicit(C, memberPattern, memberTanContextType);
425+
424426
auto *memberBinding = PatternBindingDecl::createImplicit(
425427
C, StaticSpellingKind::None, memberPattern, /*initExpr*/ nullptr,
426428
structDecl);

lib/Sema/DerivedConformanceDistributedActor.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,6 @@ static ValueDecl *deriveDistributedActor_id(DerivedConformance &derived) {
456456
PatternBindingDecl *pbDecl;
457457
std::tie(propDecl, pbDecl) = derived.declareDerivedProperty(
458458
DerivedConformance::SynthesizedIntroducer::Let, C.Id_id, propertyType,
459-
propertyType,
460459
/*isStatic=*/false, /*isFinal=*/true);
461460

462461
// mark as nonisolated, allowing access to it from everywhere
@@ -488,8 +487,7 @@ static ValueDecl *deriveDistributedActor_actorSystem(
488487
PatternBindingDecl *pbDecl;
489488
std::tie(propDecl, pbDecl) = derived.declareDerivedProperty(
490489
DerivedConformance::SynthesizedIntroducer::Let, C.Id_actorSystem,
491-
propertyType, propertyType,
492-
/*isStatic=*/false, /*isFinal=*/true);
490+
propertyType, /*isStatic=*/false, /*isFinal=*/true);
493491

494492
// mark as nonisolated, allowing access to it from everywhere
495493
propDecl->getAttrs().add(
@@ -791,8 +789,7 @@ static ValueDecl *deriveDistributedActor_unownedExecutor(DerivedConformance &der
791789

792790
auto propertyPair = derived.declareDerivedProperty(
793791
DerivedConformance::SynthesizedIntroducer::Var, ctx.Id_unownedExecutor,
794-
executorType, executorType,
795-
/*static*/ false, /*final*/ false);
792+
executorType, /*static*/ false, /*final*/ false);
796793
auto property = propertyPair.first;
797794
property->setSynthesized(true);
798795
property->getAttrs().add(new (ctx) SemanticsAttr(SEMANTICS_DEFAULT_ACTOR,
@@ -815,8 +812,7 @@ static ValueDecl *deriveDistributedActor_unownedExecutor(DerivedConformance &der
815812
AvailabilityInference::applyInferredAvailableAttrs(
816813
property, asAvailableAs, ctx);
817814

818-
auto getter =
819-
derived.addGetterToReadOnlyDerivedProperty(property, executorType);
815+
auto getter = derived.addGetterToReadOnlyDerivedProperty(property);
820816
getter->setBodySynthesizer(deriveBodyDistributedActor_unownedExecutor);
821817

822818
// IMPORTANT: MUST BE AFTER [id, actorSystem].

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,6 @@ static ValueDecl *deriveHashable_hashValue(DerivedConformance &derived) {
919919
Pattern *hashValuePat =
920920
NamedPattern::createImplicit(C, hashValueDecl, intType);
921921
hashValuePat = TypedPattern::createImplicit(C, hashValuePat, intType);
922-
hashValuePat->setType(intType);
923922

924923
auto *patDecl = PatternBindingDecl::createImplicit(
925924
C, StaticSpellingKind::None, hashValuePat, /*InitExpr*/ nullptr,

lib/Sema/DerivedConformanceError.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,12 @@ deriveBridgedNSError_enum_nsErrorDomain(
9898
PatternBindingDecl *pbDecl;
9999
std::tie(propDecl, pbDecl) = derived.declareDerivedProperty(
100100
DerivedConformance::SynthesizedIntroducer::Var,
101-
derived.Context.Id_nsErrorDomain, stringTy, stringTy, /*isStatic=*/true,
101+
derived.Context.Id_nsErrorDomain, stringTy, /*isStatic=*/true,
102102
/*isFinal=*/true);
103103
addNonIsolatedToSynthesized(derived.Nominal, propDecl);
104104

105105
// Define the getter.
106-
auto getterDecl = derived.addGetterToReadOnlyDerivedProperty(
107-
propDecl, stringTy);
106+
auto getterDecl = derived.addGetterToReadOnlyDerivedProperty(propDecl);
108107
getterDecl->setBodySynthesizer(synthesizer);
109108

110109
derived.addMembersToConformanceContext({propDecl, pbDecl});

lib/Sema/DerivedConformanceRawRepresentable.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,22 +157,19 @@ static VarDecl *deriveRawRepresentable_raw(DerivedConformance &derived) {
157157
ASTContext &C = derived.Context;
158158

159159
auto enumDecl = cast<EnumDecl>(derived.Nominal);
160-
auto parentDC = derived.getConformanceContext();
161160
auto rawInterfaceType = enumDecl->getRawType();
162-
auto rawType = parentDC->mapTypeIntoContext(rawInterfaceType);
163161

164162
// Define the property.
165163
VarDecl *propDecl;
166164
PatternBindingDecl *pbDecl;
167165
std::tie(propDecl, pbDecl) = derived.declareDerivedProperty(
168166
DerivedConformance::SynthesizedIntroducer::Var, C.Id_rawValue,
169-
rawInterfaceType, rawType, /*isStatic=*/false,
170-
/*isFinal=*/false);
167+
rawInterfaceType, /*isStatic=*/false, /*isFinal=*/false);
171168
addNonIsolatedToSynthesized(enumDecl, propDecl);
172169

173170
// Define the getter.
174-
auto getterDecl = DerivedConformance::addGetterToReadOnlyDerivedProperty(
175-
propDecl, rawType);
171+
auto getterDecl =
172+
DerivedConformance::addGetterToReadOnlyDerivedProperty(propDecl);
176173
getterDecl->setBodySynthesizer(&deriveBodyRawRepresentable_raw);
177174

178175
// If the containing module is not resilient, make sure clients can get at

lib/Sema/DerivedConformances.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,9 @@ CallExpr *DerivedConformance::createDiagnoseUnavailableCodeReachedCallExpr(
510510
return callExpr;
511511
}
512512

513-
AccessorDecl *DerivedConformance::
514-
addGetterToReadOnlyDerivedProperty(VarDecl *property,
515-
Type propertyContextType) {
516-
auto getter =
517-
declareDerivedPropertyGetter(property, propertyContextType);
513+
AccessorDecl *
514+
DerivedConformance::addGetterToReadOnlyDerivedProperty(VarDecl *property) {
515+
auto getter = declareDerivedPropertyGetter(property);
518516

519517
property->setImplInfo(StorageImplInfo::getImmutableComputed());
520518
property->setAccessors(SourceLoc(), {getter}, SourceLoc());
@@ -523,8 +521,7 @@ addGetterToReadOnlyDerivedProperty(VarDecl *property,
523521
}
524522

525523
AccessorDecl *
526-
DerivedConformance::declareDerivedPropertyGetter(VarDecl *property,
527-
Type propertyContextType) {
524+
DerivedConformance::declareDerivedPropertyGetter(VarDecl *property) {
528525
auto &C = property->getASTContext();
529526
auto parentDC = property->getDeclContext();
530527
ParameterList *params = ParameterList::createEmpty(C);
@@ -558,7 +555,6 @@ std::pair<VarDecl *, PatternBindingDecl *>
558555
DerivedConformance::declareDerivedProperty(SynthesizedIntroducer intro,
559556
Identifier name,
560557
Type propertyInterfaceType,
561-
Type propertyContextType,
562558
bool isStatic, bool isFinal) {
563559
auto parentDC = getConformanceContext();
564560

@@ -569,11 +565,13 @@ DerivedConformance::declareDerivedProperty(SynthesizedIntroducer intro,
569565
propDecl->copyFormalAccessFrom(Nominal, /*sourceIsParentContext*/ true);
570566
propDecl->setInterfaceType(propertyInterfaceType);
571567

568+
auto propertyContextType =
569+
getConformanceContext()->mapTypeIntoContext(propertyInterfaceType);
570+
572571
Pattern *propPat =
573572
NamedPattern::createImplicit(Context, propDecl, propertyContextType);
574573

575574
propPat = TypedPattern::createImplicit(Context, propPat, propertyContextType);
576-
propPat->setType(propertyContextType);
577575

578576
auto *pbDecl = PatternBindingDecl::createImplicit(
579577
Context, StaticSpellingKind::None, propPat, /*InitExpr*/ nullptr,
@@ -770,7 +768,7 @@ DeclRefExpr *DerivedConformance::convertEnumToIndex(SmallVectorImpl<ASTNode> &st
770768
// generate: var indexVar
771769
Pattern *indexPat = NamedPattern::createImplicit(C, indexVar, intType);
772770
indexPat = TypedPattern::createImplicit(C, indexPat, intType);
773-
indexPat->setType(intType);
771+
774772
auto *indexBind = PatternBindingDecl::createImplicit(
775773
C, StaticSpellingKind::None, indexPat, /*InitExpr*/ nullptr, funcDecl);
776774

lib/Sema/DerivedConformances.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,18 +368,14 @@ class DerivedConformance {
368368
/// Declare a read-only property.
369369
std::pair<VarDecl *, PatternBindingDecl *>
370370
declareDerivedProperty(SynthesizedIntroducer intro, Identifier name,
371-
Type propertyInterfaceType, Type propertyContextType,
372-
bool isStatic, bool isFinal);
371+
Type propertyContextType, bool isStatic, bool isFinal);
373372

374373
/// Add a getter to a derived property. The property becomes read-only.
375-
static AccessorDecl *
376-
addGetterToReadOnlyDerivedProperty(VarDecl *property,
377-
Type propertyContextType);
374+
static AccessorDecl *addGetterToReadOnlyDerivedProperty(VarDecl *property);
378375

379376
/// Declare a getter for a derived property.
380377
/// The getter will not be added to the property yet.
381-
static AccessorDecl *declareDerivedPropertyGetter(VarDecl *property,
382-
Type propertyContextType);
378+
static AccessorDecl *declareDerivedPropertyGetter(VarDecl *property);
383379

384380
/// Build a reference to the 'self' decl of a derived function.
385381
static DeclRefExpr *createSelfDeclRef(AbstractFunctionDecl *fn);

lib/Sema/TypeCheckPattern.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,8 @@ Type TypeChecker::typeCheckPattern(ContextualPattern pattern) {
754754
ASTContext &ctx = dc->getASTContext();
755755
if (auto type = evaluateOrDefault(ctx.evaluator, PatternTypeRequest{pattern},
756756
Type())) {
757+
ASSERT(!type->hasTypeParameter() &&
758+
"pattern should have a contextual type");
757759
return type;
758760
}
759761

0 commit comments

Comments
 (0)