Skip to content

Commit 33589de

Browse files
committed
Experimentally emit diagnostics from the new Swift parser
Introduce the experimental feature `ParserDiagnostics`, which emits diagnostics from the new Swift parser *first* for a source file. If that produces any errors, we suppress any diagnostics emitted from the C++ parser.
1 parent 1a3c6d7 commit 33589de

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

include/swift/Basic/Features.def

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ EXPERIMENTAL_FEATURE(ParserRoundTrip, false)
138138
/// Swift parser.
139139
EXPERIMENTAL_FEATURE(ParserValidation, false)
140140

141-
/// Whether to fold sequence expressions in the syntax tree produced by the
142-
/// Swift Swift parser.
143-
EXPERIMENTAL_FEATURE(ParserSequenceFolding, false)
141+
/// Whether to emit diagnostics from the new parser first, and only emit
142+
/// diagnostics from the existing parser when there are none from the new
143+
/// parser.
144+
EXPERIMENTAL_FEATURE(ParserDiagnostics, false)
144145

145146
/// Enables implicit some while also enabling existential `any`
146147
EXPERIMENTAL_FEATURE(ImplicitSome, false)

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2974,7 +2974,7 @@ static bool usesFeatureParserValidation(Decl *decl) {
29742974
return false;
29752975
}
29762976

2977-
static bool usesFeatureParserSequenceFolding(Decl *decl) {
2977+
static bool usesFeatureParserDiagnostics(Decl *decl) {
29782978
return false;
29792979
}
29802980

lib/Parse/ParseDecl.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,11 @@ extern "C" void swift_ASTGen_buildTopLevelASTNodes(void *sourceFile,
205205
/// \endverbatim
206206
void Parser::parseTopLevelItems(SmallVectorImpl<ASTNode> &items) {
207207
#if SWIFT_SWIFT_PARSER
208+
Optional<DiagnosticTransaction> existingParsingTransaction;
208209
if ((Context.LangOpts.hasFeature(Feature::Macros) ||
209210
Context.LangOpts.hasFeature(Feature::BuiltinMacros) ||
210211
Context.LangOpts.hasFeature(Feature::ParserRoundTrip) ||
212+
Context.LangOpts.hasFeature(Feature::ParserDiagnostics) ||
211213
Context.LangOpts.hasFeature(Feature::ParserValidation) ||
212214
Context.LangOpts.hasFeature(Feature::ParserASTGen)) &&
213215
!SourceMgr.hasIDEInspectionTargetBuffer() &&
@@ -225,6 +227,18 @@ void Parser::parseTopLevelItems(SmallVectorImpl<ASTNode> &items) {
225227
swift_ASTGen_destroySourceFile(exportedSourceFile);
226228
});
227229

230+
// If we're supposed to emit diagnostics from the parser, do so now.
231+
if ((Context.LangOpts.hasFeature(Feature::ParserDiagnostics) ||
232+
Context.LangOpts.hasFeature(Feature::ParserASTGen)) &&
233+
swift_ASTGen_emitParserDiagnostics(
234+
&Context.Diags, SF.exportedSourceFile) &&
235+
Context.Diags.hadAnyError() &&
236+
!Context.LangOpts.hasFeature(Feature::ParserASTGen)) {
237+
// Errors were emitted, and we're still using the C++ parser, so
238+
// disable diagnostics from the C++ parser.
239+
existingParsingTransaction.emplace(Context.Diags);
240+
}
241+
228242
// If we want to do ASTGen, do so now.
229243
if (Context.LangOpts.hasFeature(Feature::ParserASTGen)) {
230244
swift_ASTGen_buildTopLevelASTNodes(
@@ -287,6 +301,9 @@ void Parser::parseTopLevelItems(SmallVectorImpl<ASTNode> &items) {
287301
}
288302

289303
#if SWIFT_SWIFT_PARSER
304+
if (existingParsingTransaction)
305+
existingParsingTransaction->abort();
306+
290307
// Perform round-trip and/or validation checking.
291308
if ((Context.LangOpts.hasFeature(Feature::ParserRoundTrip) ||
292309
Context.LangOpts.hasFeature(Feature::ParserValidation)) &&
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature ParserDiagnostics
2+
3+
// FIXME: Swift parser is not enabled on Linux CI yet.
4+
// REQUIRES: OS=macosx
5+
// REQUIRES: asserts
6+
7+
_ = [(Int) -> async throws Int]()
8+
// expected-error@-1{{'async throws' may only occur before '->'}}

0 commit comments

Comments
 (0)