Skip to content

Commit 11959a8

Browse files
committed
Fix invalid suggestion from type error for derive macro
1 parent 0769736 commit 11959a8

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_middle::ty::{
2323
};
2424
use rustc_session::errors::ExprParenthesesNeeded;
2525
use rustc_span::source_map::Spanned;
26-
use rustc_span::{Ident, Span, Symbol, sym};
26+
use rustc_span::{ExpnKind, Ident, MacroKind, Span, Symbol, sym};
2727
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
2828
use rustc_trait_selection::error_reporting::traits::DefIdOrName;
2929
use rustc_trait_selection::infer::InferCtxtExt;
@@ -1365,6 +1365,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13651365
self.param_env,
13661366
ty::TraitRef::new(self.tcx, into_def_id, [expr_ty, expected_ty]),
13671367
))
1368+
&& !expr
1369+
.span
1370+
.macro_backtrace()
1371+
.any(|x| matches!(x.kind, ExpnKind::Macro(MacroKind::Attr | MacroKind::Derive, ..)))
13681372
{
13691373
let span = expr.span.find_oldest_ancestor_in_same_ctxt();
13701374

@@ -1380,10 +1384,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13801384
sugg.insert(0, (expr.span.shrink_to_lo(), format!("{}: ", name)));
13811385
}
13821386
diag.multipart_suggestion(
1383-
format!("call `Into::into` on this expression to convert `{expr_ty}` into `{expected_ty}`"),
1384-
sugg,
1385-
Applicability::MaybeIncorrect
1386-
);
1387+
format!("call `Into::into` on this expression to convert `{expr_ty}` into `{expected_ty}`"),
1388+
sugg,
1389+
Applicability::MaybeIncorrect
1390+
);
13871391
return true;
13881392
}
13891393

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extern crate proc_macro;
2+
use proc_macro::TokenStream;
3+
4+
#[proc_macro_derive(Sample)]
5+
pub fn sample(_: TokenStream) -> TokenStream {
6+
"fn bad<T: Into<U>, U>(a: T) -> U { a }".parse().unwrap()
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ proc-macro: derive-demo-issue-136343.rs
2+
3+
#[macro_use]
4+
extern crate derive_demo_issue_136343;
5+
6+
#[derive(Sample)] //~ ERROR mismatched types
7+
struct Test;
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/invalid-sugg-for-derive-macro-issue-136343.rs:6:10
3+
|
4+
LL | #[derive(Sample)]
5+
| ^^^^^^
6+
| |
7+
| expected type parameter `U`, found type parameter `T`
8+
| expected `U` because of return type
9+
|
10+
= note: expected type parameter `U`
11+
found type parameter `T`
12+
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
13+
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
14+
= note: the caller chooses a type for `U` which can be different from `T`
15+
= note: this error originates in the derive macro `Sample` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
17+
error: aborting due to 1 previous error
18+
19+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)