Skip to content

Tweak spans providing type context on errors when involving macros #145227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) {
match (self.tcx.parent_hir_node(expr.hir_id), error) {
(hir::Node::LetStmt(hir::LetStmt { ty: Some(ty), init: Some(init), .. }), _)
if init.hir_id == expr.hir_id =>
if init.hir_id == expr.hir_id && !ty.span.source_equal(init.span) =>
{
// Point at `let` assignment type.
err.span_label(ty.span, "expected due to this");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
span,
format!("this is an iterator with items of type `{}`", args.type_at(0)),
);
} else {
} else if !span.overlaps(cause.span) {
let expected_ty = self.tcx.short_string(expected_ty, err.long_ty_path());
err.span_label(span, format!("this expression has type `{expected_ty}`"));
}
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/proc-macro/auxiliary/match-expander.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro]
pub fn matcher(input: TokenStream) -> TokenStream {
"
struct S(());
let s = S(());
match s {
true => {}
_ => {}
}
".parse().unwrap()
}
12 changes: 12 additions & 0 deletions tests/ui/proc-macro/match-expander.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@ proc-macro: match-expander.rs
// Ensure that we don't point at macro invocation when providing inference contexts.

#[macro_use]
extern crate match_expander;

fn main() {
match_expander::matcher!();
//~^ ERROR: mismatched types
//~| NOTE: expected `S`, found `bool`
//~| NOTE: in this expansion of match_expander::matcher!
}
11 changes: 11 additions & 0 deletions tests/ui/proc-macro/match-expander.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0308]: mismatched types
--> $DIR/match-expander.rs:8:5
|
LL | match_expander::matcher!();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `S`, found `bool`
|
= note: this error originates in the macro `match_expander::matcher` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ LL | quote!($($nonrep $nonrep)*);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
| expected due to this
| here the type of `has_iter` is inferred to be `ThereIsNoIteratorInRepetition`

error: aborting due to 1 previous error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ LL | quote!($($nonrep)*);
| ^^^^^^^^^^^^^^^^^^^
| |
| expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
| expected due to this
| here the type of `has_iter` is inferred to be `ThereIsNoIteratorInRepetition`

error: aborting due to 1 previous error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ error[E0308]: mismatched types
--> $DIR/does-not-have-iter-separated.rs:8:5
|
LL | quote!($(a b),*);
| ^^^^^^^^^^^^^^^^
| |
| expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
| expected due to this
| ^^^^^^^^^^^^^^^^ expected `HasIterator`, found `ThereIsNoIteratorInRepetition`

error: aborting due to 1 previous error

Expand Down
5 changes: 1 addition & 4 deletions tests/ui/proc-macro/quote/does-not-have-iter.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ error[E0308]: mismatched types
--> $DIR/does-not-have-iter.rs:8:5
|
LL | quote!($(a b)*);
| ^^^^^^^^^^^^^^^
| |
| expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
| expected due to this
| ^^^^^^^^^^^^^^^ expected `HasIterator`, found `ThereIsNoIteratorInRepetition`

error: aborting due to 1 previous error

Expand Down
Loading