Skip to content

Commit 52cf365

Browse files
committed
Sema: Simplify validateDecl()
Remove the early return in the case where one of our parent contexts was being validated, and replace it with a simpler check that is only performed in some callers related to associated type inference; we want to bail out in one specific case only, which is that the declaration is inside an extension whose generic signature is in the process of being computed.
1 parent 1d80980 commit 52cf365

File tree

6 files changed

+10
-64
lines changed

6 files changed

+10
-64
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -857,17 +857,6 @@ class alignas(1 << DeclAlignInBits) Decl {
857857
return getValidationState() > ValidationState::Unchecked;
858858
}
859859

860-
/// Manually indicate that validation is complete for the declaration. For
861-
/// example: during importing, code synthesis, or derived conformances.
862-
///
863-
/// For normal code validation, please use DeclValidationRAII instead.
864-
///
865-
/// FIXME -- Everything should use DeclValidationRAII instead of this.
866-
void setValidationToChecked() {
867-
if (!isBeingValidated())
868-
Bits.Decl.ValidationState = unsigned(ValidationState::Checked);
869-
}
870-
871860
bool escapedFromIfConfig() const {
872861
return Bits.Decl.EscapedFromIfConfig;
873862
}

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2706,7 +2706,7 @@ bool ValueDecl::hasInterfaceType() const {
27062706
}
27072707

27082708
bool ValueDecl::isRecursiveValidation() const {
2709-
if (hasValidationStarted() || !hasInterfaceType())
2709+
if (hasValidationStarted() && !hasInterfaceType())
27102710
return true;
27112711

27122712
if (auto *vd = dyn_cast<VarDecl>(this))

lib/AST/NameLookup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,9 @@ static void recordShadowedDecls(ArrayRef<ValueDecl *> decls,
487487
// If the decl is currently being validated, this is likely a recursive
488488
// reference and we'll want to skip ahead so as to avoid having its type
489489
// attempt to desugar itself.
490-
auto ifaceType = decl->getInterfaceType();
491-
if (!ifaceType)
490+
if (decl->isRecursiveValidation())
492491
continue;
492+
auto ifaceType = decl->getInterfaceType();
493493

494494
// FIXME: the canonical type makes a poor signature, because we don't
495495
// canonicalize away default arguments.

lib/Sema/TypeCheckDecl.cpp

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3226,10 +3226,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
32263226
return;
32273227
}
32283228

3229-
// Validate the nominal type declaration being extended.
3230-
(void)nominal->getInterfaceType();
3231-
(void)ED->getGenericSignature();
3232-
ED->setValidationToChecked();
3229+
// Produce any diagnostics for the generic signature.
3230+
(void) ED->getGenericSignature();
32333231

32343232
if (extType && !extType->hasError()) {
32353233
// The first condition catches syntactic forms like
@@ -3865,54 +3863,14 @@ void TypeChecker::validateDecl(ValueDecl *D) {
38653863
// Handling validation failure due to re-entrancy is left
38663864
// up to the caller, who must call hasInterfaceType() to
38673865
// check that validateDecl() returned a fully-formed decl.
3868-
if (D->hasValidationStarted() || D->hasInterfaceType())
3866+
if (D->isBeingValidated() || D->hasInterfaceType())
38693867
return;
38703868

3871-
// FIXME: It would be nicer if Sema would always synthesize fully-typechecked
3872-
// declarations, but for now, you can make an imported type conform to a
3873-
// protocol with property requirements, which requires synthesizing getters
3874-
// and setters, etc.
3875-
if (!isa<VarDecl>(D) && !isa<AccessorDecl>(D)) {
3876-
assert(isa<SourceFile>(D->getDeclContext()->getModuleScopeContext()) &&
3877-
"Should not validate imported or deserialized declarations");
3878-
}
3879-
38803869
PrettyStackTraceDecl StackTrace("validating", D);
38813870
FrontendStatsTracer StatsTracer(Context.Stats, "validate-decl", D);
38823871

38833872
checkForForbiddenPrefix(D);
38843873

3885-
// Validate the context.
3886-
auto dc = D->getDeclContext();
3887-
if (auto nominal = dyn_cast<NominalTypeDecl>(dc)) {
3888-
if (!nominal->getInterfaceType())
3889-
return;
3890-
} else if (auto ext = dyn_cast<ExtensionDecl>(dc)) {
3891-
// If we're currently validating, or have already validated this extension,
3892-
// there's nothing more to do now.
3893-
if (!ext->hasValidationStarted()) {
3894-
DeclValidationRAII IBV(ext);
3895-
3896-
if (auto *nominal = ext->getExtendedNominal()) {
3897-
// Validate the nominal type declaration being extended.
3898-
// FIXME(InterfaceTypeRequest): isInvalid() should be based on the interface type.
3899-
(void)nominal->getInterfaceType();
3900-
3901-
// Eagerly validate the generic signature of the extension.
3902-
(void)ext->getGenericSignature();
3903-
}
3904-
}
3905-
if (ext->getValidationState() == Decl::ValidationState::Checking)
3906-
return;
3907-
}
3908-
3909-
// Validating the parent may have triggered validation of this declaration,
3910-
// so just return if that was the case.
3911-
if (D->hasValidationStarted() || D->hasInterfaceType()) {
3912-
assert(D->hasInterfaceType());
3913-
return;
3914-
}
3915-
39163874
if (Context.Stats)
39173875
Context.Stats->getFrontendCounters().NumDeclsValidated++;
39183876

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,6 @@ LookupTypeResult TypeChecker::lookupMemberType(DeclContext *dc,
504504
// member entirely.
505505
auto *protocol = cast<ProtocolDecl>(assocType->getDeclContext());
506506

507-
// If we're validating the protocol recursively, bail out.
508-
if (!protocol->hasInterfaceType())
509-
continue;
510-
511507
auto conformance = conformsToProtocol(type, protocol, dc,
512508
conformanceOptions);
513509
if (!conformance) {

lib/Sema/TypeCheckProtocolInference.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,12 @@ static Type mapErrorTypeToOriginal(Type type) {
492492
static Type getWitnessTypeForMatching(TypeChecker &tc,
493493
NormalProtocolConformance *conformance,
494494
ValueDecl *witness) {
495-
if (!witness->getInterfaceType())
495+
if (witness->isRecursiveValidation())
496496
return Type();
497497

498+
// FIXME: This is here to trigger the isInvalid() computation.
499+
(void) witness->getInterfaceType();
500+
498501
if (witness->isInvalid())
499502
return Type();
500503

0 commit comments

Comments
 (0)