diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index a0d9e9a72386c..9a657ab159035 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -666,99 +666,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } - fn report_no_match_method_error( + fn suggest_method_call_annotation( &self, - mut span: Span, + err: &mut Diag<'_>, + span: Span, rcvr_ty: Ty<'tcx>, item_ident: Ident, - expr_id: hir::HirId, + mode: Mode, source: SelfSource<'tcx>, - args: Option<&'tcx [hir::Expr<'tcx>]>, - sugg_span: Span, - no_match_data: &mut NoMatchData<'tcx>, expected: Expectation<'tcx>, - trait_missing_method: bool, - within_macro_span: Option, - ) -> ErrorGuaranteed { - let mode = no_match_data.mode; - let tcx = self.tcx; - let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty); - let mut ty_file = None; - let is_method = mode == Mode::MethodCall; - let unsatisfied_predicates = &no_match_data.unsatisfied_predicates; - let similar_candidate = no_match_data.similar_candidate; - let item_kind = if is_method { - "method" - } else if rcvr_ty.is_enum() { - "variant or associated item" - } else { - match (item_ident.as_str().chars().next(), rcvr_ty.is_fresh_ty()) { - (Some(name), false) if name.is_lowercase() => "function or associated item", - (Some(_), false) => "associated item", - (Some(_), true) | (None, false) => "variant or associated item", - (None, true) => "variant", - } - }; - - // We could pass the file for long types into these two, but it isn't strictly necessary - // given how targeted they are. - if let Err(guar) = - self.report_failed_method_call_on_range_end(tcx, rcvr_ty, source, span, item_ident) - { - return guar; - } - if let Err(guar) = self.report_failed_method_call_on_numerical_infer_var( - tcx, - rcvr_ty, - source, - span, - item_kind, - item_ident, - &mut ty_file, - ) { - return guar; - } - span = item_ident.span; - - let is_write = sugg_span.ctxt().outer_expn_data().macro_def_id.is_some_and(|def_id| { - tcx.is_diagnostic_item(sym::write_macro, def_id) - || tcx.is_diagnostic_item(sym::writeln_macro, def_id) - }) && item_ident.name == sym::write_fmt; - let mut err = if is_write && let SelfSource::MethodCall(rcvr_expr) = source { - self.create_missing_writer_err(rcvr_ty, rcvr_expr, ty_file) - } else { - self.create_no_assoc_err( - rcvr_ty, - item_ident, - item_kind, - trait_missing_method, - source, - is_method, - sugg_span, - unsatisfied_predicates, - ) - }; - if rcvr_ty.references_error() { - err.downgrade_to_delayed_bug(); - } - - self.set_label_for_method_error( - &mut err, - source, - rcvr_ty, - item_ident, - expr_id, - span, - sugg_span, - within_macro_span, - args, - ); - + ) { if let Mode::MethodCall = mode && let SelfSource::MethodCall(cal) = source { self.suggest_await_before_method( - &mut err, + err, item_ident, rcvr_ty, cal, @@ -767,10 +689,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ); } - self.suggest_on_pointer_type(&mut err, source, rcvr_ty, item_ident); + self.suggest_on_pointer_type(err, source, rcvr_ty, item_ident); if let SelfSource::MethodCall(rcvr_expr) = source { - self.suggest_fn_call(&mut err, rcvr_expr, rcvr_ty, |output_ty| { + self.suggest_fn_call(err, rcvr_expr, rcvr_ty, |output_ty| { let call_expr = self.tcx.hir_expect_expr(self.tcx.parent_hir_id(rcvr_expr.hir_id)); let probe = self.lookup_probe_for_diagnostic( item_ident, @@ -782,14 +704,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { probe.is_ok() }); self.note_internal_mutation_in_method( - &mut err, + err, rcvr_expr, expected.to_option(self), rcvr_ty, ); } + } - let mut custom_span_label = false; + fn suggest_static_method_candidates( + &self, + err: &mut Diag<'_>, + span: Span, + rcvr_ty: Ty<'tcx>, + item_ident: Ident, + source: SelfSource<'tcx>, + args: Option<&'tcx [hir::Expr<'tcx>]>, + sugg_span: Span, + no_match_data: &NoMatchData<'tcx>, + ) -> Vec { let mut static_candidates = no_match_data.static_candidates.clone(); // `static_candidates` may have same candidates appended by @@ -803,11 +736,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { functions must have a `self` parameter", ); err.span_label(span, "this is an associated function, not a method"); - custom_span_label = true; } if static_candidates.len() == 1 { self.suggest_associated_call_syntax( - &mut err, + err, &static_candidates, rcvr_ty, source, @@ -821,7 +753,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { source, args, span, - &mut err, + err, &mut static_candidates, None, ); @@ -832,23 +764,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { source, args, span, - &mut err, + err, &mut static_candidates, Some(sugg_span), ); } + static_candidates + } - let mut bound_spans: SortedMap> = Default::default(); + fn suggest_unsatisfied_ty_or_trait( + &self, + err: &mut Diag<'_>, + span: Span, + rcvr_ty: Ty<'tcx>, + item_ident: Ident, + item_kind: &str, + source: SelfSource<'tcx>, + unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>, + static_candidates: &[CandidateSource], + ) -> Result<(bool, bool, bool, bool, SortedMap>), ()> { let mut restrict_type_params = false; let mut suggested_derive = false; let mut unsatisfied_bounds = false; - let mut ty_span = match rcvr_ty.kind() { - ty::Param(param_type) => { - Some(param_type.span_from_generics(self.tcx, self.body_id.to_def_id())) - } - ty::Adt(def, _) if def.did().is_local() => Some(tcx.def_span(def.did())), - _ => None, - }; + let mut custom_span_label = !static_candidates.is_empty(); + let mut bound_spans: SortedMap> = Default::default(); + let tcx = self.tcx; if item_ident.name == sym::count && self.is_slice_ty(rcvr_ty, span) { let msg = "consider using `len` instead"; @@ -873,7 +813,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Applicability::MaybeIncorrect, ); } - return err.emit(); + // Report to emit the diagnostic + return Err(()); } else if !unsatisfied_predicates.is_empty() { if matches!(rcvr_ty.kind(), ty::Param(_)) { // We special case the situation where we are looking for `_` in @@ -888,7 +829,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // suggestions. } else { self.handle_unsatisfied_predicates( - &mut err, + err, rcvr_ty, item_ident, item_kind, @@ -936,21 +877,55 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } } + Ok(( + restrict_type_params, + suggested_derive, + unsatisfied_bounds, + custom_span_label, + bound_spans, + )) + } - let mut find_candidate_for_method = false; - let should_label_not_found = match source { + fn suggest_surround_method_call( + &self, + err: &mut Diag<'_>, + span: Span, + rcvr_ty: Ty<'tcx>, + item_ident: Ident, + source: SelfSource<'tcx>, + similar_candidate: &Option, + ) -> bool { + match source { // If the method name is the name of a field with a function or closure type, // give a helping note that it has to be called as `(x.f)(...)`. SelfSource::MethodCall(expr) => { - !self.suggest_calling_field_as_fn(span, rcvr_ty, expr, item_ident, &mut err) + !self.suggest_calling_field_as_fn(span, rcvr_ty, expr, item_ident, err) && similar_candidate.is_none() } _ => true, - }; + } + } + + fn find_possible_candidates_for_method( + &self, + err: &mut Diag<'_>, + span: Span, + rcvr_ty: Ty<'tcx>, + item_ident: Ident, + item_kind: &str, + mode: Mode, + source: SelfSource<'tcx>, + no_match_data: &NoMatchData<'tcx>, + expected: Expectation<'tcx>, + should_label_not_found: bool, + custom_span_label: bool, + ) { + let mut find_candidate_for_method = false; + let unsatisfied_predicates = &no_match_data.unsatisfied_predicates; if should_label_not_found && !custom_span_label { self.set_not_found_span_label( - &mut err, + err, rcvr_ty, item_ident, item_kind, @@ -963,7 +938,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } if !find_candidate_for_method { self.lookup_segments_chain_for_no_match_method( - &mut err, + err, item_ident, item_kind, source, @@ -975,7 +950,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // can't be called due to `typeof(expr): Clone` not holding. if unsatisfied_predicates.is_empty() { self.suggest_calling_method_on_field( - &mut err, + err, source, span, rcvr_ty, @@ -983,37 +958,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { expected.only_has_type(self), ); } + } - self.suggest_unwrapping_inner_self(&mut err, source, rcvr_ty, item_ident); - - if rcvr_ty.is_numeric() && rcvr_ty.is_fresh() || restrict_type_params || suggested_derive { - // skip suggesting traits to import - } else { - self.suggest_traits_to_import( - &mut err, - span, - rcvr_ty, - item_ident, - args.map(|args| args.len() + 1), - source, - no_match_data.out_of_scope_traits.clone(), - &static_candidates, - unsatisfied_bounds, - expected.only_has_type(self), - trait_missing_method, - ); - } - - self.suggest_enum_variant_for_method_call( - &mut err, - rcvr_ty, - item_ident, - span, - source, - unsatisfied_predicates, - ); + fn suggest_confusable_or_similarly_named_method( + &self, + err: &mut Diag<'_>, + span: Span, + rcvr_ty: Ty<'tcx>, + item_ident: Ident, + mode: Mode, + args: Option<&'tcx [hir::Expr<'tcx>]>, + unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>, + similar_candidate: Option, + ) { let confusable_suggested = self.confusable_method_name( - &mut err, + err, rcvr_ty, item_ident, args.map(|args| { @@ -1033,18 +992,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // and if we aren't in an expansion. && !span.from_expansion() { - self.find_likely_intended_associated_item( - &mut err, - similar_candidate, - span, - args, - mode, - ); + self.find_likely_intended_associated_item(err, similar_candidate, span, args, mode); } } + } + fn suggest_method_not_found_because_of_unsatisfied_bounds( + &self, + err: &mut Diag<'_>, + rcvr_ty: Ty<'tcx>, + item_ident: Ident, + item_kind: &str, + bound_spans: SortedMap>, + ) { + let mut ty_span = match rcvr_ty.kind() { + ty::Param(param_type) => { + Some(param_type.span_from_generics(self.tcx, self.body_id.to_def_id())) + } + ty::Adt(def, _) if def.did().is_local() => Some(self.tcx.def_span(def.did())), + _ => None, + }; for (span, mut bounds) in bound_spans { - if !tcx.sess.source_map().is_span_accessible(span) { + if !self.tcx.sess.source_map().is_span_accessible(span) { continue; } bounds.sort(); @@ -1077,6 +1046,209 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ), ); } + } + + fn report_no_match_method_error( + &self, + span: Span, + rcvr_ty: Ty<'tcx>, + item_ident: Ident, + expr_id: hir::HirId, + source: SelfSource<'tcx>, + args: Option<&'tcx [hir::Expr<'tcx>]>, + sugg_span: Span, + no_match_data: &mut NoMatchData<'tcx>, + expected: Expectation<'tcx>, + trait_missing_method: bool, + within_macro_span: Option, + ) -> ErrorGuaranteed { + let tcx = self.tcx; + let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty); + + if let Err(guar) = rcvr_ty.error_reported() { + return guar; + } + + // We could pass the file for long types into these two, but it isn't strictly necessary + // given how targeted they are. + if let Err(guar) = + self.report_failed_method_call_on_range_end(tcx, rcvr_ty, source, span, item_ident) + { + return guar; + } + + let mut ty_file = None; + let mode = no_match_data.mode; + let is_method = mode == Mode::MethodCall; + let item_kind = if is_method { + "method" + } else if rcvr_ty.is_enum() { + "variant or associated item" + } else { + match (item_ident.as_str().chars().next(), rcvr_ty.is_fresh_ty()) { + (Some(name), false) if name.is_lowercase() => "function or associated item", + (Some(_), false) => "associated item", + (Some(_), true) | (None, false) => "variant or associated item", + (None, true) => "variant", + } + }; + + if let Err(guar) = self.report_failed_method_call_on_numerical_infer_var( + tcx, + rcvr_ty, + source, + span, + item_kind, + item_ident, + &mut ty_file, + ) { + return guar; + } + + let unsatisfied_predicates = &no_match_data.unsatisfied_predicates; + let is_write = sugg_span.ctxt().outer_expn_data().macro_def_id.is_some_and(|def_id| { + tcx.is_diagnostic_item(sym::write_macro, def_id) + || tcx.is_diagnostic_item(sym::writeln_macro, def_id) + }) && item_ident.name == sym::write_fmt; + let mut err = if is_write && let SelfSource::MethodCall(rcvr_expr) = source { + self.create_missing_writer_err(rcvr_ty, rcvr_expr, ty_file) + } else { + self.create_no_assoc_err( + rcvr_ty, + item_ident, + item_kind, + trait_missing_method, + source, + is_method, + sugg_span, + unsatisfied_predicates, + ) + }; + + self.set_label_for_method_error( + &mut err, + source, + rcvr_ty, + item_ident, + expr_id, + item_ident.span, + sugg_span, + within_macro_span, + args, + ); + + self.suggest_method_call_annotation( + &mut err, + item_ident.span, + rcvr_ty, + item_ident, + mode, + source, + expected, + ); + + let static_candidates = self.suggest_static_method_candidates( + &mut err, + item_ident.span, + rcvr_ty, + item_ident, + source, + args, + sugg_span, + &no_match_data, + ); + + let Ok(( + restrict_type_params, + suggested_derive, + unsatisfied_bounds, + custom_span_label, + bound_spans, + )) = self.suggest_unsatisfied_ty_or_trait( + &mut err, + item_ident.span, + rcvr_ty, + item_ident, + item_kind, + source, + unsatisfied_predicates, + &static_candidates, + ) + else { + return err.emit(); + }; + + let similar_candidate = no_match_data.similar_candidate; + let should_label_not_found = self.suggest_surround_method_call( + &mut err, + item_ident.span, + rcvr_ty, + item_ident, + source, + &similar_candidate, + ); + + self.find_possible_candidates_for_method( + &mut err, + item_ident.span, + rcvr_ty, + item_ident, + item_kind, + mode, + source, + no_match_data, + expected, + should_label_not_found, + custom_span_label, + ); + + self.suggest_unwrapping_inner_self(&mut err, source, rcvr_ty, item_ident); + + if rcvr_ty.is_numeric() && rcvr_ty.is_fresh() || restrict_type_params || suggested_derive { + // skip suggesting traits to import + } else { + self.suggest_traits_to_import( + &mut err, + item_ident.span, + rcvr_ty, + item_ident, + args.map(|args| args.len() + 1), + source, + no_match_data.out_of_scope_traits.clone(), + &static_candidates, + unsatisfied_bounds, + expected.only_has_type(self), + trait_missing_method, + ); + } + + self.suggest_enum_variant_for_method_call( + &mut err, + rcvr_ty, + item_ident, + item_ident.span, + source, + unsatisfied_predicates, + ); + + self.suggest_confusable_or_similarly_named_method( + &mut err, + item_ident.span, + rcvr_ty, + item_ident, + mode, + args, + unsatisfied_predicates, + similar_candidate, + ); + + self.suggest_method_not_found_because_of_unsatisfied_bounds( + &mut err, + rcvr_ty, + item_ident, + item_kind, + bound_spans, + ); self.note_derefed_ty_has_method(&mut err, source, rcvr_ty, item_ident, expected); self.suggest_bounds_for_range_to_method(&mut err, source, item_ident); diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 871ed53bd3380..b5c44686cafcf 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -2025,13 +2025,11 @@ fn render_impl( let mut methods = Vec::new(); if !impl_.is_negative_trait_impl() { - for trait_item in &impl_.items { - match trait_item.kind { - clean::MethodItem(..) | clean::RequiredMethodItem(_) => { - methods.push(trait_item) - } + for impl_item in &impl_.items { + match impl_item.kind { + clean::MethodItem(..) | clean::RequiredMethodItem(_) => methods.push(impl_item), clean::RequiredAssocTypeItem(..) | clean::AssocTypeItem(..) => { - assoc_types.push(trait_item) + assoc_types.push(impl_item) } clean::RequiredAssocConstItem(..) | clean::ProvidedAssocConstItem(_) @@ -2041,7 +2039,7 @@ fn render_impl( &mut default_impl_items, &mut impl_items, cx, - trait_item, + impl_item, if trait_.is_some() { &i.impl_item } else { parent }, link, render_mode, diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index 6045b9a77ecae..31da7b7de9206 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -568,18 +568,14 @@ impl TypeAliasPart { if let Some(ret) = &mut ret { ret.aliases.push(type_alias_fqp); } else { - let target_did = impl_ - .inner_impl() - .trait_ - .as_ref() - .map(|trait_| trait_.def_id()) - .or_else(|| impl_.inner_impl().for_.def_id(&cx.shared.cache)); + let target_trait_did = + impl_.inner_impl().trait_.as_ref().map(|trait_| trait_.def_id()); let provided_methods; - let assoc_link = if let Some(target_did) = target_did { + let assoc_link = if let Some(target_trait_did) = target_trait_did { provided_methods = impl_.inner_impl().provided_trait_methods(cx.tcx()); AssocItemLink::GotoSource( - ItemId::DefId(target_did), + ItemId::DefId(target_trait_did), &provided_methods, ) } else { diff --git a/tests/rustdoc-gui/src/test_docs/lib.rs b/tests/rustdoc-gui/src/test_docs/lib.rs index c0771583ab658..0ee2a66d4b689 100644 --- a/tests/rustdoc-gui/src/test_docs/lib.rs +++ b/tests/rustdoc-gui/src/test_docs/lib.rs @@ -786,3 +786,13 @@ pub mod tooltips { Vec::new() } } + +pub mod tyalias { + pub struct X(pub T); + + impl X { + pub fn blob(&self) {} + } + + pub type Y = X; +} diff --git a/tests/rustdoc-gui/type-alias.goml b/tests/rustdoc-gui/type-alias.goml new file mode 100644 index 0000000000000..a07f1b4eb8147 --- /dev/null +++ b/tests/rustdoc-gui/type-alias.goml @@ -0,0 +1,7 @@ +// This test ensures that we correctly generate links to methods on type aliases. +go-to: "file://" + |DOC_PATH| + "/test_docs/tyalias/type.Y.html" + +// It's generated with JS so we need to wait for it to be done generating. +wait-for: "#implementations" +// We check that it's "#method." and not "#tymethod.". +assert-text: ('#method\.blob a.fn[href="#method.blob"]', "blob") diff --git a/tests/ui/proc-macro/auxiliary/extra-empty-derive.rs b/tests/ui/proc-macro/auxiliary/extra-empty-derive.rs new file mode 100644 index 0000000000000..9a89e04364d75 --- /dev/null +++ b/tests/ui/proc-macro/auxiliary/extra-empty-derive.rs @@ -0,0 +1,7 @@ +extern crate proc_macro; +use proc_macro::{TokenStream, TokenTree}; + +#[proc_macro_derive(Empty2, attributes(empty_helper))] +pub fn empty_derive2(_: TokenStream) -> TokenStream { + TokenStream::new() +} diff --git a/tests/ui/proc-macro/helper-attr-compat-collision.rs b/tests/ui/proc-macro/helper-attr-compat-collision.rs new file mode 100644 index 0000000000000..7755582c46322 --- /dev/null +++ b/tests/ui/proc-macro/helper-attr-compat-collision.rs @@ -0,0 +1,23 @@ +//@ proc-macro: test-macros.rs +//@ proc-macro: extra-empty-derive.rs +//@ check-pass + +#[macro_use(Empty)] +extern crate test_macros; +#[macro_use(Empty2)] +extern crate extra_empty_derive; + +// Testing the behavior of derive attributes with helpers that share the same name. +// +// Normally if the first derive below were absent the call to #[empty_helper] before it it +// introduced by its own derive would produce a future incompat error. +// +// With the extra derive also introducing that attribute in advanced the warning gets supressed. +// Demonstrates a lack of identity to helper attributes, the compiler does not track which derive +// introduced a helper, just that a derive introduced the helper. +#[derive(Empty)] +#[empty_helper] +#[derive(Empty2)] +struct S; + +fn main() {}