summaryrefslogtreecommitdiff
path: root/compiler/rustc_parse_format
diff options
context:
space:
mode:
authorChayim Refael Friedman <chayimfr@gmail.com>2022-02-16 00:38:04 +0000
committerGitHub <noreply@github.com>2022-02-16 07:34:06 +0000
commit91adb6ccd693737aaf740bc18bc6f82e3898d322 (patch)
treeac02a2e2a47eae34e20200b947efa94da3321b20 /compiler/rustc_parse_format
parent1e12aef3fab243407f9d71ba9956cb2a1bf105d5 (diff)
downloadrust-91adb6ccd693737aaf740bc18bc6f82e3898d322.tar.gz
Correctly mark the span of captured arguments in `format_args!()`
It should only include the identifier, or misspelling suggestions will be wrong.
Diffstat (limited to 'compiler/rustc_parse_format')
-rw-r--r--compiler/rustc_parse_format/src/lib.rs16
-rw-r--r--compiler/rustc_parse_format/src/tests.rs4
2 files changed, 12 insertions, 8 deletions
diff --git a/compiler/rustc_parse_format/src/lib.rs b/compiler/rustc_parse_format/src/lib.rs
index 9d653de910f..a6a2cbc277c 100644
--- a/compiler/rustc_parse_format/src/lib.rs
+++ b/compiler/rustc_parse_format/src/lib.rs
@@ -95,7 +95,7 @@ pub enum Position {
/// The argument is located at a specific index given in the format
ArgumentIs(usize),
/// The argument has a name.
- ArgumentNamed(Symbol),
+ ArgumentNamed(Symbol, InnerSpan),
}
impl Position {
@@ -147,7 +147,7 @@ pub enum Count {
/// The count is specified explicitly.
CountIs(usize),
/// The count is specified by the argument with the given name.
- CountIsName(Symbol),
+ CountIsName(Symbol, InnerSpan),
/// The count is specified by the argument at the given index.
CountIsParam(usize),
/// The count is implied and cannot be explicitly specified.
@@ -494,8 +494,11 @@ impl<'a> Parser<'a> {
Some(ArgumentIs(i))
} else {
match self.cur.peek() {
- Some(&(_, c)) if rustc_lexer::is_id_start(c) => {
- Some(ArgumentNamed(Symbol::intern(self.word())))
+ Some(&(start, c)) if rustc_lexer::is_id_start(c) => {
+ let word = self.word();
+ let end = start + word.len();
+ let span = self.to_span_index(start).to(self.to_span_index(end));
+ Some(ArgumentNamed(Symbol::intern(word), span))
}
// This is an `ArgumentNext`.
@@ -662,8 +665,9 @@ impl<'a> Parser<'a> {
if word.is_empty() {
self.cur = tmp;
(CountImplied, None)
- } else if self.consume('$') {
- (CountIsName(Symbol::intern(word)), None)
+ } else if let Some(end) = self.consume_pos('$') {
+ let span = self.to_span_index(start + 1).to(self.to_span_index(end));
+ (CountIsName(Symbol::intern(word), span), None)
} else {
self.cur = tmp;
(CountImplied, None)
diff --git a/compiler/rustc_parse_format/src/tests.rs b/compiler/rustc_parse_format/src/tests.rs
index b7693a85ad9..6c960fdc72b 100644
--- a/compiler/rustc_parse_format/src/tests.rs
+++ b/compiler/rustc_parse_format/src/tests.rs
@@ -221,8 +221,8 @@ fn format_counts() {
fill: None,
align: AlignUnknown,
flags: 0,
- precision: CountIsName(Symbol::intern("b")),
- width: CountIsName(Symbol::intern("a")),
+ precision: CountIsName(Symbol::intern("b"), InnerSpan::new(6, 7)),
+ width: CountIsName(Symbol::intern("a"), InnerSpan::new(4, 4)),
precision_span: None,
width_span: None,
ty: "?",