Skip to content

[Deserialization] Output a diagnostic when deserializing an invalid decl #36153

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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,9 @@ NOTE(serialization_compatibility_version_mismatch,none,
"(this is supported but may expose additional compiler issues)",
(StringRef, StringRef, StringRef))

ERROR(serialization_invalid_decl,none,
"deserialization of invalid declaration %0 from module '%1'",
(DeclName, StringRef))
ERROR(serialization_allowing_invalid_decl,none,
"allowing deserialization of invalid declaration %0 from module '%1'",
(DeclName, StringRef))
Expand Down
8 changes: 1 addition & 7 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,15 +439,9 @@ bool Decl::isInvalid() const {
case DeclKind::Constructor:
case DeclKind::Destructor:
case DeclKind::Func:
case DeclKind::Accessor:
case DeclKind::EnumElement:
return cast<ValueDecl>(this)->getInterfaceType()->hasError();

case DeclKind::Accessor: {
auto *AD = cast<AccessorDecl>(this);
if (AD->hasInterfaceType() && AD->getInterfaceType()->hasError())
return true;
return AD->getStorage()->isInvalid();
}
}

llvm_unreachable("Unknown decl kind");
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3242,9 +3242,9 @@ performTopLevelDeclDiagnostics(TopLevelCodeDecl *TLCD) {
/// Perform diagnostics for func/init/deinit declarations.
void swift::performAbstractFuncDeclDiagnostics(AbstractFunctionDecl *AFD) {
// Don't produce these diagnostics for implicitly generated code.
if (AFD->getLoc().isInvalid() || AFD->isImplicit() || AFD->isInvalid())
if (AFD->getLoc().isInvalid() || AFD->isImplicit())
return;

// Check for unused variables, as well as variables that are could be
// declared as constants. Skip local functions though, since they will
// be checked as part of their parent function or TopLevelCodeDecl.
Expand Down
25 changes: 13 additions & 12 deletions lib/Sema/TypeCheckDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1248,18 +1248,6 @@ Optional<ObjCReason> shouldMarkAsObjC(const ValueDecl *VD, bool allowImplicit) {

// Local function to determine whether we can implicitly infer @objc.
auto canInferImplicitObjC = [&](bool allowAnyAccess) {
if (VD->isInvalid())
return false;
if (VD->isOperator())
return false;

// Implicitly generated declarations are not @objc, except for constructors.
if (!allowImplicit && VD->isImplicit())
return false;

if (!allowAnyAccess && VD->getFormalAccess() <= AccessLevel::FilePrivate)
return false;

if (auto accessor = dyn_cast<AccessorDecl>(VD)) {
switch (accessor->getAccessorKind()) {
case AccessorKind::DidSet:
Expand All @@ -1275,6 +1263,19 @@ Optional<ObjCReason> shouldMarkAsObjC(const ValueDecl *VD, bool allowImplicit) {
break;
}
}

if (VD->isInvalid())
return false;
if (VD->isOperator())
return false;

// Implicitly generated declarations are not @objc, except for constructors.
if (!allowImplicit && VD->isImplicit())
return false;

if (!allowAnyAccess && VD->getFormalAccess() <= AccessLevel::FilePrivate)
return false;

return true;
};

Expand Down
26 changes: 17 additions & 9 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4085,15 +4085,23 @@ ModuleFile::getDeclChecked(
return deserialized;

auto *decl = declOrOffset.get();
if (isAllowModuleWithCompilerErrorsEnabled() && decl->isInvalid()) {
if (!isa<ParamDecl>(decl) && !decl->isImplicit()) {
// The parent function will be invalid if the parameter is invalid,
// implicits should have an invalid explicit as well
if (auto *VD = dyn_cast<ValueDecl>(decl)) {
getContext().Diags.diagnose(
VD->getLoc(), diag::serialization_allowing_invalid_decl,
VD->getName(), VD->getModuleContext()->getNameStr());
}
if (decl->isInvalid() && isa<ValueDecl>(decl)) {
auto *VD = cast<ValueDecl>(decl);
if (!isAllowModuleWithCompilerErrorsEnabled()) {
getContext().Diags.diagnose(
SourceLoc(), diag::serialization_invalid_decl,
VD->getName(), VD->getModuleContext()->getNameStr());
} else if (!isa<ParamDecl>(decl) && !decl->isImplicit()) {
// Grabbing the location of a parameter gets the location of its
// context, which isn't deserialized yet. Since the parent will be
// invalid if the parameter is, just skip it and wait for the
// parent to output the diagnostic.
//
// Any invalid implicit should have an invalid explicit, so ignore in
// preference for the explicit.
getContext().Diags.diagnose(
VD->getLoc(), diag::serialization_allowing_invalid_decl,
VD->getName(), VD->getModuleContext()->getNameStr());
}
}
} else if (matchAttributes) {
Expand Down