Skip to content

Fix invalid suggestion from type error for derive macro #137464

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 1 commit into from
Feb 25, 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
14 changes: 9 additions & 5 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use rustc_middle::ty::{
};
use rustc_session::errors::ExprParenthesesNeeded;
use rustc_span::source_map::Spanned;
use rustc_span::{Ident, Span, Symbol, sym};
use rustc_span::{ExpnKind, Ident, MacroKind, Span, Symbol, sym};
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
use rustc_trait_selection::error_reporting::traits::DefIdOrName;
use rustc_trait_selection::infer::InferCtxtExt;
Expand Down Expand Up @@ -1365,6 +1365,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.param_env,
ty::TraitRef::new(self.tcx, into_def_id, [expr_ty, expected_ty]),
))
&& !expr
.span
.macro_backtrace()
.any(|x| matches!(x.kind, ExpnKind::Macro(MacroKind::Attr | MacroKind::Derive, ..)))
{
let span = expr.span.find_oldest_ancestor_in_same_ctxt();

Expand All @@ -1380,10 +1384,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
sugg.insert(0, (expr.span.shrink_to_lo(), format!("{}: ", name)));
}
diag.multipart_suggestion(
format!("call `Into::into` on this expression to convert `{expr_ty}` into `{expected_ty}`"),
sugg,
Applicability::MaybeIncorrect
);
format!("call `Into::into` on this expression to convert `{expr_ty}` into `{expected_ty}`"),
sugg,
Applicability::MaybeIncorrect
);
return true;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/ui/typeck/auxiliary/derive-demo-issue-136343.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro_derive(Sample)]
pub fn sample(_: TokenStream) -> TokenStream {
"fn bad<T: Into<U>, U>(a: T) -> U { a }".parse().unwrap()
}
9 changes: 9 additions & 0 deletions tests/ui/typeck/invalid-sugg-for-derive-macro-issue-136343.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@ proc-macro: derive-demo-issue-136343.rs

#[macro_use]
extern crate derive_demo_issue_136343;

#[derive(Sample)] //~ ERROR mismatched types
struct Test;

fn main() {}
19 changes: 19 additions & 0 deletions tests/ui/typeck/invalid-sugg-for-derive-macro-issue-136343.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0308]: mismatched types
--> $DIR/invalid-sugg-for-derive-macro-issue-136343.rs:6:10
|
LL | #[derive(Sample)]
| ^^^^^^
| |
| expected type parameter `U`, found type parameter `T`
| expected `U` because of return type
|
= note: expected type parameter `U`
found type parameter `T`
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
= note: the caller chooses a type for `U` which can be different from `T`
= note: this error originates in the derive macro `Sample` (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`.
Loading