Skip to content

Fix typo correction crash #19905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ GenericSignature *GenericContext::getGenericSignature() const {
// The signature of a Protocol is trivial (Self: TheProtocol) so let's compute
// it.
if (auto PD = dyn_cast<ProtocolDecl>(this)) {
const_cast<ProtocolDecl *>(PD)->createGenericParamsIfMissing();
auto self = PD->getSelfInterfaceType()->castTo<GenericTypeParamType>();
auto req =
Requirement(RequirementKind::Conformance, self, PD->getDeclaredType());
Expand Down Expand Up @@ -3884,13 +3885,13 @@ findProtocolSelfReferences(const ProtocolDecl *proto, Type type,
}

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

// Special handling for associated types.
if (!skipAssocTypes && type->is<DependentMemberType>()) {
type = type->getRootGenericParam();
if (proto->getProtocolSelfType()->isEqual(type))
if (proto->getSelfInterfaceType()->isEqual(type))
return SelfReferenceKind::Other();
}

Expand Down Expand Up @@ -4771,8 +4772,12 @@ Type DeclContext::getSelfTypeInContext() const {
assert(isTypeContext());

// For a protocol or extension thereof, the type is 'Self'.
if (getSelfProtocolDecl())
return mapTypeIntoContext(getProtocolSelfType());
if (getSelfProtocolDecl()) {
auto selfType = getProtocolSelfType();
if (!selfType)
return ErrorType::get(getASTContext());
return mapTypeIntoContext(selfType);
}
return getDeclaredTypeInContext();
}

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

// For a protocol or extension thereof, the type is 'Self'.
if (getSelfProtocolDecl())
return getProtocolSelfType();
if (getSelfProtocolDecl()) {
auto selfType = getProtocolSelfType();
if (!selfType)
return ErrorType::get(getASTContext());
return selfType;
}
return getDeclaredInterfaceType();
}

Expand Down
6 changes: 4 additions & 2 deletions lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ ProtocolDecl *DeclContext::getExtendedProtocolDecl() const {
GenericTypeParamType *DeclContext::getProtocolSelfType() const {
assert(getSelfProtocolDecl() && "not a protocol");

GenericParamList *genericParams;
if (auto proto = dyn_cast<ProtocolDecl>(this)) {
const_cast<ProtocolDecl *>(proto)->createGenericParamsIfMissing();
genericParams = proto->getGenericParams();
} else {
genericParams = cast<ExtensionDecl>(this)->getGenericParams();
}

auto *genericParams = getGenericParamsOfContext();
if (genericParams == nullptr)
return nullptr;

Expand Down
4 changes: 2 additions & 2 deletions lib/AST/LookupVisibleDecls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1014,11 +1014,11 @@ void swift::lookupVisibleDecls(VisibleDeclConsumer &Consumer,
}
}
} else if (auto ED = dyn_cast<ExtensionDecl>(DC)) {
ExtendedType = ED->getDeclaredTypeInContext();
ExtendedType = ED->getSelfTypeInContext();
if (ExtendedType)
BaseDecl = ExtendedType->getNominalOrBoundGenericNominal();
} else if (auto ND = dyn_cast<NominalTypeDecl>(DC)) {
ExtendedType = ND->getDeclaredTypeInContext();
ExtendedType = ND->getSelfTypeInContext();
BaseDecl = ND;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/AST/ProtocolConformance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,13 @@ ProtocolConformanceRef::getInheritedConformanceRef(ProtocolDecl *base) const {
auto proto = concrete->getProtocol();
auto path =
proto->getGenericSignature()->getConformanceAccessPath(
proto->getProtocolSelfType(), base);
proto->getSelfInterfaceType(), base);
ProtocolConformanceRef result = *this;
Type resultType = concrete->getType();
bool first = true;
for (const auto &step : path) {
if (first) {
assert(step.first->isEqual(proto->getProtocolSelfType()));
assert(step.first->isEqual(proto->getSelfInterfaceType()));
assert(step.second == proto);
first = false;
continue;
Expand Down Expand Up @@ -774,7 +774,7 @@ Type ProtocolConformanceRef::getAssociatedType(Type conformingType,

// Fast path for dependent member types on 'Self' of our associated types.
auto memberType = cast<DependentMemberType>(type);
if (memberType.getBase()->isEqual(proto->getProtocolSelfType()) &&
if (memberType.getBase()->isEqual(proto->getSelfInterfaceType()) &&
memberType->getAssocType()->getProtocol() == proto &&
isConcrete())
return getConcrete()->getTypeWitness(memberType->getAssocType(), resolver);
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/SubstitutionMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ SubstitutionMap::getOverrideSubstitutions(
// For overrides within a protocol hierarchy, substitute the Self type.
if (auto baseProto = baseDecl->getDeclContext()->getSelfProtocolDecl()) {
if (auto derivedProtoSelf =
derivedDecl->getDeclContext()->getProtocolSelfType()) {
derivedDecl->getDeclContext()->getSelfInterfaceType()) {
return SubstitutionMap::getProtocolSubstitutions(
baseProto,
derivedProtoSelf,
Expand Down
4 changes: 1 addition & 3 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +817,7 @@ namespace {
conformance.getConcrete()->getType()->hasArchetype();
if (hasArchetype) {
// Bind local Self type data from the metadata argument.
CanType selfInContext =
Proto->mapTypeIntoContext(Proto->getProtocolSelfType())
->getCanonicalType();
auto selfInContext = Proto->getSelfTypeInContext()->getCanonicalType();
IGF.bindLocalTypeDataFromTypeMetadata(selfInContext, IsExact, self,
MetadataState::Abstract);
IGF.setUnscopedLocalTypeData(
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ bool irgen::isDependentConformance(const NormalProtocolConformance *conformance)
auto proto = conformance->getProtocol();
for (const auto &req : proto->getRequirementSignature()) {
if (req.getKind() != RequirementKind::Conformance ||
!req.getFirstType()->isEqual(proto->getProtocolSelfType()))
!req.getFirstType()->isEqual(proto->getSelfInterfaceType()))
continue;

auto inherited = req.getSecondType()->castTo<ProtocolType>()->getDecl();
Expand Down
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ SubstitutionMap SILGenModule::mapSubstitutionsForWitnessOverride(
SubstitutionMap subs) {
// Substitute the 'Self' type of the base protocol.
auto origProto = cast<ProtocolDecl>(original->getDeclContext());
Type origProtoSelfType = origProto->getProtocolSelfType();
Type origProtoSelfType = origProto->getSelfInterfaceType();
auto baseProto = cast<ProtocolDecl>(overridden->getDeclContext());
return SubstitutionMap::getProtocolSubstitutions(
baseProto,
Expand Down
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ SILFunction *SILGenModule::emitProtocolWitness(
// looking for the conformance of 'Self'.
if (reqtSubMap) {
auto requirement = conformance.getRequirement();
auto self = requirement->getProtocolSelfType()->getCanonicalType();
auto self = requirement->getSelfInterfaceType()->getCanonicalType();

conformance = *reqtSubMap.lookupConformance(self, requirement);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2308,7 +2308,7 @@ static void checkProtocolSelfRequirements(ProtocolDecl *proto,
case RequirementKind::Layout:
case RequirementKind::Superclass:
if (reqRepr &&
req.getFirstType()->isEqual(proto->getProtocolSelfType())) {
req.getFirstType()->isEqual(proto->getSelfInterfaceType())) {
auto &diags = proto->getASTContext().Diags;
diags.diagnose(reqRepr->getSubjectLoc().getLoc(),
diag::protocol_where_clause_self_requirement);
Expand Down
8 changes: 4 additions & 4 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3441,7 +3441,7 @@ void ConformanceChecker::ensureRequirementsAreSatisfied(
DC, Loc, Loc,
// FIXME: maybe this should be the conformance's type
proto->getDeclaredInterfaceType(),
{ Type(proto->getProtocolSelfType()) },
{ proto->getSelfInterfaceType() },
proto->getRequirementSignature(),
QuerySubstitutionMap{substitutions},
TypeChecker::LookUpConformance(DC),
Expand Down Expand Up @@ -3966,7 +3966,7 @@ Optional<ProtocolConformanceRef> TypeChecker::conformsToProtocol(

auto conditionalCheckResult = checkGenericArguments(
DC, ComplainLoc, noteLoc, T,
{Type(lookupResult->getRequirement()->getProtocolSelfType())},
{lookupResult->getRequirement()->getSelfInterfaceType()},
*condReqs,
[](SubstitutableType *dependentType) { return Type(dependentType); },
LookUpConformance(DC), options);
Expand Down Expand Up @@ -5328,7 +5328,7 @@ void TypeChecker::inferDefaultWitnesses(ProtocolDecl *proto) {
for (const auto &req : proto->getRequirementSignature()) {
if (req.getKind() != RequirementKind::Conformance)
continue;
if (req.getFirstType()->isEqual(proto->getProtocolSelfType()))
if (req.getFirstType()->isEqual(proto->getSelfInterfaceType()))
continue;

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

if (!depMemTy->getBase()->isEqual(proto->getProtocolSelfType()))
if (!depMemTy->getBase()->isEqual(proto->getSelfInterfaceType()))
continue;

auto assocType = depMemTy->getAssocType();
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckProtocolInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ bool AssociatedTypeInference::checkCurrentTypeWitnesses(
auto result =
tc.checkGenericArguments(dc, SourceLoc(), SourceLoc(),
typeInContext,
{ Type(proto->getProtocolSelfType()) },
{ proto->getSelfInterfaceType() },
sanitizedRequirements,
QuerySubstitutionMap{substitutions},
TypeChecker::LookUpConformance(dc),
Expand Down
2 changes: 1 addition & 1 deletion lib/TBDGen/TBDGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ void TBDGenVisitor::visitProtocolDecl(ProtocolDecl *PD) {
continue;

// Skip inherited requirements.
if (req.getFirstType()->isEqual(PD->getProtocolSelfType()))
if (req.getFirstType()->isEqual(PD->getSelfInterfaceType()))
continue;

AssociatedConformance conformance(
Expand Down
6 changes: 3 additions & 3 deletions test/IDE/complete_where_clause.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ protocol P2 {
}

// P2: Begin completions
// P2-DAG: Decl[GenericTypeParam]/CurrNominal: Self[#Self#];
// P2-DAG: Decl[AssociatedType]/CurrNominal: T;
// P2-DAG: Decl[AssociatedType]/CurrNominal: U;
// P2-DAG: Decl[GenericTypeParam]/Super: Self[#Self#];
// P2-DAG: Decl[AssociatedType]/Super: T;
// P2-DAG: Decl[AssociatedType]/Super: U;
// P2: End completions

// U_DOT: Begin completions
Expand Down
11 changes: 11 additions & 0 deletions test/Sema/typo_correction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,14 @@ class CircularValidationWithTypo {
didSet { }
}
}

// Crash with invalid extension that has not been bound -- https://bugs.swift.org/browse/SR-8984
protocol PP {}

func boo() { // expected-note {{did you mean 'boo'?}}
extension PP { // expected-error {{declaration is only valid at file scope}}
func g() {
booo() // expected-error {{use of unresolved identifier 'booo'}}
}
}
}