From 8d0d271cd3a446001d3dacc93116c436378a59a4 Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Tue, 26 Jul 2022 19:39:54 -0700 Subject: [PATCH] Make interpolated string with no formatting a syntax error --- Ast/include/Luau/Lexer.h | 1 + Ast/src/Lexer.cpp | 5 ++++- Ast/src/Parser.cpp | 2 ++ tests/Parser.test.cpp | 15 +++++++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Ast/include/Luau/Lexer.h b/Ast/include/Luau/Lexer.h index b91879c6..ff10d489 100644 --- a/Ast/include/Luau/Lexer.h +++ b/Ast/include/Luau/Lexer.h @@ -86,6 +86,7 @@ struct Lexeme BrokenUnicode, BrokenInterpDoubleBrace, + BrokenInterpNoFormat, Error, diff --git a/Ast/src/Lexer.cpp b/Ast/src/Lexer.cpp index 19c70708..04d97fb1 100644 --- a/Ast/src/Lexer.cpp +++ b/Ast/src/Lexer.cpp @@ -187,6 +187,9 @@ std::string Lexeme::toString() const case BrokenInterpDoubleBrace: return "'{{', which is invalid (did you mean '\\{'?)"; + case BrokenInterpNoFormat: + return "interpolated string with no formatting"; + case BrokenUnicode: if (codepoint) { @@ -631,7 +634,7 @@ Lexeme Lexer::readInterpolatedStringBegin() if (!readSectionOpt) { - LUAU_ASSERT(!"INTERP TODO: Error if there was no interpolated expression"); + return Lexeme(Location(start, position()), Lexeme::BrokenInterpNoFormat); } return *readSectionOpt; diff --git a/Ast/src/Parser.cpp b/Ast/src/Parser.cpp index 43865e4e..7e5d8a65 100644 --- a/Ast/src/Parser.cpp +++ b/Ast/src/Parser.cpp @@ -2683,6 +2683,8 @@ AstExpr* Parser::parseInterpString() return reportExprError(location, {}, "Malformed interpolated string"); case Lexeme::BrokenInterpDoubleBrace: return reportExprError(location, {}, "Double braces are not permitted within interpolated strings. Did you mean '\\{'?"); + default: + break; } } while (true); } diff --git a/tests/Parser.test.cpp b/tests/Parser.test.cpp index 1ec5314b..3091f220 100644 --- a/tests/Parser.test.cpp +++ b/tests/Parser.test.cpp @@ -1065,6 +1065,21 @@ TEST_CASE_FIXTURE(Fixture, "parse_interpolated_string_double_brace_mid") } } +TEST_CASE_FIXTURE(Fixture, "parse_interpolated_string_without_format") +{ + try + { + parse(R"( + _ = `doge` + )"); + FAIL("Expected ParseErrors to be thrown"); + } + catch (const ParseErrors& e) + { + CHECK_EQ("Expected identifier when parsing expression, got interpolated string with no formatting", e.getErrors().front().getMessage()); + } +} + TEST_CASE_FIXTURE(Fixture, "parse_nesting_based_end_detection") { try