Skip to content

Commit

Permalink
Rollup merge of rust-lang#137464 - chenyukang:yukang-fix-136343, r=es…
Browse files Browse the repository at this point in the history
…tebank

Fix invalid suggestion from type error for derive macro

Fixes rust-lang#136343

r? `@estebank`

I didn't use `from_expansion` to avoid it because of testcase
`tests/ui/typeck/issue-110017-format-into-help-deletes-macro.rs`:

https://github.com/chenyukang/rust/blob/11959a8b6e75d2c55500a703070a248342d29549/tests/ui/typeck/issue-110017-format-into-help-deletes-macro.rs#L34-L37

This type error could come up with a proper fix.
  • Loading branch information
fmease authored Feb 25, 2025
2 parents 4e3edce + 11959a8 commit 1d0e1c3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
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`.

0 comments on commit 1d0e1c3

Please sign in to comment.