Skip to content

Commit

Permalink
fix: improve error message for E0715
Browse files Browse the repository at this point in the history
  • Loading branch information
toastin0 committed Oct 28, 2023
1 parent e95291c commit 48f3c20
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 11 deletions.
4 changes: 4 additions & 0 deletions po/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2069,6 +2069,10 @@ msgstr ""
msgid "unintuitive operator precedence when using & and '{0}'; '{0}' evaluates before &"
msgstr ""

#: src/quick-lint-js/diag/diagnostic-metadata-generated.cpp
msgid "'&' here"
msgstr ""

#: test/test-diagnostic-formatter.cpp
#: test/test-vim-qflist-json-diag-reporter.cpp
msgid "something happened"
Expand Down
4 changes: 4 additions & 0 deletions src/quick-lint-js/diag/diagnostic-metadata-generated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6368,11 +6368,15 @@ const QLJS_CONSTINIT Diagnostic_Info all_diagnostic_infos[] = {
.severity = Diagnostic_Severity::warning,
.message_formats = {
QLJS_TRANSLATABLE("unintuitive operator precedence when using & and '{0}'; '{0}' evaluates before &"),
QLJS_TRANSLATABLE("'&' here"),
},
.message_args = {
{
Diagnostic_Message_Arg_Info(offsetof(Diag_Unintuitive_Bitshift_Precedence, bitshift_operator), Diagnostic_Arg_Type::source_code_span),
},
{
Diagnostic_Message_Arg_Info(offsetof(Diag_Unintuitive_Bitshift_Precedence, and_operator), Diagnostic_Arg_Type::source_code_span),
},
},
},
};
Expand Down
5 changes: 4 additions & 1 deletion src/quick-lint-js/diag/diagnostic-types-2.h
Original file line number Diff line number Diff line change
Expand Up @@ -3285,7 +3285,10 @@ struct Diag_Unintuitive_Bitshift_Precedence {
[[qljs::message(
"unintuitive operator precedence when using & and '{0}'; "
"'{0}' evaluates before &",
ARG(bitshift_operator))]] Source_Code_Span bitshift_operator;
ARG(bitshift_operator))]]
[[qljs::message("'&' here", ARG(and_operator))]]
Source_Code_Span bitshift_operator;
Source_Code_Span and_operator;
};
}
QLJS_WARNING_POP
Expand Down
7 changes: 4 additions & 3 deletions src/quick-lint-js/fe/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,15 @@ void Parser::warn_on_unintuitive_bitshift_precedence(Expression* ast) {
auto* binary_op = static_cast<Expression::Binary_Operator*>(ast);
Source_Code_Span left_op = binary_op->operator_spans_[0];
Source_Code_Span right_op = binary_op->operator_spans_[1];
if (left_op.string_view() == "&" &&
(right_op.string_view() == ">>" || right_op.string_view() == "<<")) {
if (left_op.string_view() == u8"&"_sv &&
(right_op.string_view() == u8">>"_sv || right_op.string_view() == u8"<<"_sv)) {
if (binary_op->child(0)->kind() == Expression_Kind::Variable &&
binary_op->child(1)->kind() == Expression_Kind::Literal &&
binary_op->child(2)->kind() == Expression_Kind::Literal) {
this->diag_reporter_->report(
quick_lint_js::Diag_Unintuitive_Bitshift_Precedence{
.bitshift_operator = right_op});
.bitshift_operator = right_op, .and_operator = left_op
});
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/quick-lint-js/i18n/translation-table-generated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const Translation_Table translation_data = {
{74, 87, 79, 56, 0, 59}, //
{71, 80, 60, 58, 0, 52}, //
{0, 0, 0, 0, 0, 28}, //
{31, 56, 0, 32, 0, 63}, //
{0, 0, 0, 0, 0, 63}, //
{31, 56, 0, 32, 0, 9}, //
{0, 0, 0, 0, 0, 67}, //
{0, 0, 0, 70, 0, 26}, //
{79, 25, 30, 63, 49593, 66}, //
Expand Down Expand Up @@ -1808,6 +1809,7 @@ const Translation_Table translation_data = {
u8"\"globals\" descriptor must be a boolean or an object\0"
u8"\"globals\" must be an object\0"
u8"'!' here treated as the TypeScript non-null assertion operator\0"
u8"'&' here\0"
u8"'**' operator cannot be used after unary '{1}' without parentheses\0"
u8"',' should be ';' instead\0"
u8"'.' is not allowed after generic arguments; write [\"{1}\"] instead\0"
Expand Down
5 changes: 3 additions & 2 deletions src/quick-lint-js/i18n/translation-table-generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ namespace quick_lint_js {
using namespace std::literals::string_view_literals;

constexpr std::uint32_t translation_table_locale_count = 5;
constexpr std::uint16_t translation_table_mapping_table_size = 519;
constexpr std::size_t translation_table_string_table_size = 79814;
constexpr std::uint16_t translation_table_mapping_table_size = 520;
constexpr std::size_t translation_table_string_table_size = 79823;
constexpr std::size_t translation_table_locale_table_size = 35;

QLJS_CONSTEVAL std::uint16_t translation_table_const_look_up(
Expand All @@ -33,6 +33,7 @@ QLJS_CONSTEVAL std::uint16_t translation_table_const_look_up(
"\"globals\" descriptor must be a boolean or an object"sv,
"\"globals\" must be an object"sv,
"'!' here treated as the TypeScript non-null assertion operator"sv,
"'&' here"sv,
"'**' operator cannot be used after unary '{1}' without parentheses"sv,
"',' should be ';' instead"sv,
"'.' is not allowed after generic arguments; write [\"{1}\"] instead"sv,
Expand Down
13 changes: 12 additions & 1 deletion src/quick-lint-js/i18n/translation-table-test-generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Translated_String {
};

// clang-format off
inline const Translated_String test_translation_table[518] = {
inline const Translated_String test_translation_table[519] = {
{
"\"global-groups\" entries must be strings"_translatable,
u8"\"global-groups\" entries must be strings",
Expand Down Expand Up @@ -105,6 +105,17 @@ inline const Translated_String test_translation_table[518] = {
u8"'!' here treated as the TypeScript non-null assertion operator",
},
},
{
"'&' here"_translatable,
u8"'&' here",
{
u8"'&' here",
u8"'&' here",
u8"'&' here",
u8"'&' here",
u8"'&' here",
},
},
{
"'**' operator cannot be used after unary '{1}' without parentheses"_translatable,
u8"'**' operator cannot be used after unary '{1}' without parentheses",
Expand Down
9 changes: 6 additions & 3 deletions test/test-parse-warning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,16 @@ TEST_F(Test_Parse_Warning, warn_on_xor_operation_used_as_exponentiation) {
TEST_F(Test_Parse_Warning, warn_on_unintuitive_precedence_when_using_bitshift) {
test_parse_and_visit_expression(
u8"var1 & 0x01 >> 0x02"_sv,
u8" ^^ Diag_Unintuitive_Bitshift_Precedence"_diag);
u8" ^^ Diag_Unintuitive_Bitshift_Precedence.bitshift_operator\n"_diag
u8" ^ .and_operator"_diag);
test_parse_and_visit_expression(
u8"var2 & 0x1234 << 0x4321"_sv,
u8" ^^ Diag_Unintuitive_Bitshift_Precedence"_diag);
u8" ^^ Diag_Unintuitive_Bitshift_Precedence.bitshift_operator\n"_diag
u8" ^ .and_operator"_diag);
test_parse_and_visit_statement(
u8"const x = a & 10 << 12"_sv,
u8" ^^ Diag_Unintuitive_Bitshift_Precedence"_diag);
u8" ^^ Diag_Unintuitive_Bitshift_Precedence.bitshift_operator\n"_diag
u8" ^ .and_operator"_diag);
test_parse_and_visit_expression(u8"0x111 << 0x222 & var1"_sv, no_diags);
test_parse_and_visit_expression(u8"a&b>>c"_sv, no_diags);
test_parse_and_visit_expression(u8"x & y << z"_sv, no_diags);
Expand Down

0 comments on commit 48f3c20

Please sign in to comment.