Skip to content

Commit 5226b6d

Browse files
authored
Merge pull request #8224 from JRI98/fix-multiline-strings-fmt
Fix fuzz crashes related multiline strings by adding a newline after them
2 parents ead43c9 + ee812ce commit 5226b6d

23 files changed

Lines changed: 127 additions & 88 deletions

src/fmt/fmt.zig

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -719,16 +719,7 @@ const Formatter = struct {
719719
}
720720
_ = try formatter(fmt, item_idx);
721721
if (multiline) {
722-
// special case for multiline_strings
723-
var node = fmt.ast.store.nodes.get(@enumFromInt(@intFromEnum(item_idx)));
724-
if (node.tag == .record_field) {
725-
const field = fmt.ast.store.getRecordField(@enumFromInt(@intFromEnum(item_idx)));
726-
if (field.value) |v| {
727-
node = fmt.ast.store.nodes.get(@enumFromInt(@intFromEnum(v)));
728-
}
729-
}
730-
if (node.tag == .multiline_string) {
731-
try fmt.ensureNewline();
722+
if (fmt.has_newline) {
732723
try fmt.pushIndent();
733724
}
734725
try fmt.push(',');
@@ -853,6 +844,7 @@ const Formatter = struct {
853844
},
854845
}
855846
}
847+
try fmt.ensureNewline();
856848
},
857849
.single_quote => |s| {
858850
try fmt.pushTokenText(s.token);
@@ -946,14 +938,8 @@ const Formatter = struct {
946938
}
947939
const field_region = try fmt.formatRecordField(field_idx);
948940
if (multiline) {
949-
const field = fmt.ast.store.getRecordField(field_idx);
950-
if (field.value) |v| {
951-
const node = fmt.ast.store.nodes.get(@enumFromInt(@intFromEnum(v)));
952-
// special case for multiline_strings
953-
if (node.tag == .multiline_string) {
954-
try fmt.ensureNewline();
955-
try fmt.pushIndent();
956-
}
941+
if (fmt.has_newline) {
942+
try fmt.pushIndent();
957943
}
958944
try fmt.push(',');
959945
_ = try fmt.flushCommentsAfter(field_region.end);
@@ -1931,7 +1917,7 @@ const Formatter = struct {
19311917

19321918
fn flushComments(fmt: *Formatter, between_text: []const u8) !bool {
19331919
var found_comment = false;
1934-
var newline_count: usize = 0;
1920+
var newline_count: usize = if (fmt.has_newline) 1 else 0;
19351921
var i: usize = 0;
19361922
while (i < between_text.len) {
19371923
if (between_text[i] == '#') {

test/snapshots/can_import_nested_modules.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,6 @@ import json.Parser
508508
import http.Client
509509
510510
511-
512511
# Test multi-level type qualification
513512
parseConfig : Config.Settings -> Str
514513
parseConfig = |settings| Config.toString(settings)

test/snapshots/fuzz_crash/fuzz_crash_009.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ StringStart(6:5-6:6),StringPart(6:6-6:12),EndOfFile(6:12-6:12),
125125
~~~roc
126126
127127
128-
129128
foo =
130129
131130
"onmo %"

test/snapshots/fuzz_crash/fuzz_crash_021.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ UpperIdent(3:1-3:5),NoSpaceOpenRound(3:5-3:6),LowerIdent(3:6-3:7),Comma(3:7-3:8)
168168
# FORMATTED
169169
~~~roc
170170
171-
172171
<malformed> :
173172
~~~
174173
# CANONICALIZE

test/snapshots/fuzz_crash/fuzz_crash_022.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ OpUnaryMinus(8:1-8:2),LowerIdent(8:2-8:6),OpAssign(8:7-8:8),OpBar(8:9-8:10),Unde
194194
~~~roc
195195
f :
196196
197-
198197
UserId : U64
199198
200199
ser : UserId -> Str

test/snapshots/fuzz_crash/fuzz_crash_024.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ KwVar(7:1-7:4),LowerIdent(7:5-7:6),OpAssign(7:6-7:7),Int(7:8-7:9),EndOfFile(7:9-
208208
module []
209209
pf :
210210
211-
212211
# el
213212
t =
214213

test/snapshots/fuzz_crash/fuzz_crash_025.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,10 @@ b = 65535
247247
c : U32
248248
c = 429496729
249249
250-
251250
e : U128
252251
e = 3402823669209384634633746074317682114553.14
253252
254253
255-
256254
g : I16
257255
g = -32768
258256
-1 Bytes
Binary file not shown.

test/snapshots/fuzz_crash/fuzz_crash_027.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,8 +1659,8 @@ main! = |_| { # Yeah Ie
16591659
tag_with = Ok(number)
16601660
ited = "Hello, ${world}"
16611661
list = [
1662-
, # afarg
1663-
,
1662+
, # afarg
1663+
,
16641664
456, # ee
16651665
]
16661666
for n in list {

test/snapshots/fuzz_crash/fuzz_crash_031.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ LowerIdent(4:1-4:6),LowerIdent(4:7-4:8),OpAssign(4:8-4:9),MalformedSingleQuoteUn
108108
# FORMATTED
109109
~~~roc
110110
111-
112111
# el
113112
t =
114113
~~~

0 commit comments

Comments
 (0)