Skip to content

[parser] Fix invalid tuple type round-tripping issue #27286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ class Parser {
///
/// If the input is malformed, this emits the specified error diagnostic.
bool parseToken(tok K, SourceLoc &TokLoc, const Diagnostic &D);
llvm::Optional<ParsedTokenSyntax> parseTokenSyntax(tok K,
llvm::Optional<ParsedTokenSyntax> parseTokenSyntax(tok K, SourceLoc &TokLoc,
const Diagnostic &D);

template<typename ...DiagArgTypes, typename ...ArgTypes>
Expand All @@ -896,7 +896,8 @@ class Parser {
SourceLoc OtherLoc);

llvm::Optional<ParsedTokenSyntax>
parseMatchingTokenSyntax(tok K, Diag<> ErrorDiag, SourceLoc OtherLoc);
parseMatchingTokenSyntax(tok K, SourceLoc &TokLoc, Diag<> ErrorDiag,
SourceLoc OtherLoc);

/// Returns the proper location for a missing right brace, parenthesis, etc.
SourceLoc getLocForMissingMatchingToken() const;
Expand Down
13 changes: 9 additions & 4 deletions lib/Parse/ParseType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ Parser::parseLayoutConstraintSyntax() {
if (Tok.is(tok::r_paren))
builder.useRightParen(consumeTokenSyntax(tok::r_paren));
} else {
auto rParen = parseMatchingTokenSyntax(tok::r_paren,
SourceLoc rParenLoc;
auto rParen = parseMatchingTokenSyntax(tok::r_paren, rParenLoc,
diag::expected_rparen_layout_constraint,
lParenLoc);
if (rParen)
Expand Down Expand Up @@ -1166,6 +1167,8 @@ Parser::TypeResult Parser::parseTypeTupleBody() {
});

if (!Status.isSuccess()) {
if (RParen)
Junk.push_back(std::move(*RParen));
auto ty = ParsedSyntaxRecorder::makeUnknownType(Junk, *SyntaxContext);
return makeParsedResult(std::move(ty), Status);
}
Expand Down Expand Up @@ -1253,9 +1256,9 @@ Parser::TypeResult Parser::parseTypeArray(ParsedTypeSyntax Base,
// Ignore integer literal between '[' and ']'
ignoreIf(tok::integer_literal);

auto RSquareLoc = Tok.getLoc();
SourceLoc RSquareLoc;
auto RSquare = parseMatchingTokenSyntax(
tok::r_square, diag::expected_rbracket_array_type, LSquareLoc);
tok::r_square, RSquareLoc, diag::expected_rbracket_array_type, LSquareLoc);

if (RSquare) {
// If we parsed something valid, diagnose it with a fixit to rewrite it to
Expand Down Expand Up @@ -1301,7 +1304,9 @@ Parser::TypeResult Parser::parseTypeCollection() {
auto Diag = Colon ? diag::expected_rbracket_dictionary_type
: diag::expected_rbracket_array_type;

auto RSquare = parseMatchingTokenSyntax(tok::r_square, Diag, LSquareLoc);
SourceLoc RSquareLoc;
auto RSquare = parseMatchingTokenSyntax(tok::r_square, RSquareLoc, Diag,
LSquareLoc);
if (!RSquare)
Status.setIsParseError();

Expand Down
21 changes: 15 additions & 6 deletions lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1242,8 +1242,10 @@ bool Parser::parseMatchingToken(tok K, SourceLoc &TokLoc, Diag<> ErrorDiag,
return false;
}

Optional<ParsedTokenSyntax> Parser::parseTokenSyntax(tok K, const Diagnostic &D) {
Optional<ParsedTokenSyntax> Parser::parseTokenSyntax(tok K, SourceLoc &TokLoc,
const Diagnostic &D) {
if (Tok.is(K)) {
TokLoc = Tok.getLoc();
return consumeTokenSyntax();
}

Expand All @@ -1253,7 +1255,7 @@ Optional<ParsedTokenSyntax> Parser::parseTokenSyntax(tok K, const Diagnostic &D)
}

Optional<ParsedTokenSyntax>
Parser::parseMatchingTokenSyntax(tok K, Diag<> ErrorDiag, SourceLoc OtherLoc) {
Parser::parseMatchingTokenSyntax(tok K, SourceLoc &TokLoc, Diag<> ErrorDiag, SourceLoc OtherLoc) {
Diag<> OtherNote;
switch (K) {
case tok::r_paren: OtherNote = diag::opening_paren; break;
Expand All @@ -1262,9 +1264,11 @@ Parser::parseMatchingTokenSyntax(tok K, Diag<> ErrorDiag, SourceLoc OtherLoc) {
default: llvm_unreachable("unknown matching token!");
}

auto Token = parseTokenSyntax(K, ErrorDiag);
if (!Token)
auto Token = parseTokenSyntax(K, TokLoc, ErrorDiag);
if (!Token) {
TokLoc = getLocForMissingMatchingToken();
diagnose(OtherLoc, OtherNote);
}
return Token;
}

Expand Down Expand Up @@ -1391,9 +1395,14 @@ Parser::parseListSyntax(tok RightK, SourceLoc LeftLoc,

if (Status.isError()) {
// If we've already got errors, don't emit missing RightK diagnostics.
RightLoc = Tok.is(RightK) ? consumeToken() : PreviousLoc;
if (Tok.is(RightK)) {
RightLoc = Tok.getLoc();
Right = consumeTokenSyntax(RightK);
} else {
RightLoc = getLocForMissingMatchingToken();
}
} else {
Right = parseMatchingTokenSyntax(RightK, ErrorDiag, LeftLoc);
Right = parseMatchingTokenSyntax(RightK, RightLoc, ErrorDiag, LeftLoc);
if (!Right)
Status.setIsParseError();
}
Expand Down
3 changes: 2 additions & 1 deletion test/Syntax/Outputs/round_trip_invalid.swift.withkinds
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ class C <MemberDeclBlock>{<MemberDeclListItem><StructDecl>
struct S <MemberDeclBlock>{<MemberDeclListItem><EnumDecl>
enum E <MemberDeclBlock>{<MemberDeclListItem><ProtocolDecl>
protocol P <MemberDeclBlock>{<MemberDeclListItem><ExtensionDecl>
extension <SimpleTypeIdentifier>P </SimpleTypeIdentifier><MemberDeclBlock>{</MemberDeclBlock></ExtensionDecl></MemberDeclListItem></MemberDeclBlock></ProtocolDecl></MemberDeclListItem></MemberDeclBlock></EnumDecl></MemberDeclListItem></MemberDeclBlock></StructDecl></MemberDeclListItem></MemberDeclBlock></ClassDecl></CodeBlock></FunctionDecl>
extension <SimpleTypeIdentifier>P </SimpleTypeIdentifier><MemberDeclBlock>{<MemberDeclListItem><InitializerDecl><DeclModifier>
public </DeclModifier>init<ParameterClause>(<FunctionParameter>a: (<TupleTypeElement><SimpleTypeIdentifier>Int </SimpleTypeIdentifier></TupleTypeElement>|| String)</FunctionParameter>)</ParameterClause></InitializerDecl></MemberDeclListItem></MemberDeclBlock></ExtensionDecl></MemberDeclListItem></MemberDeclBlock></ProtocolDecl></MemberDeclListItem></MemberDeclBlock></EnumDecl></MemberDeclListItem></MemberDeclBlock></StructDecl></MemberDeclListItem></MemberDeclBlock></ClassDecl></CodeBlock></FunctionDecl>
1 change: 1 addition & 0 deletions test/Syntax/round_trip_invalid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ struct S {
enum E {
protocol P {
extension P {
public init(a: (Int || String))