Skip to content

Commit

Permalink
[DSLX:fmt] Fix handling of inline comment on unary operation.
Browse files Browse the repository at this point in the history
The unary operation was not tagged with the correct position data from the
original source text, so the comment emission process was getting confused.

Fixes #1372

PiperOrigin-RevId: 623587102
  • Loading branch information
cdleary authored and copybara-github committed Apr 10, 2024
1 parent 225668f commit 7e811c0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
8 changes: 8 additions & 0 deletions xls/dslx/fmt/ast_fmt_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,14 @@ struct Foo {
)");
}

TEST_F(ModuleFmtTest, UnaryWithCommentGithub1372) {
Run(
R"(fn main(x: bool) -> bool {
!x // Gotta negate it!
}
)");
}

TEST_F(ModuleFmtTest, StructDefTwoParametrics) {
const std::string_view kProgram =
"pub struct Point<M: u32, N: u32> { x: bits[M], y: bits[N] }\n";
Expand Down
2 changes: 1 addition & 1 deletion xls/dslx/frontend/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,7 @@ absl::StatusOr<Expr*> Parser::ParseTermLhs(Bindings& outer_bindings,
default:
LOG(FATAL) << "Inconsistent unary operation token kind.";
}
Span span(start_pos, GetPos());
Span span(tok.span().start(), arg->span().limit());
lhs = module_->Make<Unop>(span, unop_kind, arg);
} else if (peek->IsTypeKeyword() ||
(peek->kind() == TokenKind::kIdentifier &&
Expand Down
5 changes: 4 additions & 1 deletion xls/dslx/frontend/parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,10 @@ TEST_F(ParserTest, LocalConstBinding) {
EXPECT_EQ(definer, const_let);
}

TEST_F(ParserTest, ParenthesizedUnop) { RoundTripExpr("(!x)", {"x"}); }
TEST_F(ParserTest, ParenthesizedUnop) {
Expr* e = RoundTripExpr("(!x)", {"x"});
EXPECT_EQ(e->span().ToString(), "test.x:1:2-1:4");
}

TEST_F(ParserTest, BitSliceOfCall) { RoundTripExpr("id(x)[0:8]", {"id", "x"}); }

Expand Down

0 comments on commit 7e811c0

Please sign in to comment.