Skip to content

Revert "Reject bad string interpolations (#17074)" #17190

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 1 commit into from
Jun 14, 2018
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
10 changes: 0 additions & 10 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1224,16 +1224,6 @@ ERROR(expected_type_after_as,none,
ERROR(string_interpolation_extra,none,
"extra tokens after interpolated string expression", ())

// Interpolated value accidentally forms a tuple.
ERROR(string_interpolation_single_expr,none,
"interpolations should be a single expression", ())
NOTE(string_interpolation_form_tuple,none,
"insert parentheses to form a tuple", ())
NOTE(string_interpolation_delete_empty,none,
"delete empty interpolation", ())
ERROR(string_interpolation_keyword_argument,PointsToFirstBadToken,
"interpolations cannot start with a keyword argument", ())

// Keypath expressions.
ERROR(expr_keypath_expected_lparen,PointsToFirstBadToken,
"expected '(' following '#keyPath'", ())
Expand Down
44 changes: 0 additions & 44 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1907,50 +1907,6 @@ parseStringSegments(SmallVectorImpl<Lexer::StringSegment> &Segments,
Status |= E;
if (E.isNonNull()) {
Exprs.push_back(E.get());

if (auto tuple = dyn_cast<TupleExpr>(Exprs.back())) {
// If parseExprList() returns a TupleExpr instead of a ParenExpr,
// the interpolation must have had an argument label, or the wrong
// number of elements, or both. Reject these.
if (tuple->getNumElements() > 1) {
SourceLoc StartLoc = tuple->getStartLoc();
SourceLoc SecondExprLoc = tuple->getElement(1)->getStartLoc();
SourceLoc EndLoc = tuple->getEndLoc();

diagnose(SecondExprLoc, diag::string_interpolation_single_expr);
diagnose(StartLoc, diag::string_interpolation_form_tuple)
.fixItInsert(StartLoc, "(")
.fixItInsertAfter(EndLoc, ")");

Exprs.back() =
new (Context) ParenExpr(SourceLoc(), tuple, SourceLoc(),
/*hasTrailingClosure=*/false);
} else if (tuple->getNumElements() == 1 &&
!tuple->getElementName(0).empty()) {
SourceLoc NameStart = tuple->getElementNameLoc(0);
SourceLoc ArgStart = tuple->getElement(0)->getStartLoc();

diagnose(NameStart, diag::string_interpolation_keyword_argument)
.fixItRemoveChars(NameStart, ArgStart);

Exprs.back() =
new (Context) ParenExpr(tuple->getLParenLoc(), tuple->getElement(0),
tuple->getRParenLoc(), /*hasTrailingClosure=*/false);
} else if (tuple->getNumElements() == 0 && !Status.isError()) {
SourceLoc StartLoc = tuple->getStartLoc();
SourceLoc EndLoc = tuple->getEndLoc();
SourceLoc SlashLoc = StartLoc.getAdvancedLocOrInvalid(-1);

diagnose(EndLoc, diag::string_interpolation_single_expr);
diagnose(SlashLoc, diag::string_interpolation_delete_empty)
.fixItRemoveChars(SlashLoc, EndLoc.getAdvancedLocOrInvalid(1));

auto Error = new (Context) ErrorExpr(SourceRange(EndLoc, EndLoc));
Exprs.back() =
new (Context) ParenExpr(SourceLoc(), Error, SourceLoc(),
/*hasTrailingClosure=*/false);
}
}

if (!Tok.is(tok::eof)) {
diagnose(Tok, diag::string_interpolation_extra);
Expand Down
10 changes: 1 addition & 9 deletions lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,10 +902,6 @@ ParserStatus
Parser::parseList(tok RightK, SourceLoc LeftLoc, SourceLoc &RightLoc,
bool AllowSepAfterLast, Diag<> ErrorDiag, SyntaxKind Kind,
llvm::function_ref<ParserStatus()> callback) {
auto TokIsStringInterpolationEOF = [&]() -> bool {
return Tok.is(tok::eof) && Tok.getText() == ")" && RightK == tok::r_paren;
};

llvm::Optional<SyntaxParsingContext> ListContext;
ListContext.emplace(SyntaxContext, Kind);
if (Kind == SyntaxKind::Unknown)
Expand All @@ -918,10 +914,6 @@ Parser::parseList(tok RightK, SourceLoc LeftLoc, SourceLoc &RightLoc,
RightLoc = consumeToken(RightK);
return makeParserSuccess();
}
if (TokIsStringInterpolationEOF()) {
RightLoc = Tok.getLoc();
return makeParserSuccess();
}

ParserStatus Status;
while (true) {
Expand All @@ -941,7 +933,7 @@ Parser::parseList(tok RightK, SourceLoc LeftLoc, SourceLoc &RightLoc,
// If the lexer stopped with an EOF token whose spelling is ")", then this
// is actually the tuple that is a string literal interpolation context.
// Just accept the ")" and build the tuple as we usually do.
if (TokIsStringInterpolationEOF()) {
if (Tok.is(tok::eof) && Tok.getText() == ")" && RightK == tok::r_paren) {
RightLoc = Tok.getLoc();
return Status;
}
Expand Down
26 changes: 0 additions & 26 deletions test/Parse/interpolation_tuple_errors.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: not %target-swift-frontend %s -emit-ir
// RUN: not --crash %target-swift-frontend %s -emit-ir

func foo<U>(_ x: U?) {
_ = "\(anyLabelHere: x)"
Expand Down