use crate::ty::SubtypePredicate;
use crate::util::nodemap::{FxHashMap, FxHashSet};
-use errors::{Applicability, DiagnosticBuilder, pluralize};
+use errors::{Applicability, DiagnosticBuilder, pluralize, Style};
use std::fmt;
use syntax::ast;
use syntax::symbol::{sym, kw};
}
match obligation.predicate {
ty::Predicate::Trait(ref trait_predicate) => {
- let trait_predicate =
- self.resolve_vars_if_possible(trait_predicate);
+ let trait_predicate = self.resolve_vars_if_possible(trait_predicate);
if self.tcx.sess.has_errors() && trait_predicate.references_error() {
return;
}
let trait_ref = trait_predicate.to_poly_trait_ref();
- let (post_message, pre_message) =
- self.get_parent_trait_ref(&obligation.cause.code)
- .map(|t| (format!(" in `{}`", t), format!("within `{}`, ", t)))
+ let (
+ post_message,
+ pre_message,
+ ) = self.get_parent_trait_ref(&obligation.cause.code)
+ .map(|t| (format!(" in `{}`", t), format!("within `{}`, ", t)))
.unwrap_or_default();
- let OnUnimplementedNote { message, label, note }
- = self.on_unimplemented_note(trait_ref, obligation);
+ let OnUnimplementedNote {
+ message,
+ label,
+ note,
+ } = self.on_unimplemented_note(trait_ref, obligation);
let have_alt_message = message.is_some() || label.is_some();
let is_try = self.tcx.sess.source_map().span_to_snippet(span)
.map(|s| &s == "?")
)
};
+ if self.suggest_add_reference_to_arg(
+ &obligation,
+ &mut err,
+ &trait_ref,
+ points_at_arg,
+ have_alt_message,
+ ) {
+ self.note_obligation_cause(&mut err, obligation);
+ err.emit();
+ return;
+ }
if let Some(ref s) = label {
// If it has a custom `#[rustc_on_unimplemented]`
// error message, let's display it as the label!
}
}
+ fn mk_obligation_for_def_id(
+ &self,
+ def_id: DefId,
+ output_ty: Ty<'tcx>,
+ cause: ObligationCause<'tcx>,
+ param_env: ty::ParamEnv<'tcx>,
+ ) -> PredicateObligation<'tcx> {
+ let new_trait_ref = ty::TraitRef {
+ def_id,
+ substs: self.tcx.mk_substs_trait(output_ty, &[]),
+ };
+ Obligation::new(cause, param_env, new_trait_ref.to_predicate())
+ }
+
+ /// Given a closure's `DefId`, return the given name of the closure.
+ ///
+ /// This doesn't account for reassignments, but it's only used for suggestions.
+ fn get_closure_name(
+ &self,
+ def_id: DefId,
+ err: &mut DiagnosticBuilder<'_>,
+ msg: &str,
+ ) -> Option<String> {
+ let get_name = |err: &mut DiagnosticBuilder<'_>, kind: &hir::PatKind| -> Option<String> {
+ // Get the local name of this closure. This can be inaccurate because
+ // of the possibility of reassignment, but this should be good enough.
+ match &kind {
+ hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, _, name, None) => {
+ Some(format!("{}", name))
+ }
+ _ => {
+ err.note(&msg);
+ None
+ }
+ }
+ };
+
+ let hir = self.tcx.hir();
+ let hir_id = hir.as_local_hir_id(def_id)?;
+ let parent_node = hir.get_parent_node(hir_id);
+ match hir.find(parent_node) {
+ Some(hir::Node::Stmt(hir::Stmt {
+ kind: hir::StmtKind::Local(local), ..
+ })) => get_name(err, &local.pat.kind),
+ // Different to previous arm because one is `&hir::Local` and the other
+ // is `P<hir::Local>`.
+ Some(hir::Node::Local(local)) => get_name(err, &local.pat.kind),
+ _ => return None,
+ }
+ }
+
+ /// We tried to apply the bound to an `fn` or closure. Check whether calling it would
+ /// evaluate to a type that *would* satisfy the trait binding. If it would, suggest calling
+ /// it: `bar(foo)` → `bar(foo())`. This case is *very* likely to be hit if `foo` is `async`.
fn suggest_fn_call(
&self,
obligation: &PredicateObligation<'tcx>,
points_at_arg: bool,
) {
let self_ty = trait_ref.self_ty();
- match self_ty.kind {
+ let (def_id, output_ty, callable) = match self_ty.kind {
+ ty::Closure(def_id, substs) => {
+ (def_id, self.closure_sig(def_id, substs).output(), "closure")
+ }
ty::FnDef(def_id, _) => {
- // We tried to apply the bound to an `fn`. Check whether calling it would evaluate
- // to a type that *would* satisfy the trait binding. If it would, suggest calling
- // it: `bar(foo)` -> `bar(foo)`. This case is *very* likely to be hit if `foo` is
- // `async`.
- let output_ty = self_ty.fn_sig(self.tcx).output();
- let new_trait_ref = ty::TraitRef {
- def_id: trait_ref.def_id(),
- substs: self.tcx.mk_substs_trait(output_ty.skip_binder(), &[]),
+ (def_id, self_ty.fn_sig(self.tcx).output(), "function")
+ }
+ _ => return,
+ };
+ let msg = format!("use parentheses to call the {}", callable);
+
+ let obligation = self.mk_obligation_for_def_id(
+ trait_ref.def_id(),
+ output_ty.skip_binder(),
+ obligation.cause.clone(),
+ obligation.param_env,
+ );
+
+ match self.evaluate_obligation(&obligation) {
+ Ok(EvaluationResult::EvaluatedToOk) |
+ Ok(EvaluationResult::EvaluatedToOkModuloRegions) |
+ Ok(EvaluationResult::EvaluatedToAmbig) => {}
+ _ => return,
+ }
+ let hir = self.tcx.hir();
+ // Get the name of the callable and the arguments to be used in the suggestion.
+ let snippet = match hir.get_if_local(def_id) {
+ Some(hir::Node::Expr(hir::Expr {
+ kind: hir::ExprKind::Closure(_, decl, _, span, ..),
+ ..
+ })) => {
+ err.span_label(*span, "consider calling this closure");
+ let name = match self.get_closure_name(def_id, err, &msg) {
+ Some(name) => name,
+ None => return,
};
- let obligation = Obligation::new(
- obligation.cause.clone(),
- obligation.param_env,
- new_trait_ref.to_predicate(),
- );
- match self.evaluate_obligation(&obligation) {
- Ok(EvaluationResult::EvaluatedToOk) |
- Ok(EvaluationResult::EvaluatedToOkModuloRegions) |
- Ok(EvaluationResult::EvaluatedToAmbig) => {
- if let Some(hir::Node::Item(hir::Item {
- ident,
- kind: hir::ItemKind::Fn(.., body_id),
- ..
- })) = self.tcx.hir().get_if_local(def_id) {
- let body = self.tcx.hir().body(*body_id);
- let msg = "use parentheses to call the function";
- let snippet = format!(
- "{}({})",
- ident,
- body.params.iter()
- .map(|arg| match &arg.pat.kind {
- hir::PatKind::Binding(_, _, ident, None)
- if ident.name != kw::SelfLower => ident.to_string(),
- _ => "_".to_string(),
- }).collect::<Vec<_>>().join(", "),
- );
- // When the obligation error has been ensured to have been caused by
- // an argument, the `obligation.cause.span` points at the expression
- // of the argument, so we can provide a suggestion. This is signaled
- // by `points_at_arg`. Otherwise, we give a more general note.
- if points_at_arg {
- err.span_suggestion(
- obligation.cause.span,
- msg,
- snippet,
- Applicability::HasPlaceholders,
- );
- } else {
- err.help(&format!("{}: `{}`", msg, snippet));
- }
- }
+ let args = decl.inputs.iter()
+ .map(|_| "_")
+ .collect::<Vec<_>>()
+ .join(", ");
+ format!("{}({})", name, args)
+ }
+ Some(hir::Node::Item(hir::Item {
+ ident,
+ kind: hir::ItemKind::Fn(.., body_id),
+ ..
+ })) => {
+ err.span_label(ident.span, "consider calling this function");
+ let body = hir.body(*body_id);
+ let args = body.params.iter()
+ .map(|arg| match &arg.pat.kind {
+ hir::PatKind::Binding(_, _, ident, None)
+ // FIXME: provide a better suggestion when encountering `SelfLower`, it
+ // should suggest a method call.
+ if ident.name != kw::SelfLower => ident.to_string(),
+ _ => "_".to_string(),
+ })
+ .collect::<Vec<_>>()
+ .join(", ");
+ format!("{}({})", ident, args)
+ }
+ _ => return,
+ };
+ if points_at_arg {
+ // When the obligation error has been ensured to have been caused by
+ // an argument, the `obligation.cause.span` points at the expression
+ // of the argument, so we can provide a suggestion. This is signaled
+ // by `points_at_arg`. Otherwise, we give a more general note.
+ err.span_suggestion(
+ obligation.cause.span,
+ &msg,
+ snippet,
+ Applicability::HasPlaceholders,
+ );
+ } else {
+ err.help(&format!("{}: `{}`", msg, snippet));
+ }
+ }
+
+ fn suggest_add_reference_to_arg(
+ &self,
+ obligation: &PredicateObligation<'tcx>,
+ err: &mut DiagnosticBuilder<'tcx>,
+ trait_ref: &ty::Binder<ty::TraitRef<'tcx>>,
+ points_at_arg: bool,
+ has_custom_message: bool,
+ ) -> bool {
+ if !points_at_arg {
+ return false;
+ }
+
+ let span = obligation.cause.span;
+ let param_env = obligation.param_env;
+ let trait_ref = trait_ref.skip_binder();
+
+ if let ObligationCauseCode::ImplDerivedObligation(obligation) = &obligation.cause.code {
+ // Try to apply the original trait binding obligation by borrowing.
+ let self_ty = trait_ref.self_ty();
+ let found = self_ty.to_string();
+ let new_self_ty = self.tcx.mk_imm_ref(self.tcx.lifetimes.re_static, self_ty);
+ let substs = self.tcx.mk_substs_trait(new_self_ty, &[]);
+ let new_trait_ref = ty::TraitRef::new(obligation.parent_trait_ref.def_id(), substs);
+ let new_obligation = Obligation::new(
+ ObligationCause::dummy(),
+ param_env,
+ new_trait_ref.to_predicate(),
+ );
+ if self.predicate_must_hold_modulo_regions(&new_obligation) {
+ if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
+ // We have a very specific type of error, where just borrowing this argument
+ // might solve the problem. In cases like this, the important part is the
+ // original type obligation, not the last one that failed, which is arbitrary.
+ // Because of this, we modify the error to refer to the original obligation and
+ // return early in the caller.
+ let msg = format!(
+ "the trait bound `{}: {}` is not satisfied",
+ found,
+ obligation.parent_trait_ref.skip_binder(),
+ );
+ if has_custom_message {
+ err.note(&msg);
+ } else {
+ err.message = vec![(msg, Style::NoStyle)];
}
- _ => {}
+ if snippet.starts_with('&') {
+ // This is already a literal borrow and the obligation is failing
+ // somewhere else in the obligation chain. Do not suggest non-sense.
+ return false;
+ }
+ err.span_label(span, &format!(
+ "expected an implementor of trait `{}`",
+ obligation.parent_trait_ref.skip_binder(),
+ ));
+ err.span_suggestion(
+ span,
+ "consider borrowing here",
+ format!("&{}", snippet),
+ Applicability::MaybeIncorrect,
+ );
+ return true;
}
}
- _ => {}
}
+ false
}
/// Whenever references are used by mistake, like `for (i, e) in &vec.iter().enumerate()`,
if let ty::Ref(_, t_type, _) = trait_type.kind {
trait_type = t_type;
- let substs = self.tcx.mk_substs_trait(trait_type, &[]);
- let new_trait_ref = ty::TraitRef::new(trait_ref.def_id, substs);
- let new_obligation = Obligation::new(
+ let new_obligation = self.mk_obligation_for_def_id(
+ trait_ref.def_id,
+ trait_type,
ObligationCause::dummy(),
obligation.param_env,
- new_trait_ref.to_predicate(),
);
if self.predicate_may_hold(&new_obligation) {
hir::Mutability::Immutable => self.tcx.mk_mut_ref(region, t_type),
};
- let substs = self.tcx.mk_substs_trait(&trait_type, &[]);
- let new_trait_ref = ty::TraitRef::new(trait_ref.skip_binder().def_id, substs);
- let new_obligation = Obligation::new(
+ let new_obligation = self.mk_obligation_for_def_id(
+ trait_ref.skip_binder().def_id,
+ trait_type,
ObligationCause::dummy(),
obligation.param_env,
- new_trait_ref.to_predicate(),
);
if self.evaluate_obligation_no_overflow(
err.note(&format!("required by cast to type `{}`",
self.ty_to_string(target)));
}
- ObligationCauseCode::RepeatVec(suggest_const_in_array_repeat_expression) => {
+ ObligationCauseCode::RepeatVec(suggest_const_in_array_repeat_expressions) => {
err.note("the `Copy` trait is required because the \
repeated element will be copied");
- if suggest_const_in_array_repeat_expression {
+ if suggest_const_in_array_repeat_expressions {
err.note("this array initializer can be evaluated at compile-time, for more \
information, see issue \
https://github.com/rust-lang/rust/issues/49147");
if tcx.sess.opts.unstable_features.is_nightly_build() {
- err.help("add `#![feature(const_in_array_repeat_expression)]` to the \
+ err.help("add `#![feature(const_in_array_repeat_expressions)]` to the \
crate attributes to enable");
}
}