Skip to content

Commit b399b92

Browse files
committed
[Parser] Remove the notion of a "local context".
The "local context" was only used to prevent parsing of closures in a non-local context, and also string interpolations because they are similar-ish to closures. However, this isn't something a parser should decide, so remove this special-case semantic check from the parser and eliminate the notion of "local context" entirely.
1 parent b9aee05 commit b399b92

File tree

11 files changed

+11
-105
lines changed

11 files changed

+11
-105
lines changed

include/swift/Parse/LocalContext.h

Lines changed: 0 additions & 42 deletions
This file was deleted.

include/swift/Parse/Parser.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "swift/AST/Stmt.h"
2828
#include "swift/Basic/OptionSet.h"
2929
#include "swift/Parse/Lexer.h"
30-
#include "swift/Parse/LocalContext.h"
3130
#include "swift/Parse/PersistentParserState.h"
3231
#include "swift/Parse/Token.h"
3332
#include "swift/Parse/ParserPosition.h"
@@ -176,8 +175,6 @@ class Parser {
176175
bool InInactiveClauseEnvironment = false;
177176
bool InSwiftKeyPath = false;
178177

179-
LocalContext *CurLocalContext = nullptr;
180-
181178
/// Whether we should delay parsing nominal type, extension, and function
182179
/// bodies.
183180
bool isDelayedParsingEnabled() const;
@@ -241,18 +238,15 @@ class Parser {
241238
protected:
242239
Parser &P;
243240
DeclContext *OldContext; // null signals that this has been popped
244-
LocalContext *OldLocal;
245241

246242
ContextChange(const ContextChange &) = delete;
247243
ContextChange &operator=(const ContextChange &) = delete;
248244

249245
public:
250-
ContextChange(Parser &P, DeclContext *DC,
251-
LocalContext *newLocal = nullptr)
252-
: P(P), OldContext(P.CurDeclContext), OldLocal(P.CurLocalContext) {
246+
ContextChange(Parser &P, DeclContext *DC)
247+
: P(P), OldContext(P.CurDeclContext) {
253248
assert(DC && "pushing null context?");
254249
P.CurDeclContext = DC;
255-
P.CurLocalContext = newLocal;
256250
}
257251

258252
/// Prematurely pop the DeclContext installed by the constructor.
@@ -270,16 +264,15 @@ class Parser {
270264
private:
271265
void popImpl() {
272266
P.CurDeclContext = OldContext;
273-
P.CurLocalContext = OldLocal;
274267
}
275268
};
276269

277270
/// A RAII object for parsing a new local context.
278-
class ParseFunctionBody : public LocalContext {
271+
class ParseFunctionBody {
279272
private:
280273
ContextChange CC;
281274
public:
282-
ParseFunctionBody(Parser &P, DeclContext *DC) : CC(P, DC, this) {
275+
ParseFunctionBody(Parser &P, DeclContext *DC) : CC(P, DC) {
283276
assert(!isa<TopLevelCodeDecl>(DC) &&
284277
"top-level code should be parsed using TopLevelCodeContext!");
285278
}

include/swift/Parse/PersistentParserState.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#define SWIFT_PARSE_PERSISTENTPARSERSTATE_H
1919

2020
#include "swift/Basic/SourceLoc.h"
21-
#include "swift/Parse/LocalContext.h"
2221

2322
namespace swift {
2423

@@ -54,9 +53,6 @@ class IDEInspectionDelayedDeclState {
5453
class PersistentParserState {
5554
std::unique_ptr<IDEInspectionDelayedDeclState> IDEInspectionDelayedDeclStat;
5655

57-
/// The local context for all top-level code.
58-
TopLevelContext TopLevelCode;
59-
6056
public:
6157
PersistentParserState();
6258
PersistentParserState(ASTContext &ctx) : PersistentParserState() { }
@@ -88,10 +84,6 @@ class PersistentParserState {
8884
assert(hasIDEInspectionDelayedDeclState());
8985
return std::move(IDEInspectionDelayedDeclStat);
9086
}
91-
92-
TopLevelContext &getTopLevelContext() {
93-
return TopLevelCode;
94-
}
9587
};
9688

9789
} // end namespace swift

lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4614,7 +4614,7 @@ void Parser::consumeDecl(ParserPosition BeginParserPosition,
46144614

46154615
void Parser::recordLocalType(TypeDecl *TD) {
46164616
// If we're not in a local context, this is unnecessary.
4617-
if (!CurLocalContext || !TD->getDeclContext()->isLocalContext())
4617+
if (!TD->getDeclContext()->isLocalContext())
46184618
return;
46194619

46204620
if (!InInactiveClauseEnvironment)
@@ -7117,8 +7117,7 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
71177117
Optional<ParseFunctionBody> initParser;
71187118
Optional<ContextChange> topLevelParser;
71197119
if (topLevelDecl)
7120-
topLevelParser.emplace(*this, topLevelDecl,
7121-
&State->getTopLevelContext());
7120+
topLevelParser.emplace(*this, topLevelDecl);
71227121
if (initContext)
71237122
initParser.emplace(*this, initContext);
71247123

@@ -7789,16 +7788,7 @@ Parser::parseDeclEnumCase(ParseDeclOptions Flags,
77897788
{
77907789
IDEInspectionCallbacks::InEnumElementRawValueRAII
77917790
InEnumElementRawValue(IDECallbacks);
7792-
if (!CurLocalContext) {
7793-
// A local context is needed for parsing closures. We want to parse
7794-
// them anyways for proper diagnosis.
7795-
LocalContext tempContext{};
7796-
CurLocalContext = &tempContext;
7797-
RawValueExpr = parseExpr(diag::expected_expr_enum_case_raw_value);
7798-
CurLocalContext = nullptr;
7799-
} else {
7800-
RawValueExpr = parseExpr(diag::expected_expr_enum_case_raw_value);
7801-
}
7791+
RawValueExpr = parseExpr(diag::expected_expr_enum_case_raw_value);
78027792
}
78037793
if (RawValueExpr.hasCodeCompletion()) {
78047794
Status.setHasCodeCompletionAndIsError();

lib/Parse/ParseExpr.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,15 +2012,6 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
20122012
// whole InterpolatedStringLiteral.
20132013
llvm::SaveAndRestore<SourceLoc> SavedPreviousLoc(PreviousLoc);
20142014

2015-
// We're not in a place where an interpolation would be valid.
2016-
if (!CurLocalContext) {
2017-
// Return an error, but include an empty InterpolatedStringLiteralExpr
2018-
// so that parseDeclPoundDiagnostic() can figure out why this string
2019-
// literal was bad.
2020-
return makeParserErrorResult(new (Context) InterpolatedStringLiteralExpr(
2021-
Loc, Loc.getAdvancedLoc(CloseQuoteBegin), 0, 0, nullptr));
2022-
}
2023-
20242015
unsigned LiteralCapacity = 0;
20252016
unsigned InterpolationCount = 0;
20262017
TapExpr * AppendingExpr;
@@ -2778,16 +2769,6 @@ ParserResult<Expr> Parser::parseExprClosure() {
27782769
attributes, bracketRange, captureList, capturedSelfDecl, params, asyncLoc,
27792770
throwsLoc, arrowLoc, explicitResultType, inLoc);
27802771

2781-
// If the closure was created in the context of an array type signature's
2782-
// size expression, there will not be a local context. A parse error will
2783-
// be reported at the signature's declaration site.
2784-
if (!CurLocalContext) {
2785-
skipUntil(tok::r_brace);
2786-
if (Tok.is(tok::r_brace))
2787-
consumeToken();
2788-
return makeParserError();
2789-
}
2790-
27912772
// Create the closure expression and enter its context.
27922773
auto *closure = new (Context) ClosureExpr(
27932774
attributes, bracketRange, capturedSelfDecl, params, asyncLoc, throwsLoc,

lib/Parse/ParsePattern.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,13 +1034,6 @@ ParserResult<Pattern> Parser::parseTypedPattern() {
10341034
!SourceMgr.hasIDEInspectionTargetBuffer()) {
10351035
CancellableBacktrackingScope backtrack(*this);
10361036

1037-
// Create a local context if needed so we can parse trailing closures.
1038-
LocalContext dummyContext;
1039-
Optional<ContextChange> contextChange;
1040-
if (!CurLocalContext) {
1041-
contextChange.emplace(*this, CurDeclContext, &dummyContext);
1042-
}
1043-
10441037
SmallVector<ExprListElt, 2> elts;
10451038
auto argListResult = parseArgumentList(tok::l_paren, tok::r_paren,
10461039
/*isExprBasic*/ false);

lib/Parse/ParseStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
396396
// If this is a statement or expression at the top level of the module,
397397
// Parse it as a child of a TopLevelCodeDecl.
398398
auto *TLCD = new (Context) TopLevelCodeDecl(CurDeclContext);
399-
ContextChange CC(*this, TLCD, &State->getTopLevelContext());
399+
ContextChange CC(*this, TLCD);
400400
SourceLoc StartLoc = Tok.getLoc();
401401

402402
// Expressions can't begin with a closure literal at statement position.

lib/Parse/Parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void Parser::performIDEInspectionSecondPassImpl(
159159
// Re-enter the top-level code decl context.
160160
// FIXME: this can issue discriminators out-of-order?
161161
auto *TLCD = cast<TopLevelCodeDecl>(DC);
162-
ContextChange CC(*this, TLCD, &State->getTopLevelContext());
162+
ContextChange CC(*this, TLCD);
163163

164164
SourceLoc StartLoc = Tok.getLoc();
165165
ASTNode Result;

lib/Sema/TypeCheckMacros.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,7 @@ Expr *swift::expandMacroExpr(
243243

244244
// Set up a "local context" for parsing, so that we have a source of
245245
// closure and local-variable discriminators.
246-
LocalContext tempContext{};
247246
parser.CurDeclContext = dc;
248-
parser.CurLocalContext = &tempContext;
249247

250248
auto parsedResult = parser.parseExpr(diag::expected_macro_expansion_expr);
251249
if (parsedResult.isParseError() || parsedResult.isNull()) {

lib/Sema/TypeCheckStmt.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#include "swift/Basic/Statistic.h"
4141
#include "swift/Basic/TopCollection.h"
4242
#include "swift/Parse/Lexer.h"
43-
#include "swift/Parse/LocalContext.h"
4443
#include "swift/Parse/Parser.h"
4544
#include "swift/Sema/IDETypeChecking.h"
4645
#include "llvm/ADT/DenseMap.h"

0 commit comments

Comments
 (0)