Skip to content

Commit a331dee

Browse files
authored
Merge pull request swiftlang#27340 from CodaFi/party-crashers
Extensions Do Not Have Parent Signatures
2 parents b88ee7a + 574a450 commit a331dee

File tree

6 files changed

+30
-24
lines changed

6 files changed

+30
-24
lines changed

include/swift/AST/TypeCheckRequests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ class InferredGenericSignatureRequest :
10941094
public SimpleRequest<InferredGenericSignatureRequest,
10951095
GenericSignature *(ModuleDecl *,
10961096
GenericSignature *,
1097-
SmallVector<GenericParamList *, 2>,
1097+
GenericParamList *,
10981098
SmallVector<Requirement, 2>,
10991099
SmallVector<TypeLoc, 2>,
11001100
bool),
@@ -1110,7 +1110,7 @@ class InferredGenericSignatureRequest :
11101110
evaluate(Evaluator &evaluator,
11111111
ModuleDecl *module,
11121112
GenericSignature *baseSignature,
1113-
SmallVector<GenericParamList *, 2> addedParameters,
1113+
GenericParamList *gpl,
11141114
SmallVector<Requirement, 2> addedRequirements,
11151115
SmallVector<TypeLoc, 2> inferenceSources,
11161116
bool allowConcreteGenericParams) const;

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ SWIFT_REQUEST(NameLookup, GenericSignatureRequest,
5454
SeparatelyCached, NoLocationInfo)
5555
SWIFT_REQUEST(TypeChecker, InferredGenericSignatureRequest,
5656
GenericSignature *(ModuleDecl *, GenericSignature *,
57-
SmallVector<GenericParamList *, 2>,
57+
GenericParamList *,
5858
SmallVector<Requirement, 2>,
5959
SmallVector<TypeLoc, 2>, bool),
6060
Cached, NoLocationInfo)

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7690,7 +7690,7 @@ llvm::Expected<GenericSignature *>
76907690
InferredGenericSignatureRequest::evaluate(
76917691
Evaluator &evaluator, ModuleDecl *parentModule,
76927692
GenericSignature *parentSig,
7693-
SmallVector<GenericParamList *, 2> gpLists,
7693+
GenericParamList *gpl,
76947694
SmallVector<Requirement, 2> addedRequirements,
76957695
SmallVector<TypeLoc, 2> inferenceSources,
76967696
bool allowConcreteGenericParams) const {
@@ -7701,6 +7701,19 @@ InferredGenericSignatureRequest::evaluate(
77017701
// from that context.
77027702
builder.addGenericSignature(parentSig);
77037703

7704+
// Type check the generic parameters, treating all generic type
7705+
// parameters as dependent, unresolved.
7706+
SmallVector<GenericParamList *, 2> gpLists;
7707+
if (gpl->getOuterParameters() && !parentSig) {
7708+
for (auto *outerParams = gpl;
7709+
outerParams != nullptr;
7710+
outerParams = outerParams->getOuterParameters()) {
7711+
gpLists.push_back(outerParams);
7712+
}
7713+
} else {
7714+
gpLists.push_back(gpl);
7715+
}
7716+
77047717
// The generic parameter lists MUST appear from innermost to outermost.
77057718
// We walk them backwards to order outer requirements before
77067719
// inner requirements.

lib/Sema/TypeCheckDecl.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,10 +3088,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
30883088

30893089
// Validate the nominal type declaration being extended.
30903090
(void)nominal->getInterfaceType();
3091-
// Don't bother computing the generic signature if the extended nominal
3092-
// type didn't pass validation so we don't crash.
3093-
if (!nominal->isInvalid())
3094-
(void)ED->getGenericSignature();
3091+
(void)ED->getGenericSignature();
30953092
ED->setValidationToChecked();
30963093

30973094
if (extType && !extType->hasError()) {
@@ -3772,8 +3769,7 @@ void TypeChecker::validateDecl(ValueDecl *D) {
37723769
(void)nominal->getInterfaceType();
37733770

37743771
// Eagerly validate the generic signature of the extension.
3775-
if (!nominal->isInvalid())
3776-
(void)ext->getGenericSignature();
3772+
(void)ext->getGenericSignature();
37773773
}
37783774
}
37793775
if (ext->getValidationState() == Decl::ValidationState::Checking)

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -448,26 +448,16 @@ void TypeChecker::checkReferencedGenericParams(GenericContext *dc) {
448448
///
449449

450450
GenericSignature *TypeChecker::checkGenericSignature(
451-
GenericParamList *genericParams,
451+
GenericParamList *genericParamList,
452452
DeclContext *dc,
453453
GenericSignature *parentSig,
454454
bool allowConcreteGenericParams,
455455
SmallVector<Requirement, 2> additionalRequirements,
456456
SmallVector<TypeLoc, 2> inferenceSources) {
457-
assert(genericParams && "Missing generic parameters?");
458-
459-
// Type check the generic parameters, treating all generic type
460-
// parameters as dependent, unresolved.
461-
SmallVector<GenericParamList *, 2> gpLists;
462-
for (auto *outerParams = genericParams;
463-
outerParams != nullptr;
464-
outerParams = outerParams->getOuterParameters()) {
465-
gpLists.push_back(outerParams);
466-
}
457+
assert(genericParamList && "Missing generic parameters?");
467458

468459
auto request = InferredGenericSignatureRequest{
469-
dc->getParentModule(), parentSig,
470-
gpLists,
460+
dc->getParentModule(), parentSig, genericParamList,
471461
additionalRequirements, inferenceSources,
472462
allowConcreteGenericParams};
473463
auto *sig = evaluateOrDefault(dc->getASTContext().evaluator,
@@ -636,6 +626,7 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
636626
return cast<SubscriptDecl>(accessor->getStorage())->getGenericSignature();
637627
}
638628

629+
auto *parentSig = GC->getParent()->getGenericSignatureOfContext();
639630
bool allowConcreteGenericParams = false;
640631
SmallVector<TypeLoc, 2> inferenceSources;
641632
SmallVector<Requirement, 2> sameTypeReqs;
@@ -716,13 +707,15 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
716707

717708
// Allow parameters to be equated with concrete types.
718709
allowConcreteGenericParams = true;
710+
// Extensions must occur at the top level, they have no
711+
// (valid) parent signature.
712+
parentSig = nullptr;
719713
inferenceSources.emplace_back(nullptr, extInterfaceType);
720714
}
721715

722716
// EGREGIOUS HACK: The GSB cannot handle the addition of parent signatures
723717
// from malformed decls in many cases. Check the invalid bit and null out the
724718
// parent signature.
725-
auto *parentSig = GC->getParent()->getGenericSignatureOfContext();
726719
if (auto *DD = GC->getParent()->getAsDecl()) {
727720
parentSig = DD->isInvalid() ? nullptr : parentSig;
728721
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: not %target-swift-frontend %s -typecheck -o /dev/null
2+
3+
extension Result {
4+
extension Result where Result.Undefined == Int {

0 commit comments

Comments
 (0)