Skip to content

Commit 95a3a43

Browse files
authored
Merge pull request #16174 from ahoppen/libsyntax-silgen-preparation
[libSyntax, tests] Preparations for verification of syntax tree on SILGen tests
2 parents 0808744 + dc3e43b commit 95a3a43

20 files changed

+154
-114
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,8 +1585,8 @@ ERROR(availability_query_repeated_platform, none,
15851585
//------------------------------------------------------------------------------
15861586
// syntax parsing diagnostics
15871587
//------------------------------------------------------------------------------
1588-
WARNING(unknown_syntax_entity, PointsToFirstBadToken,
1589-
"unknown %0 syntax exists in the source", (StringRef))
1588+
ERROR(unknown_syntax_entity, PointsToFirstBadToken,
1589+
"unknown %0 syntax exists in the source", (StringRef))
15901590

15911591
#ifndef DIAG_NO_UNDEF
15921592
# if defined(DIAG)

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ def verify_generic_signatures : Separate<["-"], "verify-generic-signatures">,
8181
MetaVarName<"<module-name>">,
8282
HelpText<"Verify the generic signatures in the given module">;
8383

84+
def verify_syntax_tree : Flag<["-"], "verify-syntax-tree">,
85+
HelpText<"Verify that no unknown nodes exist in the libSyntax tree">;
86+
8487
def show_diagnostics_after_fatal : Flag<["-"], "show-diagnostics-after-fatal">,
8588
HelpText<"Keep emitting subsequent diagnostics after a fatal error">;
8689

lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
214214
Opts.NamedLazyMemberLoading &= !Args.hasArg(OPT_disable_named_lazy_member_loading);
215215
Opts.DebugGenericSignatures |= Args.hasArg(OPT_debug_generic_signatures);
216216

217+
if (Args.hasArg(OPT_verify_syntax_tree)) {
218+
Opts.BuildSyntaxTree = true;
219+
Opts.VerifySyntaxTree = true;
220+
}
221+
217222
Opts.DebuggerSupport |= Args.hasArg(OPT_debugger_support);
218223
if (Opts.DebuggerSupport)
219224
Opts.EnableDollarIdentifiers = true;

lib/Parse/ParseDecl.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3870,7 +3870,11 @@ static ParameterList *parseOptionalAccessorArgument(SourceLoc SpecifierLoc,
38703870
return ParameterList::create(P.Context, StartLoc, param, EndLoc);
38713871
}
38723872

3873-
static unsigned skipUntilMatchingRBrace(Parser &P) {
3873+
static unsigned skipUntilMatchingRBrace(Parser &P,
3874+
SyntaxParsingContext *&SyntaxContext) {
3875+
SyntaxParsingContext BlockItemListContext(SyntaxContext,
3876+
SyntaxKind::CodeBlockItemList);
3877+
SyntaxParsingContext BodyContext(SyntaxContext, SyntaxKind::TokenList);
38743878
unsigned OpenBraces = 1;
38753879
while (OpenBraces != 0 && P.Tok.isNot(tok::eof)) {
38763880
if (P.consumeIf(tok::l_brace)) {
@@ -3888,9 +3892,11 @@ static unsigned skipUntilMatchingRBrace(Parser &P) {
38883892
return OpenBraces;
38893893
}
38903894

3891-
static unsigned skipBracedBlock(Parser &P) {
3895+
static unsigned skipBracedBlock(Parser &P,
3896+
SyntaxParsingContext *&SyntaxContext) {
3897+
SyntaxParsingContext CodeBlockContext(SyntaxContext, SyntaxKind::CodeBlock);
38923898
P.consumeToken(tok::l_brace);
3893-
unsigned OpenBraces = skipUntilMatchingRBrace(P);
3899+
unsigned OpenBraces = skipUntilMatchingRBrace(P, SyntaxContext);
38943900
if (P.consumeIf(tok::r_brace))
38953901
OpenBraces--;
38963902
return OpenBraces;
@@ -3904,7 +3910,7 @@ void Parser::consumeGetSetBody(AbstractFunctionDecl *AFD,
39043910
BodyRange.Start = Tok.getLoc();
39053911

39063912
// Skip until the next '}' at the correct nesting level.
3907-
unsigned OpenBraces = skipUntilMatchingRBrace(*this);
3913+
unsigned OpenBraces = skipUntilMatchingRBrace(*this, SyntaxContext);
39083914

39093915
if (OpenBraces != 1) {
39103916
// FIXME: implement some error recovery?
@@ -5102,7 +5108,7 @@ void Parser::consumeAbstractFunctionBody(AbstractFunctionDecl *AFD,
51025108
BodyRange.Start = Tok.getLoc();
51035109

51045110
// Consume the '{', and find the matching '}'.
5105-
unsigned OpenBraces = skipBracedBlock(*this);
5111+
unsigned OpenBraces = skipBracedBlock(*this, SyntaxContext);
51065112
if (OpenBraces != 0 && Tok.isNot(tok::code_complete)) {
51075113
assert(Tok.is(tok::eof));
51085114
// We hit EOF, and not every brace has a pair. Recover by searching
@@ -6458,7 +6464,7 @@ Parser::parseDeclPrecedenceGroup(ParseDeclOptions flags,
64586464
(void) consumeIf(tok::r_brace);
64596465
} else if (Tok.isNot(tok::eof) && peekToken().is(tok::l_brace)) {
64606466
consumeToken();
6461-
skipBracedBlock(*this);
6467+
skipBracedBlock(*this, SyntaxContext);
64626468
}
64636469
return nullptr;
64646470
}

lib/Parse/ParseExpr.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3563,8 +3563,13 @@ ParserResult<Expr> Parser::parseExprTypeOf() {
35633563
// DynamicTypeExpr.
35643564
SyntaxParsingContext CallCtxt(SyntaxContext, SyntaxKind::FunctionCallExpr);
35653565

3566-
// Consume 'type'
3567-
SourceLoc keywordLoc = consumeToken();
3566+
SourceLoc keywordLoc;
3567+
{
3568+
SyntaxParsingContext IdentifierExprContext(SyntaxContext,
3569+
SyntaxKind::IdentifierExpr);
3570+
// Consume 'type'
3571+
keywordLoc = consumeToken();
3572+
}
35683573

35693574
// Parse the leading '('.
35703575
SourceLoc lParenLoc = consumeToken(tok::l_paren);

test/SILGen/Inputs/AppKit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ public func NSApplicationMain(
99
return argv.withMemoryRebound(to:UnsafePointer<Int8>.self, capacity: Int(argc)) {
1010
argv in
1111
return __NSApplicationMain(argc, argv)
12-
})
12+
}
1313
}

test/SILGen/enum_generic_raw_value.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ enum E<T>: Int {
66
}
77

88
// CHECK-LABEL: sil hidden @$S22enum_generic_raw_value1FO
9-
enum F<T: ExpressibleByIntegerLiteral where T: Equatable>: T {
9+
enum F<T: ExpressibleByIntegerLiteral>: T where T: Equatable {
1010
case A = 1
1111
}

test/SILGen/generic_signatures.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,36 @@ class C {}
1414

1515
func a<T>(x: T) {}
1616
func b<T: P>(x: G<T>, y: T.Assoc) {}
17-
func c<T where T: P>(x: T, y: T.Assoc) {}
17+
func c<T>(x: T, y: T.Assoc) where T: P {}
1818
func d<T: P, U: P & Q>(x: T, y: U) {}
19-
func e<T, U where T: P, U: P, U: Q>(x: T, y: U) {}
19+
func e<T, U>(x: T, y: U) where T: P, U: P, U: Q {}
2020
// FIXME: Same-type constraints expose a typechecker bug.
2121
// <rdar://problem/15730168>
22-
func f<T: Q where T.Assoc1 == T.Assoc2>(x: T) {}
23-
func g<T where T: Q, T.Assoc1 == T.Assoc2>(x: T) {}
24-
func h<T: P, U where T.Assoc == U>(x: T) {}
25-
func i<T: P where T.Assoc: Q, T.Assoc.Assoc1 == T.Assoc.Assoc2>(x: T) {}
22+
func f<T: Q>(x: T) where T.Assoc1 == T.Assoc2 {}
23+
func g<T>(x: T) where T: Q, T.Assoc1 == T.Assoc2 {}
24+
func h<T: P, U>(x: T) where T.Assoc == U {}
25+
func i<T: P>(x: T) where T.Assoc: Q, T.Assoc.Assoc1 == T.Assoc.Assoc2 {}
2626
func j<T: C>(_: T) {}
27-
func k<T where T: C>(_: T) {}
28-
func l<T: C where T: P>(_: T) {}
29-
func m<T: P where T.Assoc: C>(_: T) {}
27+
func k<T>(_: T) where T: C {}
28+
func l<T: C>(_: T) where T: P {}
29+
func m<T: P>(_: T) where T.Assoc: C {}
3030

3131
struct Foo<V> {
3232
func z() {}
3333

3434
func a<T>(x: T) {}
3535
func b<T: P>(x: G<T>, y: T.Assoc) {}
36-
func c<T where T: P>(x: T, y: T.Assoc) {}
36+
func c<T>(x: T, y: T.Assoc) where T: P {}
3737
func d<T: P, U: P & Q>(x: T, y: U) {}
38-
func e<T, U where T: P, U: P, U: Q>(x: T, y: U) {}
39-
func f<T: Q where T.Assoc1 == T.Assoc2>(x: T) {}
40-
func g<T where T: Q, T.Assoc1 == T.Assoc2>(x: T) {}
41-
func h<T: P, U where T.Assoc == U>(x: T) {}
42-
func i<T: P where T.Assoc: Q, T.Assoc.Assoc1 == T.Assoc.Assoc2>(x: T) {}
38+
func e<T, U>(x: T, y: U) where T: P, U: P, U: Q {}
39+
func f<T: Q>(x: T) where T.Assoc1 == T.Assoc2 {}
40+
func g<T>(x: T) where T: Q, T.Assoc1 == T.Assoc2 {}
41+
func h<T: P, U>(x: T) where T.Assoc == U {}
42+
func i<T: P>(x: T) where T.Assoc: Q, T.Assoc.Assoc1 == T.Assoc.Assoc2 {}
4343
func j<T: C>(_: T) {}
44-
func k<T where T: C>(_: T) {}
45-
func l<T: C where T: P>(_: T) {}
46-
func m<T: P where T.Assoc: C>(_: T) {}
44+
func k<T>(_: T) where T: C {}
45+
func l<T: C>(_: T) where T: P {}
46+
func m<T: P>(_: T) where T.Assoc: C {}
4747
}
4848

4949
// Test that we handle interface type lowering when accessing a dependent

0 commit comments

Comments
 (0)