Skip to content

Commit 409aa35

Browse files
committed
[Diagnostic] Add a diagnostic for invalid declaration refs
If AST node is a reference to an invalid declaration let's diagnose it as such unless some errors have been emitted (invalid declaration itself should have a diagnostic describing a reason why it's invalid).
1 parent f1fd528 commit 409aa35

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3726,6 +3726,9 @@ ERROR(capture_across_type_decl,none,
37263726
"%0 declaration cannot close over value %1 defined in outer scope",
37273727
(DescriptiveDeclKind, Identifier))
37283728

3729+
ERROR(reference_to_invalid_decl,none,
3730+
"cannot reference invalid declaration %0", (DeclName))
3731+
37293732
//------------------------------------------------------------------------------
37303733
// MARK: Type Check Statements
37313734
//------------------------------------------------------------------------------

lib/Sema/CSDiagnostics.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7002,3 +7002,21 @@ bool MissingContextualTypeForNil::diagnoseAsError() {
70027002
emitDiagnostic(diag::unresolved_nil_literal);
70037003
return true;
70047004
}
7005+
7006+
bool ReferenceToInvalidDeclaration::diagnoseAsError() {
7007+
auto *decl = castToExpr<DeclRefExpr>(getAnchor())->getDecl();
7008+
assert(decl);
7009+
7010+
auto &DE = getASTContext().Diags;
7011+
// This problem should have been already diagnosed during
7012+
// validation of the declaration.
7013+
if (DE.hadAnyError())
7014+
return true;
7015+
7016+
// If no errors have been emitted yet, let's emit one
7017+
// about reference to an invalid declaration.
7018+
7019+
emitDiagnostic(diag::reference_to_invalid_decl, decl->getName());
7020+
emitDiagnosticAt(decl, diag::decl_declared_here, decl->getName());
7021+
return true;
7022+
}

lib/Sema/CSDiagnostics.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,6 +2281,21 @@ class MissingContextualTypeForNil final : public FailureDiagnostic {
22812281
bool diagnoseAsError() override;
22822282
};
22832283

2284+
/// Diagnostic situations where AST node references an invalid declaration.
2285+
///
2286+
/// \code
2287+
/// let foo = doesntExist // or something invalid
2288+
/// foo(42)
2289+
/// \endcode
2290+
class ReferenceToInvalidDeclaration final : public FailureDiagnostic {
2291+
public:
2292+
ReferenceToInvalidDeclaration(const Solution &solution,
2293+
ConstraintLocator *locator)
2294+
: FailureDiagnostic(solution, locator) {}
2295+
2296+
bool diagnoseAsError() override;
2297+
};
2298+
22842299
} // end namespace constraints
22852300
} // end namespace swift
22862301

lib/Sema/CSFix.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,8 @@ SpecifyContextualTypeForNil::create(ConstraintSystem &cs,
16181618

16191619
bool AllowRefToInvalidDecl::diagnose(const Solution &solution,
16201620
bool asNote) const {
1621-
return false;
1621+
ReferenceToInvalidDeclaration failure(solution, getLocator());
1622+
return failure.diagnose(asNote);
16221623
}
16231624

16241625
AllowRefToInvalidDecl *

0 commit comments

Comments
 (0)