Skip to content

Commit 675e6d8

Browse files
authored
Merge pull request #15703 from slavapestov/decl-checker-cleanup-part-4
Remove almost all remaining uses of DeclChecker::IsFirstPass
2 parents 03dce36 + b4f2324 commit 675e6d8

File tree

9 files changed

+88
-184
lines changed

9 files changed

+88
-184
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,10 +666,6 @@ NOTE(note_typo_candidate_implicit_member,none,
666666
"did you mean the implicitly-synthesized %1 '%0'?", (StringRef, StringRef))
667667
NOTE(note_remapped_type,none,
668668
"did you mean to use '%0'?", (StringRef))
669-
ERROR(identifier_init_failure,none,
670-
"could not infer type for %0", (Identifier))
671-
ERROR(pattern_used_in_type,none,
672-
"%0 used within its own type", (Identifier))
673669
NOTE(note_module_as_type,none,
674670
"cannot use module %0 as a type", (Identifier))
675671

include/swift/AST/LazyResolver.h

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -101,71 +101,6 @@ class LazyResolver {
101101
DeclContext *dc) = 0;
102102
};
103103

104-
/// An implementation of LazyResolver that delegates to another.
105-
class DelegatingLazyResolver : public LazyResolver {
106-
protected:
107-
LazyResolver &Principal;
108-
public:
109-
DelegatingLazyResolver(LazyResolver &principal) : Principal(principal) {}
110-
~DelegatingLazyResolver(); // v-table anchor
111-
112-
void resolveTypeWitness(const NormalProtocolConformance *conformance,
113-
AssociatedTypeDecl *assocType) override {
114-
Principal.resolveTypeWitness(conformance, assocType);
115-
}
116-
117-
void resolveWitness(const NormalProtocolConformance *conformance,
118-
ValueDecl *requirement) override {
119-
Principal.resolveWitness(conformance, requirement);
120-
}
121-
122-
void resolveAccessControl(ValueDecl *VD) override {
123-
Principal.resolveAccessControl(VD);
124-
}
125-
126-
void resolveDeclSignature(ValueDecl *VD) override {
127-
Principal.resolveDeclSignature(VD);
128-
}
129-
130-
void resolveInheritanceClause(
131-
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl) override {
132-
Principal.resolveInheritanceClause(decl);
133-
}
134-
135-
void resolveSuperclass(ClassDecl *classDecl) override {
136-
Principal.resolveSuperclass(classDecl);
137-
}
138-
139-
void resolveRawType(EnumDecl *enumDecl) override {
140-
Principal.resolveRawType(enumDecl);
141-
}
142-
143-
void resolveInheritedProtocols(ProtocolDecl *protocol) override {
144-
Principal.resolveInheritedProtocols(protocol);
145-
}
146-
147-
void bindExtension(ExtensionDecl *ext) override {
148-
Principal.bindExtension(ext);
149-
}
150-
151-
void resolveExtension(ExtensionDecl *ext) override {
152-
Principal.resolveExtension(ext);
153-
}
154-
155-
void resolveImplicitConstructors(NominalTypeDecl *nominal) override {
156-
Principal.resolveImplicitConstructors(nominal);
157-
}
158-
159-
void resolveImplicitMember(NominalTypeDecl *nominal, DeclName member) override {
160-
Principal.resolveImplicitMember(nominal, member);
161-
}
162-
163-
void markConformanceUsed(ProtocolConformanceRef conformance,
164-
DeclContext *dc) override {
165-
return Principal.markConformanceUsed(conformance, dc);
166-
}
167-
};
168-
169104
class LazyMemberLoader;
170105

171106
/// Context data for lazy deserialization.

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ STATISTIC(NumCollapsedSpecializedProtocolConformances,
6767
#define SWIFT_GSB_EXPENSIVE_ASSERTIONS 0
6868

6969
LazyResolver::~LazyResolver() = default;
70-
DelegatingLazyResolver::~DelegatingLazyResolver() = default;
7170
void ModuleLoader::anchor() {}
7271
void ClangModuleLoader::anchor() {}
7372

lib/Sema/TypeCheckDecl.cpp

Lines changed: 74 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3974,6 +3974,9 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
39743974

39753975
DeclVisitor<DeclChecker>::visit(decl);
39763976

3977+
if (IsFirstPass)
3978+
TC.checkUnsupportedProtocolType(decl);
3979+
39773980
if (auto VD = dyn_cast<ValueDecl>(decl)) {
39783981
checkRedeclaration(TC, VD);
39793982

@@ -3995,17 +3998,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
39953998
"`" + VD->getBaseName().userFacingName().str() + "`");
39963999
}
39974000
}
3998-
3999-
if (!IsFirstPass) {
4000-
TC.checkUnsupportedProtocolType(decl);
4001-
if (auto nominal = dyn_cast<NominalTypeDecl>(decl)) {
4002-
TC.checkDeclCircularity(nominal);
4003-
}
4004-
if (auto protocol = dyn_cast<ProtocolDecl>(decl)) {
4005-
if (protocol->isResilient())
4006-
TC.inferDefaultWitnesses(protocol);
4007-
}
4008-
}
40094001
}
40104002

40114003
//===--------------------------------------------------------------------===//
@@ -4143,66 +4135,59 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
41434135
}
41444136

41454137
void visitPatternBindingDecl(PatternBindingDecl *PBD) {
4146-
// Check all the pattern/init pairs in the PBD.
4147-
validatePatternBindingEntries(TC, PBD);
4138+
if (!IsFirstPass)
4139+
return;
41484140

41494141
if (PBD->isBeingValidated())
41504142
return;
41514143

4152-
// If the initializers in the PBD aren't checked yet, do so now.
4153-
if (!IsFirstPass) {
4154-
for (unsigned i = 0, e = PBD->getNumPatternEntries(); i != e; ++i) {
4155-
if (!PBD->isInitializerChecked(i) && PBD->getInit(i))
4156-
TC.typeCheckPatternBinding(PBD, i, /*skipApplyingSolution*/false);
4157-
}
4158-
}
4144+
// Check all the pattern/init pairs in the PBD.
4145+
validatePatternBindingEntries(TC, PBD);
41594146

41604147
TC.checkDeclAttributesEarly(PBD);
41614148

4162-
if (IsFirstPass) {
4163-
for (unsigned i = 0, e = PBD->getNumPatternEntries(); i != e; ++i) {
4164-
// Type check each VarDecl that this PatternBinding handles.
4165-
visitBoundVars(PBD->getPattern(i));
4149+
for (unsigned i = 0, e = PBD->getNumPatternEntries(); i != e; ++i) {
4150+
// Type check each VarDecl that this PatternBinding handles.
4151+
visitBoundVars(PBD->getPattern(i));
41664152

4167-
// If we have a type but no initializer, check whether the type is
4168-
// default-initializable. If so, do it.
4169-
if (PBD->getPattern(i)->hasType() &&
4170-
!PBD->getInit(i) &&
4171-
PBD->getPattern(i)->hasStorage() &&
4172-
!PBD->getPattern(i)->getType()->hasError()) {
4153+
// If we have a type but no initializer, check whether the type is
4154+
// default-initializable. If so, do it.
4155+
if (PBD->getPattern(i)->hasType() &&
4156+
!PBD->getInit(i) &&
4157+
PBD->getPattern(i)->hasStorage() &&
4158+
!PBD->getPattern(i)->getType()->hasError()) {
41734159

4174-
// If we have a type-adjusting attribute (like ownership), apply it now.
4175-
if (auto var = PBD->getSingleVar())
4176-
TC.checkTypeModifyingDeclAttributes(var);
4160+
// If we have a type-adjusting attribute (like ownership), apply it now.
4161+
if (auto var = PBD->getSingleVar())
4162+
TC.checkTypeModifyingDeclAttributes(var);
41774163

4178-
// Decide whether we should suppress default initialization.
4179-
//
4180-
// Note: Swift 4 had a bug where properties with a desugared optional
4181-
// type like Optional<Int> had a half-way behavior where sometimes
4182-
// they behave like they are default initialized, and sometimes not.
4183-
//
4184-
// In Swift 5 mode, use the right condition here, and only default
4185-
// initialize properties with a sugared Optional type.
4186-
//
4187-
// (The restriction to sugared types only comes because we don't have
4188-
// the iterative declaration checker yet; so in general, we cannot
4189-
// look at the type of a property at all, and can only look at the
4190-
// TypeRepr, because we haven't validated the property yet.)
4191-
if (TC.Context.isSwiftVersionAtLeast(5)) {
4192-
if (!PBD->isDefaultInitializable(i))
4193-
continue;
4194-
} else {
4195-
if (PBD->getPattern(i)->isNeverDefaultInitializable())
4196-
continue;
4197-
}
4164+
// Decide whether we should suppress default initialization.
4165+
//
4166+
// Note: Swift 4 had a bug where properties with a desugared optional
4167+
// type like Optional<Int> had a half-way behavior where sometimes
4168+
// they behave like they are default initialized, and sometimes not.
4169+
//
4170+
// In Swift 5 mode, use the right condition here, and only default
4171+
// initialize properties with a sugared Optional type.
4172+
//
4173+
// (The restriction to sugared types only comes because we don't have
4174+
// the iterative declaration checker yet; so in general, we cannot
4175+
// look at the type of a property at all, and can only look at the
4176+
// TypeRepr, because we haven't validated the property yet.)
4177+
if (TC.Context.isSwiftVersionAtLeast(5)) {
4178+
if (!PBD->isDefaultInitializable(i))
4179+
continue;
4180+
} else {
4181+
if (PBD->getPattern(i)->isNeverDefaultInitializable())
4182+
continue;
4183+
}
41984184

4199-
auto type = PBD->getPattern(i)->getType();
4200-
if (auto defaultInit = buildDefaultInitializer(TC, type)) {
4201-
// If we got a default initializer, install it and re-type-check it
4202-
// to make sure it is properly coerced to the pattern type.
4203-
PBD->setInit(i, defaultInit);
4204-
TC.typeCheckPatternBinding(PBD, i, /*skipApplyingSolution*/false);
4205-
}
4185+
auto type = PBD->getPattern(i)->getType();
4186+
if (auto defaultInit = buildDefaultInitializer(TC, type)) {
4187+
// If we got a default initializer, install it and re-type-check it
4188+
// to make sure it is properly coerced to the pattern type.
4189+
PBD->setInit(i, defaultInit);
4190+
TC.typeCheckPatternBinding(PBD, i, /*skipApplyingSolution*/false);
42064191
}
42074192
}
42084193
}
@@ -4269,9 +4254,13 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
42694254
}
42704255

42714256
TC.checkDeclAttributes(PBD);
4257+
checkAccessControl(TC, PBD);
42724258

4273-
if (IsFirstPass)
4274-
checkAccessControl(TC, PBD);
4259+
// If the initializers in the PBD aren't checked yet, do so now.
4260+
for (unsigned i = 0, e = PBD->getNumPatternEntries(); i != e; ++i) {
4261+
if (!PBD->isInitializerChecked(i) && PBD->getInit(i))
4262+
TC.typeCheckPatternBinding(PBD, i, /*skipApplyingSolution*/false);
4263+
}
42754264
}
42764265

42774266
void visitSubscriptDecl(SubscriptDecl *SD) {
@@ -4396,6 +4385,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
43964385
// enums haven't.
43974386
checkEnumRawValues(TC, ED);
43984387
}
4388+
4389+
TC.checkDeclCircularity(ED);
43994390
}
44004391

44014392
void visitStructDecl(StructDecl *SD) {
@@ -4422,6 +4413,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
44224413

44234414
TC.checkDeclAttributes(SD);
44244415
checkAccessControl(TC, SD);
4416+
4417+
TC.checkDeclCircularity(SD);
44254418
}
44264419

44274420
/// Check whether the given properties can be @NSManaged in this class.
@@ -4654,6 +4647,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
46544647

46554648
TC.checkDeclAttributes(CD);
46564649
checkAccessControl(TC, CD);
4650+
4651+
TC.checkDeclCircularity(CD);
46574652
}
46584653

46594654
void visitProtocolDecl(ProtocolDecl *PD) {
@@ -4710,6 +4705,10 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
47104705
GenericTypeToArchetypeResolver resolver(PD);
47114706
TC.validateWhereClauses(PD, &resolver);
47124707

4708+
TC.checkDeclCircularity(PD);
4709+
if (PD->isResilient())
4710+
TC.inferDefaultWitnesses(PD);
4711+
47134712
if (TC.Context.LangOpts.DebugGenericSignatures) {
47144713
auto requirementsSig =
47154714
GenericSignature::get({PD->getProtocolSelfType()},
@@ -7039,6 +7038,10 @@ void TypeChecker::validateDecl(ValueDecl *D) {
70397038
case DeclKind::Param: {
70407039
auto VD = cast<VarDecl>(D);
70417040

7041+
if (PatternBindingDecl *PBD = VD->getParentPatternBinding())
7042+
if (PBD->isBeingValidated())
7043+
return;
7044+
70427045
D->setIsBeingValidated();
70437046

70447047
if (!VD->hasInterfaceType()) {
@@ -7049,25 +7052,12 @@ void TypeChecker::validateDecl(ValueDecl *D) {
70497052
}
70507053
recordSelfContextType(cast<AbstractFunctionDecl>(VD->getDeclContext()));
70517054
} else if (PatternBindingDecl *PBD = VD->getParentPatternBinding()) {
7052-
if (PBD->isBeingValidated()) {
7053-
diagnose(VD, diag::pattern_used_in_type, VD->getName());
7054-
7055-
} else {
7056-
validatePatternBindingEntries(*this, PBD);
7057-
}
7055+
validatePatternBindingEntries(*this, PBD);
70587056

70597057
auto parentPattern = VD->getParentPattern();
70607058
if (PBD->isInvalid() || !parentPattern->hasType()) {
70617059
parentPattern->setType(ErrorType::get(Context));
70627060
setBoundVarsTypeError(parentPattern, Context);
7063-
7064-
// If no type has been set for the initializer, we need to diagnose
7065-
// the failure.
7066-
if (VD->getParentInitializer() &&
7067-
!VD->getParentInitializer()->getType()) {
7068-
diagnose(parentPattern->getLoc(), diag::identifier_init_failure,
7069-
parentPattern->getBoundName());
7070-
}
70717061
}
70727062
} else {
70737063
// FIXME: This case is hit when code completion occurs in a function
@@ -7175,6 +7165,14 @@ void TypeChecker::validateDecl(ValueDecl *D) {
71757165
auto *FD = cast<FuncDecl>(D);
71767166
assert(!FD->hasInterfaceType());
71777167

7168+
// Bail out if we're in a recursive validation situation.
7169+
if (auto accessor = dyn_cast<AccessorDecl>(FD)) {
7170+
auto *storage = accessor->getStorage();
7171+
validateDecl(storage);
7172+
if (!storage->hasValidSignature())
7173+
return;
7174+
}
7175+
71787176
checkDeclAttributesEarly(FD);
71797177
computeAccessLevel(FD);
71807178

@@ -7211,7 +7209,6 @@ void TypeChecker::validateDecl(ValueDecl *D) {
72117209
// FIXME: should this include the generic signature?
72127210
if (auto accessor = dyn_cast<AccessorDecl>(FD)) {
72137211
auto storage = accessor->getStorage();
7214-
validateDecl(storage);
72157212

72167213
// Note that it's important for correctness that we're filling in
72177214
// empty TypeLocs, because otherwise revertGenericFuncSignature might

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -539,28 +539,6 @@ static bool isLocInVarInit(TypeChecker &TC, VarDecl *var, SourceLoc loc) {
539539
return TC.Context.SourceMgr.rangeContainsTokenLoc(initRange, loc);
540540
}
541541

542-
namespace {
543-
class TypoCorrectionResolver : public DelegatingLazyResolver {
544-
TypeChecker &TC() { return static_cast<TypeChecker&>(Principal); }
545-
SourceLoc NameLoc;
546-
public:
547-
TypoCorrectionResolver(TypeChecker &TC, SourceLoc nameLoc)
548-
: DelegatingLazyResolver(TC), NameLoc(nameLoc) {}
549-
550-
void resolveDeclSignature(ValueDecl *VD) override {
551-
if (VD->isInvalid() || VD->hasInterfaceType()) return;
552-
553-
// Don't process a variable if we're within its initializer.
554-
if (auto var = dyn_cast<VarDecl>(VD)) {
555-
if (isLocInVarInit(TC(), var, NameLoc))
556-
return;
557-
}
558-
559-
DelegatingLazyResolver::resolveDeclSignature(VD);
560-
}
561-
};
562-
} // end anonymous namespace
563-
564542
void TypeChecker::performTypoCorrection(DeclContext *DC, DeclRefKind refKind,
565543
Type baseTypeOrNull,
566544
DeclName targetDeclName,
@@ -608,12 +586,11 @@ void TypeChecker::performTypoCorrection(DeclContext *DC, DeclRefKind refKind,
608586
entries.insert(distance, std::move(decl));
609587
});
610588

611-
TypoCorrectionResolver resolver(*this, nameLoc);
612589
if (baseTypeOrNull) {
613-
lookupVisibleMemberDecls(consumer, baseTypeOrNull, DC, &resolver,
590+
lookupVisibleMemberDecls(consumer, baseTypeOrNull, DC, this,
614591
/*include instance members*/ true, gsb);
615592
} else {
616-
lookupVisibleDecls(consumer, DC, &resolver, /*top level*/ true, nameLoc);
593+
lookupVisibleDecls(consumer, DC, this, /*top level*/ true, nameLoc);
617594
}
618595

619596
// Impose a maximum distance from the best score.

0 commit comments

Comments
 (0)