2015-05-02 23:30:59 -06:00
|
|
|
|
//! Give useful errors and suggestions to users when an item can't be
|
2015-01-13 17:05:04 +11:00
|
|
|
|
//! found or is otherwise invalid.
|
|
|
|
|
|
|
2019-02-08 22:30:58 +09:00
|
|
|
|
use crate::check::FnCtxt;
|
2022-09-26 00:55:35 +02:00
|
|
|
|
use rustc_ast::ast::Mutability;
|
2020-02-19 14:26:46 -08:00
|
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2022-01-27 09:44:25 +00:00
|
|
|
|
use rustc_errors::{
|
2022-01-23 12:34:26 -06:00
|
|
|
|
pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
|
2022-03-24 02:03:04 +00:00
|
|
|
|
MultiSpan,
|
2022-01-27 09:44:25 +00:00
|
|
|
|
};
|
2020-01-05 02:37:57 +01:00
|
|
|
|
use rustc_hir as hir;
|
2022-04-26 17:04:44 -04:00
|
|
|
|
use rustc_hir::def::DefKind;
|
2022-03-18 17:13:38 -04:00
|
|
|
|
use rustc_hir::def_id::DefId;
|
2020-08-18 11:47:27 +01:00
|
|
|
|
use rustc_hir::lang_items::LangItem;
|
2020-01-05 02:37:57 +01:00
|
|
|
|
use rustc_hir::{ExprKind, Node, QPath};
|
2020-01-06 23:12:31 +01:00
|
|
|
|
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
|
2022-02-16 15:01:19 +11:00
|
|
|
|
use rustc_middle::traits::util::supertraits;
|
2022-02-21 13:44:55 +01:00
|
|
|
|
use rustc_middle::ty::fast_reject::{simplify_type, TreatParams};
|
2020-03-29 17:19:48 +02:00
|
|
|
|
use rustc_middle::ty::print::with_crate_prefix;
|
2022-06-17 13:15:00 +01:00
|
|
|
|
use rustc_middle::ty::{self, DefIdTree, ToPredicate, Ty, TyCtxt, TypeVisitable};
|
2022-09-03 07:24:55 +00:00
|
|
|
|
use rustc_middle::ty::{IsSuggestable, ToPolyTraitRef};
|
2020-07-08 11:04:10 +10:00
|
|
|
|
use rustc_span::symbol::{kw, sym, Ident};
|
2022-07-17 04:09:20 +09:00
|
|
|
|
use rustc_span::Symbol;
|
2022-03-24 02:03:04 +00:00
|
|
|
|
use rustc_span::{lev_distance, source_map, ExpnKind, FileName, MacroKind, Span};
|
2022-03-08 11:04:20 -07:00
|
|
|
|
use rustc_trait_selection::traits::error_reporting::on_unimplemented::InferCtxtExt as _;
|
|
|
|
|
|
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
|
2021-10-13 14:28:28 +00:00
|
|
|
|
use rustc_trait_selection::traits::{
|
2022-03-08 11:04:20 -07:00
|
|
|
|
FulfillmentError, Obligation, ObligationCause, ObligationCauseCode, OnUnimplementedNote,
|
2021-10-13 14:28:28 +00:00
|
|
|
|
};
|
2015-01-13 17:05:04 +11:00
|
|
|
|
|
2015-01-14 10:49:17 +11:00
|
|
|
|
use std::cmp::Ordering;
|
2021-02-12 22:44:43 +00:00
|
|
|
|
use std::iter;
|
2015-01-14 10:49:17 +11:00
|
|
|
|
|
2022-09-26 00:55:35 +02:00
|
|
|
|
use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope};
|
2022-08-27 20:59:07 +00:00
|
|
|
|
use super::{CandidateSource, MethodError, NoMatchData};
|
2015-01-13 17:05:04 +11:00
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
|
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
2019-04-26 13:41:10 +02:00
|
|
|
|
fn is_fn_ty(&self, ty: Ty<'tcx>, span: Span) -> bool {
|
2016-05-11 08:48:12 +03:00
|
|
|
|
let tcx = self.tcx;
|
2020-08-03 00:49:11 +02:00
|
|
|
|
match ty.kind() {
|
2018-11-27 02:59:49 +00:00
|
|
|
|
// Not all of these (e.g., unsafe fns) implement `FnOnce`,
|
|
|
|
|
|
// so we look for these beforehand.
|
2018-08-22 01:35:02 +01:00
|
|
|
|
ty::Closure(..) | ty::FnDef(..) | ty::FnPtr(_) => true,
|
2018-11-27 02:59:49 +00:00
|
|
|
|
// If it's not a simple function, look for things which implement `FnOnce`.
|
2016-05-11 08:48:12 +03:00
|
|
|
|
_ => {
|
2022-02-19 00:44:45 +01:00
|
|
|
|
let Some(fn_once) = tcx.lang_items().fn_once_trait() else {
|
|
|
|
|
|
return false;
|
2016-05-24 14:37:11 +03:00
|
|
|
|
};
|
2016-04-03 22:58:44 +02:00
|
|
|
|
|
2022-02-13 13:06:06 -07:00
|
|
|
|
// This conditional prevents us from asking to call errors and unresolved types.
|
|
|
|
|
|
// It might seem that we can use `predicate_must_hold_modulo_regions`,
|
|
|
|
|
|
// but since a Dummy binder is used to fill in the FnOnce trait's arguments,
|
|
|
|
|
|
// type resolution always gives a "maybe" here.
|
|
|
|
|
|
if self.autoderef(span, ty).any(|(ty, _)| {
|
|
|
|
|
|
info!("check deref {:?} error", ty);
|
|
|
|
|
|
matches!(ty.kind(), ty::Error(_) | ty::Infer(_))
|
|
|
|
|
|
}) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-04 09:06:07 +05:30
|
|
|
|
self.autoderef(span, ty).any(|(ty, _)| {
|
2022-02-13 13:06:06 -07:00
|
|
|
|
info!("check deref {:?} impl FnOnce", ty);
|
2016-10-04 09:06:07 +05:30
|
|
|
|
self.probe(|_| {
|
2018-05-16 09:34:09 +03:00
|
|
|
|
let fn_once_substs = tcx.mk_substs_trait(
|
|
|
|
|
|
ty,
|
2019-03-21 00:00:24 +00:00
|
|
|
|
&[self
|
|
|
|
|
|
.next_ty_var(TypeVariableOrigin {
|
|
|
|
|
|
kind: TypeVariableOriginKind::MiscVariable,
|
|
|
|
|
|
span,
|
|
|
|
|
|
})
|
|
|
|
|
|
.into()],
|
2018-05-16 09:34:09 +03:00
|
|
|
|
);
|
2016-10-04 09:06:07 +05:30
|
|
|
|
let trait_ref = ty::TraitRef::new(fn_once, fn_once_substs);
|
2021-09-15 22:55:10 -04:00
|
|
|
|
let poly_trait_ref = ty::Binder::dummy(trait_ref);
|
2016-10-04 09:06:07 +05:30
|
|
|
|
let obligation = Obligation::misc(
|
2017-05-23 04:19:47 -04:00
|
|
|
|
span,
|
|
|
|
|
|
self.body_id,
|
|
|
|
|
|
self.param_env,
|
2020-05-07 10:12:19 +00:00
|
|
|
|
poly_trait_ref.without_const().to_predicate(tcx),
|
2017-05-23 04:19:47 -04:00
|
|
|
|
);
|
2018-04-08 01:56:27 -05:00
|
|
|
|
self.predicate_may_hold(&obligation)
|
2016-10-04 09:06:07 +05:30
|
|
|
|
})
|
|
|
|
|
|
})
|
2016-03-05 00:26:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-05-24 14:37:11 +03:00
|
|
|
|
|
2021-07-29 23:23:17 -07:00
|
|
|
|
fn is_slice_ty(&self, ty: Ty<'tcx>, span: Span) -> bool {
|
|
|
|
|
|
self.autoderef(span, ty).any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-29 19:53:48 -07:00
|
|
|
|
pub fn report_method_error(
|
2019-03-04 19:24:52 -08:00
|
|
|
|
&self,
|
2021-08-21 18:45:56 -05:00
|
|
|
|
mut span: Span,
|
2019-03-04 19:24:52 -08:00
|
|
|
|
rcvr_ty: Ty<'tcx>,
|
2020-04-19 13:00:18 +02:00
|
|
|
|
item_name: Ident,
|
2021-03-29 19:53:48 -07:00
|
|
|
|
source: SelfSource<'tcx>,
|
2019-03-04 19:24:52 -08:00
|
|
|
|
error: MethodError<'tcx>,
|
2022-09-01 13:27:31 +09:00
|
|
|
|
args: Option<(&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>])>,
|
2022-01-23 12:34:26 -06:00
|
|
|
|
) -> Option<DiagnosticBuilder<'_, ErrorGuaranteed>> {
|
2018-11-27 02:59:49 +00:00
|
|
|
|
// Avoid suggestions when we don't know what's going on.
|
2016-05-11 08:48:12 +03:00
|
|
|
|
if rcvr_ty.references_error() {
|
2019-09-01 02:22:42 -07:00
|
|
|
|
return None;
|
2016-05-11 08:48:12 +03:00
|
|
|
|
}
|
2016-03-12 03:06:24 +02:00
|
|
|
|
|
2019-03-10 16:20:15 -07:00
|
|
|
|
let report_candidates = |span: Span,
|
2022-01-23 20:41:46 +00:00
|
|
|
|
err: &mut Diagnostic,
|
2019-03-10 16:20:15 -07:00
|
|
|
|
mut sources: Vec<CandidateSource>,
|
2019-12-03 22:19:18 -08:00
|
|
|
|
sugg_span: Span| {
|
2016-05-11 08:48:12 +03:00
|
|
|
|
sources.sort();
|
|
|
|
|
|
sources.dedup();
|
2016-05-02 10:37:28 +02:00
|
|
|
|
// Dynamic limit to avoid hiding just one candidate, which is silly.
|
|
|
|
|
|
let limit = if sources.len() == 5 { 5 } else { 4 };
|
2016-05-11 08:48:12 +03:00
|
|
|
|
|
2016-05-02 10:37:28 +02:00
|
|
|
|
for (idx, source) in sources.iter().take(limit).enumerate() {
|
2016-05-11 08:48:12 +03:00
|
|
|
|
match *source {
|
2022-04-04 11:48:47 +02:00
|
|
|
|
CandidateSource::Impl(impl_did) => {
|
2016-05-11 08:48:12 +03:00
|
|
|
|
// Provide the best span we can. Use the item, if local to crate, else
|
|
|
|
|
|
// the impl, if local to crate (item may be defaulted), else nothing.
|
2022-02-19 00:44:45 +01:00
|
|
|
|
let Some(item) = self.associated_value(impl_did, item_name).or_else(|| {
|
2022-01-08 12:38:14 +01:00
|
|
|
|
let impl_trait_ref = self.tcx.impl_trait_ref(impl_did)?;
|
|
|
|
|
|
self.associated_value(impl_trait_ref.def_id, item_name)
|
2022-02-19 00:44:45 +01:00
|
|
|
|
}) else {
|
|
|
|
|
|
continue;
|
2019-05-10 16:57:03 -07:00
|
|
|
|
};
|
2022-07-04 17:23:24 +09:00
|
|
|
|
|
|
|
|
|
|
let note_span = if item.def_id.is_local() {
|
|
|
|
|
|
Some(self.tcx.def_span(item.def_id))
|
|
|
|
|
|
} else if impl_did.is_local() {
|
|
|
|
|
|
Some(self.tcx.def_span(impl_did))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
None
|
|
|
|
|
|
};
|
2016-05-11 08:48:12 +03:00
|
|
|
|
|
2020-04-13 11:36:37 +09:00
|
|
|
|
let impl_ty = self.tcx.at(span).type_of(impl_did);
|
2016-05-11 08:48:12 +03:00
|
|
|
|
|
|
|
|
|
|
let insertion = match self.tcx.impl_trait_ref(impl_did) {
|
2018-07-28 14:40:32 +02:00
|
|
|
|
None => String::new(),
|
2016-05-11 08:48:12 +03:00
|
|
|
|
Some(trait_ref) => format!(
|
|
|
|
|
|
" of the trait `{}`",
|
2018-12-19 12:31:35 +02:00
|
|
|
|
self.tcx.def_path_str(trait_ref.def_id)
|
2019-12-22 17:42:04 -05:00
|
|
|
|
),
|
2016-05-11 08:48:12 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
2019-12-03 22:19:18 -08:00
|
|
|
|
let (note_str, idx) = if sources.len() > 1 {
|
|
|
|
|
|
(
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"candidate #{} is defined in an impl{} for the type `{}`",
|
|
|
|
|
|
idx + 1,
|
|
|
|
|
|
insertion,
|
|
|
|
|
|
impl_ty,
|
|
|
|
|
|
),
|
|
|
|
|
|
Some(idx + 1),
|
|
|
|
|
|
)
|
2018-05-27 13:30:16 -07:00
|
|
|
|
} else {
|
2019-12-03 22:19:18 -08:00
|
|
|
|
(
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"the candidate is defined in an impl{} for the type `{}`",
|
|
|
|
|
|
insertion, impl_ty,
|
|
|
|
|
|
),
|
|
|
|
|
|
None,
|
|
|
|
|
|
)
|
2018-05-27 13:30:16 -07:00
|
|
|
|
};
|
2016-05-11 08:48:12 +03:00
|
|
|
|
if let Some(note_span) = note_span {
|
|
|
|
|
|
// We have a span pointing to the method. Show note with snippet.
|
2022-07-04 17:23:24 +09:00
|
|
|
|
err.span_note(note_span, ¬e_str);
|
2016-05-11 08:48:12 +03:00
|
|
|
|
} else {
|
|
|
|
|
|
err.note(¬e_str);
|
2016-03-12 03:06:24 +02:00
|
|
|
|
}
|
2019-07-24 01:42:39 +02:00
|
|
|
|
if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_did) {
|
2019-12-03 22:19:18 -08:00
|
|
|
|
let path = self.tcx.def_path_str(trait_ref.def_id);
|
|
|
|
|
|
|
|
|
|
|
|
let ty = match item.kind {
|
2020-05-10 12:15:51 +01:00
|
|
|
|
ty::AssocKind::Const | ty::AssocKind::Type => rcvr_ty,
|
2020-04-01 10:09:50 +08:00
|
|
|
|
ty::AssocKind::Fn => self
|
2019-12-03 22:19:18 -08:00
|
|
|
|
.tcx
|
|
|
|
|
|
.fn_sig(item.def_id)
|
|
|
|
|
|
.inputs()
|
|
|
|
|
|
.skip_binder()
|
|
|
|
|
|
.get(0)
|
|
|
|
|
|
.filter(|ty| ty.is_region_ptr() && !rcvr_ty.is_region_ptr())
|
2020-02-29 13:14:52 +01:00
|
|
|
|
.copied()
|
2019-12-03 22:19:18 -08:00
|
|
|
|
.unwrap_or(rcvr_ty),
|
|
|
|
|
|
};
|
2019-12-11 18:20:29 -08:00
|
|
|
|
print_disambiguation_help(
|
|
|
|
|
|
item_name,
|
|
|
|
|
|
args,
|
|
|
|
|
|
err,
|
|
|
|
|
|
path,
|
|
|
|
|
|
ty,
|
|
|
|
|
|
item.kind,
|
2020-04-01 10:09:50 +08:00
|
|
|
|
item.def_id,
|
2019-12-11 18:20:29 -08:00
|
|
|
|
sugg_span,
|
|
|
|
|
|
idx,
|
|
|
|
|
|
self.tcx.sess.source_map(),
|
2021-09-25 21:47:33 +02:00
|
|
|
|
item.fn_has_self_parameter,
|
2019-12-11 18:20:29 -08:00
|
|
|
|
);
|
2019-07-24 01:42:39 +02:00
|
|
|
|
}
|
2016-03-12 03:06:24 +02:00
|
|
|
|
}
|
2022-04-04 11:48:47 +02:00
|
|
|
|
CandidateSource::Trait(trait_did) => {
|
2022-02-19 00:44:45 +01:00
|
|
|
|
let Some(item) = self.associated_value(trait_did, item_name) else { continue };
|
2022-07-04 17:23:24 +09:00
|
|
|
|
let item_span = self.tcx.def_span(item.def_id);
|
2019-12-03 22:19:18 -08:00
|
|
|
|
let idx = if sources.len() > 1 {
|
2019-12-31 21:25:16 +01:00
|
|
|
|
let msg = &format!(
|
2018-05-27 13:30:16 -07:00
|
|
|
|
"candidate #{} is defined in the trait `{}`",
|
|
|
|
|
|
idx + 1,
|
2018-12-19 12:31:35 +02:00
|
|
|
|
self.tcx.def_path_str(trait_did)
|
|
|
|
|
|
);
|
2019-12-31 21:25:16 +01:00
|
|
|
|
err.span_note(item_span, msg);
|
2019-12-03 22:19:18 -08:00
|
|
|
|
Some(idx + 1)
|
2018-05-27 13:30:16 -07:00
|
|
|
|
} else {
|
2019-12-31 21:25:16 +01:00
|
|
|
|
let msg = &format!(
|
2018-05-27 13:30:16 -07:00
|
|
|
|
"the candidate is defined in the trait `{}`",
|
2018-12-19 12:31:35 +02:00
|
|
|
|
self.tcx.def_path_str(trait_did)
|
|
|
|
|
|
);
|
2019-12-31 21:25:16 +01:00
|
|
|
|
err.span_note(item_span, msg);
|
2019-12-03 22:19:18 -08:00
|
|
|
|
None
|
|
|
|
|
|
};
|
|
|
|
|
|
let path = self.tcx.def_path_str(trait_did);
|
2019-12-11 18:20:29 -08:00
|
|
|
|
print_disambiguation_help(
|
|
|
|
|
|
item_name,
|
|
|
|
|
|
args,
|
|
|
|
|
|
err,
|
|
|
|
|
|
path,
|
|
|
|
|
|
rcvr_ty,
|
|
|
|
|
|
item.kind,
|
2020-04-01 10:09:50 +08:00
|
|
|
|
item.def_id,
|
2019-12-11 18:20:29 -08:00
|
|
|
|
sugg_span,
|
|
|
|
|
|
idx,
|
|
|
|
|
|
self.tcx.sess.source_map(),
|
2021-09-25 21:47:33 +02:00
|
|
|
|
item.fn_has_self_parameter,
|
2019-12-11 18:20:29 -08:00
|
|
|
|
);
|
2015-06-15 12:28:14 -04:00
|
|
|
|
}
|
2015-02-20 08:17:05 +02:00
|
|
|
|
}
|
2015-12-23 19:27:20 +13:00
|
|
|
|
}
|
2016-05-02 10:37:28 +02:00
|
|
|
|
if sources.len() > limit {
|
|
|
|
|
|
err.note(&format!("and {} others", sources.len() - limit));
|
|
|
|
|
|
}
|
2016-05-11 08:48:12 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
2019-12-03 22:19:18 -08:00
|
|
|
|
let sugg_span = if let SelfSource::MethodCall(expr) = source {
|
|
|
|
|
|
// Given `foo.bar(baz)`, `expr` is `bar`, but we want to point to the whole thing.
|
|
|
|
|
|
self.tcx.hir().expect_expr(self.tcx.hir().get_parent_node(expr.hir_id)).span
|
|
|
|
|
|
} else {
|
|
|
|
|
|
span
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
|
match error {
|
2017-11-15 18:55:45 -08:00
|
|
|
|
MethodError::NoMatch(NoMatchData {
|
|
|
|
|
|
static_candidates: static_sources,
|
|
|
|
|
|
unsatisfied_predicates,
|
|
|
|
|
|
out_of_scope_traits,
|
|
|
|
|
|
lev_candidate,
|
|
|
|
|
|
mode,
|
|
|
|
|
|
}) => {
|
2016-05-11 08:48:12 +03:00
|
|
|
|
let tcx = self.tcx;
|
|
|
|
|
|
|
2020-10-24 02:21:18 +02:00
|
|
|
|
let actual = self.resolve_vars_if_possible(rcvr_ty);
|
2018-11-27 02:59:49 +00:00
|
|
|
|
let ty_str = self.ty_to_string(actual);
|
2017-11-16 09:20:51 -08:00
|
|
|
|
let is_method = mode == Mode::MethodCall;
|
2018-06-02 16:23:26 -04:00
|
|
|
|
let item_kind = if is_method {
|
2017-11-15 22:18:52 -08:00
|
|
|
|
"method"
|
|
|
|
|
|
} else if actual.is_enum() {
|
2019-04-08 17:58:18 -04:00
|
|
|
|
"variant or associated item"
|
2017-11-15 22:18:52 -08:00
|
|
|
|
} else {
|
|
|
|
|
|
match (item_name.as_str().chars().next(), actual.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",
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2021-11-17 13:28:54 -07:00
|
|
|
|
|
2022-04-24 15:24:20 -07:00
|
|
|
|
if self.suggest_constraining_numerical_ty(
|
|
|
|
|
|
tcx, actual, source, span, item_kind, item_name, &ty_str,
|
|
|
|
|
|
) {
|
2022-04-24 15:05:06 -07:00
|
|
|
|
return None;
|
|
|
|
|
|
}
|
2022-04-24 15:24:20 -07:00
|
|
|
|
|
2022-04-24 15:05:06 -07:00
|
|
|
|
span = item_name.span;
|
2021-04-15 16:58:17 +02:00
|
|
|
|
|
2022-04-24 15:05:06 -07:00
|
|
|
|
// Don't show generic arguments when the method can't be found in any implementation (#81576).
|
|
|
|
|
|
let mut ty_str_reported = ty_str.clone();
|
|
|
|
|
|
if let ty::Adt(_, generics) = actual.kind() {
|
|
|
|
|
|
if generics.len() > 0 {
|
|
|
|
|
|
let mut autoderef = self.autoderef(span, actual);
|
|
|
|
|
|
let candidate_found = autoderef.any(|(ty, _)| {
|
|
|
|
|
|
if let ty::Adt(adt_deref, _) = ty.kind() {
|
|
|
|
|
|
self.tcx
|
|
|
|
|
|
.inherent_impls(adt_deref.did())
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.filter_map(|def_id| self.associated_value(*def_id, item_name))
|
|
|
|
|
|
.count()
|
|
|
|
|
|
>= 1
|
|
|
|
|
|
} else {
|
|
|
|
|
|
false
|
2018-01-04 12:23:14 -08:00
|
|
|
|
}
|
2022-04-24 15:05:06 -07:00
|
|
|
|
});
|
|
|
|
|
|
let has_deref = autoderef.step_count() > 0;
|
|
|
|
|
|
if !candidate_found && !has_deref && unsatisfied_predicates.is_empty() {
|
|
|
|
|
|
if let Some((path_string, _)) = ty_str.split_once('<') {
|
|
|
|
|
|
ty_str_reported = path_string.to_string();
|
2021-04-15 16:58:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-04-24 15:05:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-04-15 16:58:17 +02:00
|
|
|
|
|
2022-04-24 15:05:06 -07:00
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
|
|
tcx.sess,
|
|
|
|
|
|
span,
|
|
|
|
|
|
E0599,
|
|
|
|
|
|
"no {} named `{}` found for {} `{}` in the current scope",
|
|
|
|
|
|
item_kind,
|
|
|
|
|
|
item_name,
|
|
|
|
|
|
actual.prefix_string(self.tcx),
|
|
|
|
|
|
ty_str_reported,
|
|
|
|
|
|
);
|
|
|
|
|
|
if actual.references_error() {
|
|
|
|
|
|
err.downgrade_to_delayed_bug();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Mode::MethodCall = mode && let SelfSource::MethodCall(cal) = source {
|
2022-04-24 15:24:20 -07:00
|
|
|
|
self.suggest_await_before_method(
|
|
|
|
|
|
&mut err, item_name, actual, cal, span,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2022-04-24 15:05:06 -07:00
|
|
|
|
if let Some(span) = tcx.resolutions(()).confused_type_with_std_module.get(&span) {
|
2022-06-07 13:39:21 -07:00
|
|
|
|
err.span_suggestion(
|
|
|
|
|
|
span.shrink_to_lo(),
|
|
|
|
|
|
"you are looking for the module in `std`, not the primitive type",
|
2022-06-13 15:48:40 +09:00
|
|
|
|
"std::",
|
2022-06-07 13:39:21 -07:00
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
|
);
|
2022-04-24 15:05:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
if let ty::RawPtr(_) = &actual.kind() {
|
|
|
|
|
|
err.note(
|
|
|
|
|
|
"try using `<*const T>::as_ref()` to get a reference to the \
|
2022-06-07 13:39:21 -07:00
|
|
|
|
type behind the pointer: https://doc.rust-lang.org/std/\
|
|
|
|
|
|
primitive.pointer.html#method.as_ref",
|
2022-04-24 15:05:06 -07:00
|
|
|
|
);
|
|
|
|
|
|
err.note(
|
2022-06-07 13:39:21 -07:00
|
|
|
|
"using `<*const T>::as_ref()` on a pointer which is unaligned or points \
|
|
|
|
|
|
to invalid or uninitialized memory is undefined behavior",
|
2022-04-24 15:05:06 -07:00
|
|
|
|
);
|
2022-01-26 04:40:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-21 00:43:20 +09:00
|
|
|
|
let ty_span = match actual.kind() {
|
|
|
|
|
|
ty::Param(param_type) => {
|
|
|
|
|
|
let generics = self.tcx.generics_of(self.body_id.owner.to_def_id());
|
|
|
|
|
|
let type_param = generics.type_param(param_type, self.tcx);
|
|
|
|
|
|
Some(self.tcx.def_span(type_param.def_id))
|
|
|
|
|
|
}
|
2022-07-01 16:47:26 -07:00
|
|
|
|
ty::Adt(def, _) if def.did().is_local() => Some(tcx.def_span(def.did())),
|
2022-06-21 00:43:20 +09:00
|
|
|
|
_ => None,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(span) = ty_span {
|
|
|
|
|
|
err.span_label(
|
|
|
|
|
|
span,
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"{item_kind} `{item_name}` not found for this {}",
|
|
|
|
|
|
actual.prefix_string(self.tcx)
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
2017-11-15 22:18:52 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-27 20:59:07 +00:00
|
|
|
|
if let SelfSource::MethodCall(rcvr_expr) = source {
|
|
|
|
|
|
self.suggest_fn_call(&mut err, rcvr_expr, rcvr_ty, |output_ty| {
|
|
|
|
|
|
let call_expr = self
|
|
|
|
|
|
.tcx
|
|
|
|
|
|
.hir()
|
|
|
|
|
|
.expect_expr(self.tcx.hir().get_parent_node(rcvr_expr.hir_id));
|
|
|
|
|
|
let probe = self.lookup_probe(
|
|
|
|
|
|
span,
|
|
|
|
|
|
item_name,
|
|
|
|
|
|
output_ty,
|
|
|
|
|
|
call_expr,
|
|
|
|
|
|
ProbeScope::AllTraits,
|
|
|
|
|
|
);
|
|
|
|
|
|
probe.is_ok()
|
|
|
|
|
|
});
|
2016-03-05 17:17:36 +00:00
|
|
|
|
}
|
2016-03-05 00:26:45 +00:00
|
|
|
|
|
2022-03-08 11:04:20 -07:00
|
|
|
|
let mut custom_span_label = false;
|
|
|
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
|
if !static_sources.is_empty() {
|
2016-10-04 09:06:07 +05:30
|
|
|
|
err.note(
|
|
|
|
|
|
"found the following associated functions; to be used as methods, \
|
2020-02-25 12:08:22 -08:00
|
|
|
|
functions must have a `self` parameter",
|
2016-10-04 09:06:07 +05:30
|
|
|
|
);
|
2018-05-28 10:29:49 -07:00
|
|
|
|
err.span_label(span, "this is an associated function, not a method");
|
2022-03-08 11:04:20 -07:00
|
|
|
|
custom_span_label = true;
|
2018-05-28 10:12:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
if static_sources.len() == 1 {
|
2022-04-04 11:48:47 +02:00
|
|
|
|
let ty_str =
|
|
|
|
|
|
if let Some(CandidateSource::Impl(impl_did)) = static_sources.get(0) {
|
|
|
|
|
|
// When the "method" is resolved through dereferencing, we really want the
|
|
|
|
|
|
// original type that has the associated function for accurate suggestions.
|
|
|
|
|
|
// (#61411)
|
|
|
|
|
|
let ty = tcx.at(span).type_of(*impl_did);
|
|
|
|
|
|
match (&ty.peel_refs().kind(), &actual.peel_refs().kind()) {
|
|
|
|
|
|
(ty::Adt(def, _), ty::Adt(def_actual, _)) if def == def_actual => {
|
|
|
|
|
|
// Use `actual` as it will have more `substs` filled in.
|
|
|
|
|
|
self.ty_to_value_string(actual.peel_refs())
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => self.ty_to_value_string(ty.peel_refs()),
|
2019-10-05 16:57:14 -07:00
|
|
|
|
}
|
2022-04-04 11:48:47 +02:00
|
|
|
|
} else {
|
|
|
|
|
|
self.ty_to_value_string(actual.peel_refs())
|
|
|
|
|
|
};
|
2018-12-03 00:56:47 +00:00
|
|
|
|
if let SelfSource::MethodCall(expr) = source {
|
2019-10-05 16:11:36 -07:00
|
|
|
|
err.span_suggestion(
|
|
|
|
|
|
expr.span.to(span),
|
|
|
|
|
|
"use associated function syntax instead",
|
2019-10-05 16:57:14 -07:00
|
|
|
|
format!("{}::{}", ty_str, item_name),
|
2019-10-05 16:11:36 -07:00
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
|
);
|
2018-05-27 13:30:16 -07:00
|
|
|
|
} else {
|
2019-10-05 16:11:36 -07:00
|
|
|
|
err.help(&format!("try with `{}::{}`", ty_str, item_name,));
|
2018-05-27 13:30:16 -07:00
|
|
|
|
}
|
2015-01-14 10:49:17 +11:00
|
|
|
|
|
2019-12-03 22:19:18 -08:00
|
|
|
|
report_candidates(span, &mut err, static_sources, sugg_span);
|
2018-05-28 10:12:23 -07:00
|
|
|
|
} else if static_sources.len() > 1 {
|
2019-12-03 22:19:18 -08:00
|
|
|
|
report_candidates(span, &mut err, static_sources, sugg_span);
|
2016-05-11 08:48:12 +03:00
|
|
|
|
}
|
2015-06-18 22:20:45 -04:00
|
|
|
|
|
2022-03-08 11:04:20 -07:00
|
|
|
|
let mut bound_spans = vec![];
|
2020-02-24 23:25:54 -08:00
|
|
|
|
let mut restrict_type_params = false;
|
2021-05-01 17:18:04 -07:00
|
|
|
|
let mut unsatisfied_bounds = false;
|
2021-07-29 23:23:17 -07:00
|
|
|
|
if item_name.name == sym::count && self.is_slice_ty(actual, span) {
|
|
|
|
|
|
let msg = "consider using `len` instead";
|
|
|
|
|
|
if let SelfSource::MethodCall(_expr) = source {
|
|
|
|
|
|
err.span_suggestion_short(
|
|
|
|
|
|
span,
|
|
|
|
|
|
msg,
|
2022-06-13 15:48:40 +09:00
|
|
|
|
"len",
|
2021-07-29 23:23:17 -07:00
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
err.span_label(span, msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
if let Some(iterator_trait) = self.tcx.get_diagnostic_item(sym::Iterator) {
|
|
|
|
|
|
let iterator_trait = self.tcx.def_path_str(iterator_trait);
|
|
|
|
|
|
err.note(&format!("`count` is defined on `{iterator_trait}`, which `{actual}` does not implement"));
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if !unsatisfied_predicates.is_empty() {
|
2020-02-19 14:26:46 -08:00
|
|
|
|
let mut type_params = FxHashMap::default();
|
2022-03-08 11:04:20 -07:00
|
|
|
|
|
|
|
|
|
|
// Pick out the list of unimplemented traits on the receiver.
|
|
|
|
|
|
// This is used for custom error messages with the `#[rustc_on_unimplemented]` attribute.
|
|
|
|
|
|
let mut unimplemented_traits = FxHashMap::default();
|
2022-03-11 16:58:14 -07:00
|
|
|
|
let mut unimplemented_traits_only = true;
|
2022-03-08 11:04:20 -07:00
|
|
|
|
for (predicate, _parent_pred, cause) in &unsatisfied_predicates {
|
|
|
|
|
|
if let (ty::PredicateKind::Trait(p), Some(cause)) =
|
|
|
|
|
|
(predicate.kind().skip_binder(), cause.as_ref())
|
|
|
|
|
|
{
|
|
|
|
|
|
if p.trait_ref.self_ty() != rcvr_ty {
|
|
|
|
|
|
// This is necessary, not just to keep the errors clean, but also
|
|
|
|
|
|
// because our derived obligations can wind up with a trait ref that
|
|
|
|
|
|
// requires a different param_env to be correctly compared.
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
unimplemented_traits.entry(p.trait_ref.def_id).or_insert((
|
|
|
|
|
|
predicate.kind().rebind(p.trait_ref),
|
|
|
|
|
|
Obligation {
|
|
|
|
|
|
cause: cause.clone(),
|
|
|
|
|
|
param_env: self.param_env,
|
2022-05-26 13:14:24 +02:00
|
|
|
|
predicate: *predicate,
|
2022-03-08 11:04:20 -07:00
|
|
|
|
recursion_depth: 0,
|
|
|
|
|
|
},
|
|
|
|
|
|
));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-06-18 20:41:43 +02:00
|
|
|
|
|
2022-03-11 16:58:14 -07:00
|
|
|
|
// Make sure that, if any traits other than the found ones were involved,
|
|
|
|
|
|
// we don't don't report an unimplemented trait.
|
2022-03-20 20:52:22 +02:00
|
|
|
|
// We don't want to say that `iter::Cloned` is not an iterator, just
|
2022-03-11 16:58:14 -07:00
|
|
|
|
// because of some non-Clone item being iterated over.
|
|
|
|
|
|
for (predicate, _parent_pred, _cause) in &unsatisfied_predicates {
|
|
|
|
|
|
match predicate.kind().skip_binder() {
|
|
|
|
|
|
ty::PredicateKind::Trait(p)
|
|
|
|
|
|
if unimplemented_traits.contains_key(&p.trait_ref.def_id) => {}
|
|
|
|
|
|
_ => {
|
|
|
|
|
|
unimplemented_traits_only = false;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-21 15:42:18 +02:00
|
|
|
|
let mut collect_type_param_suggestions =
|
2022-01-27 17:00:16 +11:00
|
|
|
|
|self_ty: Ty<'tcx>, parent_pred: ty::Predicate<'tcx>, obligation: &str| {
|
2020-06-18 20:41:43 +02:00
|
|
|
|
// We don't care about regions here, so it's fine to skip the binder here.
|
2021-07-22 21:56:07 +08:00
|
|
|
|
if let (ty::Param(_), ty::PredicateKind::Trait(p)) =
|
2021-01-07 11:20:28 -05:00
|
|
|
|
(self_ty.kind(), parent_pred.kind().skip_binder())
|
2020-07-09 00:35:55 +02:00
|
|
|
|
{
|
2021-10-13 16:07:22 +00:00
|
|
|
|
let node = match p.trait_ref.self_ty().kind() {
|
|
|
|
|
|
ty::Param(_) => {
|
|
|
|
|
|
// Account for `fn` items like in `issue-35677.rs` to
|
|
|
|
|
|
// suggest restricting its type params.
|
|
|
|
|
|
let did = self.tcx.hir().body_owner_def_id(hir::BodyId {
|
|
|
|
|
|
hir_id: self.body_id,
|
|
|
|
|
|
});
|
|
|
|
|
|
Some(
|
|
|
|
|
|
self.tcx
|
|
|
|
|
|
.hir()
|
|
|
|
|
|
.get(self.tcx.hir().local_def_id_to_hir_id(did)),
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
2022-03-05 07:28:41 +11:00
|
|
|
|
ty::Adt(def, _) => def.did().as_local().map(|def_id| {
|
2020-08-12 12:22:56 +02:00
|
|
|
|
self.tcx
|
|
|
|
|
|
.hir()
|
|
|
|
|
|
.get(self.tcx.hir().local_def_id_to_hir_id(def_id))
|
2021-10-13 16:07:22 +00:00
|
|
|
|
}),
|
|
|
|
|
|
_ => None,
|
|
|
|
|
|
};
|
|
|
|
|
|
if let Some(hir::Node::Item(hir::Item { kind, .. })) = node {
|
|
|
|
|
|
if let Some(g) = kind.generics() {
|
2022-06-05 17:37:45 -07:00
|
|
|
|
let key = (
|
|
|
|
|
|
g.tail_span_for_predicate_suggestion(),
|
|
|
|
|
|
g.add_where_or_trailing_comma(),
|
|
|
|
|
|
);
|
2021-10-13 16:07:22 +00:00
|
|
|
|
type_params
|
|
|
|
|
|
.entry(key)
|
|
|
|
|
|
.or_insert_with(FxHashSet::default)
|
|
|
|
|
|
.insert(obligation.to_owned());
|
2020-02-19 14:26:46 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-07-21 15:42:18 +02:00
|
|
|
|
};
|
2020-02-18 18:06:03 -08:00
|
|
|
|
let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
|
|
|
|
|
|
let msg = format!(
|
2020-02-19 14:26:46 -08:00
|
|
|
|
"doesn't satisfy `{}`",
|
2020-02-18 18:06:03 -08:00
|
|
|
|
if obligation.len() > 50 { quiet } else { obligation }
|
|
|
|
|
|
);
|
2020-08-03 00:49:11 +02:00
|
|
|
|
match &self_ty.kind() {
|
2020-02-18 18:06:03 -08:00
|
|
|
|
// Point at the type that couldn't satisfy the bound.
|
2022-07-04 17:23:24 +09:00
|
|
|
|
ty::Adt(def, _) => {
|
|
|
|
|
|
bound_spans.push((self.tcx.def_span(def.did()), msg))
|
|
|
|
|
|
}
|
2020-02-18 18:06:03 -08:00
|
|
|
|
// Point at the trait object that couldn't satisfy the bound.
|
2022-04-13 16:11:28 -07:00
|
|
|
|
ty::Dynamic(preds, _, _) => {
|
2020-12-11 15:02:46 -05:00
|
|
|
|
for pred in preds.iter() {
|
|
|
|
|
|
match pred.skip_binder() {
|
2022-07-04 17:23:24 +09:00
|
|
|
|
ty::ExistentialPredicate::Trait(tr) => bound_spans
|
|
|
|
|
|
.push((self.tcx.def_span(tr.def_id), msg.clone())),
|
2020-02-18 17:53:25 -08:00
|
|
|
|
ty::ExistentialPredicate::Projection(_)
|
|
|
|
|
|
| ty::ExistentialPredicate::AutoTrait(_) => {}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-02-18 18:06:03 -08:00
|
|
|
|
// Point at the closure that couldn't satisfy the bound.
|
2022-07-04 17:23:24 +09:00
|
|
|
|
ty::Closure(def_id, _) => bound_spans.push((
|
|
|
|
|
|
tcx.def_span(*def_id),
|
|
|
|
|
|
format!("doesn't satisfy `{}`", quiet),
|
|
|
|
|
|
)),
|
2020-02-18 17:53:25 -08:00
|
|
|
|
_ => {}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2020-05-11 22:06:41 +02:00
|
|
|
|
let mut format_pred = |pred: ty::Predicate<'tcx>| {
|
2021-01-07 11:20:28 -05:00
|
|
|
|
let bound_predicate = pred.kind();
|
2020-10-07 20:02:06 -04:00
|
|
|
|
match bound_predicate.skip_binder() {
|
2021-01-07 11:20:28 -05:00
|
|
|
|
ty::PredicateKind::Projection(pred) => {
|
2020-10-16 14:04:11 -04:00
|
|
|
|
let pred = bound_predicate.rebind(pred);
|
2020-02-18 16:35:47 -08:00
|
|
|
|
// `<Foo as Iterator>::Item = String`.
|
2021-02-12 22:44:43 +00:00
|
|
|
|
let projection_ty = pred.skip_binder().projection_ty;
|
|
|
|
|
|
|
|
|
|
|
|
let substs_with_infer_self = tcx.mk_substs(
|
2021-08-30 17:45:16 -04:00
|
|
|
|
iter::once(tcx.mk_ty_var(ty::TyVid::from_u32(0)).into())
|
2021-02-12 22:44:43 +00:00
|
|
|
|
.chain(projection_ty.substs.iter().skip(1)),
|
2020-02-18 18:06:03 -08:00
|
|
|
|
);
|
2021-02-12 22:44:43 +00:00
|
|
|
|
|
|
|
|
|
|
let quiet_projection_ty = ty::ProjectionTy {
|
|
|
|
|
|
substs: substs_with_infer_self,
|
|
|
|
|
|
item_def_id: projection_ty.item_def_id,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-01-11 19:18:18 +00:00
|
|
|
|
let term = pred.skip_binder().term;
|
2021-02-12 22:44:43 +00:00
|
|
|
|
|
2022-01-11 19:18:18 +00:00
|
|
|
|
let obligation = format!("{} = {}", projection_ty, term);
|
|
|
|
|
|
let quiet = format!("{} = {}", quiet_projection_ty, term);
|
2021-02-12 22:44:43 +00:00
|
|
|
|
|
|
|
|
|
|
bound_span_label(projection_ty.self_ty(), &obligation, &quiet);
|
|
|
|
|
|
Some((obligation, projection_ty.self_ty()))
|
2020-02-18 16:35:47 -08:00
|
|
|
|
}
|
2021-07-22 21:56:07 +08:00
|
|
|
|
ty::PredicateKind::Trait(poly_trait_ref) => {
|
2020-10-07 20:02:06 -04:00
|
|
|
|
let p = poly_trait_ref.trait_ref;
|
2020-02-18 16:35:47 -08:00
|
|
|
|
let self_ty = p.self_ty();
|
|
|
|
|
|
let path = p.print_only_trait_path();
|
2020-02-19 14:26:46 -08:00
|
|
|
|
let obligation = format!("{}: {}", self_ty, path);
|
|
|
|
|
|
let quiet = format!("_: {}", path);
|
2020-02-18 18:06:03 -08:00
|
|
|
|
bound_span_label(self_ty, &obligation, &quiet);
|
2020-02-19 14:26:46 -08:00
|
|
|
|
Some((obligation, self_ty))
|
2020-02-18 02:13:30 -08:00
|
|
|
|
}
|
2020-02-18 16:35:47 -08:00
|
|
|
|
_ => None,
|
2020-02-18 17:53:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
2021-10-13 14:28:28 +00:00
|
|
|
|
|
|
|
|
|
|
// Find all the requirements that come from a local `impl` block.
|
|
|
|
|
|
let mut skip_list: FxHashSet<_> = Default::default();
|
|
|
|
|
|
let mut spanned_predicates: FxHashMap<MultiSpan, _> = Default::default();
|
2022-07-01 16:47:26 -07:00
|
|
|
|
for (data, p, parent_p, impl_def_id, cause) in unsatisfied_predicates
|
2021-10-13 14:28:28 +00:00
|
|
|
|
.iter()
|
|
|
|
|
|
.filter_map(|(p, parent, c)| c.as_ref().map(|c| (p, parent, c)))
|
2021-11-11 12:01:12 +11:00
|
|
|
|
.filter_map(|(p, parent, c)| match c.code() {
|
2021-10-13 14:28:28 +00:00
|
|
|
|
ObligationCauseCode::ImplDerivedObligation(ref data) => {
|
2022-07-01 16:47:26 -07:00
|
|
|
|
Some((&data.derived, p, parent, data.impl_def_id, data))
|
2021-10-13 14:28:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
_ => None,
|
|
|
|
|
|
})
|
|
|
|
|
|
{
|
2021-12-24 22:50:44 +08:00
|
|
|
|
let parent_trait_ref = data.parent_trait_pred;
|
|
|
|
|
|
let path = parent_trait_ref.print_modifiers_and_trait_path();
|
2021-10-13 14:28:28 +00:00
|
|
|
|
let tr_self_ty = parent_trait_ref.skip_binder().self_ty();
|
2022-06-29 21:16:43 +09:00
|
|
|
|
let unsatisfied_msg = "unsatisfied trait bound introduced here";
|
2021-10-13 13:58:41 +00:00
|
|
|
|
let derive_msg =
|
|
|
|
|
|
"unsatisfied trait bound introduced in this `derive` macro";
|
|
|
|
|
|
match self.tcx.hir().get_if_local(impl_def_id) {
|
|
|
|
|
|
// Unmet obligation comes from a `derive` macro, point at it once to
|
|
|
|
|
|
// avoid multiple span labels pointing at the same place.
|
|
|
|
|
|
Some(Node::Item(hir::Item {
|
|
|
|
|
|
kind: hir::ItemKind::Trait(..),
|
|
|
|
|
|
ident,
|
|
|
|
|
|
..
|
|
|
|
|
|
})) if matches!(
|
|
|
|
|
|
ident.span.ctxt().outer_expn_data().kind,
|
|
|
|
|
|
ExpnKind::Macro(MacroKind::Derive, _)
|
|
|
|
|
|
) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
let span = ident.span.ctxt().outer_expn_data().call_site;
|
|
|
|
|
|
let mut spans: MultiSpan = span.into();
|
2022-06-29 21:16:43 +09:00
|
|
|
|
spans.push_span_label(span, derive_msg);
|
2021-10-13 13:58:41 +00:00
|
|
|
|
let entry = spanned_predicates.entry(spans);
|
|
|
|
|
|
entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Some(Node::Item(hir::Item {
|
|
|
|
|
|
kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, .. }),
|
|
|
|
|
|
..
|
|
|
|
|
|
})) if matches!(
|
|
|
|
|
|
self_ty.span.ctxt().outer_expn_data().kind,
|
|
|
|
|
|
ExpnKind::Macro(MacroKind::Derive, _)
|
|
|
|
|
|
) || matches!(
|
|
|
|
|
|
of_trait.as_ref().map(|t| t
|
|
|
|
|
|
.path
|
|
|
|
|
|
.span
|
|
|
|
|
|
.ctxt()
|
|
|
|
|
|
.outer_expn_data()
|
|
|
|
|
|
.kind),
|
|
|
|
|
|
Some(ExpnKind::Macro(MacroKind::Derive, _))
|
|
|
|
|
|
) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
let span = self_ty.span.ctxt().outer_expn_data().call_site;
|
|
|
|
|
|
let mut spans: MultiSpan = span.into();
|
2022-06-29 21:16:43 +09:00
|
|
|
|
spans.push_span_label(span, derive_msg);
|
2022-05-26 13:14:24 +02:00
|
|
|
|
let entry = spanned_predicates.entry(spans);
|
2021-10-13 13:58:41 +00:00
|
|
|
|
entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Unmet obligation coming from a `trait`.
|
|
|
|
|
|
Some(Node::Item(hir::Item {
|
|
|
|
|
|
kind: hir::ItemKind::Trait(..),
|
|
|
|
|
|
ident,
|
|
|
|
|
|
span: item_span,
|
|
|
|
|
|
..
|
|
|
|
|
|
})) if !matches!(
|
|
|
|
|
|
ident.span.ctxt().outer_expn_data().kind,
|
|
|
|
|
|
ExpnKind::Macro(MacroKind::Derive, _)
|
|
|
|
|
|
) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if let Some(pred) = parent_p {
|
|
|
|
|
|
// Done to add the "doesn't satisfy" `span_label`.
|
|
|
|
|
|
let _ = format_pred(*pred);
|
2021-10-13 14:28:28 +00:00
|
|
|
|
}
|
2021-10-13 13:58:41 +00:00
|
|
|
|
skip_list.insert(p);
|
2022-07-01 16:47:26 -07:00
|
|
|
|
let mut spans = if cause.span != *item_span {
|
|
|
|
|
|
let mut spans: MultiSpan = cause.span.into();
|
|
|
|
|
|
spans.push_span_label(cause.span, unsatisfied_msg);
|
2021-10-13 13:58:41 +00:00
|
|
|
|
spans
|
|
|
|
|
|
} else {
|
|
|
|
|
|
ident.span.into()
|
|
|
|
|
|
};
|
2022-06-29 21:16:43 +09:00
|
|
|
|
spans.push_span_label(ident.span, "in this trait");
|
2022-05-26 13:14:24 +02:00
|
|
|
|
let entry = spanned_predicates.entry(spans);
|
2021-10-13 13:58:41 +00:00
|
|
|
|
entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Unmet obligation coming from an `impl`.
|
|
|
|
|
|
Some(Node::Item(hir::Item {
|
2022-07-01 16:47:26 -07:00
|
|
|
|
kind:
|
|
|
|
|
|
hir::ItemKind::Impl(hir::Impl {
|
|
|
|
|
|
of_trait, self_ty, generics, ..
|
|
|
|
|
|
}),
|
2021-10-13 13:58:41 +00:00
|
|
|
|
span: item_span,
|
|
|
|
|
|
..
|
|
|
|
|
|
})) if !matches!(
|
|
|
|
|
|
self_ty.span.ctxt().outer_expn_data().kind,
|
|
|
|
|
|
ExpnKind::Macro(MacroKind::Derive, _)
|
|
|
|
|
|
) && !matches!(
|
|
|
|
|
|
of_trait.as_ref().map(|t| t
|
|
|
|
|
|
.path
|
|
|
|
|
|
.span
|
|
|
|
|
|
.ctxt()
|
|
|
|
|
|
.outer_expn_data()
|
|
|
|
|
|
.kind),
|
|
|
|
|
|
Some(ExpnKind::Macro(MacroKind::Derive, _))
|
|
|
|
|
|
) =>
|
|
|
|
|
|
{
|
2022-07-01 16:47:26 -07:00
|
|
|
|
let sized_pred =
|
|
|
|
|
|
unsatisfied_predicates.iter().any(|(pred, _, _)| {
|
|
|
|
|
|
match pred.kind().skip_binder() {
|
|
|
|
|
|
ty::PredicateKind::Trait(pred) => {
|
|
|
|
|
|
Some(pred.def_id())
|
|
|
|
|
|
== self.tcx.lang_items().sized_trait()
|
|
|
|
|
|
&& pred.polarity == ty::ImplPolarity::Positive
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => false,
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
for param in generics.params {
|
|
|
|
|
|
if param.span == cause.span && sized_pred {
|
|
|
|
|
|
let (sp, sugg) = match param.colon_span {
|
|
|
|
|
|
Some(sp) => (sp.shrink_to_hi(), " ?Sized +"),
|
|
|
|
|
|
None => (param.span.shrink_to_hi(), ": ?Sized"),
|
|
|
|
|
|
};
|
|
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
|
|
sp,
|
|
|
|
|
|
"consider relaxing the type parameter's implicit \
|
|
|
|
|
|
`Sized` bound",
|
|
|
|
|
|
sugg,
|
|
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-10-13 13:58:41 +00:00
|
|
|
|
if let Some(pred) = parent_p {
|
|
|
|
|
|
// Done to add the "doesn't satisfy" `span_label`.
|
|
|
|
|
|
let _ = format_pred(*pred);
|
|
|
|
|
|
}
|
|
|
|
|
|
skip_list.insert(p);
|
2022-07-01 16:47:26 -07:00
|
|
|
|
let mut spans = if cause.span != *item_span {
|
|
|
|
|
|
let mut spans: MultiSpan = cause.span.into();
|
|
|
|
|
|
spans.push_span_label(cause.span, unsatisfied_msg);
|
2021-10-13 13:58:41 +00:00
|
|
|
|
spans
|
|
|
|
|
|
} else {
|
2021-10-13 14:28:28 +00:00
|
|
|
|
let mut spans = Vec::with_capacity(2);
|
|
|
|
|
|
if let Some(trait_ref) = of_trait {
|
|
|
|
|
|
spans.push(trait_ref.path.span);
|
|
|
|
|
|
}
|
|
|
|
|
|
spans.push(self_ty.span);
|
2021-10-13 13:58:41 +00:00
|
|
|
|
spans.into()
|
|
|
|
|
|
};
|
|
|
|
|
|
if let Some(trait_ref) = of_trait {
|
2022-06-29 21:16:43 +09:00
|
|
|
|
spans.push_span_label(trait_ref.path.span, "");
|
2021-10-13 14:28:28 +00:00
|
|
|
|
}
|
2022-06-29 21:16:43 +09:00
|
|
|
|
spans.push_span_label(self_ty.span, "");
|
2021-10-13 13:58:41 +00:00
|
|
|
|
|
2022-05-26 13:14:24 +02:00
|
|
|
|
let entry = spanned_predicates.entry(spans);
|
2021-10-13 13:58:41 +00:00
|
|
|
|
entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p);
|
2021-10-13 14:28:28 +00:00
|
|
|
|
}
|
2021-10-13 13:58:41 +00:00
|
|
|
|
_ => {}
|
2021-10-13 14:28:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-10-13 13:58:41 +00:00
|
|
|
|
let mut spanned_predicates: Vec<_> = spanned_predicates.into_iter().collect();
|
|
|
|
|
|
spanned_predicates.sort_by_key(|(span, (_, _, _))| span.primary_span());
|
|
|
|
|
|
for (span, (_path, _self_ty, preds)) in spanned_predicates {
|
|
|
|
|
|
let mut preds: Vec<_> = preds
|
|
|
|
|
|
.into_iter()
|
|
|
|
|
|
.filter_map(|pred| format_pred(*pred))
|
|
|
|
|
|
.map(|(p, _)| format!("`{}`", p))
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
preds.sort();
|
|
|
|
|
|
preds.dedup();
|
|
|
|
|
|
let msg = if let [pred] = &preds[..] {
|
|
|
|
|
|
format!("trait bound {} was not satisfied", pred)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"the following trait bounds were not satisfied:\n{}",
|
|
|
|
|
|
preds.join("\n"),
|
|
|
|
|
|
)
|
|
|
|
|
|
};
|
|
|
|
|
|
err.span_note(span, &msg);
|
|
|
|
|
|
unsatisfied_bounds = true;
|
2021-10-13 14:28:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// The requirements that didn't have an `impl` span to show.
|
2020-02-18 17:53:25 -08:00
|
|
|
|
let mut bound_list = unsatisfied_predicates
|
|
|
|
|
|
.iter()
|
2021-10-13 14:28:28 +00:00
|
|
|
|
.filter_map(|(pred, parent_pred, _cause)| {
|
2021-10-13 16:07:22 +00:00
|
|
|
|
format_pred(*pred).map(|(p, self_ty)| {
|
2022-01-27 17:00:16 +11:00
|
|
|
|
collect_type_param_suggestions(self_ty, *pred, &p);
|
2021-10-13 13:58:41 +00:00
|
|
|
|
(
|
|
|
|
|
|
match parent_pred {
|
2021-10-13 16:07:22 +00:00
|
|
|
|
None => format!("`{}`", &p),
|
2021-10-13 13:58:41 +00:00
|
|
|
|
Some(parent_pred) => match format_pred(*parent_pred) {
|
|
|
|
|
|
None => format!("`{}`", &p),
|
|
|
|
|
|
Some((parent_p, _)) => {
|
|
|
|
|
|
collect_type_param_suggestions(
|
|
|
|
|
|
self_ty,
|
|
|
|
|
|
*parent_pred,
|
|
|
|
|
|
&p,
|
|
|
|
|
|
);
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"`{}`\nwhich is required by `{}`",
|
|
|
|
|
|
p, parent_p
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2021-10-13 16:07:22 +00:00
|
|
|
|
},
|
2021-10-13 13:58:41 +00:00
|
|
|
|
*pred,
|
|
|
|
|
|
)
|
2020-02-18 17:53:25 -08:00
|
|
|
|
})
|
2020-02-17 23:22:19 -08:00
|
|
|
|
})
|
2021-10-13 13:58:41 +00:00
|
|
|
|
.filter(|(_, pred)| !skip_list.contains(&pred))
|
|
|
|
|
|
.map(|(t, _)| t)
|
2020-02-24 18:55:51 -08:00
|
|
|
|
.enumerate()
|
|
|
|
|
|
.collect::<Vec<(usize, String)>>();
|
2021-10-13 16:07:22 +00:00
|
|
|
|
|
2022-06-05 17:37:45 -07:00
|
|
|
|
for ((span, add_where_or_comma), obligations) in type_params.into_iter() {
|
2020-02-24 23:25:54 -08:00
|
|
|
|
restrict_type_params = true;
|
2020-07-31 09:03:14 +09:00
|
|
|
|
// #74886: Sort here so that the output is always the same.
|
|
|
|
|
|
let mut obligations = obligations.into_iter().collect::<Vec<_>>();
|
|
|
|
|
|
obligations.sort();
|
2020-02-19 14:26:46 -08:00
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
|
|
span,
|
|
|
|
|
|
&format!(
|
|
|
|
|
|
"consider restricting the type parameter{s} to satisfy the \
|
2020-02-25 12:08:22 -08:00
|
|
|
|
trait bound{s}",
|
2020-02-19 14:26:46 -08:00
|
|
|
|
s = pluralize!(obligations.len())
|
|
|
|
|
|
),
|
2022-06-05 18:45:45 -07:00
|
|
|
|
format!("{} {}", add_where_or_comma, obligations.join(", ")),
|
2020-02-19 14:26:46 -08:00
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-30 19:38:50 +02:00
|
|
|
|
bound_list.sort_by(|(_, a), (_, b)| a.cmp(b)); // Sort alphabetically.
|
2020-02-24 18:55:51 -08:00
|
|
|
|
bound_list.dedup_by(|(_, a), (_, b)| a == b); // #35677
|
|
|
|
|
|
bound_list.sort_by_key(|(pos, _)| *pos); // Keep the original predicate order.
|
2022-03-08 11:04:20 -07:00
|
|
|
|
|
2021-10-13 14:28:28 +00:00
|
|
|
|
if !bound_list.is_empty() || !skip_list.is_empty() {
|
2020-02-24 18:55:51 -08:00
|
|
|
|
let bound_list = bound_list
|
|
|
|
|
|
.into_iter()
|
|
|
|
|
|
.map(|(_, path)| path)
|
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
|
.join("\n");
|
2021-01-28 18:16:52 -08:00
|
|
|
|
let actual_prefix = actual.prefix_string(self.tcx);
|
2022-03-08 11:04:20 -07:00
|
|
|
|
info!("unimplemented_traits.len() == {}", unimplemented_traits.len());
|
2022-07-22 18:49:35 +00:00
|
|
|
|
let (primary_message, label) =
|
|
|
|
|
|
if unimplemented_traits.len() == 1 && unimplemented_traits_only {
|
|
|
|
|
|
unimplemented_traits
|
|
|
|
|
|
.into_iter()
|
|
|
|
|
|
.next()
|
|
|
|
|
|
.map(|(_, (trait_ref, obligation))| {
|
|
|
|
|
|
if trait_ref.self_ty().references_error()
|
|
|
|
|
|
|| actual.references_error()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Avoid crashing.
|
|
|
|
|
|
return (None, None);
|
|
|
|
|
|
}
|
|
|
|
|
|
let OnUnimplementedNote { message, label, .. } =
|
|
|
|
|
|
self.on_unimplemented_note(trait_ref, &obligation);
|
|
|
|
|
|
(message, label)
|
|
|
|
|
|
})
|
|
|
|
|
|
.unwrap_or((None, None))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
(None, None)
|
|
|
|
|
|
};
|
2022-03-08 11:04:20 -07:00
|
|
|
|
let primary_message = primary_message.unwrap_or_else(|| format!(
|
2020-09-26 17:20:14 -04:00
|
|
|
|
"the {item_kind} `{item_name}` exists for {actual_prefix} `{ty_str}`, but its trait bounds were not satisfied"
|
|
|
|
|
|
));
|
2022-03-08 11:04:20 -07:00
|
|
|
|
err.set_primary_message(&primary_message);
|
|
|
|
|
|
if let Some(label) = label {
|
|
|
|
|
|
custom_span_label = true;
|
|
|
|
|
|
err.span_label(span, label);
|
|
|
|
|
|
}
|
2021-10-13 14:28:28 +00:00
|
|
|
|
if !bound_list.is_empty() {
|
|
|
|
|
|
err.note(&format!(
|
|
|
|
|
|
"the following trait bounds were not satisfied:\n{bound_list}"
|
|
|
|
|
|
));
|
|
|
|
|
|
}
|
2021-07-26 20:26:23 +02:00
|
|
|
|
self.suggest_derive(&mut err, &unsatisfied_predicates);
|
|
|
|
|
|
|
2021-05-01 17:18:04 -07:00
|
|
|
|
unsatisfied_bounds = true;
|
2020-02-18 02:13:30 -08:00
|
|
|
|
}
|
2016-05-11 08:48:12 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-10 03:39:41 +00:00
|
|
|
|
let label_span_not_found = |err: &mut Diagnostic| {
|
2022-03-08 11:04:20 -07:00
|
|
|
|
if unsatisfied_predicates.is_empty() {
|
|
|
|
|
|
err.span_label(span, format!("{item_kind} not found in `{ty_str}`"));
|
|
|
|
|
|
let is_string_or_ref_str = match actual.kind() {
|
|
|
|
|
|
ty::Ref(_, ty, _) => {
|
|
|
|
|
|
ty.is_str()
|
|
|
|
|
|
|| matches!(
|
|
|
|
|
|
ty.kind(),
|
2022-03-05 07:28:41 +11:00
|
|
|
|
ty::Adt(adt, _) if self.tcx.is_diagnostic_item(sym::String, adt.did())
|
2022-03-08 11:04:20 -07:00
|
|
|
|
)
|
|
|
|
|
|
}
|
2022-03-05 07:28:41 +11:00
|
|
|
|
ty::Adt(adt, _) => self.tcx.is_diagnostic_item(sym::String, adt.did()),
|
2022-03-08 11:04:20 -07:00
|
|
|
|
_ => false,
|
|
|
|
|
|
};
|
|
|
|
|
|
if is_string_or_ref_str && item_name.name == sym::iter {
|
|
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
|
|
item_name.span,
|
|
|
|
|
|
"because of the in-memory representation of `&str`, to obtain \
|
|
|
|
|
|
an `Iterator` over each of its codepoint use method `chars`",
|
2022-06-13 15:48:40 +09:00
|
|
|
|
"chars",
|
2022-03-08 11:04:20 -07:00
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
if let ty::Adt(adt, _) = rcvr_ty.kind() {
|
|
|
|
|
|
let mut inherent_impls_candidate = self
|
|
|
|
|
|
.tcx
|
2022-03-05 07:28:41 +11:00
|
|
|
|
.inherent_impls(adt.did())
|
2022-03-08 11:04:20 -07:00
|
|
|
|
.iter()
|
|
|
|
|
|
.copied()
|
|
|
|
|
|
.filter(|def_id| {
|
|
|
|
|
|
if let Some(assoc) = self.associated_value(*def_id, item_name) {
|
|
|
|
|
|
// Check for both mode is the same so we avoid suggesting
|
|
|
|
|
|
// incorrect associated item.
|
|
|
|
|
|
match (mode, assoc.fn_has_self_parameter, source) {
|
|
|
|
|
|
(Mode::MethodCall, true, SelfSource::MethodCall(_)) => {
|
|
|
|
|
|
// We check that the suggest type is actually
|
|
|
|
|
|
// different from the received one
|
|
|
|
|
|
// So we avoid suggestion method with Box<Self>
|
|
|
|
|
|
// for instance
|
|
|
|
|
|
self.tcx.at(span).type_of(*def_id) != actual
|
|
|
|
|
|
&& self.tcx.at(span).type_of(*def_id) != rcvr_ty
|
|
|
|
|
|
}
|
|
|
|
|
|
(Mode::Path, false, _) => true,
|
|
|
|
|
|
_ => false,
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
false
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
if !inherent_impls_candidate.is_empty() {
|
|
|
|
|
|
inherent_impls_candidate.sort();
|
|
|
|
|
|
inherent_impls_candidate.dedup();
|
|
|
|
|
|
|
|
|
|
|
|
// number of type to shows at most.
|
|
|
|
|
|
let limit = if inherent_impls_candidate.len() == 5 { 5 } else { 4 };
|
|
|
|
|
|
let type_candidates = inherent_impls_candidate
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.take(limit)
|
|
|
|
|
|
.map(|impl_item| {
|
|
|
|
|
|
format!("- `{}`", self.tcx.at(span).type_of(*impl_item))
|
|
|
|
|
|
})
|
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
|
.join("\n");
|
|
|
|
|
|
let additional_types = if inherent_impls_candidate.len() > limit {
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"\nand {} more types",
|
|
|
|
|
|
inherent_impls_candidate.len() - limit
|
|
|
|
|
|
)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
"".to_string()
|
|
|
|
|
|
};
|
|
|
|
|
|
err.note(&format!(
|
|
|
|
|
|
"the {item_kind} was found for\n{}{}",
|
|
|
|
|
|
type_candidates, additional_types
|
|
|
|
|
|
));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
err.span_label(span, format!("{item_kind} cannot be called on `{ty_str}` due to unsatisfied trait bounds"));
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 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)(...)`.
|
|
|
|
|
|
if let SelfSource::MethodCall(expr) = source {
|
2022-04-24 15:24:20 -07:00
|
|
|
|
if !self.suggest_field_call(span, rcvr_ty, expr, item_name, &mut err)
|
|
|
|
|
|
&& lev_candidate.is_none()
|
|
|
|
|
|
&& !custom_span_label
|
|
|
|
|
|
{
|
|
|
|
|
|
label_span_not_found(&mut err);
|
2022-03-08 11:04:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
} else if !custom_span_label {
|
2022-04-24 15:24:20 -07:00
|
|
|
|
label_span_not_found(&mut err);
|
2022-03-08 11:04:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-03 06:54:21 +00:00
|
|
|
|
// Don't suggest (for example) `expr.field.method()` if `expr.method()`
|
|
|
|
|
|
// doesn't exist due to unsatisfied predicates.
|
|
|
|
|
|
if unsatisfied_predicates.is_empty() {
|
|
|
|
|
|
self.check_for_field_method(&mut err, source, span, actual, item_name);
|
|
|
|
|
|
}
|
2022-04-24 16:45:39 -07:00
|
|
|
|
|
2022-09-26 00:55:35 +02:00
|
|
|
|
self.check_for_inner_self(&mut err, source, span, actual, item_name);
|
2022-04-24 16:45:39 -07:00
|
|
|
|
|
2022-03-08 11:04:20 -07:00
|
|
|
|
bound_spans.sort();
|
|
|
|
|
|
bound_spans.dedup();
|
|
|
|
|
|
for (span, msg) in bound_spans.into_iter() {
|
|
|
|
|
|
err.span_label(span, &msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-24 23:25:54 -08:00
|
|
|
|
if actual.is_numeric() && actual.is_fresh() || restrict_type_params {
|
2018-01-03 19:18:17 -08:00
|
|
|
|
} else {
|
|
|
|
|
|
self.suggest_traits_to_import(
|
|
|
|
|
|
&mut err,
|
|
|
|
|
|
span,
|
|
|
|
|
|
rcvr_ty,
|
|
|
|
|
|
item_name,
|
2022-09-01 13:27:31 +09:00
|
|
|
|
args.map(|(_, args)| args.len() + 1),
|
2018-12-03 00:56:47 +00:00
|
|
|
|
source,
|
2018-01-03 19:18:17 -08:00
|
|
|
|
out_of_scope_traits,
|
2020-02-25 12:10:48 -08:00
|
|
|
|
&unsatisfied_predicates,
|
2021-05-01 17:18:04 -07:00
|
|
|
|
unsatisfied_bounds,
|
2018-01-03 19:18:17 -08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
2017-03-20 20:02:51 +01:00
|
|
|
|
|
2020-09-26 17:20:14 -04:00
|
|
|
|
// Don't emit a suggestion if we found an actual method
|
|
|
|
|
|
// that had unsatisfied trait bounds
|
|
|
|
|
|
if unsatisfied_predicates.is_empty() && actual.is_enum() {
|
2019-04-08 17:58:18 -04:00
|
|
|
|
let adt_def = actual.ty_adt_def().expect("enum is not an ADT");
|
|
|
|
|
|
if let Some(suggestion) = lev_distance::find_best_match_for_name(
|
2022-03-05 07:28:41 +11:00
|
|
|
|
&adt_def.variants().iter().map(|s| s.name).collect::<Vec<_>>(),
|
2020-07-08 20:03:37 +10:00
|
|
|
|
item_name.name,
|
2019-04-08 17:58:18 -04:00
|
|
|
|
None,
|
|
|
|
|
|
) {
|
|
|
|
|
|
err.span_suggestion(
|
|
|
|
|
|
span,
|
|
|
|
|
|
"there is a variant with a similar name",
|
2022-06-13 15:48:40 +09:00
|
|
|
|
suggestion,
|
2019-04-08 17:58:18 -04:00
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-08 11:04:10 +10:00
|
|
|
|
if item_name.name == sym::as_str && actual.peel_refs().is_str() {
|
2021-07-29 23:23:17 -07:00
|
|
|
|
let msg = "remove this method call";
|
|
|
|
|
|
let mut fallback_span = true;
|
2019-10-07 16:18:02 -07:00
|
|
|
|
if let SelfSource::MethodCall(expr) = source {
|
|
|
|
|
|
let call_expr =
|
|
|
|
|
|
self.tcx.hir().expect_expr(self.tcx.hir().get_parent_node(expr.hir_id));
|
|
|
|
|
|
if let Some(span) = call_expr.span.trim_start(expr.span) {
|
2022-06-13 15:48:40 +09:00
|
|
|
|
err.span_suggestion(span, msg, "", Applicability::MachineApplicable);
|
2019-10-07 16:18:02 -07:00
|
|
|
|
fallback_span = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if fallback_span {
|
|
|
|
|
|
err.span_label(span, msg);
|
|
|
|
|
|
}
|
2019-09-24 17:49:25 +08:00
|
|
|
|
} else if let Some(lev_candidate) = lev_candidate {
|
2020-09-26 17:20:14 -04:00
|
|
|
|
// Don't emit a suggestion if we found an actual method
|
|
|
|
|
|
// that had unsatisfied trait bounds
|
|
|
|
|
|
if unsatisfied_predicates.is_empty() {
|
|
|
|
|
|
let def_kind = lev_candidate.kind.as_def_kind();
|
2022-07-08 12:42:29 -07:00
|
|
|
|
// Methods are defined within the context of a struct and their first parameter is always self,
|
|
|
|
|
|
// which represents the instance of the struct the method is being called on
|
|
|
|
|
|
// Associated functions don’t take self as a parameter and
|
|
|
|
|
|
// they are not methods because they don’t have an instance of the struct to work with.
|
|
|
|
|
|
if def_kind == DefKind::AssocFn && lev_candidate.fn_has_self_parameter {
|
|
|
|
|
|
err.span_suggestion(
|
|
|
|
|
|
span,
|
|
|
|
|
|
&format!("there is a method with a similar name",),
|
|
|
|
|
|
lev_candidate.name,
|
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
err.span_suggestion(
|
|
|
|
|
|
span,
|
|
|
|
|
|
&format!(
|
|
|
|
|
|
"there is {} {} with a similar name",
|
|
|
|
|
|
def_kind.article(),
|
|
|
|
|
|
def_kind.descr(lev_candidate.def_id),
|
|
|
|
|
|
),
|
|
|
|
|
|
lev_candidate.name,
|
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2020-09-26 17:20:14 -04:00
|
|
|
|
}
|
2017-03-20 20:02:51 +01:00
|
|
|
|
}
|
2019-04-08 17:58:18 -04:00
|
|
|
|
|
2022-09-03 07:24:55 +00:00
|
|
|
|
self.check_for_deref_method(&mut err, source, rcvr_ty, item_name);
|
|
|
|
|
|
|
2019-09-01 02:22:42 -07:00
|
|
|
|
return Some(err);
|
2015-12-21 10:00:43 +13:00
|
|
|
|
}
|
2015-12-23 19:27:20 +13:00
|
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
|
MethodError::Ambiguity(sources) => {
|
2016-10-04 09:06:07 +05:30
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
|
|
self.sess(),
|
2020-03-22 16:09:42 -07:00
|
|
|
|
item_name.span,
|
2016-10-04 09:06:07 +05:30
|
|
|
|
E0034,
|
2016-05-11 08:48:12 +03:00
|
|
|
|
"multiple applicable items in scope"
|
|
|
|
|
|
);
|
2020-03-22 16:09:42 -07:00
|
|
|
|
err.span_label(item_name.span, format!("multiple `{}` found", item_name));
|
2015-01-13 17:05:04 +11:00
|
|
|
|
|
2019-12-03 22:19:18 -08:00
|
|
|
|
report_candidates(span, &mut err, sources, sugg_span);
|
2016-05-11 08:48:12 +03:00
|
|
|
|
err.emit();
|
|
|
|
|
|
}
|
2015-01-13 17:05:04 +11:00
|
|
|
|
|
2019-08-04 02:07:35 +03:00
|
|
|
|
MethodError::PrivateMatch(kind, def_id, out_of_scope_traits) => {
|
2020-03-22 15:36:54 -07:00
|
|
|
|
let kind = kind.descr(def_id);
|
2017-08-08 14:12:06 +02:00
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
|
|
self.tcx.sess,
|
2020-03-22 16:09:42 -07:00
|
|
|
|
item_name.span,
|
2017-08-08 14:12:06 +02:00
|
|
|
|
E0624,
|
2019-08-04 02:07:35 +03:00
|
|
|
|
"{} `{}` is private",
|
2020-03-22 15:36:54 -07:00
|
|
|
|
kind,
|
2019-08-04 02:07:35 +03:00
|
|
|
|
item_name
|
|
|
|
|
|
);
|
2020-03-22 16:09:42 -07:00
|
|
|
|
err.span_label(item_name.span, &format!("private {}", kind));
|
2021-07-17 11:13:50 -07:00
|
|
|
|
let sp = self
|
|
|
|
|
|
.tcx
|
|
|
|
|
|
.hir()
|
|
|
|
|
|
.span_if_local(def_id)
|
|
|
|
|
|
.unwrap_or_else(|| self.tcx.def_span(def_id));
|
|
|
|
|
|
err.span_label(sp, &format!("private {} defined here", kind));
|
2017-08-08 14:12:06 +02:00
|
|
|
|
self.suggest_valid_traits(&mut err, out_of_scope_traits);
|
|
|
|
|
|
err.emit();
|
2016-05-11 08:48:12 +03:00
|
|
|
|
}
|
2017-08-03 13:50:06 +02:00
|
|
|
|
|
2020-01-30 20:12:46 -08:00
|
|
|
|
MethodError::IllegalSizedBound(candidates, needs_mut, bound_span) => {
|
2017-08-03 13:50:06 +02:00
|
|
|
|
let msg = format!("the `{}` method cannot be invoked on a trait object", item_name);
|
|
|
|
|
|
let mut err = self.sess().struct_span_err(span, &msg);
|
2020-01-30 20:12:46 -08:00
|
|
|
|
err.span_label(bound_span, "this has a `Sized` requirement");
|
2017-08-03 13:50:06 +02:00
|
|
|
|
if !candidates.is_empty() {
|
2019-10-05 11:37:21 -07:00
|
|
|
|
let help = format!(
|
|
|
|
|
|
"{an}other candidate{s} {were} found in the following trait{s}, perhaps \
|
|
|
|
|
|
add a `use` for {one_of_them}:",
|
|
|
|
|
|
an = if candidates.len() == 1 { "an" } else { "" },
|
2019-11-05 15:10:24 -05:00
|
|
|
|
s = pluralize!(candidates.len()),
|
2022-07-27 10:09:06 +09:00
|
|
|
|
were = pluralize!("was", candidates.len()),
|
2019-10-05 11:37:21 -07:00
|
|
|
|
one_of_them = if candidates.len() == 1 { "it" } else { "one_of_them" },
|
|
|
|
|
|
);
|
2017-08-03 13:50:06 +02:00
|
|
|
|
self.suggest_use_candidates(&mut err, help, candidates);
|
|
|
|
|
|
}
|
2020-08-03 00:49:11 +02:00
|
|
|
|
if let ty::Ref(region, t_type, mutability) = rcvr_ty.kind() {
|
2019-10-03 17:10:37 -07:00
|
|
|
|
if needs_mut {
|
2019-10-05 11:37:21 -07:00
|
|
|
|
let trait_type = self.tcx.mk_ref(
|
2022-01-28 11:25:15 +11:00
|
|
|
|
*region,
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
|
ty::TypeAndMut { ty: *t_type, mutbl: mutability.invert() },
|
2019-10-05 11:37:21 -07:00
|
|
|
|
);
|
2019-10-03 17:39:58 -07:00
|
|
|
|
err.note(&format!("you need `{}` instead of `{}`", trait_type, rcvr_ty));
|
2019-10-03 17:10:37 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-08-03 13:50:06 +02:00
|
|
|
|
err.emit();
|
|
|
|
|
|
}
|
2017-08-16 21:23:44 +03:00
|
|
|
|
|
|
|
|
|
|
MethodError::BadReturnType => bug!("no return type expectations but got BadReturnType"),
|
2017-08-03 13:50:06 +02:00
|
|
|
|
}
|
2019-09-01 02:22:42 -07:00
|
|
|
|
None
|
2017-08-03 13:50:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-24 15:24:20 -07:00
|
|
|
|
fn suggest_field_call(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
span: Span,
|
|
|
|
|
|
rcvr_ty: Ty<'tcx>,
|
|
|
|
|
|
expr: &hir::Expr<'_>,
|
|
|
|
|
|
item_name: Ident,
|
2022-08-10 03:39:41 +00:00
|
|
|
|
err: &mut Diagnostic,
|
2022-04-24 15:24:20 -07:00
|
|
|
|
) -> bool {
|
|
|
|
|
|
let tcx = self.tcx;
|
|
|
|
|
|
let field_receiver = self.autoderef(span, rcvr_ty).find_map(|(ty, _)| match ty.kind() {
|
|
|
|
|
|
ty::Adt(def, substs) if !def.is_enum() => {
|
|
|
|
|
|
let variant = &def.non_enum_variant();
|
|
|
|
|
|
tcx.find_field_index(item_name, variant).map(|index| {
|
|
|
|
|
|
let field = &variant.fields[index];
|
|
|
|
|
|
let field_ty = field.ty(tcx, substs);
|
|
|
|
|
|
(field, field_ty)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => None,
|
|
|
|
|
|
});
|
|
|
|
|
|
if let Some((field, field_ty)) = field_receiver {
|
2022-08-28 00:10:06 +03:00
|
|
|
|
let scope = tcx.parent_module(self.body_id);
|
2022-04-24 15:24:20 -07:00
|
|
|
|
let is_accessible = field.vis.is_accessible_from(scope, tcx);
|
|
|
|
|
|
|
|
|
|
|
|
if is_accessible {
|
|
|
|
|
|
if self.is_fn_ty(field_ty, span) {
|
|
|
|
|
|
let expr_span = expr.span.to(item_name.span);
|
|
|
|
|
|
err.multipart_suggestion(
|
|
|
|
|
|
&format!(
|
|
|
|
|
|
"to call the function stored in `{}`, \
|
|
|
|
|
|
surround the field access with parentheses",
|
|
|
|
|
|
item_name,
|
|
|
|
|
|
),
|
|
|
|
|
|
vec![
|
|
|
|
|
|
(expr_span.shrink_to_lo(), '('.to_string()),
|
|
|
|
|
|
(expr_span.shrink_to_hi(), ')'.to_string()),
|
|
|
|
|
|
],
|
|
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
let call_expr = tcx.hir().expect_expr(tcx.hir().get_parent_node(expr.hir_id));
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(span) = call_expr.span.trim_start(item_name.span) {
|
|
|
|
|
|
err.span_suggestion(
|
|
|
|
|
|
span,
|
|
|
|
|
|
"remove the arguments",
|
2022-06-13 15:48:40 +09:00
|
|
|
|
"",
|
2022-04-24 15:24:20 -07:00
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let field_kind = if is_accessible { "field" } else { "private field" };
|
|
|
|
|
|
err.span_label(item_name.span, format!("{}, not a method", field_kind));
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn suggest_constraining_numerical_ty(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
|
actual: Ty<'tcx>,
|
|
|
|
|
|
source: SelfSource<'_>,
|
|
|
|
|
|
span: Span,
|
|
|
|
|
|
item_kind: &str,
|
|
|
|
|
|
item_name: Ident,
|
|
|
|
|
|
ty_str: &str,
|
|
|
|
|
|
) -> bool {
|
|
|
|
|
|
let found_candidate = all_traits(self.tcx)
|
|
|
|
|
|
.into_iter()
|
|
|
|
|
|
.any(|info| self.associated_value(info.def_id, item_name).is_some());
|
|
|
|
|
|
let found_assoc = |ty: Ty<'tcx>| {
|
2022-05-13 20:23:22 +02:00
|
|
|
|
simplify_type(tcx, ty, TreatParams::AsInfer)
|
2022-04-24 15:24:20 -07:00
|
|
|
|
.and_then(|simp| {
|
|
|
|
|
|
tcx.incoherent_impls(simp)
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.find_map(|&id| self.associated_value(id, item_name))
|
|
|
|
|
|
})
|
|
|
|
|
|
.is_some()
|
|
|
|
|
|
};
|
|
|
|
|
|
let found_candidate = found_candidate
|
|
|
|
|
|
|| found_assoc(tcx.types.i8)
|
|
|
|
|
|
|| found_assoc(tcx.types.i16)
|
|
|
|
|
|
|| found_assoc(tcx.types.i32)
|
|
|
|
|
|
|| found_assoc(tcx.types.i64)
|
|
|
|
|
|
|| found_assoc(tcx.types.i128)
|
|
|
|
|
|
|| found_assoc(tcx.types.u8)
|
|
|
|
|
|
|| found_assoc(tcx.types.u16)
|
|
|
|
|
|
|| found_assoc(tcx.types.u32)
|
|
|
|
|
|
|| found_assoc(tcx.types.u64)
|
|
|
|
|
|
|| found_assoc(tcx.types.u128)
|
|
|
|
|
|
|| found_assoc(tcx.types.f32)
|
|
|
|
|
|
|| found_assoc(tcx.types.f32);
|
|
|
|
|
|
if found_candidate
|
|
|
|
|
|
&& actual.is_numeric()
|
|
|
|
|
|
&& !actual.has_concrete_skeleton()
|
|
|
|
|
|
&& let SelfSource::MethodCall(expr) = source
|
|
|
|
|
|
{
|
|
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
|
|
tcx.sess,
|
|
|
|
|
|
span,
|
|
|
|
|
|
E0689,
|
|
|
|
|
|
"can't call {} `{}` on ambiguous numeric type `{}`",
|
|
|
|
|
|
item_kind,
|
|
|
|
|
|
item_name,
|
|
|
|
|
|
ty_str
|
|
|
|
|
|
);
|
|
|
|
|
|
let concrete_type = if actual.is_integral() { "i32" } else { "f32" };
|
|
|
|
|
|
match expr.kind {
|
|
|
|
|
|
ExprKind::Lit(ref lit) => {
|
|
|
|
|
|
// numeric literal
|
|
|
|
|
|
let snippet = tcx
|
|
|
|
|
|
.sess
|
|
|
|
|
|
.source_map()
|
|
|
|
|
|
.span_to_snippet(lit.span)
|
|
|
|
|
|
.unwrap_or_else(|_| "<numeric literal>".to_owned());
|
|
|
|
|
|
|
|
|
|
|
|
// If this is a floating point literal that ends with '.',
|
|
|
|
|
|
// get rid of it to stop this from becoming a member access.
|
|
|
|
|
|
let snippet = snippet.strip_suffix('.').unwrap_or(&snippet);
|
|
|
|
|
|
|
|
|
|
|
|
err.span_suggestion(
|
|
|
|
|
|
lit.span,
|
|
|
|
|
|
&format!(
|
|
|
|
|
|
"you must specify a concrete type for this numeric value, \
|
|
|
|
|
|
like `{}`",
|
|
|
|
|
|
concrete_type
|
|
|
|
|
|
),
|
|
|
|
|
|
format!("{snippet}_{concrete_type}"),
|
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
ExprKind::Path(QPath::Resolved(_, path)) => {
|
|
|
|
|
|
// local binding
|
|
|
|
|
|
if let hir::def::Res::Local(hir_id) = path.res {
|
|
|
|
|
|
let span = tcx.hir().span(hir_id);
|
|
|
|
|
|
let filename = tcx.sess.source_map().span_to_filename(span);
|
|
|
|
|
|
|
|
|
|
|
|
let parent_node =
|
|
|
|
|
|
self.tcx.hir().get(self.tcx.hir().get_parent_node(hir_id));
|
|
|
|
|
|
let msg = format!(
|
|
|
|
|
|
"you must specify a type for this binding, like `{}`",
|
|
|
|
|
|
concrete_type,
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2022-08-27 02:23:21 +09:00
|
|
|
|
match (filename, parent_node) {
|
2022-04-24 15:24:20 -07:00
|
|
|
|
(
|
|
|
|
|
|
FileName::Real(_),
|
|
|
|
|
|
Node::Local(hir::Local {
|
|
|
|
|
|
source: hir::LocalSource::Normal,
|
|
|
|
|
|
ty,
|
|
|
|
|
|
..
|
|
|
|
|
|
}),
|
|
|
|
|
|
) => {
|
2022-08-27 02:23:21 +09:00
|
|
|
|
let type_span = ty.map(|ty| ty.span.with_lo(span.hi())).unwrap_or(span.shrink_to_hi());
|
2022-04-24 15:24:20 -07:00
|
|
|
|
err.span_suggestion(
|
|
|
|
|
|
// account for `let x: _ = 42;`
|
2022-08-27 02:23:21 +09:00
|
|
|
|
// ^^^
|
|
|
|
|
|
type_span,
|
2022-04-24 15:24:20 -07:00
|
|
|
|
&msg,
|
2022-08-27 02:23:21 +09:00
|
|
|
|
format!(": {concrete_type}"),
|
2022-04-24 15:24:20 -07:00
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => {
|
|
|
|
|
|
err.span_label(span, msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => {}
|
|
|
|
|
|
}
|
|
|
|
|
|
err.emit();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-26 19:59:50 -07:00
|
|
|
|
fn check_for_field_method(
|
|
|
|
|
|
&self,
|
2022-08-10 03:39:41 +00:00
|
|
|
|
err: &mut Diagnostic,
|
2022-04-26 19:59:50 -07:00
|
|
|
|
source: SelfSource<'tcx>,
|
|
|
|
|
|
span: Span,
|
|
|
|
|
|
actual: Ty<'tcx>,
|
|
|
|
|
|
item_name: Ident,
|
|
|
|
|
|
) {
|
|
|
|
|
|
if let SelfSource::MethodCall(expr) = source
|
2022-08-23 01:58:33 +00:00
|
|
|
|
&& let mod_id = self.tcx.parent_module(expr.hir_id).to_def_id()
|
|
|
|
|
|
&& let Some((fields, substs)) =
|
|
|
|
|
|
self.get_field_candidates_considering_privacy(span, actual, mod_id)
|
2022-04-26 19:59:50 -07:00
|
|
|
|
{
|
|
|
|
|
|
let call_expr = self.tcx.hir().expect_expr(self.tcx.hir().get_parent_node(expr.hir_id));
|
2022-08-23 01:58:33 +00:00
|
|
|
|
|
|
|
|
|
|
let lang_items = self.tcx.lang_items();
|
|
|
|
|
|
let never_mention_traits = [
|
|
|
|
|
|
lang_items.clone_trait(),
|
|
|
|
|
|
lang_items.deref_trait(),
|
|
|
|
|
|
lang_items.deref_mut_trait(),
|
|
|
|
|
|
self.tcx.get_diagnostic_item(sym::AsRef),
|
|
|
|
|
|
self.tcx.get_diagnostic_item(sym::AsMut),
|
|
|
|
|
|
self.tcx.get_diagnostic_item(sym::Borrow),
|
|
|
|
|
|
self.tcx.get_diagnostic_item(sym::BorrowMut),
|
|
|
|
|
|
];
|
|
|
|
|
|
let candidate_fields: Vec<_> = fields
|
|
|
|
|
|
.filter_map(|candidate_field| {
|
|
|
|
|
|
self.check_for_nested_field_satisfying(
|
|
|
|
|
|
span,
|
|
|
|
|
|
&|_, field_ty| {
|
|
|
|
|
|
self.lookup_probe(
|
|
|
|
|
|
span,
|
|
|
|
|
|
item_name,
|
|
|
|
|
|
field_ty,
|
|
|
|
|
|
call_expr,
|
|
|
|
|
|
ProbeScope::TraitsInScope,
|
|
|
|
|
|
)
|
|
|
|
|
|
.map_or(false, |pick| {
|
|
|
|
|
|
!never_mention_traits
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.flatten()
|
|
|
|
|
|
.any(|def_id| self.tcx.parent(pick.item.def_id) == *def_id)
|
|
|
|
|
|
})
|
|
|
|
|
|
},
|
|
|
|
|
|
candidate_field,
|
|
|
|
|
|
substs,
|
|
|
|
|
|
vec![],
|
|
|
|
|
|
mod_id,
|
|
|
|
|
|
)
|
|
|
|
|
|
})
|
|
|
|
|
|
.map(|field_path| {
|
|
|
|
|
|
field_path
|
2022-04-26 19:59:50 -07:00
|
|
|
|
.iter()
|
|
|
|
|
|
.map(|id| id.name.to_ident_string())
|
|
|
|
|
|
.collect::<Vec<String>>()
|
2022-08-23 01:58:33 +00:00
|
|
|
|
.join(".")
|
|
|
|
|
|
})
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
|
|
let len = candidate_fields.len();
|
|
|
|
|
|
if len > 0 {
|
|
|
|
|
|
err.span_suggestions(
|
|
|
|
|
|
item_name.span.shrink_to_lo(),
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"{} of the expressions' fields {} a method of the same name",
|
|
|
|
|
|
if len > 1 { "some" } else { "one" },
|
|
|
|
|
|
if len > 1 { "have" } else { "has" },
|
|
|
|
|
|
),
|
|
|
|
|
|
candidate_fields.iter().map(|path| format!("{path}.")),
|
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
2022-04-26 19:59:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-26 00:55:35 +02:00
|
|
|
|
fn check_for_inner_self(
|
2022-04-26 19:59:50 -07:00
|
|
|
|
&self,
|
2022-08-10 03:39:41 +00:00
|
|
|
|
err: &mut Diagnostic,
|
2022-04-26 19:59:50 -07:00
|
|
|
|
source: SelfSource<'tcx>,
|
|
|
|
|
|
span: Span,
|
|
|
|
|
|
actual: Ty<'tcx>,
|
|
|
|
|
|
item_name: Ident,
|
|
|
|
|
|
) {
|
|
|
|
|
|
let tcx = self.tcx;
|
|
|
|
|
|
let SelfSource::MethodCall(expr) = source else { return; };
|
|
|
|
|
|
let call_expr = tcx.hir().expect_expr(tcx.hir().get_parent_node(expr.hir_id));
|
|
|
|
|
|
|
|
|
|
|
|
let ty::Adt(kind, substs) = actual.kind() else { return; };
|
2022-09-26 00:55:35 +02:00
|
|
|
|
match kind.adt_kind() {
|
|
|
|
|
|
ty::AdtKind::Enum => {
|
|
|
|
|
|
let matching_variants: Vec<_> = kind
|
|
|
|
|
|
.variants()
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.flat_map(|variant| {
|
|
|
|
|
|
let [field] = &variant.fields[..] else { return None; };
|
|
|
|
|
|
let field_ty = field.ty(tcx, substs);
|
|
|
|
|
|
|
|
|
|
|
|
// Skip `_`, since that'll just lead to ambiguity.
|
|
|
|
|
|
if self.resolve_vars_if_possible(field_ty).is_ty_var() {
|
|
|
|
|
|
return None;
|
|
|
|
|
|
}
|
2022-04-26 19:59:50 -07:00
|
|
|
|
|
2022-09-26 00:55:35 +02:00
|
|
|
|
self.lookup_probe(
|
|
|
|
|
|
span,
|
|
|
|
|
|
item_name,
|
|
|
|
|
|
field_ty,
|
|
|
|
|
|
call_expr,
|
2022-09-27 13:06:31 +02:00
|
|
|
|
ProbeScope::TraitsInScope,
|
2022-09-26 00:55:35 +02:00
|
|
|
|
)
|
|
|
|
|
|
.ok()
|
|
|
|
|
|
.map(|pick| (variant, field, pick))
|
|
|
|
|
|
})
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
|
|
let ret_ty_matches = |diagnostic_item| {
|
|
|
|
|
|
if let Some(ret_ty) = self
|
|
|
|
|
|
.ret_coercion
|
|
|
|
|
|
.as_ref()
|
|
|
|
|
|
.map(|c| self.resolve_vars_if_possible(c.borrow().expected_ty()))
|
|
|
|
|
|
&& let ty::Adt(kind, _) = ret_ty.kind()
|
|
|
|
|
|
&& tcx.get_diagnostic_item(diagnostic_item) == Some(kind.did())
|
|
|
|
|
|
{
|
|
|
|
|
|
true
|
|
|
|
|
|
} else {
|
|
|
|
|
|
false
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2022-04-26 19:59:50 -07:00
|
|
|
|
|
2022-09-26 00:55:35 +02:00
|
|
|
|
match &matching_variants[..] {
|
|
|
|
|
|
[(_, field, pick)] => {
|
|
|
|
|
|
let self_ty = field.ty(tcx, substs);
|
|
|
|
|
|
err.span_note(
|
|
|
|
|
|
tcx.def_span(pick.item.def_id),
|
|
|
|
|
|
&format!("the method `{item_name}` exists on the type `{self_ty}`"),
|
|
|
|
|
|
);
|
|
|
|
|
|
let (article, kind, variant, question) =
|
|
|
|
|
|
if tcx.is_diagnostic_item(sym::Result, kind.did()) {
|
|
|
|
|
|
("a", "Result", "Err", ret_ty_matches(sym::Result))
|
|
|
|
|
|
} else if tcx.is_diagnostic_item(sym::Option, kind.did()) {
|
|
|
|
|
|
("an", "Option", "None", ret_ty_matches(sym::Option))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return;
|
|
|
|
|
|
};
|
|
|
|
|
|
if question {
|
|
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
|
|
expr.span.shrink_to_hi(),
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"use the `?` operator to extract the `{self_ty}` value, propagating \
|
|
|
|
|
|
{article} `{kind}::{variant}` value to the caller"
|
|
|
|
|
|
),
|
|
|
|
|
|
"?",
|
|
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
|
|
expr.span.shrink_to_hi(),
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"consider using `{kind}::expect` to unwrap the `{self_ty}` value, \
|
|
|
|
|
|
panicking if the value is {article} `{kind}::{variant}`"
|
|
|
|
|
|
),
|
|
|
|
|
|
".expect(\"REASON\")",
|
|
|
|
|
|
Applicability::HasPlaceholders,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// FIXME(compiler-errors): Support suggestions for other matching enum variants
|
|
|
|
|
|
_ => {}
|
2022-04-26 19:59:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-09-26 00:55:35 +02:00
|
|
|
|
// Target wrapper types - types that wrap or pretend to wrap another type,
|
|
|
|
|
|
// perhaps this inner type is meant to be called?
|
|
|
|
|
|
ty::AdtKind::Struct | ty::AdtKind::Union => {
|
|
|
|
|
|
let [first] = ***substs else { return; };
|
|
|
|
|
|
let ty::GenericArgKind::Type(ty) = first.unpack() else { return; };
|
|
|
|
|
|
let Ok(pick) = self.lookup_probe(
|
|
|
|
|
|
span,
|
|
|
|
|
|
item_name,
|
|
|
|
|
|
ty,
|
|
|
|
|
|
call_expr,
|
2022-09-27 13:06:31 +02:00
|
|
|
|
ProbeScope::TraitsInScope,
|
2022-09-26 00:55:35 +02:00
|
|
|
|
) else { return; };
|
|
|
|
|
|
|
|
|
|
|
|
let name = self.ty_to_value_string(actual);
|
|
|
|
|
|
let inner_id = kind.did();
|
2022-09-27 13:06:31 +02:00
|
|
|
|
let mutable = if let Some(AutorefOrPtrAdjustment::Autoref { mutbl, .. }) =
|
|
|
|
|
|
pick.autoref_or_ptr_adjustment
|
|
|
|
|
|
{
|
|
|
|
|
|
Some(mutbl)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
None
|
|
|
|
|
|
};
|
2022-09-26 00:55:35 +02:00
|
|
|
|
|
|
|
|
|
|
if tcx.is_diagnostic_item(sym::LocalKey, inner_id) {
|
2022-09-27 13:06:31 +02:00
|
|
|
|
err.help("use `with` or `try_with` to access thread local storage");
|
2022-09-26 00:55:35 +02:00
|
|
|
|
} else if Some(kind.did()) == tcx.lang_items().maybe_uninit() {
|
|
|
|
|
|
err.help(format!(
|
|
|
|
|
|
"if this `{name}` has been initialized, \
|
|
|
|
|
|
use one of the `assume_init` methods to access the inner value"
|
|
|
|
|
|
));
|
|
|
|
|
|
} else if tcx.is_diagnostic_item(sym::RefCell, inner_id) {
|
2022-09-27 13:06:31 +02:00
|
|
|
|
match mutable {
|
|
|
|
|
|
Some(Mutability::Not) => {
|
2022-09-26 00:55:35 +02:00
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
|
|
expr.span.shrink_to_hi(),
|
|
|
|
|
|
format!(
|
2022-09-27 13:06:31 +02:00
|
|
|
|
"use `.borrow()` to borrow the `{ty}`, \
|
|
|
|
|
|
panicking if any outstanding mutable borrows exist."
|
2022-09-26 00:55:35 +02:00
|
|
|
|
),
|
|
|
|
|
|
".borrow()",
|
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2022-09-27 13:06:31 +02:00
|
|
|
|
Some(Mutability::Mut) => {
|
2022-09-26 00:55:35 +02:00
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
|
|
expr.span.shrink_to_hi(),
|
|
|
|
|
|
format!(
|
2022-09-27 13:06:31 +02:00
|
|
|
|
"use `.borrow_mut()` to mutably borrow the `{ty}`, \
|
|
|
|
|
|
panicking if any outstanding borrows exist."
|
2022-09-26 00:55:35 +02:00
|
|
|
|
),
|
|
|
|
|
|
".borrow_mut()",
|
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2022-09-27 13:06:31 +02:00
|
|
|
|
None => return,
|
2022-09-26 00:55:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
} else if tcx.is_diagnostic_item(sym::Mutex, inner_id) {
|
2022-04-26 19:59:50 -07:00
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
|
|
expr.span.shrink_to_hi(),
|
2022-05-30 14:18:03 -07:00
|
|
|
|
format!(
|
2022-09-27 13:06:31 +02:00
|
|
|
|
"use `.lock()` to borrow the `{ty}`, \
|
2022-09-26 00:55:35 +02:00
|
|
|
|
blocking the current thread until it can be acquired"
|
2022-05-30 14:18:03 -07:00
|
|
|
|
),
|
2022-09-26 00:55:35 +02:00
|
|
|
|
".lock().unwrap()",
|
|
|
|
|
|
Applicability::MaybeIncorrect,
|
2022-04-26 19:59:50 -07:00
|
|
|
|
);
|
2022-09-27 13:06:31 +02:00
|
|
|
|
} else if tcx.is_diagnostic_item(sym::RwLock, inner_id) {
|
|
|
|
|
|
match mutable {
|
|
|
|
|
|
Some(Mutability::Not) => {
|
|
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
|
|
expr.span.shrink_to_hi(),
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"use `.read()` to borrow the `{ty}`, \
|
|
|
|
|
|
blocking the current thread until it can be acquired"
|
|
|
|
|
|
),
|
|
|
|
|
|
".read().unwrap()",
|
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
Some(Mutability::Mut) => {
|
|
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
|
|
expr.span.shrink_to_hi(),
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"use `.write()` to mutably borrow the `{ty}`, \
|
|
|
|
|
|
blocking the current thread until it can be acquired"
|
|
|
|
|
|
),
|
|
|
|
|
|
".write().unwrap()",
|
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
None => return,
|
|
|
|
|
|
}
|
2022-04-26 19:59:50 -07:00
|
|
|
|
} else {
|
2022-09-26 00:55:35 +02:00
|
|
|
|
return;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
err.span_note(
|
|
|
|
|
|
tcx.def_span(pick.item.def_id),
|
|
|
|
|
|
&format!("the method `{item_name}` exists on the type `{ty}`"),
|
|
|
|
|
|
);
|
2022-04-26 19:59:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
|
pub(crate) fn note_unmet_impls_on_type(
|
2021-09-28 14:48:54 +00:00
|
|
|
|
&self,
|
2022-01-23 20:41:46 +00:00
|
|
|
|
err: &mut Diagnostic,
|
2021-09-28 14:48:54 +00:00
|
|
|
|
errors: Vec<FulfillmentError<'tcx>>,
|
|
|
|
|
|
) {
|
|
|
|
|
|
let all_local_types_needing_impls =
|
|
|
|
|
|
errors.iter().all(|e| match e.obligation.predicate.kind().skip_binder() {
|
|
|
|
|
|
ty::PredicateKind::Trait(pred) => match pred.self_ty().kind() {
|
2022-03-05 07:28:41 +11:00
|
|
|
|
ty::Adt(def, _) => def.did().is_local(),
|
2021-09-28 14:48:54 +00:00
|
|
|
|
_ => false,
|
|
|
|
|
|
},
|
|
|
|
|
|
_ => false,
|
|
|
|
|
|
});
|
|
|
|
|
|
let mut preds: Vec<_> = errors
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.filter_map(|e| match e.obligation.predicate.kind().skip_binder() {
|
|
|
|
|
|
ty::PredicateKind::Trait(pred) => Some(pred),
|
|
|
|
|
|
_ => None,
|
|
|
|
|
|
})
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
preds.sort_by_key(|pred| (pred.def_id(), pred.self_ty()));
|
|
|
|
|
|
let def_ids = preds
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.filter_map(|pred| match pred.self_ty().kind() {
|
2022-03-05 07:28:41 +11:00
|
|
|
|
ty::Adt(def, _) => Some(def.did()),
|
2021-09-28 14:48:54 +00:00
|
|
|
|
_ => None,
|
|
|
|
|
|
})
|
|
|
|
|
|
.collect::<FxHashSet<_>>();
|
|
|
|
|
|
let mut spans: MultiSpan = def_ids
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.filter_map(|def_id| {
|
|
|
|
|
|
let span = self.tcx.def_span(*def_id);
|
2022-07-04 17:23:24 +09:00
|
|
|
|
if span.is_dummy() { None } else { Some(span) }
|
2021-09-28 14:48:54 +00:00
|
|
|
|
})
|
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
|
.into();
|
|
|
|
|
|
|
|
|
|
|
|
for pred in &preds {
|
|
|
|
|
|
match pred.self_ty().kind() {
|
2022-07-04 17:23:24 +09:00
|
|
|
|
ty::Adt(def, _) if def.did().is_local() => {
|
2021-09-28 14:48:54 +00:00
|
|
|
|
spans.push_span_label(
|
2022-07-04 17:23:24 +09:00
|
|
|
|
self.tcx.def_span(def.did()),
|
2021-09-28 14:48:54 +00:00
|
|
|
|
format!("must implement `{}`", pred.trait_ref.print_only_trait_path()),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => {}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if all_local_types_needing_impls && spans.primary_span().is_some() {
|
|
|
|
|
|
let msg = if preds.len() == 1 {
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"an implementation of `{}` might be missing for `{}`",
|
|
|
|
|
|
preds[0].trait_ref.print_only_trait_path(),
|
|
|
|
|
|
preds[0].self_ty()
|
|
|
|
|
|
)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"the following type{} would have to `impl` {} required trait{} for this \
|
|
|
|
|
|
operation to be valid",
|
|
|
|
|
|
pluralize!(def_ids.len()),
|
|
|
|
|
|
if def_ids.len() == 1 { "its" } else { "their" },
|
|
|
|
|
|
pluralize!(preds.len()),
|
|
|
|
|
|
)
|
|
|
|
|
|
};
|
|
|
|
|
|
err.span_note(spans, &msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-13 14:28:28 +00:00
|
|
|
|
let preds: Vec<_> = errors
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.map(|e| (e.obligation.predicate, None, Some(e.obligation.cause.clone())))
|
|
|
|
|
|
.collect();
|
2021-09-28 14:48:54 +00:00
|
|
|
|
self.suggest_derive(err, &preds);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-26 20:26:23 +02:00
|
|
|
|
fn suggest_derive(
|
|
|
|
|
|
&self,
|
2022-01-23 20:41:46 +00:00
|
|
|
|
err: &mut Diagnostic,
|
2021-12-04 23:15:45 +11:00
|
|
|
|
unsatisfied_predicates: &[(
|
2021-10-13 14:28:28 +00:00
|
|
|
|
ty::Predicate<'tcx>,
|
|
|
|
|
|
Option<ty::Predicate<'tcx>>,
|
|
|
|
|
|
Option<ObligationCause<'tcx>>,
|
2021-12-04 23:15:45 +11:00
|
|
|
|
)],
|
2021-07-26 20:26:23 +02:00
|
|
|
|
) {
|
2022-07-17 04:09:20 +09:00
|
|
|
|
let mut derives = Vec::<(String, Span, Symbol)>::new();
|
2021-10-04 16:11:22 -05:00
|
|
|
|
let mut traits = Vec::<Span>::new();
|
2021-10-13 14:28:28 +00:00
|
|
|
|
for (pred, _, _) in unsatisfied_predicates {
|
2022-02-19 00:44:45 +01:00
|
|
|
|
let ty::PredicateKind::Trait(trait_pred) = pred.kind().skip_binder() else { continue };
|
2021-10-04 16:11:22 -05:00
|
|
|
|
let adt = match trait_pred.self_ty().ty_adt_def() {
|
2022-03-05 07:28:41 +11:00
|
|
|
|
Some(adt) if adt.did().is_local() => adt,
|
2021-10-04 16:11:22 -05:00
|
|
|
|
_ => continue,
|
|
|
|
|
|
};
|
2022-02-12 17:43:21 +11:00
|
|
|
|
if let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) {
|
|
|
|
|
|
let can_derive = match diagnostic_name {
|
|
|
|
|
|
sym::Default => !adt.is_enum(),
|
2021-10-04 16:11:22 -05:00
|
|
|
|
sym::Eq
|
|
|
|
|
|
| sym::PartialEq
|
|
|
|
|
|
| sym::Ord
|
|
|
|
|
|
| sym::PartialOrd
|
|
|
|
|
|
| sym::Clone
|
|
|
|
|
|
| sym::Copy
|
|
|
|
|
|
| sym::Hash
|
2022-02-12 17:43:21 +11:00
|
|
|
|
| sym::Debug => true,
|
|
|
|
|
|
_ => false,
|
|
|
|
|
|
};
|
|
|
|
|
|
if can_derive {
|
|
|
|
|
|
let self_name = trait_pred.self_ty().to_string();
|
2022-03-05 07:28:41 +11:00
|
|
|
|
let self_span = self.tcx.def_span(adt.did());
|
2022-02-12 17:43:21 +11:00
|
|
|
|
if let Some(poly_trait_ref) = pred.to_opt_poly_trait_pred() {
|
2022-02-16 15:01:19 +11:00
|
|
|
|
for super_trait in supertraits(self.tcx, poly_trait_ref.to_poly_trait_ref())
|
|
|
|
|
|
{
|
2022-02-12 17:43:21 +11:00
|
|
|
|
if let Some(parent_diagnostic_name) =
|
|
|
|
|
|
self.tcx.get_diagnostic_name(super_trait.def_id())
|
|
|
|
|
|
{
|
|
|
|
|
|
derives.push((
|
|
|
|
|
|
self_name.clone(),
|
2022-05-26 13:14:24 +02:00
|
|
|
|
self_span,
|
2022-07-17 04:09:20 +09:00
|
|
|
|
parent_diagnostic_name,
|
2022-02-12 17:43:21 +11:00
|
|
|
|
));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-07-17 04:09:20 +09:00
|
|
|
|
derives.push((self_name, self_span, diagnostic_name));
|
2022-02-12 17:43:21 +11:00
|
|
|
|
} else {
|
|
|
|
|
|
traits.push(self.tcx.def_span(trait_pred.def_id()));
|
2022-02-06 17:44:41 +11:00
|
|
|
|
}
|
2021-10-04 16:11:22 -05:00
|
|
|
|
} else {
|
|
|
|
|
|
traits.push(self.tcx.def_span(trait_pred.def_id()));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-09-28 14:48:54 +00:00
|
|
|
|
traits.sort();
|
|
|
|
|
|
traits.dedup();
|
|
|
|
|
|
|
2021-12-04 23:15:45 +11:00
|
|
|
|
derives.sort();
|
|
|
|
|
|
derives.dedup();
|
|
|
|
|
|
|
|
|
|
|
|
let mut derives_grouped = Vec::<(String, Span, String)>::new();
|
|
|
|
|
|
for (self_name, self_span, trait_name) in derives.into_iter() {
|
|
|
|
|
|
if let Some((last_self_name, _, ref mut last_trait_names)) = derives_grouped.last_mut()
|
|
|
|
|
|
{
|
|
|
|
|
|
if last_self_name == &self_name {
|
|
|
|
|
|
last_trait_names.push_str(format!(", {}", trait_name).as_str());
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-07-17 04:09:20 +09:00
|
|
|
|
derives_grouped.push((self_name, self_span, trait_name.to_string()));
|
2021-12-04 23:15:45 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-28 14:48:54 +00:00
|
|
|
|
let len = traits.len();
|
|
|
|
|
|
if len > 0 {
|
|
|
|
|
|
let span: MultiSpan = traits.into();
|
|
|
|
|
|
err.span_note(
|
|
|
|
|
|
span,
|
|
|
|
|
|
&format!("the following trait{} must be implemented", pluralize!(len),),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-26 20:26:23 +02:00
|
|
|
|
for (self_name, self_span, traits) in &derives_grouped {
|
|
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
|
|
self_span.shrink_to_lo(),
|
|
|
|
|
|
&format!("consider annotating `{}` with `#[derive({})]`", self_name, traits),
|
|
|
|
|
|
format!("#[derive({})]\n", traits),
|
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-03 07:24:55 +00:00
|
|
|
|
fn check_for_deref_method(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
err: &mut Diagnostic,
|
|
|
|
|
|
self_source: SelfSource<'tcx>,
|
|
|
|
|
|
rcvr_ty: Ty<'tcx>,
|
|
|
|
|
|
item_name: Ident,
|
|
|
|
|
|
) {
|
|
|
|
|
|
let SelfSource::QPath(ty) = self_source else { return; };
|
|
|
|
|
|
for (deref_ty, _) in self.autoderef(rustc_span::DUMMY_SP, rcvr_ty).skip(1) {
|
|
|
|
|
|
if let Ok(pick) = self.probe_for_name(
|
|
|
|
|
|
ty.span,
|
|
|
|
|
|
Mode::Path,
|
|
|
|
|
|
item_name,
|
|
|
|
|
|
IsSuggestion(true),
|
|
|
|
|
|
deref_ty,
|
|
|
|
|
|
ty.hir_id,
|
|
|
|
|
|
ProbeScope::TraitsInScope,
|
|
|
|
|
|
) {
|
|
|
|
|
|
if deref_ty.is_suggestable(self.tcx, true)
|
|
|
|
|
|
// If this method receives `&self`, then the provided
|
|
|
|
|
|
// argument _should_ coerce, so it's valid to suggest
|
|
|
|
|
|
// just changing the path.
|
|
|
|
|
|
&& pick.item.fn_has_self_parameter
|
|
|
|
|
|
&& let Some(self_ty) =
|
|
|
|
|
|
self.tcx.fn_sig(pick.item.def_id).inputs().skip_binder().get(0)
|
|
|
|
|
|
&& self_ty.is_ref()
|
|
|
|
|
|
{
|
|
|
|
|
|
let suggested_path = match deref_ty.kind() {
|
|
|
|
|
|
ty::Bool
|
|
|
|
|
|
| ty::Char
|
|
|
|
|
|
| ty::Int(_)
|
|
|
|
|
|
| ty::Uint(_)
|
|
|
|
|
|
| ty::Float(_)
|
|
|
|
|
|
| ty::Adt(_, _)
|
|
|
|
|
|
| ty::Str
|
|
|
|
|
|
| ty::Projection(_)
|
|
|
|
|
|
| ty::Param(_) => format!("{deref_ty}"),
|
|
|
|
|
|
_ => format!("<{deref_ty}>"),
|
|
|
|
|
|
};
|
|
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
|
|
ty.span,
|
|
|
|
|
|
format!("the function `{item_name}` is implemented on `{deref_ty}`"),
|
|
|
|
|
|
suggested_path,
|
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
err.span_note(
|
|
|
|
|
|
ty.span,
|
|
|
|
|
|
format!("the function `{item_name}` is implemented on `{deref_ty}`"),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-05 16:11:36 -07:00
|
|
|
|
/// Print out the type for use in value namespace.
|
|
|
|
|
|
fn ty_to_value_string(&self, ty: Ty<'tcx>) -> String {
|
2020-08-03 00:49:11 +02:00
|
|
|
|
match ty.kind() {
|
2022-03-05 07:28:41 +11:00
|
|
|
|
ty::Adt(def, substs) => format!("{}", ty::Instance::new(def.did(), substs)),
|
2019-10-05 16:11:36 -07:00
|
|
|
|
_ => self.ty_to_string(ty),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-20 15:34:08 +08:00
|
|
|
|
fn suggest_await_before_method(
|
|
|
|
|
|
&self,
|
2022-01-23 20:41:46 +00:00
|
|
|
|
err: &mut Diagnostic,
|
2020-08-20 15:34:08 +08:00
|
|
|
|
item_name: Ident,
|
|
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
|
|
call: &hir::Expr<'_>,
|
|
|
|
|
|
span: Span,
|
|
|
|
|
|
) {
|
2022-07-22 18:49:35 +00:00
|
|
|
|
let output_ty = match self.get_impl_future_output_ty(ty) {
|
2021-12-21 22:08:47 +00:00
|
|
|
|
Some(output_ty) => self.resolve_vars_if_possible(output_ty).skip_binder(),
|
2020-10-23 10:54:34 -07:00
|
|
|
|
_ => return,
|
|
|
|
|
|
};
|
|
|
|
|
|
let method_exists = self.method_exists(item_name, output_ty, call.hir_id, true);
|
|
|
|
|
|
debug!("suggest_await_before_method: is_method_exist={}", method_exists);
|
|
|
|
|
|
if method_exists {
|
|
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
|
|
span.shrink_to_lo(),
|
|
|
|
|
|
"consider `await`ing on the `Future` and calling the method on its `Output`",
|
2022-06-13 15:48:40 +09:00
|
|
|
|
"await.",
|
2020-10-23 10:54:34 -07:00
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
2020-08-20 15:34:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-18 17:13:38 -04:00
|
|
|
|
fn suggest_use_candidates(&self, err: &mut Diagnostic, msg: String, candidates: Vec<DefId>) {
|
2021-11-30 17:20:38 -08:00
|
|
|
|
let parent_map = self.tcx.visible_parent_map(());
|
|
|
|
|
|
|
|
|
|
|
|
// Separate out candidates that must be imported with a glob, because they are named `_`
|
|
|
|
|
|
// and cannot be referred with their identifier.
|
|
|
|
|
|
let (candidates, globs): (Vec<_>, Vec<_>) = candidates.into_iter().partition(|trait_did| {
|
|
|
|
|
|
if let Some(parent_did) = parent_map.get(trait_did) {
|
|
|
|
|
|
// If the item is re-exported as `_`, we should suggest a glob-import instead.
|
2022-04-25 22:08:45 +03:00
|
|
|
|
if *parent_did != self.tcx.parent(*trait_did)
|
2021-11-30 17:20:38 -08:00
|
|
|
|
&& self
|
|
|
|
|
|
.tcx
|
2021-12-23 16:12:34 +08:00
|
|
|
|
.module_children(*parent_did)
|
2021-11-30 17:20:38 -08:00
|
|
|
|
.iter()
|
|
|
|
|
|
.filter(|child| child.res.opt_def_id() == Some(*trait_did))
|
|
|
|
|
|
.all(|child| child.ident.name == kw::Underscore)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
true
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2020-02-11 14:38:16 +01:00
|
|
|
|
let module_did = self.tcx.parent_module(self.body_id);
|
2022-03-18 17:13:38 -04:00
|
|
|
|
let (module, _, _) = self.tcx.hir().get_module(module_did);
|
|
|
|
|
|
let span = module.spans.inject_use_span;
|
2017-11-16 13:14:22 +01:00
|
|
|
|
|
2022-03-18 17:13:38 -04:00
|
|
|
|
let path_strings = candidates.iter().map(|trait_did| {
|
|
|
|
|
|
format!("use {};\n", with_crate_prefix!(self.tcx.def_path_str(*trait_did)),)
|
|
|
|
|
|
});
|
2021-11-30 17:20:38 -08:00
|
|
|
|
|
2022-03-18 17:13:38 -04:00
|
|
|
|
let glob_path_strings = globs.iter().map(|trait_did| {
|
|
|
|
|
|
let parent_did = parent_map.get(trait_did).unwrap();
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"use {}::*; // trait {}\n",
|
|
|
|
|
|
with_crate_prefix!(self.tcx.def_path_str(*parent_did)),
|
|
|
|
|
|
self.tcx.item_name(*trait_did),
|
|
|
|
|
|
)
|
|
|
|
|
|
});
|
2021-11-30 17:20:38 -08:00
|
|
|
|
|
2022-03-18 17:13:38 -04:00
|
|
|
|
err.span_suggestions(
|
|
|
|
|
|
span,
|
|
|
|
|
|
&msg,
|
|
|
|
|
|
path_strings.chain(glob_path_strings),
|
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
2016-05-11 08:48:12 +03:00
|
|
|
|
}
|
2015-02-01 06:07:52 -05:00
|
|
|
|
|
2019-09-30 00:58:05 +01:00
|
|
|
|
fn suggest_valid_traits(
|
|
|
|
|
|
&self,
|
2022-01-23 20:41:46 +00:00
|
|
|
|
err: &mut Diagnostic,
|
2019-09-30 00:58:05 +01:00
|
|
|
|
valid_out_of_scope_traits: Vec<DefId>,
|
|
|
|
|
|
) -> bool {
|
2016-05-11 08:48:12 +03:00
|
|
|
|
if !valid_out_of_scope_traits.is_empty() {
|
|
|
|
|
|
let mut candidates = valid_out_of_scope_traits;
|
|
|
|
|
|
candidates.sort();
|
|
|
|
|
|
candidates.dedup();
|
2021-10-22 23:21:46 -04:00
|
|
|
|
|
|
|
|
|
|
// `TryFrom` and `FromIterator` have no methods
|
|
|
|
|
|
let edition_fix = candidates
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.find(|did| self.tcx.is_diagnostic_item(sym::TryInto, **did))
|
2021-11-07 10:33:27 +01:00
|
|
|
|
.copied();
|
2021-10-22 23:21:46 -04:00
|
|
|
|
|
2017-06-03 17:06:46 -07:00
|
|
|
|
err.help("items from traits can only be used if the trait is in scope");
|
2019-09-30 00:58:05 +01:00
|
|
|
|
let msg = format!(
|
|
|
|
|
|
"the following {traits_are} implemented but not in scope; \
|
|
|
|
|
|
perhaps add a `use` for {one_of_them}:",
|
|
|
|
|
|
traits_are = if candidates.len() == 1 { "trait is" } else { "traits are" },
|
|
|
|
|
|
one_of_them = if candidates.len() == 1 { "it" } else { "one of them" },
|
|
|
|
|
|
);
|
2017-08-03 13:50:06 +02:00
|
|
|
|
|
|
|
|
|
|
self.suggest_use_candidates(err, msg, candidates);
|
2021-10-22 23:21:46 -04:00
|
|
|
|
if let Some(did) = edition_fix {
|
|
|
|
|
|
err.note(&format!(
|
|
|
|
|
|
"'{}' is included in the prelude starting in Edition 2021",
|
2022-02-16 13:04:48 -05:00
|
|
|
|
with_crate_prefix!(self.tcx.def_path_str(did))
|
2021-10-22 23:21:46 -04:00
|
|
|
|
));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-08 14:12:06 +02:00
|
|
|
|
true
|
|
|
|
|
|
} else {
|
|
|
|
|
|
false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-29 19:53:48 -07:00
|
|
|
|
fn suggest_traits_to_import(
|
2019-07-17 16:34:13 -07:00
|
|
|
|
&self,
|
2022-01-23 20:41:46 +00:00
|
|
|
|
err: &mut Diagnostic,
|
2019-07-17 16:34:13 -07:00
|
|
|
|
span: Span,
|
|
|
|
|
|
rcvr_ty: Ty<'tcx>,
|
2020-04-19 13:00:18 +02:00
|
|
|
|
item_name: Ident,
|
2022-06-25 15:46:47 -07:00
|
|
|
|
inputs_len: Option<usize>,
|
2021-03-29 19:53:48 -07:00
|
|
|
|
source: SelfSource<'tcx>,
|
2019-07-17 16:34:13 -07:00
|
|
|
|
valid_out_of_scope_traits: Vec<DefId>,
|
2021-10-13 14:28:28 +00:00
|
|
|
|
unsatisfied_predicates: &[(
|
|
|
|
|
|
ty::Predicate<'tcx>,
|
|
|
|
|
|
Option<ty::Predicate<'tcx>>,
|
|
|
|
|
|
Option<ObligationCause<'tcx>>,
|
|
|
|
|
|
)],
|
2021-05-01 17:18:04 -07:00
|
|
|
|
unsatisfied_bounds: bool,
|
2019-07-17 16:34:13 -07:00
|
|
|
|
) {
|
2021-03-29 19:53:48 -07:00
|
|
|
|
let mut alt_rcvr_sugg = false;
|
2021-05-01 17:18:04 -07:00
|
|
|
|
if let (SelfSource::MethodCall(rcvr), false) = (source, unsatisfied_bounds) {
|
2021-03-30 10:01:22 -07:00
|
|
|
|
debug!(?span, ?item_name, ?rcvr_ty, ?rcvr);
|
2021-04-23 18:24:02 -07:00
|
|
|
|
let skippable = [
|
|
|
|
|
|
self.tcx.lang_items().clone_trait(),
|
|
|
|
|
|
self.tcx.lang_items().deref_trait(),
|
|
|
|
|
|
self.tcx.lang_items().deref_mut_trait(),
|
|
|
|
|
|
self.tcx.lang_items().drop_trait(),
|
2021-10-28 00:05:38 +03:00
|
|
|
|
self.tcx.get_diagnostic_item(sym::AsRef),
|
2021-04-23 18:24:02 -07:00
|
|
|
|
];
|
2021-03-30 10:01:22 -07:00
|
|
|
|
// Try alternative arbitrary self types that could fulfill this call.
|
|
|
|
|
|
// FIXME: probe for all types that *could* be arbitrary self-types, not
|
|
|
|
|
|
// just this list.
|
|
|
|
|
|
for (rcvr_ty, post) in &[
|
|
|
|
|
|
(rcvr_ty, ""),
|
2022-01-28 11:25:15 +11:00
|
|
|
|
(self.tcx.mk_mut_ref(self.tcx.lifetimes.re_erased, rcvr_ty), "&mut "),
|
|
|
|
|
|
(self.tcx.mk_imm_ref(self.tcx.lifetimes.re_erased, rcvr_ty), "&"),
|
2021-03-30 10:01:22 -07:00
|
|
|
|
] {
|
2022-04-26 19:59:50 -07:00
|
|
|
|
match self.lookup_probe(span, item_name, *rcvr_ty, rcvr, ProbeScope::AllTraits) {
|
2022-02-21 16:25:59 -08:00
|
|
|
|
Ok(pick) => {
|
|
|
|
|
|
// If the method is defined for the receiver we have, it likely wasn't `use`d.
|
|
|
|
|
|
// We point at the method, but we just skip the rest of the check for arbitrary
|
|
|
|
|
|
// self types and rely on the suggestion to `use` the trait from
|
|
|
|
|
|
// `suggest_valid_traits`.
|
2022-03-13 00:52:25 +01:00
|
|
|
|
let did = Some(pick.item.container_id(self.tcx));
|
2022-02-21 16:25:59 -08:00
|
|
|
|
let skip = skippable.contains(&did);
|
|
|
|
|
|
if pick.autoderefs == 0 && !skip {
|
|
|
|
|
|
err.span_label(
|
|
|
|
|
|
pick.item.ident(self.tcx).span,
|
|
|
|
|
|
&format!("the method is available for `{}` here", rcvr_ty),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
Err(MethodError::Ambiguity(_)) => {
|
|
|
|
|
|
// If the method is defined (but ambiguous) for the receiver we have, it is also
|
|
|
|
|
|
// likely we haven't `use`d it. It may be possible that if we `Box`/`Pin`/etc.
|
|
|
|
|
|
// the receiver, then it might disambiguate this method, but I think these
|
|
|
|
|
|
// suggestions are generally misleading (see #94218).
|
|
|
|
|
|
break;
|
2021-04-23 18:24:02 -07:00
|
|
|
|
}
|
2022-02-21 16:25:59 -08:00
|
|
|
|
_ => {}
|
2021-04-23 18:24:02 -07:00
|
|
|
|
}
|
2022-02-21 16:25:59 -08:00
|
|
|
|
|
2021-03-30 10:01:22 -07:00
|
|
|
|
for (rcvr_ty, pre) in &[
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
|
(self.tcx.mk_lang_item(*rcvr_ty, LangItem::OwnedBox), "Box::new"),
|
|
|
|
|
|
(self.tcx.mk_lang_item(*rcvr_ty, LangItem::Pin), "Pin::new"),
|
|
|
|
|
|
(self.tcx.mk_diagnostic_item(*rcvr_ty, sym::Arc), "Arc::new"),
|
|
|
|
|
|
(self.tcx.mk_diagnostic_item(*rcvr_ty, sym::Rc), "Rc::new"),
|
2021-03-29 19:53:48 -07:00
|
|
|
|
] {
|
2022-04-26 19:59:50 -07:00
|
|
|
|
if let Some(new_rcvr_t) = *rcvr_ty
|
|
|
|
|
|
&& let Ok(pick) = self.lookup_probe(
|
|
|
|
|
|
span,
|
|
|
|
|
|
item_name,
|
|
|
|
|
|
new_rcvr_t,
|
|
|
|
|
|
rcvr,
|
|
|
|
|
|
ProbeScope::AllTraits,
|
|
|
|
|
|
)
|
|
|
|
|
|
{
|
2022-02-26 07:43:47 -03:00
|
|
|
|
debug!("try_alt_rcvr: pick candidate {:?}", pick);
|
2022-03-13 00:52:25 +01:00
|
|
|
|
let did = Some(pick.item.container_id(self.tcx));
|
2022-02-26 07:43:47 -03:00
|
|
|
|
// We don't want to suggest a container type when the missing
|
|
|
|
|
|
// method is `.clone()` or `.deref()` otherwise we'd suggest
|
|
|
|
|
|
// `Arc::new(foo).clone()`, which is far from what the user wants.
|
|
|
|
|
|
// Explicitly ignore the `Pin::as_ref()` method as `Pin` does not
|
|
|
|
|
|
// implement the `AsRef` trait.
|
|
|
|
|
|
let skip = skippable.contains(&did)
|
2022-06-25 15:46:47 -07:00
|
|
|
|
|| (("Pin::new" == *pre) && (sym::as_ref == item_name.name))
|
|
|
|
|
|
|| inputs_len.map_or(false, |inputs_len| pick.item.kind == ty::AssocKind::Fn && self.tcx.fn_sig(pick.item.def_id).skip_binder().inputs().len() != inputs_len);
|
2022-02-26 07:43:47 -03:00
|
|
|
|
// Make sure the method is defined for the *actual* receiver: we don't
|
|
|
|
|
|
// want to treat `Box<Self>` as a receiver if it only works because of
|
|
|
|
|
|
// an autoderef to `&self`
|
|
|
|
|
|
if pick.autoderefs == 0 && !skip {
|
|
|
|
|
|
err.span_label(
|
|
|
|
|
|
pick.item.ident(self.tcx).span,
|
|
|
|
|
|
&format!("the method is available for `{}` here", new_rcvr_t),
|
|
|
|
|
|
);
|
|
|
|
|
|
err.multipart_suggestion(
|
|
|
|
|
|
"consider wrapping the receiver expression with the \
|
|
|
|
|
|
appropriate type",
|
|
|
|
|
|
vec![
|
|
|
|
|
|
(rcvr.span.shrink_to_lo(), format!("{}({}", pre, post)),
|
|
|
|
|
|
(rcvr.span.shrink_to_hi(), ")".to_string()),
|
|
|
|
|
|
],
|
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
|
|
|
|
|
// We don't care about the other suggestions.
|
|
|
|
|
|
alt_rcvr_sugg = true;
|
2021-03-29 19:53:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-04-23 18:12:54 -07:00
|
|
|
|
if self.suggest_valid_traits(err, valid_out_of_scope_traits) {
|
2016-10-04 09:06:07 +05:30
|
|
|
|
return;
|
2015-02-01 06:07:52 -05:00
|
|
|
|
}
|
2016-02-25 02:11:29 +00:00
|
|
|
|
|
2018-12-03 00:56:47 +00:00
|
|
|
|
let type_is_local = self.type_derefs_to_local(span, rcvr_ty, source);
|
2016-05-11 08:48:12 +03:00
|
|
|
|
|
2020-02-19 15:57:21 -08:00
|
|
|
|
let mut arbitrary_rcvr = vec![];
|
2018-11-27 02:59:49 +00:00
|
|
|
|
// There are no traits implemented, so lets suggest some traits to
|
2016-05-11 08:48:12 +03:00
|
|
|
|
// implement, by finding ones that have the item name, and are
|
|
|
|
|
|
// legal to implement.
|
2016-10-04 02:19:40 +03:00
|
|
|
|
let mut candidates = all_traits(self.tcx)
|
2018-04-01 08:15:25 +02:00
|
|
|
|
.into_iter()
|
2020-06-17 15:08:37 -07:00
|
|
|
|
// Don't issue suggestions for unstable traits since they're
|
|
|
|
|
|
// unlikely to be implementable anyway
|
|
|
|
|
|
.filter(|info| match self.tcx.lookup_stability(info.def_id) {
|
|
|
|
|
|
Some(attr) => attr.level.is_stable(),
|
|
|
|
|
|
None => true,
|
|
|
|
|
|
})
|
2016-05-11 08:48:12 +03:00
|
|
|
|
.filter(|info| {
|
2018-11-27 02:59:49 +00:00
|
|
|
|
// We approximate the coherence rules to only suggest
|
2016-05-11 08:48:12 +03:00
|
|
|
|
// traits that are legal to implement by requiring that
|
2018-11-27 02:59:49 +00:00
|
|
|
|
// either the type or trait is local. Multi-dispatch means
|
2016-05-11 08:48:12 +03:00
|
|
|
|
// this isn't perfect (that is, there are cases when
|
|
|
|
|
|
// implementing a trait would be legal but is rejected
|
|
|
|
|
|
// here).
|
2021-10-13 14:28:28 +00:00
|
|
|
|
unsatisfied_predicates.iter().all(|(p, _, _)| {
|
2021-01-07 11:20:28 -05:00
|
|
|
|
match p.kind().skip_binder() {
|
2020-06-18 20:41:43 +02:00
|
|
|
|
// Hide traits if they are present in predicates as they can be fixed without
|
|
|
|
|
|
// having to implement them.
|
2021-07-22 21:56:07 +08:00
|
|
|
|
ty::PredicateKind::Trait(t) => t.def_id() == info.def_id,
|
2021-01-07 11:20:28 -05:00
|
|
|
|
ty::PredicateKind::Projection(p) => {
|
2020-06-18 20:41:43 +02:00
|
|
|
|
p.projection_ty.item_def_id == info.def_id
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => false,
|
|
|
|
|
|
}
|
2020-05-04 20:48:17 +02:00
|
|
|
|
}) && (type_is_local || info.def_id.is_local())
|
|
|
|
|
|
&& self
|
2022-01-08 12:38:14 +01:00
|
|
|
|
.associated_value(info.def_id, item_name)
|
2020-05-04 20:48:17 +02:00
|
|
|
|
.filter(|item| {
|
|
|
|
|
|
if let ty::AssocKind::Fn = item.kind {
|
|
|
|
|
|
let id = item
|
|
|
|
|
|
.def_id
|
|
|
|
|
|
.as_local()
|
2020-08-12 12:22:56 +02:00
|
|
|
|
.map(|def_id| self.tcx.hir().local_def_id_to_hir_id(def_id));
|
2020-05-04 20:48:17 +02:00
|
|
|
|
if let Some(hir::Node::TraitItem(hir::TraitItem {
|
|
|
|
|
|
kind: hir::TraitItemKind::Fn(fn_sig, method),
|
|
|
|
|
|
..
|
|
|
|
|
|
})) = id.map(|id| self.tcx.hir().get(id))
|
|
|
|
|
|
{
|
|
|
|
|
|
let self_first_arg = match method {
|
|
|
|
|
|
hir::TraitFn::Required([ident, ..]) => {
|
|
|
|
|
|
ident.name == kw::SelfLower
|
|
|
|
|
|
}
|
|
|
|
|
|
hir::TraitFn::Provided(body_id) => {
|
|
|
|
|
|
self.tcx.hir().body(*body_id).params.first().map_or(
|
|
|
|
|
|
false,
|
|
|
|
|
|
|param| {
|
|
|
|
|
|
matches!(
|
|
|
|
|
|
param.pat.kind,
|
|
|
|
|
|
hir::PatKind::Binding(_, _, ident, _)
|
|
|
|
|
|
if ident.name == kw::SelfLower
|
|
|
|
|
|
)
|
|
|
|
|
|
},
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => false,
|
|
|
|
|
|
};
|
2020-02-19 15:57:21 -08:00
|
|
|
|
|
2020-05-04 20:48:17 +02:00
|
|
|
|
if !fn_sig.decl.implicit_self.has_implicit_self()
|
|
|
|
|
|
&& self_first_arg
|
|
|
|
|
|
{
|
|
|
|
|
|
if let Some(ty) = fn_sig.decl.inputs.get(0) {
|
|
|
|
|
|
arbitrary_rcvr.push(ty.span);
|
2020-02-19 15:57:21 -08:00
|
|
|
|
}
|
2020-05-04 20:48:17 +02:00
|
|
|
|
return false;
|
2020-02-19 15:57:21 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-05-04 20:48:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
// We only want to suggest public or local traits (#45781).
|
2022-03-12 21:40:43 +01:00
|
|
|
|
item.visibility(self.tcx).is_public() || info.def_id.is_local()
|
2020-05-04 20:48:17 +02:00
|
|
|
|
})
|
|
|
|
|
|
.is_some()
|
2016-05-11 08:48:12 +03:00
|
|
|
|
})
|
|
|
|
|
|
.collect::<Vec<_>>();
|
2020-02-19 15:57:21 -08:00
|
|
|
|
for span in &arbitrary_rcvr {
|
|
|
|
|
|
err.span_label(
|
|
|
|
|
|
*span,
|
|
|
|
|
|
"the method might not be found because of this arbitrary self type",
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2021-03-29 19:53:48 -07:00
|
|
|
|
if alt_rcvr_sugg {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-05-11 08:48:12 +03:00
|
|
|
|
|
|
|
|
|
|
if !candidates.is_empty() {
|
2018-11-27 02:59:49 +00:00
|
|
|
|
// Sort from most relevant to least relevant.
|
2016-05-11 08:48:12 +03:00
|
|
|
|
candidates.sort_by(|a, b| a.cmp(b).reverse());
|
|
|
|
|
|
candidates.dedup();
|
|
|
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
|
let param_type = match rcvr_ty.kind() {
|
2019-07-17 16:34:13 -07:00
|
|
|
|
ty::Param(param) => Some(param),
|
2020-08-03 00:49:11 +02:00
|
|
|
|
ty::Ref(_, ty, _) => match ty.kind() {
|
2019-07-17 16:34:13 -07:00
|
|
|
|
ty::Param(param) => Some(param),
|
|
|
|
|
|
_ => None,
|
|
|
|
|
|
},
|
|
|
|
|
|
_ => None,
|
|
|
|
|
|
};
|
|
|
|
|
|
err.help(if param_type.is_some() {
|
|
|
|
|
|
"items from traits can only be used if the type parameter is bounded by the trait"
|
|
|
|
|
|
} else {
|
|
|
|
|
|
"items from traits can only be used if the trait is implemented and in scope"
|
|
|
|
|
|
});
|
2020-12-07 12:34:27 +01:00
|
|
|
|
let candidates_len = candidates.len();
|
2019-10-09 10:10:54 -07:00
|
|
|
|
let message = |action| {
|
|
|
|
|
|
format!(
|
2019-07-17 16:34:13 -07:00
|
|
|
|
"the following {traits_define} an item `{name}`, perhaps you need to {action} \
|
2020-02-18 01:35:18 -08:00
|
|
|
|
{one_of_them}:",
|
2019-07-17 16:34:13 -07:00
|
|
|
|
traits_define =
|
2020-12-07 12:34:27 +01:00
|
|
|
|
if candidates_len == 1 { "trait defines" } else { "traits define" },
|
2019-10-09 10:10:54 -07:00
|
|
|
|
action = action,
|
2020-12-07 12:34:27 +01:00
|
|
|
|
one_of_them = if candidates_len == 1 { "it" } else { "one of them" },
|
2019-07-17 16:34:13 -07:00
|
|
|
|
name = item_name,
|
|
|
|
|
|
)
|
|
|
|
|
|
};
|
|
|
|
|
|
// Obtain the span for `param` and use it for a structured suggestion.
|
2022-06-21 00:43:20 +09:00
|
|
|
|
if let Some(param) = param_type {
|
|
|
|
|
|
let generics = self.tcx.generics_of(self.body_id.owner.to_def_id());
|
2020-06-26 06:34:43 +03:00
|
|
|
|
let type_param = generics.type_param(param, self.tcx);
|
2022-02-10 13:04:59 +01:00
|
|
|
|
let hir = self.tcx.hir();
|
2020-06-26 06:34:43 +03:00
|
|
|
|
if let Some(def_id) = type_param.def_id.as_local() {
|
2020-08-12 12:22:56 +02:00
|
|
|
|
let id = hir.local_def_id_to_hir_id(def_id);
|
2020-06-26 06:34:43 +03:00
|
|
|
|
// Get the `hir::Param` to verify whether it already has any bounds.
|
|
|
|
|
|
// We do this to avoid suggesting code that ends up as `T: FooBar`,
|
|
|
|
|
|
// instead we suggest `T: Foo + Bar` in that case.
|
|
|
|
|
|
match hir.get(id) {
|
2021-09-30 19:38:50 +02:00
|
|
|
|
Node::GenericParam(param) => {
|
2022-04-28 21:59:41 +02:00
|
|
|
|
enum Introducer {
|
|
|
|
|
|
Plus,
|
|
|
|
|
|
Colon,
|
|
|
|
|
|
Nothing,
|
|
|
|
|
|
}
|
2022-09-20 14:11:23 +09:00
|
|
|
|
let ast_generics = hir.get_generics(id.owner.def_id).unwrap();
|
2022-04-28 21:59:41 +02:00
|
|
|
|
let (sp, mut introducer) = if let Some(span) =
|
2022-02-07 22:58:30 +01:00
|
|
|
|
ast_generics.bounds_span_for_suggestions(def_id)
|
2022-04-13 11:02:01 -03:00
|
|
|
|
{
|
2022-04-28 21:59:41 +02:00
|
|
|
|
(span, Introducer::Plus)
|
|
|
|
|
|
} else if let Some(colon_span) = param.colon_span {
|
|
|
|
|
|
(colon_span.shrink_to_hi(), Introducer::Nothing)
|
2020-06-26 06:34:43 +03:00
|
|
|
|
} else {
|
2022-04-28 21:59:41 +02:00
|
|
|
|
(param.span.shrink_to_hi(), Introducer::Colon)
|
2020-06-26 06:34:43 +03:00
|
|
|
|
};
|
2022-04-28 21:59:41 +02:00
|
|
|
|
if matches!(
|
|
|
|
|
|
param.kind,
|
|
|
|
|
|
hir::GenericParamKind::Type { synthetic: true, .. },
|
|
|
|
|
|
) {
|
|
|
|
|
|
introducer = Introducer::Plus
|
|
|
|
|
|
}
|
2022-02-07 22:58:30 +01:00
|
|
|
|
let trait_def_ids: FxHashSet<DefId> = ast_generics
|
|
|
|
|
|
.bounds_for_param(def_id)
|
|
|
|
|
|
.flat_map(|bp| bp.bounds.iter())
|
2021-02-17 23:23:57 +01:00
|
|
|
|
.filter_map(|bound| bound.trait_ref()?.trait_def_id())
|
2020-06-26 06:34:43 +03:00
|
|
|
|
.collect();
|
|
|
|
|
|
if !candidates.iter().any(|t| trait_def_ids.contains(&t.def_id)) {
|
2019-10-09 10:10:54 -07:00
|
|
|
|
err.span_suggestions(
|
|
|
|
|
|
sp,
|
2020-06-26 06:34:43 +03:00
|
|
|
|
&message(format!(
|
|
|
|
|
|
"restrict type parameter `{}` with",
|
|
|
|
|
|
param.name.ident(),
|
|
|
|
|
|
)),
|
2019-10-09 10:10:54 -07:00
|
|
|
|
candidates.iter().map(|t| {
|
2020-06-26 06:34:43 +03:00
|
|
|
|
format!(
|
2022-02-07 22:58:30 +01:00
|
|
|
|
"{} {}",
|
2022-04-28 21:59:41 +02:00
|
|
|
|
match introducer {
|
|
|
|
|
|
Introducer::Plus => " +",
|
|
|
|
|
|
Introducer::Colon => ":",
|
|
|
|
|
|
Introducer::Nothing => "",
|
|
|
|
|
|
},
|
2020-06-26 06:34:43 +03:00
|
|
|
|
self.tcx.def_path_str(t.def_id),
|
|
|
|
|
|
)
|
2019-10-09 10:10:54 -07:00
|
|
|
|
}),
|
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
2019-08-22 10:15:57 -07:00
|
|
|
|
}
|
2020-12-07 12:34:27 +01:00
|
|
|
|
return;
|
2020-06-26 06:34:43 +03:00
|
|
|
|
}
|
|
|
|
|
|
Node::Item(hir::Item {
|
|
|
|
|
|
kind: hir::ItemKind::Trait(.., bounds, _),
|
|
|
|
|
|
ident,
|
|
|
|
|
|
..
|
|
|
|
|
|
}) => {
|
|
|
|
|
|
let (sp, sep, article) = if bounds.is_empty() {
|
|
|
|
|
|
(ident.span.shrink_to_hi(), ":", "a")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
(bounds.last().unwrap().span().shrink_to_hi(), " +", "another")
|
|
|
|
|
|
};
|
|
|
|
|
|
err.span_suggestions(
|
|
|
|
|
|
sp,
|
|
|
|
|
|
&message(format!("add {} supertrait for", article)),
|
|
|
|
|
|
candidates.iter().map(|t| {
|
|
|
|
|
|
format!("{} {}", sep, self.tcx.def_path_str(t.def_id),)
|
|
|
|
|
|
}),
|
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
|
);
|
2020-12-07 12:34:27 +01:00
|
|
|
|
return;
|
2019-07-17 16:34:13 -07:00
|
|
|
|
}
|
2020-06-26 06:34:43 +03:00
|
|
|
|
_ => {}
|
2019-07-17 16:34:13 -07:00
|
|
|
|
}
|
2020-06-26 06:34:43 +03:00
|
|
|
|
}
|
2019-07-17 16:34:13 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-07 12:34:27 +01:00
|
|
|
|
let (potential_candidates, explicitly_negative) = if param_type.is_some() {
|
|
|
|
|
|
// FIXME: Even though negative bounds are not implemented, we could maybe handle
|
|
|
|
|
|
// cases where a positive bound implies a negative impl.
|
|
|
|
|
|
(candidates, Vec::new())
|
2022-02-21 13:44:55 +01:00
|
|
|
|
} else if let Some(simp_rcvr_ty) =
|
2022-05-13 20:23:22 +02:00
|
|
|
|
simplify_type(self.tcx, rcvr_ty, TreatParams::AsPlaceholder)
|
2021-05-07 20:00:02 +02:00
|
|
|
|
{
|
2020-12-07 12:34:27 +01:00
|
|
|
|
let mut potential_candidates = Vec::new();
|
|
|
|
|
|
let mut explicitly_negative = Vec::new();
|
|
|
|
|
|
for candidate in candidates {
|
|
|
|
|
|
// Check if there's a negative impl of `candidate` for `rcvr_ty`
|
|
|
|
|
|
if self
|
|
|
|
|
|
.tcx
|
|
|
|
|
|
.all_impls(candidate.def_id)
|
|
|
|
|
|
.filter(|imp_did| {
|
|
|
|
|
|
self.tcx.impl_polarity(*imp_did) == ty::ImplPolarity::Negative
|
|
|
|
|
|
})
|
|
|
|
|
|
.any(|imp_did| {
|
|
|
|
|
|
let imp = self.tcx.impl_trait_ref(imp_did).unwrap();
|
2022-01-25 12:50:00 +01:00
|
|
|
|
let imp_simp =
|
2022-05-13 20:23:22 +02:00
|
|
|
|
simplify_type(self.tcx, imp.self_ty(), TreatParams::AsPlaceholder);
|
2021-01-11 20:45:33 +01:00
|
|
|
|
imp_simp.map_or(false, |s| s == simp_rcvr_ty)
|
2020-12-07 12:34:27 +01:00
|
|
|
|
})
|
|
|
|
|
|
{
|
|
|
|
|
|
explicitly_negative.push(candidate);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
potential_candidates.push(candidate);
|
2020-02-18 01:35:18 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-12-07 12:34:27 +01:00
|
|
|
|
(potential_candidates, explicitly_negative)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// We don't know enough about `recv_ty` to make proper suggestions.
|
|
|
|
|
|
(candidates, Vec::new())
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let action = if let Some(param) = param_type {
|
|
|
|
|
|
format!("restrict type parameter `{}` with", param)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// FIXME: it might only need to be imported into scope, not implemented.
|
|
|
|
|
|
"implement".to_string()
|
|
|
|
|
|
};
|
|
|
|
|
|
match &potential_candidates[..] {
|
|
|
|
|
|
[] => {}
|
|
|
|
|
|
[trait_info] if trait_info.def_id.is_local() => {
|
|
|
|
|
|
err.span_note(
|
2022-07-04 17:23:24 +09:00
|
|
|
|
self.tcx.def_span(trait_info.def_id),
|
2020-12-07 12:34:27 +01:00
|
|
|
|
&format!(
|
|
|
|
|
|
"`{}` defines an item `{}`, perhaps you need to {} it",
|
|
|
|
|
|
self.tcx.def_path_str(trait_info.def_id),
|
|
|
|
|
|
item_name,
|
|
|
|
|
|
action
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
trait_infos => {
|
2020-02-18 01:35:18 -08:00
|
|
|
|
let mut msg = message(action);
|
2020-12-07 12:34:27 +01:00
|
|
|
|
for (i, trait_info) in trait_infos.iter().enumerate() {
|
2020-02-18 01:35:18 -08:00
|
|
|
|
msg.push_str(&format!(
|
|
|
|
|
|
"\ncandidate #{}: `{}`",
|
|
|
|
|
|
i + 1,
|
|
|
|
|
|
self.tcx.def_path_str(trait_info.def_id),
|
|
|
|
|
|
));
|
|
|
|
|
|
}
|
2020-12-07 12:34:27 +01:00
|
|
|
|
err.note(&msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
match &explicitly_negative[..] {
|
|
|
|
|
|
[] => {}
|
|
|
|
|
|
[trait_info] => {
|
|
|
|
|
|
let msg = format!(
|
2022-02-11 16:23:39 +01:00
|
|
|
|
"the trait `{}` defines an item `{}`, but is explicitly unimplemented",
|
2020-12-07 12:34:27 +01:00
|
|
|
|
self.tcx.def_path_str(trait_info.def_id),
|
|
|
|
|
|
item_name
|
|
|
|
|
|
);
|
|
|
|
|
|
err.note(&msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
trait_infos => {
|
|
|
|
|
|
let mut msg = format!(
|
2022-02-11 16:23:39 +01:00
|
|
|
|
"the following traits define an item `{}`, but are explicitly unimplemented:",
|
2020-12-07 12:34:27 +01:00
|
|
|
|
item_name
|
|
|
|
|
|
);
|
|
|
|
|
|
for trait_info in trait_infos {
|
|
|
|
|
|
msg.push_str(&format!("\n{}", self.tcx.def_path_str(trait_info.def_id)));
|
|
|
|
|
|
}
|
|
|
|
|
|
err.note(&msg);
|
2019-07-17 16:34:13 -07:00
|
|
|
|
}
|
2016-05-11 08:48:12 +03:00
|
|
|
|
}
|
2015-01-13 17:05:04 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-01-14 10:49:17 +11:00
|
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
|
/// Checks whether there is a local type somewhere in the chain of
|
|
|
|
|
|
/// autoderefs of `rcvr_ty`.
|
2021-03-29 19:53:48 -07:00
|
|
|
|
fn type_derefs_to_local(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
span: Span,
|
|
|
|
|
|
rcvr_ty: Ty<'tcx>,
|
|
|
|
|
|
source: SelfSource<'tcx>,
|
|
|
|
|
|
) -> bool {
|
2019-02-25 09:09:02 +01:00
|
|
|
|
fn is_local(ty: Ty<'_>) -> bool {
|
2020-08-03 00:49:11 +02:00
|
|
|
|
match ty.kind() {
|
2022-03-05 07:28:41 +11:00
|
|
|
|
ty::Adt(def, _) => def.did().is_local(),
|
2018-08-22 01:35:29 +01:00
|
|
|
|
ty::Foreign(did) => did.is_local(),
|
2021-09-30 19:38:50 +02:00
|
|
|
|
ty::Dynamic(tr, ..) => tr.principal().map_or(false, |d| d.def_id().is_local()),
|
2018-08-22 01:35:29 +01:00
|
|
|
|
ty::Param(_) => true,
|
2015-01-27 11:29:30 +11:00
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
|
// Everything else (primitive types, etc.) is effectively
|
|
|
|
|
|
// non-local (there are "edge" cases, e.g., `(LocalType,)`, but
|
2016-05-11 08:48:12 +03:00
|
|
|
|
// the noise from these sort of types is usually just really
|
|
|
|
|
|
// annoying, rather than any sort of help).
|
2016-10-04 09:06:07 +05:30
|
|
|
|
_ => false,
|
2016-05-11 08:48:12 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-01-27 11:29:30 +11:00
|
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
|
// This occurs for UFCS desugaring of `T::method`, where there is no
|
|
|
|
|
|
// receiver expression for the method call, and thus no autoderef.
|
2018-12-03 00:56:47 +00:00
|
|
|
|
if let SelfSource::QPath(_) = source {
|
2019-10-19 12:04:16 +13:00
|
|
|
|
return is_local(self.resolve_vars_with_obligations(rcvr_ty));
|
2015-02-20 08:17:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-24 14:37:11 +03:00
|
|
|
|
self.autoderef(span, rcvr_ty).any(|(ty, _)| is_local(ty))
|
2015-02-20 08:17:05 +02:00
|
|
|
|
}
|
2016-03-12 03:06:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-29 19:53:48 -07:00
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
2018-12-03 00:56:47 +00:00
|
|
|
|
pub enum SelfSource<'a> {
|
2019-12-01 16:08:58 +01:00
|
|
|
|
QPath(&'a hir::Ty<'a>),
|
2019-11-30 15:08:22 +01:00
|
|
|
|
MethodCall(&'a hir::Expr<'a> /* rcvr */),
|
2018-12-03 00:56:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
|
#[derive(Copy, Clone)]
|
2015-01-14 10:49:17 +11:00
|
|
|
|
pub struct TraitInfo {
|
2015-08-16 06:32:28 -04:00
|
|
|
|
pub def_id: DefId,
|
2015-01-14 10:49:17 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl PartialEq for TraitInfo {
|
|
|
|
|
|
fn eq(&self, other: &TraitInfo) -> bool {
|
|
|
|
|
|
self.cmp(other) == Ordering::Equal
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
impl Eq for TraitInfo {}
|
|
|
|
|
|
impl PartialOrd for TraitInfo {
|
2016-10-04 09:06:07 +05:30
|
|
|
|
fn partial_cmp(&self, other: &TraitInfo) -> Option<Ordering> {
|
|
|
|
|
|
Some(self.cmp(other))
|
|
|
|
|
|
}
|
2015-01-14 10:49:17 +11:00
|
|
|
|
}
|
|
|
|
|
|
impl Ord for TraitInfo {
|
|
|
|
|
|
fn cmp(&self, other: &TraitInfo) -> Ordering {
|
2018-11-27 02:59:49 +00:00
|
|
|
|
// Local crates are more important than remote ones (local:
|
|
|
|
|
|
// `cnum == 0`), and otherwise we throw in the defid for totality.
|
2015-01-14 10:49:17 +11:00
|
|
|
|
|
2015-09-04 13:52:28 -04:00
|
|
|
|
let lhs = (other.def_id.krate, other.def_id);
|
|
|
|
|
|
let rhs = (self.def_id.krate, self.def_id);
|
2015-01-14 10:49:17 +11:00
|
|
|
|
lhs.cmp(&rhs)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-24 11:09:32 +08:00
|
|
|
|
/// Retrieves all traits in this crate and any dependent crates,
|
|
|
|
|
|
/// and wraps them into `TraitInfo` for custom sorting.
|
2019-06-21 23:49:03 +02:00
|
|
|
|
pub fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> {
|
2021-12-24 11:09:32 +08:00
|
|
|
|
tcx.all_traits().map(|def_id| TraitInfo { def_id }).collect()
|
2015-01-14 10:49:17 +11:00
|
|
|
|
}
|
2017-11-16 13:14:22 +01:00
|
|
|
|
|
2021-12-13 21:45:08 -04:00
|
|
|
|
fn print_disambiguation_help<'tcx>(
|
2020-04-19 13:00:18 +02:00
|
|
|
|
item_name: Ident,
|
2022-09-01 13:27:31 +09:00
|
|
|
|
args: Option<(&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>])>,
|
2022-01-23 20:41:46 +00:00
|
|
|
|
err: &mut Diagnostic,
|
2019-12-11 18:20:29 -08:00
|
|
|
|
trait_name: String,
|
|
|
|
|
|
rcvr_ty: Ty<'_>,
|
|
|
|
|
|
kind: ty::AssocKind,
|
2020-04-01 10:09:50 +08:00
|
|
|
|
def_id: DefId,
|
2019-12-11 18:20:29 -08:00
|
|
|
|
span: Span,
|
|
|
|
|
|
candidate: Option<usize>,
|
|
|
|
|
|
source_map: &source_map::SourceMap,
|
2021-09-25 21:47:33 +02:00
|
|
|
|
fn_has_self_parameter: bool,
|
2019-12-11 18:20:29 -08:00
|
|
|
|
) {
|
|
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
2022-09-01 13:27:31 +09:00
|
|
|
|
let (span, sugg) = if let (ty::AssocKind::Fn, Some((receiver, args))) = (kind, args) {
|
2021-08-09 16:47:09 +00:00
|
|
|
|
let args = format!(
|
2019-12-11 18:20:29 -08:00
|
|
|
|
"({}{})",
|
|
|
|
|
|
if rcvr_ty.is_region_ptr() {
|
|
|
|
|
|
if rcvr_ty.is_mutable_ptr() { "&mut " } else { "&" }
|
|
|
|
|
|
} else {
|
|
|
|
|
|
""
|
|
|
|
|
|
},
|
2022-09-01 13:27:31 +09:00
|
|
|
|
std::iter::once(receiver)
|
|
|
|
|
|
.chain(args.iter())
|
2019-12-11 18:20:29 -08:00
|
|
|
|
.map(|arg| source_map.span_to_snippet(arg.span).unwrap_or_else(|_| {
|
|
|
|
|
|
applicability = Applicability::HasPlaceholders;
|
|
|
|
|
|
"_".to_owned()
|
|
|
|
|
|
}))
|
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
|
.join(", "),
|
2021-08-09 16:47:09 +00:00
|
|
|
|
);
|
2021-09-25 21:47:33 +02:00
|
|
|
|
let trait_name = if !fn_has_self_parameter {
|
|
|
|
|
|
format!("<{} as {}>", rcvr_ty, trait_name)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
trait_name
|
|
|
|
|
|
};
|
2021-08-09 16:47:09 +00:00
|
|
|
|
(span, format!("{}::{}{}", trait_name, item_name, args))
|
2019-12-11 18:20:29 -08:00
|
|
|
|
} else {
|
2021-09-25 21:47:33 +02:00
|
|
|
|
(span.with_hi(item_name.span.lo()), format!("<{} as {}>::", rcvr_ty, trait_name))
|
2019-12-11 18:20:29 -08:00
|
|
|
|
};
|
2021-08-09 16:47:09 +00:00
|
|
|
|
err.span_suggestion_verbose(
|
2019-12-11 18:20:29 -08:00
|
|
|
|
span,
|
|
|
|
|
|
&format!(
|
|
|
|
|
|
"disambiguate the {} for {}",
|
2020-04-01 10:09:50 +08:00
|
|
|
|
kind.as_def_kind().descr(def_id),
|
2019-12-11 18:20:29 -08:00
|
|
|
|
if let Some(candidate) = candidate {
|
|
|
|
|
|
format!("candidate #{}", candidate)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
"the candidate".to_string()
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
sugg,
|
|
|
|
|
|
applicability,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|