Skip to content

Commit d92374c

Browse files
committed
[ParseSIL] Return empty SILModule on error
Because we were previously performing SIL parsing during `performSema`, we were relying on the pipeline being stopped before reaching the SIL pipeline passes. However under a lazy evaluation model, we can't rely on that. Instead, just return an empty SILModule if we encounter a parsing error.
1 parent 11d8f70 commit d92374c

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

include/swift/Parse/Parser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,8 @@ class Parser {
888888
void parseTopLevel(SmallVectorImpl<Decl *> &decls);
889889

890890
/// Parse the top-level SIL decls into the SIL module.
891-
void parseTopLevelSIL();
891+
/// \returns \c true if there was a parsing error.
892+
bool parseTopLevelSIL();
892893

893894
/// Flags that control the parsing of declarations.
894895
enum ParseDeclFlags {

lib/Parse/ParseDecl.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ void Parser::parseTopLevel(SmallVectorImpl<Decl *> &decls) {
241241
TokReceiver->finalize();
242242
}
243243

244-
void Parser::parseTopLevelSIL() {
244+
bool Parser::parseTopLevelSIL() {
245245
assert(SIL && isInSILMode());
246246

247247
// Prime the lexer.
@@ -253,6 +253,7 @@ void Parser::parseTopLevelSIL() {
253253
skipSingle();
254254
};
255255

256+
auto hadError = false;
256257
while (!Tok.is(tok::eof)) {
257258
// If we run into a Swift decl, skip over until we find the next SIL decl.
258259
if (isStartOfSwiftDecl()) {
@@ -269,6 +270,7 @@ void Parser::parseTopLevelSIL() {
269270
if (SIL->parse##NAME(*this)) { \
270271
Lexer::SILBodyRAII sbr(*L); \
271272
skipToNextSILDecl(); \
273+
hadError = true; \
272274
} \
273275
break; \
274276
}
@@ -288,9 +290,11 @@ void Parser::parseTopLevelSIL() {
288290
// or a SIL decl. Emit an error and skip ahead to the next SIL decl.
289291
diagnose(Tok, diag::expected_sil_keyword);
290292
skipToNextSILDecl();
293+
hadError = true;
291294
break;
292295
}
293296
}
297+
return hadError;
294298
}
295299

296300
ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,14 @@ ParseSILModuleRequest::evaluate(Evaluator &evaluator,
121121
SILParserState parserState(silMod.get());
122122
Parser parser(*bufferID, *SF, parserState.Impl.get());
123123
PrettyStackTraceParser StackTrace(parser);
124-
parser.parseTopLevelSIL();
124+
125+
auto hadError = parser.parseTopLevelSIL();
126+
if (hadError) {
127+
// The rest of the SIL pipeline expects well-formed SIL, so if we encounter
128+
// a parsing error, just return an empty SIL module.
129+
return SILModule::createEmptyModule(mod, desc.conv, desc.opts,
130+
desc.isWholeModule());
131+
}
125132
return silMod;
126133
}
127134

0 commit comments

Comments
 (0)