Skip to content

Commit a1a747c

Browse files
authored
Merge pull request #19905 from slavapestov/fix-typo-correction-crash
Fix typo correction crash
2 parents 8954e5f + ebe19ca commit a1a747c

15 files changed

+50
-30
lines changed

lib/AST/Decl.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ GenericSignature *GenericContext::getGenericSignature() const {
731731
// The signature of a Protocol is trivial (Self: TheProtocol) so let's compute
732732
// it.
733733
if (auto PD = dyn_cast<ProtocolDecl>(this)) {
734+
const_cast<ProtocolDecl *>(PD)->createGenericParamsIfMissing();
734735
auto self = PD->getSelfInterfaceType()->castTo<GenericTypeParamType>();
735736
auto req =
736737
Requirement(RequirementKind::Conformance, self, PD->getDeclaredType());
@@ -3884,13 +3885,13 @@ findProtocolSelfReferences(const ProtocolDecl *proto, Type type,
38843885
}
38853886

38863887
// A direct reference to 'Self' is covariant.
3887-
if (proto->getProtocolSelfType()->isEqual(type))
3888+
if (proto->getSelfInterfaceType()->isEqual(type))
38883889
return SelfReferenceKind::Result();
38893890

38903891
// Special handling for associated types.
38913892
if (!skipAssocTypes && type->is<DependentMemberType>()) {
38923893
type = type->getRootGenericParam();
3893-
if (proto->getProtocolSelfType()->isEqual(type))
3894+
if (proto->getSelfInterfaceType()->isEqual(type))
38943895
return SelfReferenceKind::Other();
38953896
}
38963897

@@ -4771,8 +4772,12 @@ Type DeclContext::getSelfTypeInContext() const {
47714772
assert(isTypeContext());
47724773

47734774
// For a protocol or extension thereof, the type is 'Self'.
4774-
if (getSelfProtocolDecl())
4775-
return mapTypeIntoContext(getProtocolSelfType());
4775+
if (getSelfProtocolDecl()) {
4776+
auto selfType = getProtocolSelfType();
4777+
if (!selfType)
4778+
return ErrorType::get(getASTContext());
4779+
return mapTypeIntoContext(selfType);
4780+
}
47764781
return getDeclaredTypeInContext();
47774782
}
47784783

@@ -4781,8 +4786,12 @@ Type DeclContext::getSelfInterfaceType() const {
47814786
assert(isTypeContext());
47824787

47834788
// For a protocol or extension thereof, the type is 'Self'.
4784-
if (getSelfProtocolDecl())
4785-
return getProtocolSelfType();
4789+
if (getSelfProtocolDecl()) {
4790+
auto selfType = getProtocolSelfType();
4791+
if (!selfType)
4792+
return ErrorType::get(getASTContext());
4793+
return selfType;
4794+
}
47864795
return getDeclaredInterfaceType();
47874796
}
47884797

lib/AST/DeclContext.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@ ProtocolDecl *DeclContext::getExtendedProtocolDecl() const {
8888
GenericTypeParamType *DeclContext::getProtocolSelfType() const {
8989
assert(getSelfProtocolDecl() && "not a protocol");
9090

91+
GenericParamList *genericParams;
9192
if (auto proto = dyn_cast<ProtocolDecl>(this)) {
92-
const_cast<ProtocolDecl *>(proto)->createGenericParamsIfMissing();
93+
genericParams = proto->getGenericParams();
94+
} else {
95+
genericParams = cast<ExtensionDecl>(this)->getGenericParams();
9396
}
9497

95-
auto *genericParams = getGenericParamsOfContext();
9698
if (genericParams == nullptr)
9799
return nullptr;
98100

lib/AST/LookupVisibleDecls.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,11 +1014,11 @@ void swift::lookupVisibleDecls(VisibleDeclConsumer &Consumer,
10141014
}
10151015
}
10161016
} else if (auto ED = dyn_cast<ExtensionDecl>(DC)) {
1017-
ExtendedType = ED->getDeclaredTypeInContext();
1017+
ExtendedType = ED->getSelfTypeInContext();
10181018
if (ExtendedType)
10191019
BaseDecl = ExtendedType->getNominalOrBoundGenericNominal();
10201020
} else if (auto ND = dyn_cast<NominalTypeDecl>(DC)) {
1021-
ExtendedType = ND->getDeclaredTypeInContext();
1021+
ExtendedType = ND->getSelfTypeInContext();
10221022
BaseDecl = ND;
10231023
}
10241024

lib/AST/ProtocolConformance.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,13 @@ ProtocolConformanceRef::getInheritedConformanceRef(ProtocolDecl *base) const {
415415
auto proto = concrete->getProtocol();
416416
auto path =
417417
proto->getGenericSignature()->getConformanceAccessPath(
418-
proto->getProtocolSelfType(), base);
418+
proto->getSelfInterfaceType(), base);
419419
ProtocolConformanceRef result = *this;
420420
Type resultType = concrete->getType();
421421
bool first = true;
422422
for (const auto &step : path) {
423423
if (first) {
424-
assert(step.first->isEqual(proto->getProtocolSelfType()));
424+
assert(step.first->isEqual(proto->getSelfInterfaceType()));
425425
assert(step.second == proto);
426426
first = false;
427427
continue;
@@ -774,7 +774,7 @@ Type ProtocolConformanceRef::getAssociatedType(Type conformingType,
774774

775775
// Fast path for dependent member types on 'Self' of our associated types.
776776
auto memberType = cast<DependentMemberType>(type);
777-
if (memberType.getBase()->isEqual(proto->getProtocolSelfType()) &&
777+
if (memberType.getBase()->isEqual(proto->getSelfInterfaceType()) &&
778778
memberType->getAssocType()->getProtocol() == proto &&
779779
isConcrete())
780780
return getConcrete()->getTypeWitness(memberType->getAssocType(), resolver);

lib/AST/SubstitutionMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ SubstitutionMap::getOverrideSubstitutions(
525525
// For overrides within a protocol hierarchy, substitute the Self type.
526526
if (auto baseProto = baseDecl->getDeclContext()->getSelfProtocolDecl()) {
527527
if (auto derivedProtoSelf =
528-
derivedDecl->getDeclContext()->getProtocolSelfType()) {
528+
derivedDecl->getDeclContext()->getSelfInterfaceType()) {
529529
return SubstitutionMap::getProtocolSubstitutions(
530530
baseProto,
531531
derivedProtoSelf,

lib/IRGen/GenMeta.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -817,9 +817,7 @@ namespace {
817817
conformance.getConcrete()->getType()->hasArchetype();
818818
if (hasArchetype) {
819819
// Bind local Self type data from the metadata argument.
820-
CanType selfInContext =
821-
Proto->mapTypeIntoContext(Proto->getProtocolSelfType())
822-
->getCanonicalType();
820+
auto selfInContext = Proto->getSelfTypeInContext()->getCanonicalType();
823821
IGF.bindLocalTypeDataFromTypeMetadata(selfInContext, IsExact, self,
824822
MetadataState::Abstract);
825823
IGF.setUnscopedLocalTypeData(

lib/IRGen/GenProto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ bool irgen::isDependentConformance(const NormalProtocolConformance *conformance)
972972
auto proto = conformance->getProtocol();
973973
for (const auto &req : proto->getRequirementSignature()) {
974974
if (req.getKind() != RequirementKind::Conformance ||
975-
!req.getFirstType()->isEqual(proto->getProtocolSelfType()))
975+
!req.getFirstType()->isEqual(proto->getSelfInterfaceType()))
976976
continue;
977977

978978
auto inherited = req.getSecondType()->castTo<ProtocolType>()->getDecl();

lib/SILGen/SILGenApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ SubstitutionMap SILGenModule::mapSubstitutionsForWitnessOverride(
5050
SubstitutionMap subs) {
5151
// Substitute the 'Self' type of the base protocol.
5252
auto origProto = cast<ProtocolDecl>(original->getDeclContext());
53-
Type origProtoSelfType = origProto->getProtocolSelfType();
53+
Type origProtoSelfType = origProto->getSelfInterfaceType();
5454
auto baseProto = cast<ProtocolDecl>(overridden->getDeclContext());
5555
return SubstitutionMap::getProtocolSubstitutions(
5656
baseProto,

lib/SILGen/SILGenType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ SILFunction *SILGenModule::emitProtocolWitness(
622622
// looking for the conformance of 'Self'.
623623
if (reqtSubMap) {
624624
auto requirement = conformance.getRequirement();
625-
auto self = requirement->getProtocolSelfType()->getCanonicalType();
625+
auto self = requirement->getSelfInterfaceType()->getCanonicalType();
626626

627627
conformance = *reqtSubMap.lookupConformance(self, requirement);
628628
}

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2308,7 +2308,7 @@ static void checkProtocolSelfRequirements(ProtocolDecl *proto,
23082308
case RequirementKind::Layout:
23092309
case RequirementKind::Superclass:
23102310
if (reqRepr &&
2311-
req.getFirstType()->isEqual(proto->getProtocolSelfType())) {
2311+
req.getFirstType()->isEqual(proto->getSelfInterfaceType())) {
23122312
auto &diags = proto->getASTContext().Diags;
23132313
diags.diagnose(reqRepr->getSubjectLoc().getLoc(),
23142314
diag::protocol_where_clause_self_requirement);

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,7 +3441,7 @@ void ConformanceChecker::ensureRequirementsAreSatisfied(
34413441
DC, Loc, Loc,
34423442
// FIXME: maybe this should be the conformance's type
34433443
proto->getDeclaredInterfaceType(),
3444-
{ Type(proto->getProtocolSelfType()) },
3444+
{ proto->getSelfInterfaceType() },
34453445
proto->getRequirementSignature(),
34463446
QuerySubstitutionMap{substitutions},
34473447
TypeChecker::LookUpConformance(DC),
@@ -3966,7 +3966,7 @@ Optional<ProtocolConformanceRef> TypeChecker::conformsToProtocol(
39663966

39673967
auto conditionalCheckResult = checkGenericArguments(
39683968
DC, ComplainLoc, noteLoc, T,
3969-
{Type(lookupResult->getRequirement()->getProtocolSelfType())},
3969+
{lookupResult->getRequirement()->getSelfInterfaceType()},
39703970
*condReqs,
39713971
[](SubstitutableType *dependentType) { return Type(dependentType); },
39723972
LookUpConformance(DC), options);
@@ -5328,7 +5328,7 @@ void TypeChecker::inferDefaultWitnesses(ProtocolDecl *proto) {
53285328
for (const auto &req : proto->getRequirementSignature()) {
53295329
if (req.getKind() != RequirementKind::Conformance)
53305330
continue;
5331-
if (req.getFirstType()->isEqual(proto->getProtocolSelfType()))
5331+
if (req.getFirstType()->isEqual(proto->getSelfInterfaceType()))
53325332
continue;
53335333

53345334
// Find the innermost dependent member type (e.g., Self.AssocType), so
@@ -5341,7 +5341,7 @@ void TypeChecker::inferDefaultWitnesses(ProtocolDecl *proto) {
53415341
depMemTy->getBase()->getAs<DependentMemberType>())
53425342
depMemTy = innerDepMemTy;
53435343

5344-
if (!depMemTy->getBase()->isEqual(proto->getProtocolSelfType()))
5344+
if (!depMemTy->getBase()->isEqual(proto->getSelfInterfaceType()))
53455345
continue;
53465346

53475347
auto assocType = depMemTy->getAssocType();

lib/Sema/TypeCheckProtocolInference.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ bool AssociatedTypeInference::checkCurrentTypeWitnesses(
11211121
auto result =
11221122
tc.checkGenericArguments(dc, SourceLoc(), SourceLoc(),
11231123
typeInContext,
1124-
{ Type(proto->getProtocolSelfType()) },
1124+
{ proto->getSelfInterfaceType() },
11251125
sanitizedRequirements,
11261126
QuerySubstitutionMap{substitutions},
11271127
TypeChecker::LookUpConformance(dc),

lib/TBDGen/TBDGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ void TBDGenVisitor::visitProtocolDecl(ProtocolDecl *PD) {
420420
continue;
421421

422422
// Skip inherited requirements.
423-
if (req.getFirstType()->isEqual(PD->getProtocolSelfType()))
423+
if (req.getFirstType()->isEqual(PD->getSelfInterfaceType()))
424424
continue;
425425

426426
AssociatedConformance conformance(

test/IDE/complete_where_clause.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ protocol P2 {
127127
}
128128

129129
// P2: Begin completions
130-
// P2-DAG: Decl[GenericTypeParam]/CurrNominal: Self[#Self#];
131-
// P2-DAG: Decl[AssociatedType]/CurrNominal: T;
132-
// P2-DAG: Decl[AssociatedType]/CurrNominal: U;
130+
// P2-DAG: Decl[GenericTypeParam]/Super: Self[#Self#];
131+
// P2-DAG: Decl[AssociatedType]/Super: T;
132+
// P2-DAG: Decl[AssociatedType]/Super: U;
133133
// P2: End completions
134134

135135
// U_DOT: Begin completions

test/Sema/typo_correction.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,14 @@ class CircularValidationWithTypo {
166166
didSet { }
167167
}
168168
}
169+
170+
// Crash with invalid extension that has not been bound -- https://bugs.swift.org/browse/SR-8984
171+
protocol PP {}
172+
173+
func boo() { // expected-note {{did you mean 'boo'?}}
174+
extension PP { // expected-error {{declaration is only valid at file scope}}
175+
func g() {
176+
booo() // expected-error {{use of unresolved identifier 'booo'}}
177+
}
178+
}
179+
}

0 commit comments

Comments
 (0)