Skip to content

[WIP][Parser][SR-711] Couple "expected declaration" Diagnose With A Note #1276

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

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 6 additions & 6 deletions include/swift/AST/DeclContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,27 +240,27 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {

/// If this DeclContext is a nominal type declaration or an
/// extension thereof, return the nominal type declaration.
NominalTypeDecl *isNominalTypeOrNominalTypeExtensionContext() const;
NominalTypeDecl *getAsNominalTypeOrNominalTypeExtensionContext() const;

/// If this DeclContext is a class, or an extension on a class, return the
/// ClassDecl, otherwise return null.
ClassDecl *isClassOrClassExtensionContext() const;
ClassDecl *getAsClassOrClassExtensionContext() const;

/// If this DeclContext is an enum, or an extension on an enum, return the
/// EnumDecl, otherwise return null.
EnumDecl *isEnumOrEnumExtensionContext() const;
EnumDecl *getAsEnumOrEnumExtensionContext() const;

/// If this DeclContext is a protocol, or an extension on a
/// protocol, return the ProtocolDecl, otherwise return null.
ProtocolDecl *isProtocolOrProtocolExtensionContext() const;
ProtocolDecl *getAsProtocolOrProtocolExtensionContext() const;

/// If this DeclContext is a protocol extension, return the extended protocol.
ProtocolDecl *isProtocolExtensionContext() const;
ProtocolDecl *getAsProtocolExtensionContext() const;

/// \brief Retrieve the generic parameter 'Self' from a protocol or
/// protocol extension.
///
/// Only valid if \c isProtocolOrProtocolExtensionContext().
/// Only valid if \c getAsProtocolOrProtocolExtensionContext().
GenericTypeParamDecl *getProtocolSelf() const;

/// getDeclaredTypeOfContext - For a type context, retrieves the declared
Expand Down
5 changes: 5 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ WARNING(lex_editor_placeholder_in_playground,none,
// Declaration parsing diagnostics
//------------------------------------------------------------------------------

NOTE(note_in_decl_extension,none,
"in %select{declaration|extension}0 of '%1':", (bool, StringRef))

NOTE(note_in_extension,none, "in extension:", ())

ERROR(declaration_same_line_without_semi,none,
"consecutive declarations on a line must be separated by ';'", ())

Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -2949,7 +2949,7 @@ class WitnessMethodInst : public MethodInst {
CanType getLookupType() const { return LookupType; }
ProtocolDecl *getLookupProtocol() const {
return getMember().getDecl()->getDeclContext()
->isProtocolOrProtocolExtensionContext();
->getAsProtocolOrProtocolExtensionContext();
}
ProtocolConformanceRef getConformance() const { return Conformance; }

Expand Down
6 changes: 4 additions & 2 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1952,7 +1952,8 @@ bool ASTContext::diagnoseUnintendedObjCMethodOverrides(SourceFile &sf) {
continue;
}

auto classDecl = method->getDeclContext()->isClassOrClassExtensionContext();
auto classDecl =
method->getDeclContext()->getAsClassOrClassExtensionContext();
if (!classDecl)
continue; // error-recovery path, only

Expand Down Expand Up @@ -2238,7 +2239,8 @@ bool ASTContext::diagnoseObjCUnsatisfiedOptReqConflicts(SourceFile &sf) {
bool anyDiagnosed = false;
for (const auto &unsatisfied : localReqs) {
// Check whether there is a conflict here.
ClassDecl *classDecl = unsatisfied.first->isClassOrClassExtensionContext();
ClassDecl *classDecl =
unsatisfied.first->getAsClassOrClassExtensionContext();
auto req = unsatisfied.second;
auto selector = req->getObjCSelector();
bool isInstanceMethod = req->isInstanceMember();
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ void PrintAST::printNominalDeclName(NominalTypeDecl *decl) {
// For a protocol extension, print only the where clause; the
// generic parameter list is implicit. For other nominal types,
// print the generic parameters.
if (decl->isProtocolOrProtocolExtensionContext())
if (decl->getAsProtocolOrProtocolExtensionContext())
printWhereClause(gp->getRequirements());
else
printGenericParams(gp);
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,7 @@ struct ASTNodeBase {};
} else {
auto ext = cast<ExtensionDecl>(decl);
conformingDC = ext;
nominal = ext->isNominalTypeOrNominalTypeExtensionContext();
nominal = ext->getAsNominalTypeOrNominalTypeExtensionContext();
}

auto proto = conformance->getProtocol();
Expand Down
3 changes: 2 additions & 1 deletion lib/AST/ArchetypeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,8 @@ bool ArchetypeBuilder::addAbstractTypeParamRequirements(
// Mark all associatedtypes in this protocol as recursive (and error-type)
// to avoid later crashes dealing with this invalid protocol in other
// contexts.
auto containingProto = assocType->getDeclContext()->isProtocolOrProtocolExtensionContext();
auto containingProto =
assocType->getDeclContext()->getAsProtocolOrProtocolExtensionContext();
for (auto member : containingProto->getMembers())
if (auto assocType = dyn_cast<AssociatedTypeDecl>(member))
assocType->setIsRecursive();
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/ConformanceLookupTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ ProtocolConformance *ConformanceLookupTable::getConformance(
return nullptr;

NominalTypeDecl *conformingNominal
= conformingDC->isNominalTypeOrNominalTypeExtensionContext();
= conformingDC->getAsNominalTypeOrNominalTypeExtensionContext();

// Form the conformance.
Type type = entry->getDeclContext()->getDeclaredTypeInContext();
Expand Down Expand Up @@ -843,7 +843,7 @@ void ConformanceLookupTable::registerProtocolConformance(
ProtocolConformance *conformance) {
auto protocol = conformance->getProtocol();
auto dc = conformance->getDeclContext();
auto nominal = dc->isNominalTypeOrNominalTypeExtensionContext();
auto nominal = dc->getAsNominalTypeOrNominalTypeExtensionContext();

// If there is an entry to update, do so.
auto &dcConformances = AllConformances[dc];
Expand Down
20 changes: 11 additions & 9 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,8 @@ bool AbstractStorageDecl::hasFixedLayout() const {
return true;

// If we're in a nominal type, just query the type.
auto nominal = getDeclContext()->isNominalTypeOrNominalTypeExtensionContext();
auto nominal =
getDeclContext()->getAsNominalTypeOrNominalTypeExtensionContext();
if (nominal)
return nominal->hasFixedLayout();

Expand Down Expand Up @@ -1521,7 +1522,7 @@ OverloadSignature ValueDecl::getOverloadSignature() const {

signature.Name = getFullName();
signature.InProtocolExtension
= getDeclContext()->isProtocolExtensionContext();
= getDeclContext()->getAsProtocolExtensionContext();

// Functions, initializers, and de-initializers include their
// interface types in their signatures as well as whether they are
Expand Down Expand Up @@ -1633,7 +1634,7 @@ ArrayRef<ValueDecl *>
ValueDecl::getSatisfiedProtocolRequirements(bool Sorted) const {
// Dig out the nominal type.
NominalTypeDecl *NTD =
getDeclContext()->isNominalTypeOrNominalTypeExtensionContext();
getDeclContext()->getAsNominalTypeOrNominalTypeExtensionContext();
if (!NTD || isa<ProtocolDecl>(NTD))
return {};

Expand Down Expand Up @@ -1893,7 +1894,8 @@ Type NominalTypeDecl::computeInterfaceType() const {

// Figure out the interface type of the parent.
Type parentType;
if (auto parent = getDeclContext()->isNominalTypeOrNominalTypeExtensionContext())
if (auto parent =
getDeclContext()->getAsNominalTypeOrNominalTypeExtensionContext())
parentType = parent->getDeclaredInterfaceType();

Type type;
Expand Down Expand Up @@ -2024,7 +2026,7 @@ SourceRange GenericTypeParamDecl::getSourceRange() const {
bool GenericTypeParamDecl::isProtocolSelf() const {
if (!isImplicit()) return false;
auto dc = getDeclContext();
if (!dc->isProtocolOrProtocolExtensionContext()) return false;
if (!dc->getAsProtocolOrProtocolExtensionContext()) return false;
return dc->getProtocolSelf() == this;
}

Expand Down Expand Up @@ -3302,7 +3304,7 @@ static Type getSelfTypeForContext(DeclContext *dc) {
// For a protocol or extension thereof, the type is 'Self'.
// FIXME: Weird that we're producing an archetype for protocol Self,
// but the declared type of the context in non-protocol cases.
if (dc->isProtocolOrProtocolExtensionContext()) {
if (dc->getAsProtocolOrProtocolExtensionContext()) {
// In the parser, generic parameters won't be wired up yet, just give up on
// producing a type.
if (dc->getGenericParamsOfContext() == nullptr)
Expand Down Expand Up @@ -3971,7 +3973,7 @@ bool FuncDecl::isUnaryOperator() const {
return false;

unsigned opArgIndex
= getDeclContext()->isProtocolOrProtocolExtensionContext() ? 1 : 0;
= getDeclContext()->getAsProtocolOrProtocolExtensionContext() ? 1 : 0;

auto *params = getParameterList(opArgIndex);
return params->size() == 1 && !params->get(0)->isVariadic();
Expand All @@ -3982,7 +3984,7 @@ bool FuncDecl::isBinaryOperator() const {
return false;

unsigned opArgIndex
= getDeclContext()->isProtocolOrProtocolExtensionContext() ? 1 : 0;
= getDeclContext()->getAsProtocolOrProtocolExtensionContext() ? 1 : 0;

auto *params = getParameterList(opArgIndex);
return params->size() == 2 && !params->get(1)->isVariadic();
Expand Down Expand Up @@ -4084,7 +4086,7 @@ DynamicSelfType *FuncDecl::getDynamicSelfInterface() const {
}

bool FuncDecl::hasArchetypeSelf() const {
if (!getDeclContext()->isProtocolExtensionContext())
if (!getDeclContext()->getAsProtocolExtensionContext())
return false;

auto selfTy = getDeclContext()->getProtocolSelf()->getArchetype();
Expand Down
22 changes: 11 additions & 11 deletions lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ASTContext &DeclContext::getASTContext() const {
}

NominalTypeDecl *
DeclContext::isNominalTypeOrNominalTypeExtensionContext() const {
DeclContext::getAsNominalTypeOrNominalTypeExtensionContext() const {
switch (getContextKind()) {
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
Expand Down Expand Up @@ -73,31 +73,31 @@ DeclContext::isNominalTypeOrNominalTypeExtensionContext() const {
}
}

ClassDecl *DeclContext::isClassOrClassExtensionContext() const {
ClassDecl *DeclContext::getAsClassOrClassExtensionContext() const {
return dyn_cast_or_null<ClassDecl>(
isNominalTypeOrNominalTypeExtensionContext());
getAsNominalTypeOrNominalTypeExtensionContext());
}

EnumDecl *DeclContext::isEnumOrEnumExtensionContext() const {
EnumDecl *DeclContext::getAsEnumOrEnumExtensionContext() const {
return dyn_cast_or_null<EnumDecl>(
isNominalTypeOrNominalTypeExtensionContext());
getAsNominalTypeOrNominalTypeExtensionContext());
}

ProtocolDecl *DeclContext::isProtocolOrProtocolExtensionContext() const {
ProtocolDecl *DeclContext::getAsProtocolOrProtocolExtensionContext() const {
return dyn_cast_or_null<ProtocolDecl>(
isNominalTypeOrNominalTypeExtensionContext());
getAsNominalTypeOrNominalTypeExtensionContext());
}

ProtocolDecl *DeclContext::isProtocolExtensionContext() const {
ProtocolDecl *DeclContext::getAsProtocolExtensionContext() const {
if (getContextKind() != DeclContextKind::ExtensionDecl)
return nullptr;

return dyn_cast_or_null<ProtocolDecl>(
isNominalTypeOrNominalTypeExtensionContext());
getAsNominalTypeOrNominalTypeExtensionContext());
}

GenericTypeParamDecl *DeclContext::getProtocolSelf() const {
assert(isProtocolOrProtocolExtensionContext() && "not a protocol");
assert(getAsProtocolOrProtocolExtensionContext() && "not a protocol");
return getGenericParamsOfContext()->getParams().front();
}

Expand Down Expand Up @@ -176,7 +176,7 @@ Type DeclContext::getDeclaredTypeInContext() const {

Type DeclContext::getDeclaredInterfaceType() const {
// FIXME: Need a sugar-preserving getExtendedInterfaceType for extensions
if (auto nominal = isNominalTypeOrNominalTypeExtensionContext())
if (auto nominal = getAsNominalTypeOrNominalTypeExtensionContext())
return nominal->getDeclaredInterfaceType();
return Type();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/LookupVisibleDecls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ static void doGlobalExtensionLookup(Type BaseType,
// Look in each extension of this type.
for (auto extension : nominal->getExtensions()) {
bool validatedExtension = false;
if (TypeResolver && extension->isProtocolExtensionContext()) {
if (TypeResolver && extension->getAsProtocolExtensionContext()) {
if (!TypeResolver->isProtocolExtensionUsable(
const_cast<DeclContext *>(CurrDC), BaseType, extension)) {
continue;
Expand Down Expand Up @@ -754,7 +754,7 @@ void swift::lookupVisibleDecls(VisibleDeclConsumer &Consumer,
BaseDecl = AFD->getImplicitSelfDecl();
DC = DC->getParent();

if (DC->isProtocolExtensionContext())
if (DC->getAsProtocolExtensionContext())
ExtendedType = DC->getProtocolSelf()->getArchetype();

if (auto *FD = dyn_cast<FuncDecl>(AFD))
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ DeclContext *BoundGenericType::getGenericParamContext(
if (!gpContext)
return getDecl();

assert(gpContext->isNominalTypeOrNominalTypeExtensionContext() == getDecl() &&
assert(gpContext->getAsNominalTypeOrNominalTypeExtensionContext() == getDecl() &&
"not a valid context");
return gpContext;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ bool swift::removeShadowedDecls(SmallVectorImpl<ValueDecl*> &decls,
// If one declaration is in a protocol or extension thereof and the
// other is not, prefer the one that is not.
if ((bool)firstDecl->getDeclContext()
->isProtocolOrProtocolExtensionContext()
->getAsProtocolOrProtocolExtensionContext()
!= (bool)secondDecl->getDeclContext()
->isProtocolOrProtocolExtensionContext()) {
->getAsProtocolOrProtocolExtensionContext()) {
if (firstDecl->getDeclContext()
->isProtocolOrProtocolExtensionContext()) {
->getAsProtocolOrProtocolExtensionContext()) {
shadowed.insert(firstDecl);
break;
} else {
Expand Down Expand Up @@ -433,7 +433,7 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
isCascadingUse = AFD->isCascadingContextForLookup(false);

if (AFD->getExtensionType()) {
if (AFD->getDeclContext()->isProtocolOrProtocolExtensionContext()) {
if (AFD->getDeclContext()->getAsProtocolOrProtocolExtensionContext()) {
ExtendedType = AFD->getDeclContext()->getProtocolSelf()
->getArchetype();

Expand Down Expand Up @@ -479,13 +479,13 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
if (!isCascadingUse.hasValue())
isCascadingUse = ACE->isCascadingContextForLookup(false);
} else if (ExtensionDecl *ED = dyn_cast<ExtensionDecl>(DC)) {
if (ED->isProtocolOrProtocolExtensionContext()) {
if (ED->getAsProtocolOrProtocolExtensionContext()) {
ExtendedType = ED->getProtocolSelf()->getArchetype();
} else {
ExtendedType = ED->getExtendedType();
}

BaseDecl = ED->isNominalTypeOrNominalTypeExtensionContext();
BaseDecl = ED->getAsNominalTypeOrNominalTypeExtensionContext();
MetaBaseDecl = BaseDecl;
if (!isCascadingUse.hasValue())
isCascadingUse = ED->isCascadingContextForLookup(false);
Expand Down
6 changes: 3 additions & 3 deletions lib/AST/ProtocolConformance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ ArrayRef<ValueDecl *>
NominalTypeDecl::getSatisfiedProtocolRequirementsForMember(
const ValueDecl *member,
bool sorted) const {
assert(member->getDeclContext()->isNominalTypeOrNominalTypeExtensionContext()
assert(member->getDeclContext()->getAsNominalTypeOrNominalTypeExtensionContext()
== this);
assert(!isa<ProtocolDecl>(this));
prepareConformanceTable();
Expand All @@ -654,7 +654,7 @@ DeclContext::getLocalProtocols(
SmallVector<ProtocolDecl *, 2> result;

// Dig out the nominal type.
NominalTypeDecl *nominal = isNominalTypeOrNominalTypeExtensionContext();
NominalTypeDecl *nominal = getAsNominalTypeOrNominalTypeExtensionContext();
if (!nominal)
return result;

Expand Down Expand Up @@ -687,7 +687,7 @@ DeclContext::getLocalConformances(
SmallVector<ProtocolConformance *, 2> result;

// Dig out the nominal type.
NominalTypeDecl *nominal = isNominalTypeOrNominalTypeExtensionContext();
NominalTypeDecl *nominal = getAsNominalTypeOrNominalTypeExtensionContext();
if (!nominal)
return result;

Expand Down
4 changes: 2 additions & 2 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2411,7 +2411,7 @@ TypeSubstitutionMap TypeBase::getMemberSubstitutions(const DeclContext *dc) {

// If the member is part of a protocol or extension thereof, we need
// to substitute in the type of Self.
if (dc->isProtocolOrProtocolExtensionContext()) {
if (dc->getAsProtocolOrProtocolExtensionContext()) {
// We only substitute into archetypes for now for protocols.
// FIXME: This seems like an odd restriction. Whatever is depending on
// this, shouldn't.
Expand All @@ -2431,7 +2431,7 @@ TypeSubstitutionMap TypeBase::getMemberSubstitutions(const DeclContext *dc) {
LazyResolver *resolver = dc->getASTContext().getLazyResolver();

// Find the superclass type with the context matching that of the member.
auto ownerNominal = dc->isNominalTypeOrNominalTypeExtensionContext();
auto ownerNominal = dc->getAsNominalTypeOrNominalTypeExtensionContext();
while (!baseTy->is<ErrorType>() &&
baseTy->getAnyNominal() &&
baseTy->getAnyNominal() != ownerNominal) {
Expand Down
Loading