Skip to content

Commit 8a31911

Browse files
committed
Sema: DeclChecker::visitPatternBindingDecl() does everything in the 'first pass'
1 parent 03dce36 commit 8a31911

File tree

2 files changed

+49
-52
lines changed

2 files changed

+49
-52
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4143,66 +4143,59 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
41434143
}
41444144

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

41494149
if (PBD->isBeingValidated())
41504150
return;
41514151

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-
}
4152+
// Check all the pattern/init pairs in the PBD.
4153+
validatePatternBindingEntries(TC, PBD);
41594154

41604155
TC.checkDeclAttributesEarly(PBD);
41614156

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));
4157+
for (unsigned i = 0, e = PBD->getNumPatternEntries(); i != e; ++i) {
4158+
// Type check each VarDecl that this PatternBinding handles.
4159+
visitBoundVars(PBD->getPattern(i));
41664160

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()) {
4161+
// If we have a type but no initializer, check whether the type is
4162+
// default-initializable. If so, do it.
4163+
if (PBD->getPattern(i)->hasType() &&
4164+
!PBD->getInit(i) &&
4165+
PBD->getPattern(i)->hasStorage() &&
4166+
!PBD->getPattern(i)->getType()->hasError()) {
41734167

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

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

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-
}
4193+
auto type = PBD->getPattern(i)->getType();
4194+
if (auto defaultInit = buildDefaultInitializer(TC, type)) {
4195+
// If we got a default initializer, install it and re-type-check it
4196+
// to make sure it is properly coerced to the pattern type.
4197+
PBD->setInit(i, defaultInit);
4198+
TC.typeCheckPatternBinding(PBD, i, /*skipApplyingSolution*/false);
42064199
}
42074200
}
42084201
}
@@ -4269,9 +4262,13 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
42694262
}
42704263

42714264
TC.checkDeclAttributes(PBD);
4265+
checkAccessControl(TC, PBD);
42724266

4273-
if (IsFirstPass)
4274-
checkAccessControl(TC, PBD);
4267+
// If the initializers in the PBD aren't checked yet, do so now.
4268+
for (unsigned i = 0, e = PBD->getNumPatternEntries(); i != e; ++i) {
4269+
if (!PBD->isInitializerChecked(i) && PBD->getInit(i))
4270+
TC.typeCheckPatternBinding(PBD, i, /*skipApplyingSolution*/false);
4271+
}
42754272
}
42764273

42774274
void visitSubscriptDecl(SubscriptDecl *SD) {

test/Sema/enum_raw_representable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Outer {
7777
// scenario too.
7878
let a: Int = E.a // expected-error {{cannot convert value of type 'Outer.E' to specified type 'Int'}}
7979

80-
enum E : Array<Int> { // expected-error {{'Outer.E' declares raw type 'Array<Int>', but does not conform to RawRepresentable and conformance could not be synthesized}} // expected-error {{raw type 'Array<Int>' is not expressible by any literal}}
80+
enum E : Array<Int> { // expected-error {{raw type 'Array<Int>' is not expressible by any literal}}
8181
case a
8282
}
8383
}

0 commit comments

Comments
 (0)