about summary refs log tree commit diff
diff options
context:
space:
mode:
authoryukang <moorekang@gmail.com>2025-02-23 14:44:27 +0800
committeryukang <moorekang@gmail.com>2025-02-23 14:44:27 +0800
commit11959a8b6e75d2c55500a703070a248342d29549 (patch)
treeeba4b45a90d44ab224b32227a9c54af9c4bd02bc
parent07697360aee0cebcb4e304236ba1884d8dde5469 (diff)
downloadrust-11959a8b6e75d2c55500a703070a248342d29549.tar.gz
rust-11959a8b6e75d2c55500a703070a248342d29549.zip
Fix invalid suggestion from type error for derive macro
-rw-r--r--compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs14
-rw-r--r--tests/ui/typeck/auxiliary/derive-demo-issue-136343.rs7
-rw-r--r--tests/ui/typeck/invalid-sugg-for-derive-macro-issue-136343.rs9
-rw-r--r--tests/ui/typeck/invalid-sugg-for-derive-macro-issue-136343.stderr19
4 files changed, 44 insertions, 5 deletions
diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
index f9fc1215936..35a3491f7c0 100644
--- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
+++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
@@ -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;
@@ -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();
 
@@ -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;
         }
 
diff --git a/tests/ui/typeck/auxiliary/derive-demo-issue-136343.rs b/tests/ui/typeck/auxiliary/derive-demo-issue-136343.rs
new file mode 100644
index 00000000000..65be439b49a
--- /dev/null
+++ b/tests/ui/typeck/auxiliary/derive-demo-issue-136343.rs
@@ -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()
+}
diff --git a/tests/ui/typeck/invalid-sugg-for-derive-macro-issue-136343.rs b/tests/ui/typeck/invalid-sugg-for-derive-macro-issue-136343.rs
new file mode 100644
index 00000000000..c08030fc5c1
--- /dev/null
+++ b/tests/ui/typeck/invalid-sugg-for-derive-macro-issue-136343.rs
@@ -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() {}
diff --git a/tests/ui/typeck/invalid-sugg-for-derive-macro-issue-136343.stderr b/tests/ui/typeck/invalid-sugg-for-derive-macro-issue-136343.stderr
new file mode 100644
index 00000000000..0b9c1d9123a
--- /dev/null
+++ b/tests/ui/typeck/invalid-sugg-for-derive-macro-issue-136343.stderr
@@ -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`.