Skip to content

Commit a4d33e7

Browse files
committed
fix: return + bin ops + calls
1 parent 64133ed commit a4d33e7

File tree

8 files changed

+72
-17
lines changed

8 files changed

+72
-17
lines changed

crates/fmt/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ fn format_inner(
140140
let first_result = format_once(config.clone(), compiler, mk_file);
141141

142142
// If first pass was not successful, return the result
143-
// if first_result.is_err() {
144-
return first_result;
145-
// }
143+
if first_result.is_err() {
144+
return first_result;
145+
}
146146
let Some(first_formatted) = first_result.ok_ref() else { return first_result };
147147

148148
// Second pass formatting

crates/fmt/src/state/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ pub(super) struct State<'sess, 'ast> {
133133
single_line_stmt: Option<bool>,
134134
named_call_expr: bool,
135135
binary_expr: Option<BinOpGroup>,
136+
return_bin_expr: bool,
136137
var_init: bool,
137138
fn_body: bool,
138139
call_stack: CallStack,
@@ -220,6 +221,7 @@ impl<'sess> State<'sess, '_> {
220221
single_line_stmt: None,
221222
named_call_expr: false,
222223
binary_expr: None,
224+
return_bin_expr: false,
223225
var_init: false,
224226
fn_body: false,
225227
call_stack: CallStack::default(),

crates/fmt/src/state/sol.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,9 +1273,11 @@ impl<'ast> State<'_, 'ast> {
12731273
&& call_args.len() == 1
12741274
&& !with_single_call_chain_child
12751275
&& callee_fits;
1276-
let without_ind = !all_fits
1276+
let without_ind = (!all_fits
12771277
&& (callee_fits || s.call_stack.first().is_some_and(|c| c.is_chained()))
1278-
&& with_single_call_chain_child;
1278+
&& with_single_call_chain_child)
1279+
// exception for when returning a binary expr that has a call in one of its members
1280+
|| s.return_bin_expr;
12791281

12801282
s.print_call_args(
12811283
call_args,
@@ -1824,6 +1826,7 @@ impl<'ast> State<'_, 'ast> {
18241826
let fits_alone = space_left >= expr_size + 1;
18251827

18261828
if let Some(expr) = expr {
1829+
self.return_bin_expr = matches!(&expr.kind, ast::ExprKind::Binary(..));
18271830
let is_lit_or_ident =
18281831
matches!(&expr.kind, ast::ExprKind::Lit(..) | ast::ExprKind::Ident(..));
18291832
if is_lit_or_ident || (overflows && fits_alone) {
@@ -1848,6 +1851,7 @@ impl<'ast> State<'_, 'ast> {
18481851
}
18491852
self.print_expr(expr);
18501853
self.end();
1854+
self.return_bin_expr = false;
18511855
} else {
18521856
self.print_word("return");
18531857
}
@@ -2616,6 +2620,17 @@ fn is_call_chain(expr_kind: &ast::ExprKind<'_>, must_have_child: bool) -> bool {
26162620
}
26172621
}
26182622

2623+
fn is_call_chain_traverse_bin_ops(expr_kind: &ast::ExprKind<'_>, must_have_child: bool) -> bool {
2624+
match expr_kind {
2625+
ast::ExprKind::Binary(lhs, _, rhs) => {
2626+
is_call_chain_traverse_bin_ops(&lhs.kind, false)
2627+
|| is_call_chain_traverse_bin_ops(&rhs.kind, false)
2628+
}
2629+
ast::ExprKind::Member(child, ..) => is_call_chain_traverse_bin_ops(&child.kind, false),
2630+
_ => !must_have_child && is_call(expr_kind),
2631+
}
2632+
}
2633+
26192634
#[derive(Debug)]
26202635
struct Decision {
26212636
outcome: bool,

crates/fmt/testdata/OperatorExpressions/120.fmt.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,11 @@ function test_nested() {
5858

5959
return spender == ownerOf(tokenId) || getApproved[tokenId] == spender || isApprovedForAll[ownerOf(tokenId)][spender];
6060
}
61+
62+
function new_y(uint256 x, uint256 dx, uint256 x_basis, uint256 y, uint256 y_basis) external pure returns (uint256) {
63+
return _get_y(
64+
x * _VELODROME_TOKEN_BASIS / x_basis,
65+
dx * _VELODROME_TOKEN_BASIS / x_basis,
66+
y * _VELODROME_TOKEN_BASIS / y_basis
67+
) * y_basis / _VELODROME_TOKEN_BASIS * aReallyLongIdentifierThatMakesTheOperatorExpressionBreak;
68+
}

crates/fmt/testdata/OperatorExpressions/fmt.sol

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,18 @@ function test_nested() {
6161
return spender == ownerOf(tokenId) || getApproved[tokenId] == spender
6262
|| isApprovedForAll[ownerOf(tokenId)][spender];
6363
}
64+
65+
function new_y(
66+
uint256 x,
67+
uint256 dx,
68+
uint256 x_basis,
69+
uint256 y,
70+
uint256 y_basis
71+
) external pure returns (uint256) {
72+
return _get_y(
73+
x * _VELODROME_TOKEN_BASIS / x_basis,
74+
dx * _VELODROME_TOKEN_BASIS / x_basis,
75+
y * _VELODROME_TOKEN_BASIS / y_basis
76+
) * y_basis / _VELODROME_TOKEN_BASIS
77+
* aReallyLongIdentifierThatMakesTheOperatorExpressionBreak;
78+
}

crates/fmt/testdata/OperatorExpressions/original.sol

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,15 @@ function test_nested() {
4646
return spender == ownerOf(tokenId) || getApproved[tokenId] == spender
4747
|| isApprovedForAll[ownerOf(tokenId)][spender];
4848
}
49+
50+
function new_y(uint256 x, uint256 dx, uint256 x_basis, uint256 y, uint256 y_basis)
51+
external
52+
pure
53+
returns (uint256)
54+
{
55+
return _get_y(
56+
x * _VELODROME_TOKEN_BASIS / x_basis,
57+
dx * _VELODROME_TOKEN_BASIS / x_basis,
58+
y * _VELODROME_TOKEN_BASIS / y_basis
59+
) * y_basis / _VELODROME_TOKEN_BASIS * aReallyLongIdentifierThatMakesTheOperatorExpressionBreak;
60+
}

crates/fmt/testdata/OperatorExpressions/pow-no-space.fmt.sol

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,18 @@ function test_nested() {
6262
return spender == ownerOf(tokenId) || getApproved[tokenId] == spender
6363
|| isApprovedForAll[ownerOf(tokenId)][spender];
6464
}
65+
66+
function new_y(
67+
uint256 x,
68+
uint256 dx,
69+
uint256 x_basis,
70+
uint256 y,
71+
uint256 y_basis
72+
) external pure returns (uint256) {
73+
return _get_y(
74+
x * _VELODROME_TOKEN_BASIS / x_basis,
75+
dx * _VELODROME_TOKEN_BASIS / x_basis,
76+
y * _VELODROME_TOKEN_BASIS / y_basis
77+
) * y_basis / _VELODROME_TOKEN_BASIS
78+
* aReallyLongIdentifierThatMakesTheOperatorExpressionBreak;
79+
}

crates/fmt/testdata/ReprosCalls/original.sol

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,3 @@ function returnLongBinaryOp() returns (bytes32) {
7878
return
7979
bytes32(uint256(Feature.unwrap(feature)) << 128 | uint256(block.chainid) << 64 | uint256(Nonce.unwrap(nonce)));
8080
}
81-
82-
function new_y(uint256 x, uint256 dx, uint256 x_basis, uint256 y, uint256 y_basis)
83-
external
84-
pure
85-
returns (uint256)
86-
{
87-
return _get_y(
88-
x * _VELODROME_TOKEN_BASIS / x_basis,
89-
dx * _VELODROME_TOKEN_BASIS / x_basis,
90-
y * _VELODROME_TOKEN_BASIS / y_basis
91-
);
92-
}

0 commit comments

Comments
 (0)