Skip to content

Commit 4996858

Browse files
committed
Re-implement isInvalid for ValueDecls
1 parent 41d099b commit 4996858

23 files changed

+98
-80
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,10 +807,10 @@ class alignas(1 << DeclAlignInBits) Decl {
807807
bool walk(ASTWalker &walker);
808808

809809
/// Return whether this declaration has been determined invalid.
810-
bool isInvalid() const { return Bits.Decl.Invalid; }
810+
bool isInvalid() const;
811811

812812
/// Mark this declaration invalid.
813-
void setInvalid() { Bits.Decl.Invalid = true; }
813+
void setInvalid();
814814

815815
/// Determine whether this declaration was implicitly generated by the
816816
/// compiler (rather than explicitly written in source code).

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2366,7 +2366,7 @@ CanType ASTMangler::getDeclTypeForMangling(
23662366
parentGenericSig = GenericSignature();
23672367

23682368
auto &C = decl->getASTContext();
2369-
if (!decl->getInterfaceType() || decl->getInterfaceType()->is<ErrorType>()) {
2369+
if (decl->isInvalid()) {
23702370
if (isa<AbstractFunctionDecl>(decl))
23712371
return CanFunctionType::get({AnyFunctionType::Param(C.TheErrorType)},
23722372
C.TheErrorType);

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2872,7 +2872,7 @@ void PrintAST::printEnumElement(EnumElementDecl *elt) {
28722872

28732873

28742874
auto params = ArrayRef<AnyFunctionType::Param>();
2875-
if (elt->hasInterfaceType() && !elt->getInterfaceType()->hasError()) {
2875+
if (elt->hasInterfaceType() && !elt->isInvalid()) {
28762876
// Walk to the params of the associated values.
28772877
// (EnumMetaType) -> (AssocValues) -> Enum
28782878
params = elt->getInterfaceType()->castTo<AnyFunctionType>()
@@ -2969,7 +2969,7 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
29692969
}, [&] { // Parameters
29702970
printGenericDeclGenericParams(decl);
29712971
auto params = ArrayRef<AnyFunctionType::Param>();
2972-
if (decl->hasInterfaceType() && !decl->getInterfaceType()->hasError()) {
2972+
if (decl->hasInterfaceType() && !decl->isInvalid()) {
29732973
// Walk to the params of the subscript's indices.
29742974
params = decl->getInterfaceType()->castTo<AnyFunctionType>()->getParams();
29752975
}

lib/AST/Decl.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,77 @@ DeclContext *Decl::getInnermostDeclContext() const {
376376
return getDeclContext();
377377
}
378378

379+
bool Decl::isInvalid() const {
380+
switch (getKind()) {
381+
#define VALUE_DECL(ID, PARENT)
382+
#define DECL(ID, PARENT) \
383+
case DeclKind::ID:
384+
#include "swift/AST/DeclNodes.def"
385+
return Bits.Decl.Invalid;
386+
case DeclKind::Param: {
387+
// Parameters are special because closure parameters may not have type
388+
// annotations. In which case, the interface type request returns
389+
// ErrorType. Therefore, consider parameters with implicit types to always
390+
// be valid.
391+
auto *PD = cast<ParamDecl>(this);
392+
if (!PD->getTypeRepr() && !PD->hasInterfaceType())
393+
return false;
394+
}
395+
LLVM_FALLTHROUGH;
396+
case DeclKind::Enum:
397+
case DeclKind::Struct:
398+
case DeclKind::Class:
399+
case DeclKind::Protocol:
400+
case DeclKind::OpaqueType:
401+
case DeclKind::TypeAlias:
402+
case DeclKind::GenericTypeParam:
403+
case DeclKind::AssociatedType:
404+
case DeclKind::Module:
405+
case DeclKind::Var:
406+
case DeclKind::Subscript:
407+
case DeclKind::Constructor:
408+
case DeclKind::Destructor:
409+
case DeclKind::Func:
410+
case DeclKind::Accessor:
411+
case DeclKind::EnumElement:
412+
return cast<ValueDecl>(this)->getInterfaceType()->hasError();
413+
}
414+
415+
llvm_unreachable("Unknown decl kind");
416+
}
417+
418+
void Decl::setInvalid() {
419+
switch (getKind()) {
420+
#define VALUE_DECL(ID, PARENT)
421+
#define DECL(ID, PARENT) \
422+
case DeclKind::ID:
423+
#include "swift/AST/DeclNodes.def"
424+
Bits.Decl.Invalid = true;
425+
return;
426+
case DeclKind::Enum:
427+
case DeclKind::Struct:
428+
case DeclKind::Class:
429+
case DeclKind::Protocol:
430+
case DeclKind::OpaqueType:
431+
case DeclKind::TypeAlias:
432+
case DeclKind::GenericTypeParam:
433+
case DeclKind::AssociatedType:
434+
case DeclKind::Module:
435+
case DeclKind::Var:
436+
case DeclKind::Param:
437+
case DeclKind::Subscript:
438+
case DeclKind::Constructor:
439+
case DeclKind::Destructor:
440+
case DeclKind::Func:
441+
case DeclKind::Accessor:
442+
case DeclKind::EnumElement:
443+
cast<ValueDecl>(this)->setInterfaceType(ErrorType::get(getASTContext()));
444+
return;
445+
}
446+
447+
llvm_unreachable("Unknown decl kind");
448+
}
449+
379450
void Decl::setDeclContext(DeclContext *DC) {
380451
Context = DC;
381452
}

lib/AST/Module.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,15 @@ void SourceLookupCache::addToUnqualifiedLookupCache(Range decls,
212212
if (!NTD->hasUnparsedMembers() || NTD->maybeHasOperatorDeclarations())
213213
addToUnqualifiedLookupCache(NTD->getMembers(), true);
214214

215-
// Avoid populating the cache with the members of invalid extension
216-
// declarations. These members can be used to point validation inside of
217-
// a malformed context.
218-
if (D->isInvalid()) continue;
215+
if (auto *ED = dyn_cast<ExtensionDecl>(D)) {
216+
// Avoid populating the cache with the members of invalid extension
217+
// declarations. These members can be used to point validation inside of
218+
// a malformed context.
219+
if (ED->isInvalid()) continue;
219220

220-
if (auto *ED = dyn_cast<ExtensionDecl>(D))
221221
if (!ED->hasUnparsedMembers() || ED->maybeHasOperatorDeclarations())
222222
addToUnqualifiedLookupCache(ED->getMembers(), true);
223+
}
223224
}
224225
}
225226

lib/AST/TypeCheckRequests.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,9 +1020,6 @@ void InterfaceTypeRequest::cacheResult(Type type) const {
10201020
assert(!type->hasTypeVariable() && "Type variable in interface type");
10211021
assert(!type->is<InOutType>() && "Interface type must be materializable");
10221022
assert(!type->hasArchetype() && "Archetype in interface type");
1023-
1024-
if (type->hasError())
1025-
decl->setInvalid();
10261023
}
10271024
decl->TypeAndAccess.setPointer(type);
10281025
}

lib/AST/USRGeneration.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,6 @@ swift::USRGenerationRequest::evaluate(Evaluator &evaluator,
258258
llvm::Expected<std::string>
259259
swift::MangleLocalTypeDeclRequest::evaluate(Evaluator &evaluator,
260260
const TypeDecl *D) const {
261-
if (!D->getInterfaceType())
262-
return std::string();
263-
264261
if (isa<ModuleDecl>(D))
265262
return std::string(); // Ignore.
266263

lib/IDE/CodeCompletion.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,8 +2431,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
24312431
addTypeAnnotation(Builder, AFT->getResult());
24322432
};
24332433

2434-
if (!AFD || !AFD->getInterfaceType() ||
2435-
!AFD->getInterfaceType()->is<AnyFunctionType>()) {
2434+
if (!AFD || !AFD->getInterfaceType()->is<AnyFunctionType>()) {
24362435
// Probably, calling closure type expression.
24372436
foundFunction(AFT);
24382437
addPattern();

lib/Sema/CSApply.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1940,7 +1940,6 @@ namespace {
19401940
auto maxFloatTypeDecl = tc.Context.get_MaxBuiltinFloatTypeDecl();
19411941

19421942
if (!maxFloatTypeDecl ||
1943-
!maxFloatTypeDecl->getInterfaceType() ||
19441943
!maxFloatTypeDecl->getDeclaredInterfaceType()->is<BuiltinFloatType>()) {
19451944
tc.diagnose(expr->getLoc(), diag::no_MaxBuiltinFloatType_found);
19461945
return nullptr;

lib/Sema/CSGen.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -961,8 +961,6 @@ namespace {
961961
if (!decl)
962962
return nullptr;
963963

964-
// FIXME(InterfaceTypeRequest): isInvalid() should be based on the interface type.
965-
(void)decl->getInterfaceType();
966964
if (decl->isInvalid())
967965
return nullptr;
968966

@@ -1427,8 +1425,6 @@ namespace {
14271425
// If the result is invalid, skip it.
14281426
// FIXME: Note this as invalid, in case we don't find a solution,
14291427
// so we don't let errors cascade further.
1430-
// FIXME(InterfaceTypeRequest): isInvalid() should be based on the interface type.
1431-
(void)decls[i]->getInterfaceType();
14321428
if (decls[i]->isInvalid())
14331429
continue;
14341430

lib/Sema/CSSimplify.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5009,8 +5009,6 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
50095009
}
50105010

50115011
// If the result is invalid, skip it.
5012-
// FIXME(InterfaceTypeRequest): isInvalid() should be based on the interface type.
5013-
(void)decl->getInterfaceType();
50145012
if (decl->isInvalid()) {
50155013
result.markErrorAlreadyDiagnosed();
50165014
return;
@@ -5386,8 +5384,6 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
53865384
auto *cand = entry.getValueDecl();
53875385

53885386
// If the result is invalid, skip it.
5389-
// FIXME(InterfaceTypeRequest): isInvalid() should be based on the interface type.
5390-
(void)cand->getInterfaceType();
53915387
if (cand->isInvalid()) {
53925388
result.markErrorAlreadyDiagnosed();
53935389
return result;
@@ -7337,8 +7333,6 @@ ConstraintSystem::simplifyDynamicCallableApplicableFnConstraint(
73377333
// Record the 'dynamicallyCall` method overload set.
73387334
SmallVector<OverloadChoice, 4> choices;
73397335
for (auto candidate : candidates) {
7340-
// FIXME(InterfaceTypeRequest): isInvalid() should be based on the interface type.
7341-
(void)candidate->getInterfaceType();
73427336
if (candidate->isInvalid()) continue;
73437337
choices.push_back(
73447338
OverloadChoice(type2, candidate, FunctionRefKind::SingleApply));

lib/Sema/DerivedConformanceRawRepresentable.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,6 @@ static bool canSynthesizeRawRepresentable(DerivedConformance &derived) {
482482
if (elt->hasAssociatedValues())
483483
return false;
484484

485-
// FIXME(InterfaceTypeRequest): isInvalid() should be based on the interface type.
486-
(void)elt->getInterfaceType();
487485
if (elt->isInvalid()) {
488486
return false;
489487
}

lib/Sema/LookupVisibleDecls.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -765,9 +765,6 @@ class OverrideFilteringConsumer : public VisibleDeclConsumer {
765765
if (VD->isRecursiveValidation())
766766
continue;
767767

768-
// FIXME: This is used to compute isInvalid() below.
769-
(void) VD->getInterfaceType();
770-
771768
auto &PossiblyConflicting = DeclsByName[VD->getBaseName()];
772769

773770
if (VD->isInvalid()) {
@@ -811,9 +808,6 @@ class OverrideFilteringConsumer : public VisibleDeclConsumer {
811808
if (OtherVD->isRecursiveValidation())
812809
continue;
813810

814-
// FIXME: This is used to compute isInvalid() below.
815-
(void) OtherVD->getInterfaceType();
816-
817811
if (OtherVD->isInvalid())
818812
continue;
819813

lib/Sema/MiscDiagnostics.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,7 +2103,7 @@ class VarDeclUsageChecker : public ASTWalker {
21032103

21042104
// If the variable was invalid, ignore it and notice that the code is
21052105
// malformed.
2106-
if (!VD->getInterfaceType() || VD->isInvalid()) {
2106+
if (VD->isInvalid()) {
21072107
sawError = true;
21082108
return false;
21092109
}
@@ -3792,8 +3792,7 @@ static void diagnoseUnintendedOptionalBehavior(TypeChecker &TC, const Expr *E,
37923792
ValueDecl * fnDecl = appendMethod.getDecl();
37933793

37943794
// If things aren't set up right, just hope for the best.
3795-
if (!fnDecl || !fnDecl->getInterfaceType() ||
3796-
fnDecl->getInterfaceType()->hasError())
3795+
if (!fnDecl || fnDecl->isInvalid())
37973796
return false;
37983797

37993798
// If the decl expects an optional, that's fine.
@@ -4205,7 +4204,7 @@ static OmissionTypeName getTypeNameForOmission(Type type) {
42054204
Optional<DeclName> TypeChecker::omitNeedlessWords(AbstractFunctionDecl *afd) {
42064205
auto &Context = afd->getASTContext();
42074206

4208-
if (!afd->getInterfaceType() || afd->isInvalid() || isa<DestructorDecl>(afd))
4207+
if (afd->isInvalid() || isa<DestructorDecl>(afd))
42094208
return None;
42104209

42114210
DeclName name = afd->getFullName();
@@ -4285,7 +4284,7 @@ Optional<DeclName> TypeChecker::omitNeedlessWords(AbstractFunctionDecl *afd) {
42854284
Optional<Identifier> TypeChecker::omitNeedlessWords(VarDecl *var) {
42864285
auto &Context = var->getASTContext();
42874286

4288-
if (!var->getInterfaceType() || var->isInvalid())
4287+
if (var->isInvalid())
42894288
return None;
42904289

42914290
if (var->getName().empty())

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,9 +1354,6 @@ bool PreCheckExpression::walkToClosureExprPre(ClosureExpr *closure) {
13541354
// afterwards. This allows for better diagnostics, and keeps the
13551355
// closure expression type well-formed.
13561356
for (auto param : *PL) {
1357-
// FIXME: Forces computation of isInvalid().
1358-
(void) param->getInterfaceType();
1359-
13601357
hadParameterError |= param->isInvalid();
13611358
}
13621359

lib/Sema/TypeCheckDecl.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -634,13 +634,8 @@ static void checkRedeclaration(ASTContext &ctx, ValueDecl *current) {
634634
// Make sure we don't do this checking again.
635635
current->setCheckedRedeclaration(true);
636636

637-
// FIXME: Computes isInvalid() below.
638-
(void) current->getInterfaceType();
639-
640637
// Ignore invalid and anonymous declarations.
641-
if (current->isInvalid() ||
642-
!current->hasInterfaceType() ||
643-
!current->hasName())
638+
if (current->isInvalid() || !current->hasName())
644639
return;
645640

646641
// If this declaration isn't from a source file, don't check it.
@@ -704,9 +699,6 @@ static void checkRedeclaration(ASTContext &ctx, ValueDecl *current) {
704699
if (!conflicting(currentSig, otherSig))
705700
continue;
706701

707-
// FIXME: Computes isInvalid() below.
708-
(void) other->getInterfaceType();
709-
710702
// Skip invalid declarations.
711703
if (other->isInvalid())
712704
continue;
@@ -1593,9 +1585,6 @@ EnumRawValuesRequest::evaluate(Evaluator &eval, EnumDecl *ED,
15931585

15941586
Optional<AutomaticEnumValueKind> valueKind;
15951587
for (auto elt : ED->getAllElements()) {
1596-
// FIXME: Computes isInvalid() below.
1597-
(void) elt->getInterfaceType();
1598-
15991588
// If the element has been diagnosed up to now, skip it.
16001589
if (elt->isInvalid())
16011590
continue;
@@ -4375,7 +4364,8 @@ NamingPatternRequest::evaluate(Evaluator &evaluator, VarDecl *VD) const {
43754364
//
43764365
// Once that's through, this will only fire during circular validation.
43774366
if (!namingPattern) {
4378-
if (!VD->isInvalid() && !VD->getParentPattern()->isImplicit()) {
4367+
if (VD->hasInterfaceType() &&
4368+
!VD->isInvalid() && !VD->getParentPattern()->isImplicit()) {
43794369
VD->diagnose(diag::variable_bound_by_no_pattern, VD->getName());
43804370
}
43814371

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -811,10 +811,7 @@ bool swift::isRepresentableInObjC(
811811

812812
bool swift::isRepresentableInObjC(const VarDecl *VD, ObjCReason Reason) {
813813
// If you change this function, you must add or modify a test in PrintAsObjC.
814-
815-
// FIXME: Computes isInvalid() below.
816-
(void) VD->getInterfaceType();
817-
814+
818815
if (VD->isInvalid())
819816
return false;
820817

lib/Sema/TypeCheckExpr.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,6 @@ static Type lookupDefaultLiteralType(const DeclContext *dc,
652652
if (!TD)
653653
return Type();
654654

655-
// FIXME: Make isInvalid ask for the interface type.
656-
(void)TD->getInterfaceType();
657-
658655
if (TD->isInvalid())
659656
return Type();
660657

lib/Sema/TypeCheckPattern.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ bool TypeChecker::coercePatternToType(Pattern *&P, TypeResolution resolution,
977977
case PatternKind::Named: {
978978
NamedPattern *NP = cast<NamedPattern>(P);
979979
VarDecl *var = NP->getDecl();
980-
if (var->isInvalid())
980+
if (var->hasInterfaceType() && var->isInvalid())
981981
type = ErrorType::get(Context);
982982

983983
// In SIL mode, VarDecls are written as having reference storage types.

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,10 +1536,6 @@ checkIndividualConformance(NormalProtocolConformance *conformance,
15361536
conformance->setState(ProtocolConformanceState::Checking);
15371537
SWIFT_DEFER { conformance->setState(ProtocolConformanceState::Complete); };
15381538

1539-
// FIXME(InterfaceTypeRequest): isInvalid() should be based on the interface
1540-
// type.
1541-
(void)Proto->getInterfaceType();
1542-
15431539
// If the protocol itself is invalid, there's nothing we can do.
15441540
if (Proto->isInvalid()) {
15451541
conformance->setInvalid();
@@ -3868,7 +3864,7 @@ void ConformanceChecker::resolveValueWitnesses() {
38683864
}
38693865

38703866
// Make sure we've got an interface type.
3871-
if (!requirement->getInterfaceType() || requirement->isInvalid()) {
3867+
if (requirement->isInvalid()) {
38723868
Conformance->setInvalid();
38733869
continue;
38743870
}

0 commit comments

Comments
 (0)