From 4d1336653f43d4c9b8854f476575533a159655de Mon Sep 17 00:00:00 2001 From: Rylan Dmello Date: Thu, 15 Oct 2020 08:43:47 -0400 Subject: [PATCH 1/4] Add error message when assigning to tuple --- src/parser.yy | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/parser.yy b/src/parser.yy index 53293c5a20cd..c815742eacf1 100644 --- a/src/parser.yy +++ b/src/parser.yy @@ -265,6 +265,7 @@ stmt : expr { $$ = new ast::ExprStatement($1); } | jump_stmt { $$ = $1; } | map "=" expr { $$ = new ast::AssignMapStatement($1, $3, @2); } | var "=" expr { $$ = new ast::AssignVarStatement($1, $3, @2); } + | tuple_assignment ; compound_assignment : map LEFTASSIGN expr { $$ = new ast::AssignMapStatement($1, new ast::Binop($1, token::LEFT, $3, @2)); } @@ -289,6 +290,8 @@ compound_assignment : map LEFTASSIGN expr { $$ = new ast::AssignMapStatement($1 | var BXORASSIGN expr { $$ = new ast::AssignVarStatement($1, new ast::Binop($1, token::BXOR, $3, @2)); } ; +tuple_assignment : expr DOT INT "=" expr { error(@1 + @5, "Tuples are immutable once created. Consider creating a new tuple and assigning it instead."); YYERROR; } + int : MINUS INT { $$ = new ast::Integer(-1 * $2, @$); } | INT { $$ = new ast::Integer($1, @$); } ; From 6ac650b7fc6ce536312ed89d3d318c25b017ebcf Mon Sep 17 00:00:00 2001 From: Rylan Dmello Date: Thu, 15 Oct 2020 08:51:22 -0400 Subject: [PATCH 2/4] Test for tuple assignment error --- tests/parser.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/parser.cpp b/tests/parser.cpp index 5956f64e567c..9215b702c2dc 100644 --- a/tests/parser.cpp +++ b/tests/parser.cpp @@ -1663,6 +1663,20 @@ TEST(Parser, while_loop) )PROG"); } +TEST(Parser, tuple_assignment_error) +{ + BPFtrace bpftrace; + std::stringstream out; + Driver driver(bpftrace, out); + EXPECT_EQ(driver.parse_str("i:s:1 { @x = (1, 2); $x.1 = 1; }"), 1); + std::string expected = + R"(stdin:1:22-30: ERROR: Tuples are immutable once created. Consider creating a new tuple and assigning it instead. +i:s:1 { @x = (1, 2); $x.1 = 1; } + ~~~~~~~~ +)"; + EXPECT_EQ(out.str(), expected); +} + } // namespace parser } // namespace test } // namespace bpftrace From c6d8ba2a12457c24111b64751d813729c6cdf79a Mon Sep 17 00:00:00 2001 From: Rylan Dmello Date: Thu, 15 Oct 2020 21:38:34 -0400 Subject: [PATCH 3/4] Add some more test cases for tuple assignment parse failure --- tests/parser.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/parser.cpp b/tests/parser.cpp index 9215b702c2dc..5d81ac4944d8 100644 --- a/tests/parser.cpp +++ b/tests/parser.cpp @@ -1663,7 +1663,7 @@ TEST(Parser, while_loop) )PROG"); } -TEST(Parser, tuple_assignment_error) +TEST(Parser, tuple_assignment_error_message) { BPFtrace bpftrace; std::stringstream out; @@ -1677,6 +1677,16 @@ i:s:1 { @x = (1, 2); $x.1 = 1; } EXPECT_EQ(out.str(), expected); } +TEST(Parser, tuple_assignment_error) +{ + test_parse_failure("i:s:1 { (1, 0) = 0 }"); + test_parse_failure("i:s:1 { ((1, 0), 3).0.0 = 3 }"); + test_parse_failure("i:s:1 { ((1, 0), 3).0 = (0, 1) }"); + test_parse_failure("i:s:1 { (1, \"two\", (3, 4)).5 = \"six\"; }"); + test_parse_failure("i:s:1 { $a = 1; $a.2 = 3 }"); + test_parse_failure("i:s:1 { 0.1 = 1.0 }"); +} + } // namespace parser } // namespace test } // namespace bpftrace From 54399caa2ccbd76370401a82664ec6084ccb0674 Mon Sep 17 00:00:00 2001 From: Rylan Dmello Date: Thu, 15 Oct 2020 21:46:20 -0400 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5559519faa1..7ef3097075d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,8 @@ and this project adheres to - [#1542](https://github.com/iovisor/bpftrace/pull/1542) - Change a part of the message of '-v' output - [#1553](https://github.com/iovisor/bpftrace/pull/1553) +- Improve tuple assignment error message + - [#1563](https://github.com/iovisor/bpftrace/pull/1563) #### Deprecated