Skip to content

Commit 22f02d9

Browse files
CodaFiEMatejska
authored andcommitted
Restore local type declarations list
1 parent cc70b4b commit 22f02d9

File tree

14 files changed

+61
-79
lines changed

14 files changed

+61
-79
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,8 @@ class alignas(1 << DeclAlignInBits) Decl {
584584

585585
/// Whether this decl is missing its closing '#endif'.
586586
unsigned HadMissingEnd : 1;
587-
588-
/// Whether this condition has been resolved either statically by Parse or
589-
/// later by Condition Resolution.
590-
unsigned HasBeenResolved : 1;
591587
};
592-
enum { NumIfConfigDeclBits = NumDeclBits + 2 };
588+
enum { NumIfConfigDeclBits = NumDeclBits + 1 };
593589
static_assert(NumIfConfigDeclBits <= 32, "fits in an unsigned");
594590

595591
protected:
@@ -1932,8 +1928,7 @@ struct IfConfigDeclClause {
19321928

19331929
IfConfigDeclClause(SourceLoc Loc, Expr *Cond, ArrayRef<Decl*> Members,
19341930
bool isActive)
1935-
: Loc(Loc), Cond(Cond), Members(Members), isActive(isActive) {
1936-
}
1931+
: Loc(Loc), Cond(Cond), Members(Members), isActive(isActive) {}
19371932
};
19381933

19391934

include/swift/AST/DiagnosticsParse.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ ERROR(unsupported_conditional_compilation_unary_expression,none,
13391339
"expected unary '!' expression", ())
13401340
ERROR(unsupported_platform_condition_expression,none,
13411341
"unexpected platform condition "
1342-
"(expected 'canImport', 'os', 'arch', or 'swift')",
1342+
"(expected 'os', 'arch', or 'swift')",
13431343
())
13441344
ERROR(platform_condition_expected_one_argument,none,
13451345
"expected only one argument to platform condition",

include/swift/Parse/Parser.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,10 @@ namespace swift {
5959
TopLevelCode,
6060
/// The top-level of a file, when in parse-as-library mode.
6161
TopLevelLibrary,
62-
/// The body of the clause of an #if/#else/#endif block
63-
ConditionalBlock,
64-
/// The body of the clause of an #if/#else/#endif block that was statically
65-
/// determined to be inactive.
66-
StaticallyInactiveConditionalBlock,
62+
/// The body of the inactive clause of an #if/#else/#endif block
63+
InactiveConditionalBlock,
64+
/// The body of the active clause of an #if/#else/#endif block
65+
ActiveConditionalBlock,
6766
};
6867

6968

@@ -620,10 +619,6 @@ class Parser {
620619
BraceItemListKind::Brace);
621620
ParserResult<BraceStmt> parseBraceItemList(Diag<> ID);
622621

623-
void parseIfConfigClauseElements(bool isInactive,
624-
BraceItemListKind Kind,
625-
SmallVectorImpl<ASTNode> &Elements);
626-
627622
void parseTopLevelCodeDeclDelayed();
628623

629624
//===--------------------------------------------------------------------===//

include/swift/Parse/Scope.h

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ScopeInfo {
5454
/// scope.
5555
void addToScope(ValueDecl *D, Parser &TheParser);
5656

57-
bool isStaticallyInactiveConfigBlock() const;
57+
bool isInactiveConfigBlock() const;
5858

5959
SavedScope saveCurrentScope();
6060
};
@@ -91,7 +91,7 @@ class SavedScope {
9191
ScopeInfo::ScopedHTDetachedScopeTy HTDetachedScope;
9292
unsigned Depth;
9393
ScopeKind Kind;
94-
bool IsStaticallyInactiveConfigBlock;
94+
bool IsInactiveConfigBlock;
9595

9696
SavedScope() = delete;
9797
SavedScope(const SavedScope &) = delete;
@@ -103,9 +103,9 @@ class SavedScope {
103103
~SavedScope() = default;
104104

105105
SavedScope(ScopeInfo::ScopedHTDetachedScopeTy &&HTDetachedScope,
106-
unsigned Depth, ScopeKind Kind, bool IsStaticallyInactiveConfigBlock)
106+
unsigned Depth, ScopeKind Kind, bool isInactiveConfigBlock)
107107
: HTDetachedScope(std::move(HTDetachedScope)), Depth(Depth), Kind(Kind),
108-
IsStaticallyInactiveConfigBlock(IsStaticallyInactiveConfigBlock) {}
108+
IsInactiveConfigBlock(isInactiveConfigBlock) {}
109109
};
110110

111111
/// Scope - This class represents lexical scopes. These objects are created
@@ -125,13 +125,12 @@ class Scope {
125125
unsigned PrevResolvableDepth;
126126
unsigned Depth;
127127
ScopeKind Kind;
128-
bool IsStaticallyInactiveConfigBlock;
128+
bool IsInactiveConfigBlock;
129129

130130
/// \brief Save this scope so that it can be re-entered later. Transfers the
131131
/// ownership of the scope frame to returned object.
132132
SavedScope saveScope() {
133-
return SavedScope(HTScope.detach(), Depth, Kind,
134-
IsStaticallyInactiveConfigBlock);
133+
return SavedScope(HTScope.detach(), Depth, Kind, IsInactiveConfigBlock);
135134
}
136135

137136
unsigned getDepth() const {
@@ -142,7 +141,7 @@ class Scope {
142141

143142
public:
144143
/// \brief Create a lexical scope of the specified kind.
145-
Scope(Parser *P, ScopeKind SC, bool IsStaticallyInactiveConfigBlock = false);
144+
Scope(Parser *P, ScopeKind SC, bool isInactiveConfigBlock = false);
146145

147146
/// \brief Re-enter the specified scope, transferring the ownership of the
148147
/// scope frame to the new object.
@@ -175,15 +174,10 @@ inline ValueDecl *ScopeInfo::lookupValueName(DeclName Name) {
175174
return Res.second;
176175
}
177176

178-
inline bool ScopeInfo::isStaticallyInactiveConfigBlock() const {
179-
auto scope = CurScope;
180-
while (scope) {
181-
if (scope->IsStaticallyInactiveConfigBlock) {
182-
return true;
183-
}
184-
scope = scope->PrevScope;
185-
}
186-
return false;
177+
inline bool ScopeInfo::isInactiveConfigBlock() const {
178+
if (!CurScope)
179+
return false;
180+
return CurScope->IsInactiveConfigBlock;
187181
}
188182

189183
inline SavedScope ScopeInfo::saveCurrentScope() {

lib/Parse/ParseDecl.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,8 +1850,9 @@ void Parser::setLocalDiscriminator(ValueDecl *D) {
18501850
if (!CurLocalContext || !D->getDeclContext()->isLocalContext())
18511851
return;
18521852

1853-
if (getScopeInfo().isStaticallyInactiveConfigBlock())
1854-
return;
1853+
if (auto TD = dyn_cast<TypeDecl>(D))
1854+
if (!getScopeInfo().isInactiveConfigBlock())
1855+
SF.LocalTypeDecls.insert(TD);
18551856

18561857
Identifier name = D->getName();
18571858
unsigned discriminator = CurLocalContext->claimNextNamedDiscriminator(name);
@@ -2217,6 +2218,15 @@ ParserStatus Parser::parseDecl(ParseDeclOptions Flags,
22172218
break;
22182219
}
22192220

2221+
if (auto SF = CurDeclContext->getParentSourceFile()) {
2222+
if (!getScopeInfo().isInactiveConfigBlock()) {
2223+
for (auto Attr : Attributes) {
2224+
if (isa<ObjCAttr>(Attr) || isa<DynamicAttr>(Attr))
2225+
SF->AttrsRequiringFoundation.insert(Attr);
2226+
}
2227+
}
2228+
}
2229+
22202230
if (FoundCCTokenInAttr) {
22212231
if (CodeCompletion) {
22222232
CodeCompletion->completeDeclAttrKeyword(DeclResult.getPtrOrNull(),

lib/Parse/ParseStmt.cpp

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ static bool isTerminatorForBraceItemListKind(const Token &Tok,
157157
return false;
158158
case BraceItemListKind::TopLevelLibrary:
159159
return false;
160-
case BraceItemListKind::ConditionalBlock:
161-
case BraceItemListKind::StaticallyInactiveConditionalBlock:
160+
case BraceItemListKind::ActiveConditionalBlock:
161+
case BraceItemListKind::InactiveConditionalBlock:
162162
return Tok.isNot(tok::pound_else) && Tok.isNot(tok::pound_endif) &&
163163
Tok.isNot(tok::pound_elseif);
164164
}
@@ -235,17 +235,18 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
235235

236236
bool IsTopLevel = (Kind == BraceItemListKind::TopLevelCode) ||
237237
(Kind == BraceItemListKind::TopLevelLibrary);
238-
bool isConditionalBlock
239-
= (ConditionalBlockKind == BraceItemListKind::ConditionalBlock) ||
240-
(ConditionalBlockKind == BraceItemListKind::StaticallyInactiveConditionalBlock);
238+
bool isActiveConditionalBlock =
239+
ConditionalBlockKind == BraceItemListKind::ActiveConditionalBlock;
240+
bool isConditionalBlock = isActiveConditionalBlock ||
241+
ConditionalBlockKind == BraceItemListKind::InactiveConditionalBlock;
241242

242-
// If we're not parsing an inactive #if block, form a new lexical scope.
243+
// If we're not parsing an active #if block, form a new lexical scope.
243244
Optional<Scope> initScope;
244-
bool isInactiveConditionalBlock =
245-
ConditionalBlockKind == BraceItemListKind::StaticallyInactiveConditionalBlock;
246-
if (!isInactiveConditionalBlock) {
245+
if (!isActiveConditionalBlock) {
247246
auto scopeKind = IsTopLevel ? ScopeKind::TopLevel : ScopeKind::Brace;
248-
initScope.emplace(this, scopeKind, isInactiveConditionalBlock);
247+
initScope.emplace(this, scopeKind,
248+
ConditionalBlockKind ==
249+
BraceItemListKind::InactiveConditionalBlock);
249250
}
250251

251252
ParserStatus BraceItemsStatus;
@@ -608,16 +609,6 @@ ParserResult<BraceStmt> Parser::parseBraceItemList(Diag<> ID) {
608609
BraceStmt::create(Context, LBLoc, Entries, RBLoc));
609610
}
610611

611-
/// \brief Parses the elements in active or inactive if config clauses.
612-
void Parser::parseIfConfigClauseElements(bool isInactive,
613-
BraceItemListKind Kind,
614-
SmallVectorImpl<ASTNode> &Elements) {
615-
parseBraceItems(Elements, Kind,
616-
isInactive
617-
? BraceItemListKind::StaticallyInactiveConditionalBlock
618-
: BraceItemListKind::ConditionalBlock);
619-
}
620-
621612
/// parseStmtBreak
622613
///
623614
/// stmt-break:
@@ -1641,8 +1632,7 @@ Parser::classifyConditionalCompilationExpr(Expr *condition,
16411632
!fnName.equals("_endian") &&
16421633
!fnName.equals("_runtime") &&
16431634
!fnName.equals("swift") &&
1644-
!fnName.equals("_compiler_version") &&
1645-
!fnName.equals("canImport")) {
1635+
!fnName.equals("_compiler_version")) {
16461636
D.diagnose(CE->getLoc(), diag::unsupported_platform_condition_expression);
16471637
return ConditionalCompilationExprState::error();
16481638
}
@@ -1736,11 +1726,6 @@ Parser::classifyConditionalCompilationExpr(Expr *condition,
17361726
D.diagnose(UDRE->getLoc(), diag::unknown_platform_condition_argument,
17371727
"endianness", fnName);
17381728
}
1739-
} else if (fnName == "canImport") {
1740-
bool result =
1741-
Context.canImportModule({ argumentIdent, UDRE->getLoc() });
1742-
return {result,
1743-
ConditionalCompilationExprKind::Import};
17441729
}
17451730

17461731
// FIXME: Perform the replacement macOS -> OSX elsewhere.
@@ -1819,10 +1804,12 @@ ParserResult<Stmt> Parser::parseStmtIfConfig(BraceItemListKind Kind) {
18191804
}
18201805

18211806
SmallVector<ASTNode, 16> Elements;
1822-
if (ConfigState.shouldParse())
1823-
parseIfConfigClauseElements(ConfigState.isConditionActive(), Kind,
1824-
Elements);
1825-
else {
1807+
if (ConfigState.shouldParse()) {
1808+
parseBraceItems(Elements, Kind,
1809+
ConfigState.isConditionActive()
1810+
? BraceItemListKind::ActiveConditionalBlock
1811+
: BraceItemListKind::InactiveConditionalBlock);
1812+
} else {
18261813
DiagnosticTransaction DT(Diags);
18271814
skipUntilConditionalBlockClose();
18281815
DT.abort();

lib/Parse/Scope.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ static bool isResolvableScope(ScopeKind SK) {
5252
llvm_unreachable("Unhandled ScopeKind in switch.");
5353
}
5454

55-
Scope::Scope(Parser *P, ScopeKind SC, bool IsStaticallyInactiveConfigBlock)
55+
Scope::Scope(Parser *P, ScopeKind SC, bool isInactiveConfigBlock)
5656
: SI(P->getScopeInfo()),
5757
HTScope(SI.HT, SI.CurScope ? &SI.CurScope->HTScope : nullptr),
5858
PrevScope(SI.CurScope),
5959
PrevResolvableDepth(SI.ResolvableDepth),
6060
Kind(SC),
61-
IsStaticallyInactiveConfigBlock(IsStaticallyInactiveConfigBlock) {
61+
IsInactiveConfigBlock(isInactiveConfigBlock) {
6262
assert(PrevScope || Kind == ScopeKind::TopLevel);
6363

6464
if (SI.CurScope) {
6565
Depth = SI.CurScope->Depth + 1;
66-
IsStaticallyInactiveConfigBlock |= SI.CurScope->IsStaticallyInactiveConfigBlock;
66+
IsInactiveConfigBlock |= SI.CurScope->IsInactiveConfigBlock;
6767
} else {
6868
Depth = 0;
6969
}
@@ -79,7 +79,7 @@ Scope::Scope(Parser *P, SavedScope &&SS):
7979
PrevResolvableDepth(SI.ResolvableDepth),
8080
Depth(SS.Depth),
8181
Kind(SS.Kind),
82-
IsStaticallyInactiveConfigBlock(SS.IsStaticallyInactiveConfigBlock) {
82+
IsInactiveConfigBlock(SS.IsInactiveConfigBlock) {
8383

8484
SI.CurScope = this;
8585
if (!isResolvableScope(Kind))

test/ClangImporter/MixedSource/can_import_objc_idempotent.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) -I %S/../Inputs/custom-modules -typecheck %s -verify
1414

1515
// REQUIRES: objc_interop
16+
// REQUIRES: can_import
1617

1718
// Test that 'canImport(Foo)' directives do not open symbols from 'Foo' into the
1819
// current module. Only an 'import Foo' statement should do this.

test/ClangImporter/can_import_missing_requirement.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -I %S/Inputs/missing-requirement %s -verify
22

33
// REQUIRES: objc_interop
4+
// REQUIRES: can_import
45

56
class Unique {}
67

test/Parse/ConditionalCompilation/basicParseErrors.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var x = 0
88
var y = 0
99
#endif
1010

11-
#if foo(BAR) // expected-error {{unexpected platform condition (expected 'canImport', 'os', 'arch', or 'swift')}}
11+
#if foo(BAR) // expected-error {{unexpected platform condition (expected 'os', 'arch', or 'swift')}}
1212
var z = 0
1313
#endif
1414

@@ -28,7 +28,7 @@ func h() {}
2828
#endif /* bbb */
2929

3030
#if foo.bar()
31-
.baz() // expected-error {{unexpected platform condition (expected 'canImport', 'os', 'arch', or 'swift')}}
31+
.baz() // expected-error {{unexpected platform condition (expected 'os', 'arch', or 'swift')}}
3232

3333
#endif
3434

@@ -45,11 +45,6 @@ struct S {
4545
#endif
4646
}
4747

48-
#if canImport(01101101_01101111_01101111_01100100) // expected-error {{unexpected platform condition argument: expected identifier}}
49-
#elseif canImport(Foundation) ** FOO // expected-error{{expected '&&' or '||' expression}}
50-
#else
51-
#endif
52-
5348
#if FOO
5449
#else
5550
#else // expected-error {{further conditions after #else are unreachable}}

test/Parse/ConditionalCompilation/can_import.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// RUN: %target-typecheck-verify-swift -parse-as-library
22
// RUN: %target-typecheck-verify-swift -D WITH_PERFORM -primary-file %s %S/Inputs/can_import_nonprimary_file.swift
33

4+
// REQUIRES: can_import
5+
46
public struct LibraryDependentBool : ExpressibleByBooleanLiteral {
57
#if canImport(Swift)
68
var _description: String

test/Parse/ConditionalCompilation/can_import_idempotent.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// RUN: echo "public var dummyVar = Int()" | %target-swift-frontend -module-name DummyModule -emit-module -o %t -
55
// RUN: %target-swift-frontend -typecheck -verify -I %t %s
66

7+
// REQUIRES: can_import
8+
79
#if canImport(DummyModule)
810
print(DummyModule.dummyVar) // expected-error {{use of unresolved identifier 'DummyModule'}}
911
print(dummyVar) // expected-error {{use of unresolved identifier 'dummyVar'}}

test/Parse/ConditionalCompilation/can_import_sdk.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify %s
22

33
// REQUIRES: objc_interop
4+
// REQUIRES: can_import
45

56
class Unique {}
67

validation-test/compiler_crashers_fixed/28611-isa-x-val-cast-ty-argument-of-incompatible-type.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

88
// RUN: not %target-swift-frontend %s -emit-ir
9-
// REQUIRES: asserts
109
#if0=0

0 commit comments

Comments
 (0)