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

transpile: parenthesize subexpression of reference expressions if necessary #1121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions c2rust-ast-builder/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,13 +985,13 @@ impl Builder {
}

pub fn addr_of_expr(self, e: Box<Expr>) -> Box<Expr> {
Box::new(Expr::Reference(ExprReference {
Box::new(parenthesize_if_necessary(Expr::Reference(ExprReference {
attrs: self.attrs,
and_token: Token![&](self.span),
raw: Default::default(),
mutability: self.mutbl.to_token(),
expr: e,
}))
})))
}

pub fn mac_expr(self, mac: Macro) -> Box<Expr> {
Expand Down Expand Up @@ -2280,7 +2280,7 @@ fn expr_precedence(e: &Expr) -> u8 {
Expr::Field(_ef) => 16,
Expr::Call(_) | Expr::Index(_) => 15,
Expr::Try(_et) => 14,
Expr::Unary(_eu) => 13,
Expr::Unary(_) | Expr::Reference(_) => 13,
Expr::Cast(_ec) => 12,
Expr::Binary(eb) => 2 + binop_precedence(&eb.op),
Expr::Assign(_) | Expr::AssignOp(_) => 1,
Expand Down Expand Up @@ -2376,6 +2376,9 @@ fn parenthesize_if_necessary(mut outer: Expr) -> Expr {
Expr::Unary(ref mut eu) => {
parenthesize_if_gt(&mut eu.expr);
}
Expr::Reference(ref mut er) => {
parenthesize_if_gt(&mut er.expr);
}
Expr::Binary(ref mut eb) => {
parenthesize_if_gt(&mut eb.left);
// Because binops associate right, parenthesize same-precedence RHS
Expand Down
Loading