@@ -3974,6 +3974,9 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
3974
3974
3975
3975
DeclVisitor<DeclChecker>::visit (decl);
3976
3976
3977
+ if (IsFirstPass)
3978
+ TC.checkUnsupportedProtocolType (decl);
3979
+
3977
3980
if (auto VD = dyn_cast<ValueDecl>(decl)) {
3978
3981
checkRedeclaration (TC, VD);
3979
3982
@@ -3995,17 +3998,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
3995
3998
" `" + VD->getBaseName ().userFacingName ().str () + " `" );
3996
3999
}
3997
4000
}
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
- }
4009
4001
}
4010
4002
4011
4003
// ===--------------------------------------------------------------------===//
@@ -4143,66 +4135,59 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
4143
4135
}
4144
4136
4145
4137
void visitPatternBindingDecl (PatternBindingDecl *PBD) {
4146
- // Check all the pattern/init pairs in the PBD.
4147
- validatePatternBindingEntries (TC, PBD) ;
4138
+ if (!IsFirstPass)
4139
+ return ;
4148
4140
4149
4141
if (PBD->isBeingValidated ())
4150
4142
return ;
4151
4143
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);
4159
4146
4160
4147
TC.checkDeclAttributesEarly (PBD);
4161
4148
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));
4166
4152
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 ()) {
4173
4159
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);
4177
4163
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
+ }
4198
4184
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 );
4206
4191
}
4207
4192
}
4208
4193
}
@@ -4269,9 +4254,13 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
4269
4254
}
4270
4255
4271
4256
TC.checkDeclAttributes (PBD);
4257
+ checkAccessControl (TC, PBD);
4272
4258
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
+ }
4275
4264
}
4276
4265
4277
4266
void visitSubscriptDecl (SubscriptDecl *SD) {
@@ -4396,6 +4385,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
4396
4385
// enums haven't.
4397
4386
checkEnumRawValues (TC, ED);
4398
4387
}
4388
+
4389
+ TC.checkDeclCircularity (ED);
4399
4390
}
4400
4391
4401
4392
void visitStructDecl (StructDecl *SD) {
@@ -4422,6 +4413,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
4422
4413
4423
4414
TC.checkDeclAttributes (SD);
4424
4415
checkAccessControl (TC, SD);
4416
+
4417
+ TC.checkDeclCircularity (SD);
4425
4418
}
4426
4419
4427
4420
// / Check whether the given properties can be @NSManaged in this class.
@@ -4654,6 +4647,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
4654
4647
4655
4648
TC.checkDeclAttributes (CD);
4656
4649
checkAccessControl (TC, CD);
4650
+
4651
+ TC.checkDeclCircularity (CD);
4657
4652
}
4658
4653
4659
4654
void visitProtocolDecl (ProtocolDecl *PD) {
@@ -4710,6 +4705,10 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
4710
4705
GenericTypeToArchetypeResolver resolver (PD);
4711
4706
TC.validateWhereClauses (PD, &resolver);
4712
4707
4708
+ TC.checkDeclCircularity (PD);
4709
+ if (PD->isResilient ())
4710
+ TC.inferDefaultWitnesses (PD);
4711
+
4713
4712
if (TC.Context .LangOpts .DebugGenericSignatures ) {
4714
4713
auto requirementsSig =
4715
4714
GenericSignature::get ({PD->getProtocolSelfType ()},
@@ -7039,6 +7038,10 @@ void TypeChecker::validateDecl(ValueDecl *D) {
7039
7038
case DeclKind::Param: {
7040
7039
auto VD = cast<VarDecl>(D);
7041
7040
7041
+ if (PatternBindingDecl *PBD = VD->getParentPatternBinding ())
7042
+ if (PBD->isBeingValidated ())
7043
+ return ;
7044
+
7042
7045
D->setIsBeingValidated ();
7043
7046
7044
7047
if (!VD->hasInterfaceType ()) {
@@ -7049,25 +7052,12 @@ void TypeChecker::validateDecl(ValueDecl *D) {
7049
7052
}
7050
7053
recordSelfContextType (cast<AbstractFunctionDecl>(VD->getDeclContext ()));
7051
7054
} 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);
7058
7056
7059
7057
auto parentPattern = VD->getParentPattern ();
7060
7058
if (PBD->isInvalid () || !parentPattern->hasType ()) {
7061
7059
parentPattern->setType (ErrorType::get (Context));
7062
7060
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
- }
7071
7061
}
7072
7062
} else {
7073
7063
// FIXME: This case is hit when code completion occurs in a function
@@ -7175,6 +7165,14 @@ void TypeChecker::validateDecl(ValueDecl *D) {
7175
7165
auto *FD = cast<FuncDecl>(D);
7176
7166
assert (!FD->hasInterfaceType ());
7177
7167
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
+
7178
7176
checkDeclAttributesEarly (FD);
7179
7177
computeAccessLevel (FD);
7180
7178
@@ -7211,7 +7209,6 @@ void TypeChecker::validateDecl(ValueDecl *D) {
7211
7209
// FIXME: should this include the generic signature?
7212
7210
if (auto accessor = dyn_cast<AccessorDecl>(FD)) {
7213
7211
auto storage = accessor->getStorage ();
7214
- validateDecl (storage);
7215
7212
7216
7213
// Note that it's important for correctness that we're filling in
7217
7214
// empty TypeLocs, because otherwise revertGenericFuncSignature might
0 commit comments