Skip to content

[Parse] Create DiagnosticTransaction along with BacktrackingScope #5493

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
13 changes: 10 additions & 3 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,24 @@ class Parser {
class BacktrackingScope {
Parser &P;
ParserPosition PP;
DiagnosticTransaction DT;
bool Backtrack = true;

public:
BacktrackingScope(Parser &P) : P(P), PP(P.getParserPosition()) {}
BacktrackingScope(Parser &P)
: P(P), PP(P.getParserPosition()), DT(P.Diags) {}

~BacktrackingScope() {
if (Backtrack)
if (Backtrack) {
P.backtrackToPosition(PP);
DT.abort();
}
}

void cancelBacktrack() { Backtrack = false; }
void cancelBacktrack() {
Backtrack = false;
DT.commit();
}
};

/// RAII object that, when it is destructed, restores the parser and lexer to
Expand Down
3 changes: 1 addition & 2 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,9 +1453,8 @@ ParserResult<Expr> Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) {

// Handle the deprecated 'x.dynamicType' and migrate it to `type(of: x)`
if (Tok.getText() == "dynamicType") {
BacktrackingScope backtrackScope(*this);
auto range = Result.get()->getSourceRange();
auto dynamicTypeExprRange = SourceRange(TokLoc, consumeToken());
auto dynamicTypeExprRange = SourceRange(TokLoc, Tok.getLoc());
diagnose(TokLoc, diag::expr_dynamictype_deprecated)
.highlight(dynamicTypeExprRange)
.fixItReplace(dynamicTypeExprRange, ")")
Expand Down
11 changes: 11 additions & 0 deletions test/expr/primary/unqualified_name.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,14 @@ class C1 : C0 {
}
}

struct S1 {
init(x: Int) {} // expected-note {{'init(x:)' declared here}}

func testS1() {
_ = S1.init(x:)(1)
_ = S1.init(`x`: 1) // expected-warning {{keyword 'x' does not need to be escaped in argument list}} {{17-18=}} {{19-20=}}

// Test for unknown token.
_ = S1.init(x: 0xG) // expected-error {{expected a digit after integer literal prefix}} expected-error {{missing argument for parameter 'x' in call}}
}
}