Skip to content

Commit 7acf9c5

Browse files
Fix zero-length dbg arg list
1 parent 8deca25 commit 7acf9c5

File tree

7 files changed

+139
-77
lines changed

7 files changed

+139
-77
lines changed

crates/compiler/fmt/src/expr.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,6 @@ fn format_expr_only(
9191
buf.indent(indent);
9292
buf.push_str("try");
9393
}
94-
Expr::PncApply(
95-
loc_expr @ Loc {
96-
value: Expr::Dbg, ..
97-
},
98-
loc_args,
99-
) => {
100-
fmt_apply(loc_expr, loc_args.items, indent, buf);
101-
}
10294
Expr::PncApply(loc_expr, loc_args) => {
10395
fmt_pnc_apply(loc_expr, loc_args, indent, buf);
10496
}
@@ -1763,12 +1755,21 @@ fn fmt_dbg_stmt<'a>(
17631755
args.push(condition);
17641756
args.extend_from_slice(extra_args);
17651757

1766-
Expr::Apply(
1767-
&Loc::at_zero(Expr::Dbg),
1768-
args.into_bump_slice(),
1769-
called_via::CalledVia::Space,
1770-
)
1771-
.format_with_options(buf, parens, Newlines::Yes, indent);
1758+
if args.is_empty() {
1759+
Expr::PncApply(&Loc::at_zero(Expr::Dbg), Collection::empty()).format_with_options(
1760+
buf,
1761+
parens,
1762+
Newlines::Yes,
1763+
indent,
1764+
);
1765+
} else {
1766+
Expr::Apply(
1767+
&Loc::at_zero(Expr::Dbg),
1768+
args.into_bump_slice(),
1769+
called_via::CalledVia::Space,
1770+
)
1771+
.format_with_options(buf, parens, Newlines::Yes, indent);
1772+
}
17721773

17731774
let cont_lifted = expr_lift_spaces(Parens::NotNeeded, buf.text.bump(), &continuation.value);
17741775

crates/compiler/parse/src/expr.rs

+47-39
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,7 @@ fn loc_term<'a>() -> impl Parser<'a, Loc<Expr<'a>>, EExpr<'a>> {
242242
let mut e = expr;
243243
let orig_region = e.region;
244244
for (args_loc, maybe_suffixes) in arg_locs_with_suffixes_vec.iter() {
245-
let value = if matches!(
246-
e,
247-
Loc {
248-
value: Expr::Dbg,
249-
..
250-
}
251-
) {
252-
Expr::Apply(arena.alloc(e), args_loc.value.items, CalledVia::Space)
253-
} else if let Some(suffixes) = maybe_suffixes {
245+
let value = if let Some(suffixes) = maybe_suffixes {
254246
apply_expr_access_chain(
255247
arena,
256248
Expr::PncApply(arena.alloc(e), args_loc.value),
@@ -3150,49 +3142,65 @@ fn stmts_to_defs<'a>(
31503142
_,
31513143
) = e
31523144
{
3153-
let condition = &args[0];
3154-
let rest = stmts_to_expr(&stmts[i + 1..], arena)?;
3155-
let e = Expr::DbgStmt {
3156-
first: condition,
3157-
extra_args: &args[1..],
3158-
continuation: arena.alloc(rest),
3159-
};
3145+
if let Some((first, extra_args)) = args.split_first() {
3146+
let rest = stmts_to_expr(&stmts[i + 1..], arena)?;
3147+
let e = Expr::DbgStmt {
3148+
first,
3149+
extra_args,
3150+
continuation: arena.alloc(rest),
3151+
};
31603152

3161-
let e = if sp_stmt.before.is_empty() {
3162-
e
3163-
} else {
3164-
arena.alloc(e).before(sp_stmt.before)
3165-
};
3153+
let e = if sp_stmt.before.is_empty() {
3154+
e
3155+
} else {
3156+
arena.alloc(e).before(sp_stmt.before)
3157+
};
31663158

3167-
last_expr = Some(Loc::at(sp_stmt.item.region, e));
3159+
last_expr = Some(Loc::at(sp_stmt.item.region, e));
31683160

3169-
// don't re-process the rest of the statements; they got consumed by the dbg expr
3170-
break;
3161+
// don't re-process the rest of the statements; they got consumed by the dbg expr
3162+
break;
3163+
} else {
3164+
defs.push_value_def(
3165+
ValueDef::Stmt(arena.alloc(Loc::at(sp_stmt.item.region, e))),
3166+
sp_stmt.item.region,
3167+
sp_stmt.before,
3168+
&[],
3169+
);
3170+
}
31713171
} else if let Expr::PncApply(
31723172
Loc {
31733173
value: Expr::Dbg, ..
31743174
},
31753175
args,
31763176
) = e
31773177
{
3178-
let condition = &args.items[0];
3179-
let rest = stmts_to_expr(&stmts[i + 1..], arena)?;
3180-
let e = Expr::DbgStmt {
3181-
first: condition,
3182-
extra_args: &args.items[1..],
3183-
continuation: arena.alloc(rest),
3184-
};
3178+
if let Some((first, extra_args)) = args.items.split_first() {
3179+
let rest = stmts_to_expr(&stmts[i + 1..], arena)?;
3180+
let e = Expr::DbgStmt {
3181+
first,
3182+
extra_args,
3183+
continuation: arena.alloc(rest),
3184+
};
31853185

3186-
let e = if sp_stmt.before.is_empty() {
3187-
e
3188-
} else {
3189-
arena.alloc(e).before(sp_stmt.before)
3190-
};
3186+
let e = if sp_stmt.before.is_empty() {
3187+
e
3188+
} else {
3189+
arena.alloc(e).before(sp_stmt.before)
3190+
};
31913191

3192-
last_expr = Some(Loc::at(sp_stmt.item.region, e));
3192+
last_expr = Some(Loc::at(sp_stmt.item.region, e));
31933193

3194-
// don't re-process the rest of the statements; they got consumed by the dbg expr
3195-
break;
3194+
// don't re-process the rest of the statements; they got consumed by the dbg expr
3195+
break;
3196+
} else {
3197+
defs.push_value_def(
3198+
ValueDef::Stmt(arena.alloc(Loc::at(sp_stmt.item.region, e))),
3199+
sp_stmt.item.region,
3200+
sp_stmt.before,
3201+
&[],
3202+
);
3203+
}
31963204
} else {
31973205
defs.push_value_def(
31983206
ValueDef::Stmt(arena.alloc(Loc::at(sp_stmt.item.region, e))),

crates/compiler/parse/src/normalize.rs

+32-24
Original file line numberDiff line numberDiff line change
@@ -822,19 +822,23 @@ fn fold_defs<'a>(
822822
),
823823
..
824824
}) => {
825-
let rest = fold_defs(arena, defs, final_expr);
826-
let new_final = Expr::DbgStmt {
827-
first: args[0],
828-
extra_args: &args[1..],
829-
continuation: arena.alloc(Loc::at_zero(rest)),
830-
};
831-
if new_defs.is_empty() {
832-
return new_final;
825+
if let Some((first, extra_args)) = args.split_first() {
826+
let rest = fold_defs(arena, defs, final_expr);
827+
let new_final = Expr::DbgStmt {
828+
first,
829+
extra_args,
830+
continuation: arena.alloc(Loc::at_zero(rest)),
831+
};
832+
if new_defs.is_empty() {
833+
return new_final;
834+
}
835+
return Expr::Defs(
836+
arena.alloc(new_defs),
837+
arena.alloc(Loc::at_zero(new_final)),
838+
);
839+
} else {
840+
new_defs.push_value_def(vd, Region::zero(), &[], &[]);
833841
}
834-
return Expr::Defs(
835-
arena.alloc(new_defs),
836-
arena.alloc(Loc::at_zero(new_final)),
837-
);
838842
}
839843
ValueDef::Stmt(&Loc {
840844
value:
@@ -846,19 +850,23 @@ fn fold_defs<'a>(
846850
),
847851
..
848852
}) => {
849-
let rest = fold_defs(arena, defs, final_expr);
850-
let new_final = Expr::DbgStmt {
851-
first: args.items[0],
852-
extra_args: &args.items[1..],
853-
continuation: arena.alloc(Loc::at_zero(rest)),
854-
};
855-
if new_defs.is_empty() {
856-
return new_final;
853+
if let Some((first, extra_args)) = args.items.split_first() {
854+
let rest = fold_defs(arena, defs, final_expr);
855+
let new_final = Expr::DbgStmt {
856+
first,
857+
extra_args,
858+
continuation: arena.alloc(Loc::at_zero(rest)),
859+
};
860+
if new_defs.is_empty() {
861+
return new_final;
862+
}
863+
return Expr::Defs(
864+
arena.alloc(new_defs),
865+
arena.alloc(Loc::at_zero(new_final)),
866+
);
867+
} else {
868+
new_defs.push_value_def(vd, Region::zero(), &[], &[]);
857869
}
858-
return Expr::Defs(
859-
arena.alloc(new_defs),
860-
arena.alloc(Loc::at_zero(new_final)),
861-
);
862870
}
863871
_ => {
864872
new_defs.push_value_def(vd, Region::zero(), &[], &[]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dbg()
2+
d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
@0-7 SpaceAfter(
2+
Defs(
3+
Defs {
4+
tags: [
5+
EitherIndex(2147483648),
6+
],
7+
regions: [
8+
@0-5,
9+
],
10+
space_before: [
11+
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
12+
],
13+
space_after: [
14+
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
15+
],
16+
spaces: [],
17+
type_defs: [],
18+
value_defs: [
19+
Stmt(
20+
@0-5 PncApply(
21+
@0-3 Dbg,
22+
[],
23+
),
24+
),
25+
],
26+
},
27+
@6-7 SpaceBefore(
28+
Var {
29+
module_name: "",
30+
ident: "d",
31+
},
32+
[
33+
Newline,
34+
],
35+
),
36+
),
37+
[
38+
Newline,
39+
],
40+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dbg()
2+
d

crates/compiler/test_syntax/tests/test_snapshots.rs

+1
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ mod test_snapshots {
398398
pass/dbg_extra_parens.expr,
399399
pass/dbg_newline_apply.expr,
400400
pass/dbg_pnc_a_over_a.expr,
401+
pass/dbg_pnc_zero_args.expr,
401402
pass/dbg_stmt.expr,
402403
pass/dbg_stmt_in_parens.expr,
403404
pass/dbg_stmt_multiline.expr,

0 commit comments

Comments
 (0)