Skip to content
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

Provide better error message when assigning to tuple element #1563

Merged
merged 4 commits into from
Oct 16, 2020
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions src/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -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)); }
Expand All @@ -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, @$); }
;
Expand Down
24 changes: 24 additions & 0 deletions tests/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,30 @@ TEST(Parser, while_loop)
)PROG");
}

TEST(Parser, tuple_assignment_error_message)
{
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);
}

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