Skip to content

Commit 44c9ece

Browse files
authored
Rollup merge of #145227 - estebank:tweak-inference-span, r=joshtriplett
Tweak spans providing type context on errors when involving macros Do not point at macro invocation multiple times when we try to add span labels mentioning what type each expression has, which is unnecessary when the error is at a macro invocation.
2 parents 2f1d61a + 48816e7 commit 44c9ece

File tree

9 files changed

+42
-12
lines changed

9 files changed

+42
-12
lines changed

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
698698
) {
699699
match (self.tcx.parent_hir_node(expr.hir_id), error) {
700700
(hir::Node::LetStmt(hir::LetStmt { ty: Some(ty), init: Some(init), .. }), _)
701-
if init.hir_id == expr.hir_id =>
701+
if init.hir_id == expr.hir_id && !ty.span.source_equal(init.span) =>
702702
{
703703
// Point at `let` assignment type.
704704
err.span_label(ty.span, "expected due to this");

compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
459459
span,
460460
format!("this is an iterator with items of type `{}`", args.type_at(0)),
461461
);
462-
} else {
462+
} else if !span.overlaps(cause.span) {
463463
let expected_ty = self.tcx.short_string(expected_ty, err.long_ty_path());
464464
err.span_label(span, format!("this expression has type `{expected_ty}`"));
465465
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extern crate proc_macro;
2+
3+
use proc_macro::TokenStream;
4+
5+
#[proc_macro]
6+
pub fn matcher(input: TokenStream) -> TokenStream {
7+
"
8+
struct S(());
9+
let s = S(());
10+
match s {
11+
true => {}
12+
_ => {}
13+
}
14+
".parse().unwrap()
15+
}

tests/ui/proc-macro/match-expander.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ proc-macro: match-expander.rs
2+
// Ensure that we don't point at macro invocation when providing inference contexts.
3+
4+
#[macro_use]
5+
extern crate match_expander;
6+
7+
fn main() {
8+
match_expander::matcher!();
9+
//~^ ERROR: mismatched types
10+
//~| NOTE: expected `S`, found `bool`
11+
//~| NOTE: in this expansion of match_expander::matcher!
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/match-expander.rs:8:5
3+
|
4+
LL | match_expander::matcher!();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `S`, found `bool`
6+
|
7+
= note: this error originates in the macro `match_expander::matcher` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0308`.

tests/ui/proc-macro/quote/does-not-have-iter-interpolated-dup.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ LL | quote!($($nonrep $nonrep)*);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
| |
77
| expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
8-
| expected due to this
98
| here the type of `has_iter` is inferred to be `ThereIsNoIteratorInRepetition`
109

1110
error: aborting due to 1 previous error

tests/ui/proc-macro/quote/does-not-have-iter-interpolated.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ LL | quote!($($nonrep)*);
55
| ^^^^^^^^^^^^^^^^^^^
66
| |
77
| expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
8-
| expected due to this
98
| here the type of `has_iter` is inferred to be `ThereIsNoIteratorInRepetition`
109

1110
error: aborting due to 1 previous error

tests/ui/proc-macro/quote/does-not-have-iter-separated.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/does-not-have-iter-separated.rs:8:5
33
|
44
LL | quote!($(a b),*);
5-
| ^^^^^^^^^^^^^^^^
6-
| |
7-
| expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
8-
| expected due to this
5+
| ^^^^^^^^^^^^^^^^ expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
96

107
error: aborting due to 1 previous error
118

tests/ui/proc-macro/quote/does-not-have-iter.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/does-not-have-iter.rs:8:5
33
|
44
LL | quote!($(a b)*);
5-
| ^^^^^^^^^^^^^^^
6-
| |
7-
| expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
8-
| expected due to this
5+
| ^^^^^^^^^^^^^^^ expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
96

107
error: aborting due to 1 previous error
118

0 commit comments

Comments
 (0)