Skip to content

Commit 7b90628

Browse files
authored
Merge pull request #27922 from CodaFi/bitflip
isInvalid() Is Finally Semantic
2 parents a5b8c64 + 0267384 commit 7b90628

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+148
-208
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(bool isInvalid = true) { Bits.Decl.Invalid = isInvalid; }
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/AccessRequests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ AccessLevelRequest::evaluate(Evaluator &evaluator, ValueDecl *D) const {
8181
// Special case for dtors and enum elements: inherit from container
8282
if (D->getKind() == DeclKind::Destructor ||
8383
D->getKind() == DeclKind::EnumElement) {
84-
if (D->isInvalid()) {
84+
if (D->hasInterfaceType() && D->isInvalid()) {
8585
return AccessLevel::Private;
8686
} else {
8787
auto container = cast<NominalTypeDecl>(D->getDeclContext());

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/NameLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void DebuggerClient::anchor() {}
6969
void AccessFilteringDeclConsumer::foundDecl(
7070
ValueDecl *D, DeclVisibilityKind reason,
7171
DynamicLookupInfo dynamicLookupInfo) {
72-
if (D->isInvalid())
72+
if (D->hasInterfaceType() && D->isInvalid())
7373
return;
7474
if (!D->isAccessibleFrom(DC))
7575
return;

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/IDE/ExprContextAnalysis.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,11 @@ Expr *swift::ide::findParsedExpr(const DeclContext *DC,
160160

161161
Type swift::ide::getReturnTypeFromContext(const DeclContext *DC) {
162162
if (auto FD = dyn_cast<AbstractFunctionDecl>(DC)) {
163-
if (FD->hasInterfaceType()) {
164-
auto Ty = FD->getInterfaceType();
165-
if (FD->getDeclContext()->isTypeContext())
166-
Ty = FD->getMethodInterfaceType();
167-
if (auto FT = Ty->getAs<AnyFunctionType>())
168-
return DC->mapTypeIntoContext(FT->getResult());
169-
}
163+
auto Ty = FD->getInterfaceType();
164+
if (FD->getDeclContext()->isTypeContext())
165+
Ty = FD->getMethodInterfaceType();
166+
if (auto FT = Ty->getAs<AnyFunctionType>())
167+
return DC->mapTypeIntoContext(FT->getResult());
170168
} else if (auto ACE = dyn_cast<AbstractClosureExpr>(DC)) {
171169
if (ACE->getType() && !ACE->getType()->hasError())
172170
return ACE->getResultType();
@@ -743,8 +741,7 @@ class ExprContextAnalyzer {
743741
if (!AFD)
744742
return;
745743
auto param = AFD->getParameters()->get(initDC->getIndex());
746-
if (param->hasInterfaceType())
747-
recordPossibleType(AFD->mapTypeIntoContext(param->getInterfaceType()));
744+
recordPossibleType(AFD->mapTypeIntoContext(param->getInterfaceType()));
748745
break;
749746
}
750747
}

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4840,7 +4840,7 @@ Parser::parseDeclVarGetSet(Pattern *pattern, ParseDeclOptions Flags,
48404840
VarLoc, Identifier(),
48414841
CurDeclContext);
48424842
storage->setImplicit(true);
4843-
storage->setInvalid(true);
4843+
storage->setInvalid();
48444844

48454845
Pattern *pattern =
48464846
TypedPattern::createImplicit(Context, new (Context) NamedPattern(storage),
@@ -4850,7 +4850,7 @@ Parser::parseDeclVarGetSet(Pattern *pattern, ParseDeclOptions Flags,
48504850
auto binding = PatternBindingDecl::create(Context, StaticLoc,
48514851
StaticSpelling,
48524852
VarLoc, entry, CurDeclContext);
4853-
binding->setInvalid(true);
4853+
binding->setInvalid();
48544854
storage->setParentPatternBinding(binding);
48554855

48564856
Decls.push_back(binding);

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/CSDiag.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,6 @@ namespace {
860860
llvm::DenseMap<TypeLoc*, Type> TypeLocTypes;
861861
llvm::DenseMap<Pattern*, Type> PatternTypes;
862862
llvm::DenseMap<ParamDecl*, Type> ParamDeclInterfaceTypes;
863-
llvm::DenseSet<ValueDecl*> PossiblyInvalidDecls;
864863
ExprTypeSaverAndEraser(const ExprTypeSaverAndEraser&) = delete;
865864
void operator=(const ExprTypeSaverAndEraser&) = delete;
866865
public:
@@ -909,10 +908,6 @@ namespace {
909908
TS->ParamDeclInterfaceTypes[P] = P->getInterfaceType();
910909
P->setInterfaceType(Type());
911910
}
912-
TS->PossiblyInvalidDecls.insert(P);
913-
914-
if (P->isInvalid())
915-
P->setInvalid(false);
916911
}
917912

918913
expr->setType(nullptr);
@@ -963,16 +958,10 @@ namespace {
963958
paramDeclIfaceElt.first->setInterfaceType(paramDeclIfaceElt.second->getInOutObjectType());
964959
}
965960

966-
if (!PossiblyInvalidDecls.empty())
967-
for (auto D : PossiblyInvalidDecls)
968-
if (D->hasInterfaceType())
969-
D->setInvalid(D->getInterfaceType()->hasError());
970-
971961
// Done, don't do redundant work on destruction.
972962
ExprTypes.clear();
973963
TypeLocTypes.clear();
974964
PatternTypes.clear();
975-
PossiblyInvalidDecls.clear();
976965
}
977966

978967
// On destruction, if a type got wiped out, reset it from null to its
@@ -1000,11 +989,6 @@ namespace {
1000989
paramDeclIfaceElt.first->setInterfaceType(
1001990
getParamBaseType(paramDeclIfaceElt));
1002991
}
1003-
1004-
if (!PossiblyInvalidDecls.empty())
1005-
for (auto D : PossiblyInvalidDecls)
1006-
if (D->hasInterfaceType())
1007-
D->setInvalid(D->getInterfaceType()->hasError());
1008992
}
1009993

1010994
private:

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/ConstraintSystem.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -825,11 +825,6 @@ Type ConstraintSystem::getUnopenedTypeOfReference(VarDecl *value, Type baseType,
825825
return getType(param);
826826

827827
if (!var->hasInterfaceType()) {
828-
if (!var->isInvalid()) {
829-
TC.diagnose(var->getLoc(), diag::recursive_decl_reference,
830-
var->getDescriptiveKind(), var->getName());
831-
var->setInterfaceType(ErrorType::get(getASTContext()));
832-
}
833828
return ErrorType::get(TC.Context);
834829
}
835830

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

0 commit comments

Comments
 (0)