Skip to content

Commit

Permalink
Only point at inside of string literals if they're actually string li…
Browse files Browse the repository at this point in the history
…terals
  • Loading branch information
estebank committed Jul 23, 2018
1 parent 6bcf877 commit c55a698
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
30 changes: 24 additions & 6 deletions src/libsyntax_ext/format.rs
Expand Up @@ -117,6 +117,8 @@ struct Context<'a, 'b: 'a> {
invalid_refs: Vec<(usize, usize)>,
/// Spans of all the formatting arguments, in order.
arg_spans: Vec<Span>,
/// Wether this formatting string is a literal or it comes from a macro.
is_literal: bool,
}

/// Parses the arguments from the given list of tokens, returning None
Expand Down Expand Up @@ -276,7 +278,11 @@ impl<'a, 'b> Context<'a, 'b> {
/// format string.
fn report_invalid_references(&self, numbered_position_args: bool) {
let mut e;
let sp = MultiSpan::from_spans(self.arg_spans.clone());
let sp = if self.is_literal {
MultiSpan::from_spans(self.arg_spans.clone())
} else {
MultiSpan::from_span(self.fmtsp)
};
let mut refs: Vec<_> = self
.invalid_refs
.iter()
Expand All @@ -294,7 +300,7 @@ impl<'a, 'b> Context<'a, 'b> {
),
);
} else {
let (arg_list, sp) = match refs.len() {
let (arg_list, mut sp) = match refs.len() {
1 => {
let (reg, pos) = refs.pop().unwrap();
(
Expand All @@ -317,11 +323,14 @@ impl<'a, 'b> Context<'a, 'b> {
)
}
};
if !self.is_literal {
sp = MultiSpan::from_span(self.fmtsp);
}

e = self.ecx.mut_span_err(sp,
&format!("invalid reference to positional {} ({})",
arg_list,
self.describe_num_args()));
arg_list,
self.describe_num_args()));
e.note("positional arguments are zero-based");
};

Expand Down Expand Up @@ -370,7 +379,11 @@ impl<'a, 'b> Context<'a, 'b> {
Some(e) => *e,
None => {
let msg = format!("there is no argument named `{}`", name);
let sp = *self.arg_spans.get(self.curpiece).unwrap_or(&self.fmtsp);
let sp = if self.is_literal {
*self.arg_spans.get(self.curpiece).unwrap_or(&self.fmtsp)
} else {
self.fmtsp
};
let mut err = self.ecx.struct_span_err(sp, &msg[..]);
err.emit();
return;
Expand Down Expand Up @@ -721,7 +734,7 @@ pub fn expand_format_args<'cx>(ecx: &'cx mut ExtCtxt,

pub fn expand_format_args_nl<'cx>(
ecx: &'cx mut ExtCtxt,
mut sp: Span,
mut sp: Span,
tts: &[tokenstream::TokenTree],
) -> Box<dyn base::MacResult + 'cx> {
//if !ecx.ecfg.enable_allow_internal_unstable() {
Expand Down Expand Up @@ -784,6 +797,10 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
return DummyResult::raw_expr(sp);
}
};
let is_literal = match ecx.codemap().span_to_snippet(fmt_sp) {
Ok(ref s) if s.starts_with("\"") || s.starts_with("r#") => true,
_ => false,
};

let mut cx = Context {
ecx,
Expand All @@ -806,6 +823,7 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
fmtsp: fmt.span,
invalid_refs: Vec::new(),
arg_spans: Vec::new(),
is_literal,
};

let fmt_str = &*fmt.node.0.as_str();
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/macros/macro-backtrace-println.stderr
@@ -1,8 +1,8 @@
error: 1 positional argument in format string, but no arguments were given
--> $DIR/macro-backtrace-println.rs:24:31
--> $DIR/macro-backtrace-println.rs:24:30
|
LL | ($fmt:expr) => (myprint!(concat!($fmt, "/n"))); //~ ERROR no arguments were given
| ^^
| ^^^^^^^^^^^^^^^^^^^
...
LL | myprintln!("{}");
| ----------------- in this macro invocation
Expand Down

0 comments on commit c55a698

Please sign in to comment.