Skip to content

Commit 62187b1

Browse files
committed
Add diagnostic width span when '0$' is used as width.
1 parent 29c5a02 commit 62187b1

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

compiler/rustc_parse_format/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,10 @@ impl<'a> Parser<'a> {
572572
// '0' flag and then an ill-formatted format string with just a '$'
573573
// and no count, but this is better if we instead interpret this as
574574
// no '0' flag and '0$' as the width instead.
575-
if self.consume('$') {
575+
if let Some(end) = self.consume_pos('$') {
576576
spec.width = CountIsParam(0);
577577
havewidth = true;
578+
spec.width_span = Some(self.to_span_index(end - 1).to(self.to_span_index(end + 1)));
578579
} else {
579580
spec.flags |= 1 << (FlagSignAwareZeroPad as u32);
580581
}

compiler/rustc_parse_format/src/tests.rs

+17
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,23 @@ fn format_counts() {
178178
},
179179
})],
180180
);
181+
same(
182+
"{1:0$.10x}",
183+
&[NextArgument(Argument {
184+
position: ArgumentIs(1),
185+
format: FormatSpec {
186+
fill: None,
187+
align: AlignUnknown,
188+
flags: 0,
189+
precision: CountIs(10),
190+
width: CountIsParam(0),
191+
precision_span: None,
192+
width_span: Some(InnerSpan::new(4, 6)),
193+
ty: "x",
194+
ty_span: None,
195+
},
196+
})],
197+
);
181198
same(
182199
"{:.*x}",
183200
&[NextArgument(Argument {

0 commit comments

Comments
 (0)