diff options
| author | Matthias Krüger <476013+matthiaskrgr@users.noreply.github.com> | 2025-03-07 19:15:35 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-07 19:15:35 +0100 |
| commit | e70adad70a6d594381c42ff59e42f9753c7d1fd7 (patch) | |
| tree | 062cfedb9c0e6cc4b93390c2134e987b912dd0e9 /compiler | |
| parent | 0defc4f27f3e00df9020b7f81e43ef7f9537dea8 (diff) | |
| parent | 7e199b10c9efc765024d5bad1f964ddaf6b9a131 (diff) | |
| download | rust-e70adad70a6d594381c42ff59e42f9753c7d1fd7.tar.gz rust-e70adad70a6d594381c42ff59e42f9753c7d1fd7.zip | |
Rollup merge of #138042 - xizheyin:issue-135759, r=nnethercote
Suggest struct or union to add generic that impls trait Fixes #135759 cc ```@tdittr```
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs | 63 |
1 files changed, 61 insertions, 2 deletions
diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs index f5e075367f3..3611db7c68f 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs @@ -84,9 +84,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { rustc_errors::struct_span_code_err!(self.dcx(), self_ty.span, E0782, "{}", msg); if self_ty.span.can_be_used_for_suggestions() && !self.maybe_suggest_impl_trait(self_ty, &mut diag) + && !self.maybe_suggest_dyn_trait(self_ty, label, sugg, &mut diag) { - // FIXME: Only emit this suggestion if the trait is dyn-compatible. - diag.multipart_suggestion_verbose(label, sugg, Applicability::MachineApplicable); + self.maybe_suggest_add_generic_impl_trait(self_ty, &mut diag); } // Check if the impl trait that we are considering is an impl of a local trait. self.maybe_suggest_blanket_trait_impl(self_ty, &mut diag); @@ -123,6 +123,33 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { } } + fn maybe_suggest_add_generic_impl_trait( + &self, + self_ty: &hir::Ty<'_>, + diag: &mut Diag<'_>, + ) -> bool { + let tcx = self.tcx(); + let msg = "you might be missing a type parameter"; + let mut sugg = vec![]; + + let parent_id = tcx.hir_get_parent_item(self_ty.hir_id).def_id; + let parent_item = tcx.hir_node_by_def_id(parent_id).expect_item(); + match parent_item.kind { + hir::ItemKind::Struct(_, generics) | hir::ItemKind::Enum(_, generics) => { + sugg.push(( + generics.where_clause_span, + format!( + "<T: {}>", + self.tcx().sess.source_map().span_to_snippet(self_ty.span).unwrap() + ), + )); + sugg.push((self_ty.span, "T".to_string())); + } + _ => {} + } + diag.multipart_suggestion_verbose(msg, sugg, Applicability::MachineApplicable); + true + } /// Make sure that we are in the condition to suggest the blanket implementation. fn maybe_suggest_blanket_trait_impl<G: EmissionGuarantee>( &self, @@ -171,6 +198,38 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { } } + fn maybe_suggest_dyn_trait( + &self, + self_ty: &hir::Ty<'_>, + label: &str, + sugg: Vec<(Span, String)>, + diag: &mut Diag<'_>, + ) -> bool { + let tcx = self.tcx(); + let parent_id = tcx.hir_get_parent_item(self_ty.hir_id).def_id; + let parent_item = tcx.hir_node_by_def_id(parent_id).expect_item(); + + // If the parent item is an enum, don't suggest the dyn trait. + if let hir::ItemKind::Enum(..) = parent_item.kind { + return false; + } + + // If the parent item is a struct, check if self_ty is the last field. + if let hir::ItemKind::Struct(variant_data, _) = parent_item.kind { + if variant_data.fields().last().unwrap().ty.span != self_ty.span { + return false; + } + } + + // FIXME: Only emit this suggestion if the trait is dyn-compatible. + diag.multipart_suggestion_verbose( + label.to_string(), + sugg, + Applicability::MachineApplicable, + ); + true + } + fn add_generic_param_suggestion( &self, generics: &hir::Generics<'_>, |
