Skip to content

Commit bef8fbd

Browse files
authored
Rollup merge of #42368 - estebank:call-site, r=nikomatsakis
Use callsite's span for macro calls on suggestion When suggesting an appropriate mutability for a macro call, use the call span instead of the expanded macro's span. ``` error[E0308]: mismatched types --> $DIR/coerce-suggestions.rs:48:9 | 48 | s = format!("foo"); | ^^^^^^^^^^^^^^ expected mutable reference, found struct `std::string::String` | = note: expected type `&mut std::string::String` found type `std::string::String` = help: try with `&mut format!("foo")` = note: this error originates in a macro outside of the current crate ``` Fix #41858.
2 parents 25bbbb3 + 4142d7b commit bef8fbd

File tree

6 files changed

+36
-7
lines changed

6 files changed

+36
-7
lines changed

src/librustc_errors/emitter.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -705,11 +705,9 @@ impl EmitterWriter {
705705
if *sp == DUMMY_SP {
706706
continue;
707707
}
708-
if cm.span_to_filename(sp.clone()).contains("macros>") {
709-
let v = sp.macro_backtrace();
710-
if let Some(use_site) = v.last() {
711-
before_after.push((sp.clone(), use_site.call_site.clone()));
712-
}
708+
let call_sp = cm.call_span_if_macro(*sp);
709+
if call_sp != *sp {
710+
before_after.push((sp.clone(), call_sp));
713711
}
714712
for trace in sp.macro_backtrace().iter().rev() {
715713
// Only show macro locations that are local

src/librustc_errors/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub trait CodeMapper {
102102
fn span_to_string(&self, sp: Span) -> String;
103103
fn span_to_filename(&self, sp: Span) -> FileName;
104104
fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span>;
105+
fn call_span_if_macro(&self, sp: Span) -> Span;
105106
}
106107

107108
impl CodeSuggestion {

src/librustc_typeck/check/demand.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use syntax_pos::{self, Span};
1818
use rustc::hir;
1919
use rustc::hir::def::Def;
2020
use rustc::ty::{self, Ty, AssociatedItem};
21-
use errors::DiagnosticBuilder;
21+
use errors::{DiagnosticBuilder, CodeMapper};
2222

2323
use super::method::probe;
2424

@@ -187,7 +187,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
187187
checked_ty),
188188
};
189189
if self.can_coerce(ref_ty, expected) {
190-
if let Ok(src) = self.tcx.sess.codemap().span_to_snippet(expr.span) {
190+
// Use the callsite's span if this is a macro call. #41858
191+
let sp = self.sess().codemap().call_span_if_macro(expr.span);
192+
if let Ok(src) = self.tcx.sess.codemap().span_to_snippet(sp) {
191193
return Some(format!("try with `{}{}`",
192194
match mutability.mutbl {
193195
hir::Mutability::MutMutable => "&mut ",

src/libsyntax/codemap.rs

+9
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,15 @@ impl CodeMapper for CodeMap {
563563
fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span> {
564564
self.merge_spans(sp_lhs, sp_rhs)
565565
}
566+
fn call_span_if_macro(&self, sp: Span) -> Span {
567+
if self.span_to_filename(sp.clone()).contains("macros>") {
568+
let v = sp.macro_backtrace();
569+
if let Some(use_site) = v.last() {
570+
return use_site.call_site;
571+
}
572+
}
573+
sp
574+
}
566575
}
567576

568577
#[derive(Clone)]

src/test/ui/span/coerce-suggestions.rs

+8
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ fn main() {
4343
//~| NOTE cyclic type of infinite size
4444
//~| NOTE expected type `_`
4545
//~| NOTE found type `Box<_>`
46+
47+
let s = &mut String::new();
48+
s = format!("foo");
49+
//~^ ERROR E0308
50+
//~| NOTE expected mutable reference, found struct `std::string::String`
51+
//~| NOTE expected type `&mut std::string::String`
52+
//~| HELP try with `&mut format!("foo")`
53+
//~| NOTE this error originates in a macro outside of the current crate
4654
}

src/test/ui/span/coerce-suggestions.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,16 @@ error[E0308]: mismatched types
4747
= note: expected type `_`
4848
found type `std::boxed::Box<_>`
4949

50+
error[E0308]: mismatched types
51+
--> $DIR/coerce-suggestions.rs:48:9
52+
|
53+
48 | s = format!("foo");
54+
| ^^^^^^^^^^^^^^ expected mutable reference, found struct `std::string::String`
55+
|
56+
= note: expected type `&mut std::string::String`
57+
found type `std::string::String`
58+
= help: try with `&mut format!("foo")`
59+
= note: this error originates in a macro outside of the current crate
60+
5061
error: aborting due to previous error(s)
5162

0 commit comments

Comments
 (0)