2019-12-31 21:25:16 +01:00
|
|
|
use errors::{pluralize, struct_span_err, Applicability, DiagnosticId};
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc::hir::intravisit;
|
2016-11-07 19:25:06 +01:00
|
|
|
use rustc::infer::{self, InferOk};
|
|
|
|
use rustc::traits::{self, ObligationCause, ObligationCauseCode, Reveal};
|
2016-08-10 10:43:12 +02:00
|
|
|
use rustc::ty::error::{ExpectedFound, TypeError};
|
2019-12-22 23:42:04 +01:00
|
|
|
use rustc::ty::subst::{InternalSubsts, Subst};
|
|
|
|
use rustc::ty::util::ExplicitSelf;
|
|
|
|
use rustc::ty::{self, GenericParamDefKind, TyCtxt};
|
2016-10-19 16:45:49 +02:00
|
|
|
use rustc::util::common::ErrorReported;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def::{DefKind, Res};
|
|
|
|
use rustc_hir::{GenericParamKind, ImplItemKind, TraitItemKind};
|
2019-12-31 18:15:40 +01:00
|
|
|
use rustc_span::Span;
|
2015-01-14 22:43:17 +01:00
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
use super::{potentially_plural_count, FnCtxt, Inherited};
|
2015-01-14 22:43:17 +01:00
|
|
|
|
2019-11-11 22:46:56 +01:00
|
|
|
use rustc_error_codes::*;
|
|
|
|
|
2015-01-14 22:43:17 +01:00
|
|
|
/// Checks that a method from an impl conforms to the signature of
|
|
|
|
/// the same method as declared in the trait.
|
|
|
|
///
|
|
|
|
/// # Parameters
|
|
|
|
///
|
2019-02-08 14:53:55 +01:00
|
|
|
/// - `impl_m`: type of the method we are checking
|
|
|
|
/// - `impl_m_span`: span to use for reporting errors
|
|
|
|
/// - `trait_m`: the method in the trait
|
|
|
|
/// - `impl_trait_ref`: the TraitRef corresponding to the trait implementation
|
2015-01-14 22:43:17 +01:00
|
|
|
|
2019-12-08 17:58:22 +01:00
|
|
|
crate fn compare_impl_method<'tcx>(
|
2019-06-13 23:48:52 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 23:11:55 +02:00
|
|
|
impl_m: &ty::AssocItem,
|
|
|
|
impl_m_span: Span,
|
|
|
|
trait_m: &ty::AssocItem,
|
|
|
|
impl_trait_ref: ty::TraitRef<'tcx>,
|
|
|
|
trait_item_span: Option<Span>,
|
|
|
|
) {
|
2019-12-22 23:42:04 +01:00
|
|
|
debug!("compare_impl_method(impl_trait_ref={:?})", impl_trait_ref);
|
2015-01-14 22:43:17 +01:00
|
|
|
|
2018-08-18 12:14:09 +02:00
|
|
|
let impl_m_span = tcx.sess.source_map().def_span(impl_m_span);
|
2018-01-28 22:42:49 +01:00
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
if let Err(ErrorReported) = compare_self_type(tcx, impl_m, impl_m_span, trait_m, impl_trait_ref)
|
|
|
|
{
|
2016-10-19 16:45:49 +02:00
|
|
|
return;
|
2015-01-14 22:43:17 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
if let Err(ErrorReported) =
|
|
|
|
compare_number_of_generics(tcx, impl_m, impl_m_span, trait_m, trait_item_span)
|
|
|
|
{
|
2016-10-19 16:45:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-09-25 08:21:51 +02:00
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
if let Err(ErrorReported) =
|
|
|
|
compare_number_of_method_arguments(tcx, impl_m, impl_m_span, trait_m, trait_item_span)
|
|
|
|
{
|
2015-01-14 22:43:17 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
if let Err(ErrorReported) = compare_synthetic_generics(tcx, impl_m, trait_m) {
|
2017-11-10 18:47:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
if let Err(ErrorReported) =
|
|
|
|
compare_predicate_entailment(tcx, impl_m, impl_m_span, trait_m, impl_trait_ref)
|
|
|
|
{
|
2015-01-14 22:43:17 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-10-19 16:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 23:11:55 +02:00
|
|
|
fn compare_predicate_entailment<'tcx>(
|
2019-06-13 23:48:52 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 23:11:55 +02:00
|
|
|
impl_m: &ty::AssocItem,
|
|
|
|
impl_m_span: Span,
|
|
|
|
trait_m: &ty::AssocItem,
|
|
|
|
impl_trait_ref: ty::TraitRef<'tcx>,
|
|
|
|
) -> Result<(), ErrorReported> {
|
2016-11-10 01:06:34 +01:00
|
|
|
let trait_to_impl_substs = impl_trait_ref.substs;
|
2015-01-14 22:43:17 +01:00
|
|
|
|
2017-04-10 09:00:08 +02:00
|
|
|
// This node-id should be used for the `body_id` field on each
|
|
|
|
// `ObligationCause` (and the `FnCtxt`). This is what
|
|
|
|
// `regionck_item` expects.
|
2019-03-04 09:00:30 +01:00
|
|
|
let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id).unwrap();
|
2017-04-10 09:00:08 +02:00
|
|
|
|
2016-11-07 19:25:06 +01:00
|
|
|
let cause = ObligationCause {
|
|
|
|
span: impl_m_span,
|
2019-02-04 20:01:14 +01:00
|
|
|
body_id: impl_m_hir_id,
|
2016-11-07 19:25:06 +01:00
|
|
|
code: ObligationCauseCode::CompareImplMethodObligation {
|
2018-06-10 21:24:24 +02:00
|
|
|
item_name: impl_m.ident.name,
|
2016-11-07 19:25:06 +01:00
|
|
|
impl_item_def_id: impl_m.def_id,
|
|
|
|
trait_item_def_id: trait_m.def_id,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2015-01-14 22:43:17 +01:00
|
|
|
// This code is best explained by example. Consider a trait:
|
|
|
|
//
|
|
|
|
// trait Trait<'t,T> {
|
|
|
|
// fn method<'a,M>(t: &'t T, m: &'a M) -> Self;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// And an impl:
|
|
|
|
//
|
|
|
|
// impl<'i, 'j, U> Trait<'j, &'i U> for Foo {
|
|
|
|
// fn method<'b,N>(t: &'j &'i U, m: &'b N) -> Foo;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// We wish to decide if those two method types are compatible.
|
|
|
|
//
|
|
|
|
// We start out with trait_to_impl_substs, that maps the trait
|
|
|
|
// type parameters to impl type parameters. This is taken from the
|
|
|
|
// impl trait reference:
|
|
|
|
//
|
|
|
|
// trait_to_impl_substs = {'t => 'j, T => &'i U, Self => Foo}
|
|
|
|
//
|
|
|
|
// We create a mapping `dummy_substs` that maps from the impl type
|
|
|
|
// parameters to fresh types and regions. For type parameters,
|
|
|
|
// this is the identity transform, but we could as well use any
|
2018-09-07 15:46:53 +02:00
|
|
|
// placeholder types. For regions, we convert from bound to free
|
2015-01-14 22:43:17 +01:00
|
|
|
// regions (Note: but only early-bound regions, i.e., those
|
|
|
|
// declared on the impl or used in type parameter bounds).
|
|
|
|
//
|
|
|
|
// impl_to_skol_substs = {'i => 'i0, U => U0, N => N0 }
|
|
|
|
//
|
|
|
|
// Now we can apply skol_substs to the type of the impl method
|
2018-09-07 15:46:53 +02:00
|
|
|
// to yield a new function type in terms of our fresh, placeholder
|
2015-01-14 22:43:17 +01:00
|
|
|
// types:
|
|
|
|
//
|
|
|
|
// <'b> fn(t: &'i0 U0, m: &'b) -> Foo
|
|
|
|
//
|
|
|
|
// We now want to extract and substitute the type of the *trait*
|
|
|
|
// method and compare it. To do so, we must create a compound
|
|
|
|
// substitution by combining trait_to_impl_substs and
|
|
|
|
// impl_to_skol_substs, and also adding a mapping for the method
|
|
|
|
// type parameters. We extend the mapping to also include
|
|
|
|
// the method parameters.
|
|
|
|
//
|
|
|
|
// trait_to_skol_substs = { T => &'i0 U0, Self => Foo, M => N0 }
|
|
|
|
//
|
|
|
|
// Applying this to the trait method type yields:
|
|
|
|
//
|
|
|
|
// <'a> fn(t: &'i0 U0, m: &'a) -> Foo
|
|
|
|
//
|
|
|
|
// This type is also the same but the name of the bound region ('a
|
|
|
|
// vs 'b). However, the normal subtyping rules on fn types handle
|
|
|
|
// this kind of equivalency just fine.
|
|
|
|
//
|
2015-02-05 05:00:02 +01:00
|
|
|
// We now use these substitutions to ensure that all declared bounds are
|
2015-01-14 22:43:17 +01:00
|
|
|
// satisfied by the implementation's method.
|
|
|
|
//
|
|
|
|
// We do this by creating a parameter environment which contains a
|
2015-02-05 05:00:02 +01:00
|
|
|
// substitution corresponding to impl_to_skol_substs. We then build
|
2015-01-14 22:43:17 +01:00
|
|
|
// trait_to_skol_substs and use it to convert the predicates contained
|
2018-09-07 15:46:53 +02:00
|
|
|
// in the trait_m.generics to the placeholder form.
|
2015-01-14 22:43:17 +01:00
|
|
|
//
|
|
|
|
// Finally we register each of these predicates as an obligation in
|
|
|
|
// a fresh FulfillmentCtxt, and invoke select_all_or_error.
|
|
|
|
|
2018-09-07 15:46:53 +02:00
|
|
|
// Create mapping from impl to placeholder.
|
2019-02-26 02:30:34 +01:00
|
|
|
let impl_to_skol_substs = InternalSubsts::identity_for_item(tcx, impl_m.def_id);
|
2015-01-14 22:43:17 +01:00
|
|
|
|
2018-09-07 15:46:53 +02:00
|
|
|
// Create mapping from trait to placeholder.
|
2019-12-22 23:42:04 +01:00
|
|
|
let trait_to_skol_substs =
|
|
|
|
impl_to_skol_substs.rebase_onto(tcx, impl_m.container.id(), trait_to_impl_substs);
|
|
|
|
debug!("compare_impl_method: trait_to_skol_substs={:?}", trait_to_skol_substs);
|
2015-01-14 22:43:17 +01:00
|
|
|
|
2017-04-24 14:20:46 +02:00
|
|
|
let impl_m_generics = tcx.generics_of(impl_m.def_id);
|
|
|
|
let trait_m_generics = tcx.generics_of(trait_m.def_id);
|
|
|
|
let impl_m_predicates = tcx.predicates_of(impl_m.def_id);
|
|
|
|
let trait_m_predicates = tcx.predicates_of(trait_m.def_id);
|
2016-11-10 01:06:34 +01:00
|
|
|
|
2016-10-19 16:45:49 +02:00
|
|
|
// Check region bounds.
|
2019-12-08 17:58:22 +01:00
|
|
|
check_region_bounds_on_impl_item(
|
|
|
|
tcx,
|
|
|
|
impl_m_span,
|
|
|
|
impl_m,
|
|
|
|
trait_m,
|
|
|
|
&trait_m_generics,
|
|
|
|
&impl_m_generics,
|
|
|
|
)?;
|
2016-10-19 16:45:49 +02:00
|
|
|
|
|
|
|
// Create obligations for each predicate declared by the impl
|
|
|
|
// definition in the context of the trait's parameter
|
|
|
|
// environment. We can't just use `impl_env.caller_bounds`,
|
|
|
|
// however, because we want to replace all late-bound regions with
|
|
|
|
// region variables.
|
2017-04-24 14:20:46 +02:00
|
|
|
let impl_predicates = tcx.predicates_of(impl_m_predicates.parent.unwrap());
|
2017-05-11 14:05:00 +02:00
|
|
|
let mut hybrid_preds = impl_predicates.instantiate_identity(tcx);
|
2016-10-19 16:45:49 +02:00
|
|
|
|
|
|
|
debug!("compare_impl_method: impl_bounds={:?}", hybrid_preds);
|
|
|
|
|
|
|
|
// This is the only tricky bit of the new way we check implementation methods
|
|
|
|
// We need to build a set of predicates where only the method-level bounds
|
|
|
|
// are from the trait and we assume all other bounds from the implementation
|
|
|
|
// to be previously satisfied.
|
|
|
|
//
|
|
|
|
// We then register the obligations from the impl_m and check to see
|
|
|
|
// if all constraints hold.
|
2019-12-22 23:42:04 +01:00
|
|
|
hybrid_preds
|
|
|
|
.predicates
|
|
|
|
.extend(trait_m_predicates.instantiate_own(tcx, trait_to_skol_substs).predicates);
|
2016-10-19 16:45:49 +02:00
|
|
|
|
2018-09-07 15:46:53 +02:00
|
|
|
// Construct trait parameter environment and then shift it into the placeholder viewpoint.
|
2016-10-19 16:45:49 +02:00
|
|
|
// The key step here is to update the caller_bounds's predicates to be
|
|
|
|
// the new hybrid bounds we computed.
|
2019-02-04 20:01:14 +01:00
|
|
|
let normalize_cause = traits::ObligationCause::misc(impl_m_span, impl_m_hir_id);
|
2018-11-17 18:56:14 +01:00
|
|
|
let param_env = ty::ParamEnv::new(
|
|
|
|
tcx.intern_predicates(&hybrid_preds.predicates),
|
|
|
|
Reveal::UserFacing,
|
2019-12-22 23:42:04 +01:00
|
|
|
None,
|
|
|
|
);
|
|
|
|
let param_env = traits::normalize_param_env_or_error(
|
|
|
|
tcx,
|
|
|
|
impl_m.def_id,
|
|
|
|
param_env,
|
|
|
|
normalize_cause.clone(),
|
2018-11-17 18:56:14 +01:00
|
|
|
);
|
2017-05-10 16:28:06 +02:00
|
|
|
|
2017-06-09 09:55:16 +02:00
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
2017-05-07 18:57:51 +02:00
|
|
|
let inh = Inherited::new(infcx, impl_m.def_id);
|
2016-08-24 08:50:49 +02:00
|
|
|
let infcx = &inh.infcx;
|
2016-03-25 04:22:44 +01:00
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
debug!("compare_impl_method: caller_bounds={:?}", param_env.caller_bounds);
|
2016-03-25 04:22:44 +01:00
|
|
|
|
|
|
|
let mut selcx = traits::SelectionContext::new(&infcx);
|
|
|
|
|
2016-11-10 01:06:34 +01:00
|
|
|
let impl_m_own_bounds = impl_m_predicates.instantiate_own(tcx, impl_to_skol_substs);
|
2018-11-01 15:30:37 +01:00
|
|
|
let (impl_m_own_bounds, _) = infcx.replace_bound_vars_with_fresh_vars(
|
2018-04-25 04:45:49 +02:00
|
|
|
impl_m_span,
|
|
|
|
infer::HigherRankedType,
|
2019-12-22 23:42:04 +01:00
|
|
|
&ty::Binder::bind(impl_m_own_bounds.predicates),
|
2018-04-25 04:45:49 +02:00
|
|
|
);
|
2016-08-11 08:19:42 +02:00
|
|
|
for predicate in impl_m_own_bounds {
|
2017-04-15 22:58:50 +02:00
|
|
|
let traits::Normalized { value: predicate, obligations } =
|
2017-05-23 10:19:47 +02:00
|
|
|
traits::normalize(&mut selcx, param_env, normalize_cause.clone(), &predicate);
|
2016-03-25 04:22:44 +01:00
|
|
|
|
2017-04-15 22:58:50 +02:00
|
|
|
inh.register_predicates(obligations);
|
2017-05-23 10:19:47 +02:00
|
|
|
inh.register_predicate(traits::Obligation::new(cause.clone(), param_env, predicate));
|
2016-03-25 04:22:44 +01:00
|
|
|
}
|
2015-01-14 22:43:17 +01:00
|
|
|
|
2016-03-25 04:22:44 +01:00
|
|
|
// We now need to check that the signature of the impl method is
|
|
|
|
// compatible with that of the trait method. We do this by
|
|
|
|
// checking that `impl_fty <: trait_fty`.
|
|
|
|
//
|
|
|
|
// FIXME. Unfortunately, this doesn't quite work right now because
|
|
|
|
// associated type normalization is not integrated into subtype
|
|
|
|
// checks. For the comparison to be valid, we need to
|
|
|
|
// normalize the associated types in the impl/trait methods
|
|
|
|
// first. However, because function types bind regions, just
|
|
|
|
// calling `normalize_associated_types_in` would have no effect on
|
|
|
|
// any associated types appearing in the fn arguments or return
|
|
|
|
// type.
|
|
|
|
|
2018-09-07 15:46:53 +02:00
|
|
|
// Compute placeholder form of impl and trait method tys.
|
2016-05-25 20:12:35 +02:00
|
|
|
let tcx = infcx.tcx;
|
|
|
|
|
2018-11-01 15:30:37 +01:00
|
|
|
let (impl_sig, _) = infcx.replace_bound_vars_with_fresh_vars(
|
|
|
|
impl_m_span,
|
|
|
|
infer::HigherRankedType,
|
2019-12-22 23:42:04 +01:00
|
|
|
&tcx.fn_sig(impl_m.def_id),
|
2018-11-01 15:30:37 +01:00
|
|
|
);
|
2016-08-24 08:50:49 +02:00
|
|
|
let impl_sig =
|
2019-12-22 23:42:04 +01:00
|
|
|
inh.normalize_associated_types_in(impl_m_span, impl_m_hir_id, param_env, &impl_sig);
|
2018-04-25 04:45:49 +02:00
|
|
|
let impl_fty = tcx.mk_fn_ptr(ty::Binder::bind(impl_sig));
|
2016-05-25 20:12:35 +02:00
|
|
|
debug!("compare_impl_method: impl_fty={:?}", impl_fty);
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
let trait_sig = tcx.liberate_late_bound_regions(impl_m.def_id, &tcx.fn_sig(trait_m.def_id));
|
|
|
|
let trait_sig = trait_sig.subst(tcx, trait_to_skol_substs);
|
2016-08-24 08:50:49 +02:00
|
|
|
let trait_sig =
|
2019-12-22 23:42:04 +01:00
|
|
|
inh.normalize_associated_types_in(impl_m_span, impl_m_hir_id, param_env, &trait_sig);
|
2018-04-25 04:45:49 +02:00
|
|
|
let trait_fty = tcx.mk_fn_ptr(ty::Binder::bind(trait_sig));
|
2016-05-25 20:12:35 +02:00
|
|
|
|
|
|
|
debug!("compare_impl_method: trait_fty={:?}", trait_fty);
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
let sub_result = infcx.at(&cause, param_env).sup(trait_fty, impl_fty).map(
|
|
|
|
|InferOk { obligations, .. }| {
|
|
|
|
inh.register_predicates(obligations);
|
|
|
|
},
|
|
|
|
);
|
2016-10-04 01:19:40 +02:00
|
|
|
|
|
|
|
if let Err(terr) = sub_result {
|
2019-12-22 23:42:04 +01:00
|
|
|
debug!("sub_types failed: impl ty {:?}, trait ty {:?}", impl_fty, trait_fty);
|
|
|
|
|
|
|
|
let (impl_err_span, trait_err_span) = extract_spans_for_error_reporting(
|
|
|
|
&infcx, param_env, &terr, &cause, impl_m, impl_sig, trait_m, trait_sig,
|
|
|
|
);
|
|
|
|
|
|
|
|
let cause = ObligationCause { span: impl_err_span, ..cause };
|
|
|
|
|
|
|
|
let mut diag = struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
cause.span(tcx),
|
|
|
|
E0053,
|
|
|
|
"method `{}` has an incompatible type for trait",
|
|
|
|
trait_m.ident
|
|
|
|
);
|
2018-07-25 14:46:10 +02:00
|
|
|
if let TypeError::Mutability = terr {
|
|
|
|
if let Some(trait_err_span) = trait_err_span {
|
2019-12-22 23:42:04 +01:00
|
|
|
if let Ok(trait_err_str) = tcx.sess.source_map().span_to_snippet(trait_err_span)
|
|
|
|
{
|
2019-01-25 22:03:27 +01:00
|
|
|
diag.span_suggestion(
|
2018-07-25 14:46:10 +02:00
|
|
|
impl_err_span,
|
|
|
|
"consider change the type to match the mutability in trait",
|
2018-10-25 20:11:11 +02:00
|
|
|
trait_err_str,
|
2018-09-17 02:16:08 +02:00
|
|
|
Applicability::MachineApplicable,
|
2018-07-25 14:46:10 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 05:41:27 +02:00
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
infcx.note_type_err(
|
|
|
|
&mut diag,
|
|
|
|
&cause,
|
|
|
|
trait_err_span.map(|sp| (sp, "type in trait".to_owned())),
|
|
|
|
Some(infer::ValuePairs::Types(ExpectedFound {
|
|
|
|
expected: trait_fty,
|
|
|
|
found: impl_fty,
|
|
|
|
})),
|
|
|
|
&terr,
|
|
|
|
);
|
2016-07-21 19:12:30 +02:00
|
|
|
diag.emit();
|
2016-10-19 16:45:49 +02:00
|
|
|
return Err(ErrorReported);
|
2015-01-14 22:43:17 +01:00
|
|
|
}
|
|
|
|
|
2016-03-25 04:22:44 +01:00
|
|
|
// Check that all obligations are satisfied by the implementation's
|
|
|
|
// version.
|
2017-04-15 22:58:50 +02:00
|
|
|
if let Err(ref errors) = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx) {
|
2018-03-12 05:15:06 +01:00
|
|
|
infcx.report_fulfillment_errors(errors, None, false);
|
2016-10-19 16:45:49 +02:00
|
|
|
return Err(ErrorReported);
|
2016-03-25 04:22:44 +01:00
|
|
|
}
|
2015-04-18 17:23:14 +02:00
|
|
|
|
2016-03-25 04:22:44 +01:00
|
|
|
// Finally, resolve all regions. This catches wily misuses of
|
2016-08-24 08:50:49 +02:00
|
|
|
// lifetime parameters.
|
2019-02-04 20:01:14 +01:00
|
|
|
let fcx = FnCtxt::new(&inh, param_env, impl_m_hir_id);
|
|
|
|
fcx.regionck_item(impl_m_hir_id, impl_m_span, &[]);
|
2016-10-19 16:45:49 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-08 17:58:22 +01:00
|
|
|
fn check_region_bounds_on_impl_item<'tcx>(
|
2019-06-13 23:48:52 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 23:11:55 +02:00
|
|
|
span: Span,
|
|
|
|
impl_m: &ty::AssocItem,
|
|
|
|
trait_m: &ty::AssocItem,
|
|
|
|
trait_generics: &ty::Generics,
|
|
|
|
impl_generics: &ty::Generics,
|
|
|
|
) -> Result<(), ErrorReported> {
|
2018-05-11 01:30:34 +02:00
|
|
|
let trait_params = trait_generics.own_counts().lifetimes;
|
|
|
|
let impl_params = impl_generics.own_counts().lifetimes;
|
2016-10-19 16:45:49 +02:00
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
debug!(
|
|
|
|
"check_region_bounds_on_impl_item: \
|
2016-10-19 16:45:49 +02:00
|
|
|
trait_generics={:?} \
|
2019-12-08 17:58:22 +01:00
|
|
|
impl_generics={:?}",
|
2019-12-22 23:42:04 +01:00
|
|
|
trait_generics, impl_generics
|
|
|
|
);
|
2016-10-19 16:45:49 +02:00
|
|
|
|
|
|
|
// Must have same number of early-bound lifetime parameters.
|
|
|
|
// Unfortunately, if the user screws up the bounds, then this
|
|
|
|
// will change classification between early and late. E.g.,
|
|
|
|
// if in trait we have `<'a,'b:'a>`, and in impl we just have
|
|
|
|
// `<'a,'b>`, then we have 2 early-bound lifetime parameters
|
|
|
|
// in trait but 0 in the impl. But if we report "expected 2
|
|
|
|
// but found 0" it's confusing, because it looks like there
|
|
|
|
// are zero. Since I don't quite know how to phrase things at
|
|
|
|
// the moment, give a kind of vague error message.
|
2018-04-14 00:12:14 +02:00
|
|
|
if trait_params != impl_params {
|
2019-12-08 17:58:22 +01:00
|
|
|
let item_kind = assoc_item_kind_str(impl_m);
|
2018-08-18 12:14:09 +02:00
|
|
|
let def_span = tcx.sess.source_map().def_span(span);
|
2019-06-01 07:19:30 +02:00
|
|
|
let span = tcx.hir().get_generics(impl_m.def_id).map(|g| g.span).unwrap_or(def_span);
|
2018-06-28 00:25:18 +02:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
span,
|
|
|
|
E0195,
|
2019-12-08 17:58:22 +01:00
|
|
|
"lifetime parameters or bounds on {} `{}` do not match the trait declaration",
|
|
|
|
item_kind,
|
2018-06-28 00:25:18 +02:00
|
|
|
impl_m.ident,
|
|
|
|
);
|
2019-12-08 17:58:22 +01:00
|
|
|
err.span_label(span, &format!("lifetimes do not match {} in trait", item_kind));
|
2018-12-04 13:45:36 +01:00
|
|
|
if let Some(sp) = tcx.hir().span_if_local(trait_m.def_id) {
|
2018-08-18 12:14:09 +02:00
|
|
|
let def_sp = tcx.sess.source_map().def_span(sp);
|
2019-06-01 07:19:30 +02:00
|
|
|
let sp = tcx.hir().get_generics(trait_m.def_id).map(|g| g.span).unwrap_or(def_sp);
|
2019-12-08 17:58:22 +01:00
|
|
|
err.span_label(
|
|
|
|
sp,
|
|
|
|
&format!("lifetimes in impl do not match this {} in trait", item_kind),
|
|
|
|
);
|
2018-01-28 22:42:49 +01:00
|
|
|
}
|
|
|
|
err.emit();
|
2016-10-19 16:45:49 +02:00
|
|
|
return Err(ErrorReported);
|
|
|
|
}
|
|
|
|
|
2018-09-19 13:51:51 +02:00
|
|
|
Ok(())
|
2016-10-19 16:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-06-14 00:32:15 +02:00
|
|
|
fn extract_spans_for_error_reporting<'a, 'tcx>(
|
|
|
|
infcx: &infer::InferCtxt<'a, 'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
terr: &TypeError<'_>,
|
|
|
|
cause: &ObligationCause<'tcx>,
|
|
|
|
impl_m: &ty::AssocItem,
|
|
|
|
impl_sig: ty::FnSig<'tcx>,
|
|
|
|
trait_m: &ty::AssocItem,
|
|
|
|
trait_sig: ty::FnSig<'tcx>,
|
|
|
|
) -> (Span, Option<Span>) {
|
2016-10-19 16:45:49 +02:00
|
|
|
let tcx = infcx.tcx;
|
2019-03-04 09:00:30 +01:00
|
|
|
let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id).unwrap();
|
2019-12-22 23:42:04 +01:00
|
|
|
let (impl_m_output, impl_m_iter) = match tcx.hir().expect_impl_item(impl_m_hir_id).kind {
|
2016-10-19 16:45:49 +02:00
|
|
|
ImplItemKind::Method(ref impl_m_sig, _) => {
|
|
|
|
(&impl_m_sig.decl.output, impl_m_sig.decl.inputs.iter())
|
|
|
|
}
|
|
|
|
_ => bug!("{:?} is not a method", impl_m),
|
|
|
|
};
|
|
|
|
|
|
|
|
match *terr {
|
|
|
|
TypeError::Mutability => {
|
2019-03-04 09:00:30 +01:00
|
|
|
if let Some(trait_m_hir_id) = tcx.hir().as_local_hir_id(trait_m.def_id) {
|
2019-12-22 23:42:04 +01:00
|
|
|
let trait_m_iter = match tcx.hir().expect_trait_item(trait_m_hir_id).kind {
|
|
|
|
TraitItemKind::Method(ref trait_m_sig, _) => trait_m_sig.decl.inputs.iter(),
|
2016-12-04 03:21:06 +01:00
|
|
|
_ => bug!("{:?} is not a TraitItemKind::Method", trait_m),
|
2016-10-19 16:45:49 +02:00
|
|
|
};
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
impl_m_iter
|
|
|
|
.zip(trait_m_iter)
|
|
|
|
.find(|&(ref impl_arg, ref trait_arg)| {
|
|
|
|
match (&impl_arg.kind, &trait_arg.kind) {
|
|
|
|
(
|
|
|
|
&hir::TyKind::Rptr(_, ref impl_mt),
|
|
|
|
&hir::TyKind::Rptr(_, ref trait_mt),
|
|
|
|
)
|
|
|
|
| (&hir::TyKind::Ptr(ref impl_mt), &hir::TyKind::Ptr(ref trait_mt)) => {
|
|
|
|
impl_mt.mutbl != trait_mt.mutbl
|
|
|
|
}
|
|
|
|
_ => false,
|
2016-12-04 02:18:11 +01:00
|
|
|
}
|
2019-12-22 23:42:04 +01:00
|
|
|
})
|
|
|
|
.map(|(ref impl_arg, ref trait_arg)| (impl_arg.span, Some(trait_arg.span)))
|
|
|
|
.unwrap_or_else(|| (cause.span(tcx), tcx.hir().span_if_local(trait_m.def_id)))
|
2016-10-19 16:45:49 +02:00
|
|
|
} else {
|
2019-04-25 22:54:19 +02:00
|
|
|
(cause.span(tcx), tcx.hir().span_if_local(trait_m.def_id))
|
2016-10-19 16:45:49 +02:00
|
|
|
}
|
2015-01-14 22:43:17 +01:00
|
|
|
}
|
2016-10-19 16:45:49 +02:00
|
|
|
TypeError::Sorts(ExpectedFound { .. }) => {
|
2019-03-04 09:00:30 +01:00
|
|
|
if let Some(trait_m_hir_id) = tcx.hir().as_local_hir_id(trait_m.def_id) {
|
2016-10-19 16:45:49 +02:00
|
|
|
let (trait_m_output, trait_m_iter) =
|
2019-09-26 18:07:54 +02:00
|
|
|
match tcx.hir().expect_trait_item(trait_m_hir_id).kind {
|
2016-12-04 03:21:06 +01:00
|
|
|
TraitItemKind::Method(ref trait_m_sig, _) => {
|
2016-10-19 16:45:49 +02:00
|
|
|
(&trait_m_sig.decl.output, trait_m_sig.decl.inputs.iter())
|
|
|
|
}
|
2016-12-04 03:21:06 +01:00
|
|
|
_ => bug!("{:?} is not a TraitItemKind::Method", trait_m),
|
2016-10-19 16:45:49 +02:00
|
|
|
};
|
2015-01-14 22:43:17 +01:00
|
|
|
|
2016-11-29 03:35:38 +01:00
|
|
|
let impl_iter = impl_sig.inputs().iter();
|
|
|
|
let trait_iter = trait_sig.inputs().iter();
|
2019-12-22 23:42:04 +01:00
|
|
|
impl_iter
|
|
|
|
.zip(trait_iter)
|
|
|
|
.zip(impl_m_iter)
|
|
|
|
.zip(trait_m_iter)
|
|
|
|
.filter_map(
|
|
|
|
|(((&impl_arg_ty, &trait_arg_ty), impl_arg), trait_arg)| match infcx
|
|
|
|
.at(&cause, param_env)
|
|
|
|
.sub(trait_arg_ty, impl_arg_ty)
|
|
|
|
{
|
|
|
|
Ok(_) => None,
|
|
|
|
Err(_) => Some((impl_arg.span, Some(trait_arg.span))),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.next()
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
if infcx
|
|
|
|
.at(&cause, param_env)
|
|
|
|
.sup(trait_sig.output(), impl_sig.output())
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
(impl_m_output.span(), Some(trait_m_output.span()))
|
|
|
|
} else {
|
|
|
|
(cause.span(tcx), tcx.hir().span_if_local(trait_m.def_id))
|
|
|
|
}
|
|
|
|
})
|
2016-10-19 16:45:49 +02:00
|
|
|
} else {
|
2019-04-25 22:54:19 +02:00
|
|
|
(cause.span(tcx), tcx.hir().span_if_local(trait_m.def_id))
|
2016-10-19 16:45:49 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-25 22:54:19 +02:00
|
|
|
_ => (cause.span(tcx), tcx.hir().span_if_local(trait_m.def_id)),
|
2015-01-14 22:43:17 +01:00
|
|
|
}
|
2016-10-19 16:45:49 +02:00
|
|
|
}
|
2016-08-22 10:59:18 +02:00
|
|
|
|
2019-06-11 23:11:55 +02:00
|
|
|
fn compare_self_type<'tcx>(
|
2019-06-13 23:48:52 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 23:11:55 +02:00
|
|
|
impl_m: &ty::AssocItem,
|
|
|
|
impl_m_span: Span,
|
|
|
|
trait_m: &ty::AssocItem,
|
|
|
|
impl_trait_ref: ty::TraitRef<'tcx>,
|
|
|
|
) -> Result<(), ErrorReported> {
|
2016-10-19 16:45:49 +02:00
|
|
|
// Try to give more informative error messages about self typing
|
|
|
|
// mismatches. Note that any mismatch will also be detected
|
|
|
|
// below, where we construct a canonical function type that
|
|
|
|
// includes the self parameter as a normal parameter. It's just
|
|
|
|
// that the error messages you get out of this code are a bit more
|
|
|
|
// inscrutable, particularly for cases where one method has no
|
|
|
|
// self.
|
2016-11-10 01:06:34 +01:00
|
|
|
|
2019-05-19 10:26:08 +02:00
|
|
|
let self_string = |method: &ty::AssocItem| {
|
2016-11-10 01:06:34 +01:00
|
|
|
let untransformed_self_ty = match method.container {
|
|
|
|
ty::ImplContainer(_) => impl_trait_ref.self_ty(),
|
2019-12-22 23:42:04 +01:00
|
|
|
ty::TraitContainer(_) => tcx.types.self_param,
|
2016-11-10 01:06:34 +01:00
|
|
|
};
|
2017-05-13 16:11:52 +02:00
|
|
|
let self_arg_ty = *tcx.fn_sig(method.def_id).input(0).skip_binder();
|
2018-02-10 19:18:02 +01:00
|
|
|
let param_env = ty::ParamEnv::reveal_all();
|
2017-11-05 12:43:29 +01:00
|
|
|
|
2017-11-05 15:25:36 +01:00
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
2019-12-22 23:42:04 +01:00
|
|
|
let self_arg_ty =
|
|
|
|
tcx.liberate_late_bound_regions(method.def_id, &ty::Binder::bind(self_arg_ty));
|
2017-11-08 10:31:48 +01:00
|
|
|
let can_eq_self = |ty| infcx.can_eq(param_env, untransformed_self_ty, ty).is_ok();
|
|
|
|
match ExplicitSelf::determine(self_arg_ty, can_eq_self) {
|
2018-09-19 13:51:51 +02:00
|
|
|
ExplicitSelf::ByValue => "self".to_owned(),
|
2019-12-16 17:28:40 +01:00
|
|
|
ExplicitSelf::ByReference(_, hir::Mutability::Not) => "&self".to_owned(),
|
|
|
|
ExplicitSelf::ByReference(_, hir::Mutability::Mut) => "&mut self".to_owned(),
|
2019-12-22 23:42:04 +01:00
|
|
|
_ => format!("self: {}", self_arg_ty),
|
2017-11-05 15:25:36 +01:00
|
|
|
}
|
|
|
|
})
|
2016-11-10 01:06:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
match (trait_m.method_has_self_argument, impl_m.method_has_self_argument) {
|
|
|
|
(false, false) | (true, true) => {}
|
|
|
|
|
|
|
|
(false, true) => {
|
|
|
|
let self_descr = self_string(impl_m);
|
2019-12-22 23:42:04 +01:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
impl_m_span,
|
|
|
|
E0185,
|
|
|
|
"method `{}` has a `{}` declaration in the impl, but \
|
2016-10-19 16:45:49 +02:00
|
|
|
not in the trait",
|
2019-12-22 23:42:04 +01:00
|
|
|
trait_m.ident,
|
|
|
|
self_descr
|
|
|
|
);
|
2017-05-04 14:17:23 +02:00
|
|
|
err.span_label(impl_m_span, format!("`{}` used in impl", self_descr));
|
2018-12-04 13:45:36 +01:00
|
|
|
if let Some(span) = tcx.hir().span_if_local(trait_m.def_id) {
|
2018-01-15 14:18:06 +01:00
|
|
|
err.span_label(span, format!("trait method declared without `{}`", self_descr));
|
|
|
|
} else {
|
2019-12-22 23:42:04 +01:00
|
|
|
err.note_trait_signature(trait_m.ident.to_string(), trait_m.signature(tcx));
|
2016-10-19 16:45:49 +02:00
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
return Err(ErrorReported);
|
|
|
|
}
|
2016-11-10 01:06:34 +01:00
|
|
|
|
|
|
|
(true, false) => {
|
|
|
|
let self_descr = self_string(trait_m);
|
2019-12-22 23:42:04 +01:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
impl_m_span,
|
|
|
|
E0186,
|
|
|
|
"method `{}` has a `{}` declaration in the trait, but \
|
2016-10-19 16:45:49 +02:00
|
|
|
not in the impl",
|
2019-12-22 23:42:04 +01:00
|
|
|
trait_m.ident,
|
|
|
|
self_descr
|
|
|
|
);
|
2018-01-15 14:18:06 +01:00
|
|
|
err.span_label(impl_m_span, format!("expected `{}` in impl", self_descr));
|
2018-12-04 13:45:36 +01:00
|
|
|
if let Some(span) = tcx.hir().span_if_local(trait_m.def_id) {
|
2017-05-04 14:17:23 +02:00
|
|
|
err.span_label(span, format!("`{}` used in trait", self_descr));
|
Show trait method signature when impl differs
When the trait's span is available, it is already being used, add a
`note` for the cases where the span isn't available:
```
error[E0053]: method `fmt` has an incompatible type for trait
--> $DIR/trait_type.rs:17:4
|
17 | fn fmt(&self, x: &str) -> () { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ in mutability
|
= note: expected type `fn(&MyType, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
found type `fn(&MyType, &str)`
error[E0050]: method `fmt` has 1 parameter but the declaration in trait `std::fmt::Display::fmt` has 2
--> $DIR/trait_type.rs:21:11
|
21 | fn fmt(&self) -> () { }
| ^^^^^ expected 2 parameters, found 1
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
error[E0186]: method `fmt` has a `&self` declaration in the trait, but not in the impl
--> $DIR/trait_type.rs:25:4
|
25 | fn fmt() -> () { }
| ^^^^^^^^^^^^^^^^^^ expected `&self` in impl
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
error[E0046]: not all trait items implemented, missing: `fmt`
--> $DIR/trait_type.rs:28:1
|
28 | impl std::fmt::Display for MyType4 {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `fmt` in implementation
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
```
2017-06-01 08:14:43 +02:00
|
|
|
} else {
|
2019-12-22 23:42:04 +01:00
|
|
|
err.note_trait_signature(trait_m.ident.to_string(), trait_m.signature(tcx));
|
2016-10-19 16:45:49 +02:00
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
return Err(ErrorReported);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-06-11 22:35:39 +02:00
|
|
|
fn compare_number_of_generics<'tcx>(
|
2019-06-13 23:48:52 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-05-19 10:26:08 +02:00
|
|
|
impl_: &ty::AssocItem,
|
2019-06-01 19:35:31 +02:00
|
|
|
_impl_span: Span,
|
2019-05-19 10:26:08 +02:00
|
|
|
trait_: &ty::AssocItem,
|
2019-02-20 02:16:52 +01:00
|
|
|
trait_span: Option<Span>,
|
|
|
|
) -> Result<(), ErrorReported> {
|
|
|
|
let trait_own_counts = tcx.generics_of(trait_.def_id).own_counts();
|
|
|
|
let impl_own_counts = tcx.generics_of(impl_.def_id).own_counts();
|
|
|
|
|
|
|
|
let matchings = [
|
|
|
|
("type", trait_own_counts.types, impl_own_counts.types),
|
|
|
|
("const", trait_own_counts.consts, impl_own_counts.consts),
|
|
|
|
];
|
|
|
|
|
2019-12-08 17:58:22 +01:00
|
|
|
let item_kind = assoc_item_kind_str(impl_);
|
|
|
|
|
2019-02-20 02:16:52 +01:00
|
|
|
let mut err_occurred = false;
|
|
|
|
for &(kind, trait_count, impl_count) in &matchings {
|
|
|
|
if impl_count != trait_count {
|
|
|
|
err_occurred = true;
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
let (trait_spans, impl_trait_spans) =
|
|
|
|
if let Some(trait_hir_id) = tcx.hir().as_local_hir_id(trait_.def_id) {
|
|
|
|
let trait_item = tcx.hir().expect_trait_item(trait_hir_id);
|
|
|
|
if trait_item.generics.params.is_empty() {
|
|
|
|
(Some(vec![trait_item.generics.span]), vec![])
|
|
|
|
} else {
|
|
|
|
let arg_spans: Vec<Span> =
|
|
|
|
trait_item.generics.params.iter().map(|p| p.span).collect();
|
|
|
|
let impl_trait_spans: Vec<Span> = trait_item
|
|
|
|
.generics
|
|
|
|
.params
|
|
|
|
.iter()
|
|
|
|
.filter_map(|p| match p.kind {
|
|
|
|
GenericParamKind::Type {
|
|
|
|
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
|
|
|
|
..
|
|
|
|
} => Some(p.span),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
(Some(arg_spans), impl_trait_spans)
|
|
|
|
}
|
2019-06-01 19:35:31 +02:00
|
|
|
} else {
|
2019-12-22 23:42:04 +01:00
|
|
|
(trait_span.map(|s| vec![s]), vec![])
|
|
|
|
};
|
2016-08-22 10:59:18 +02:00
|
|
|
|
2019-06-01 19:35:31 +02:00
|
|
|
let impl_hir_id = tcx.hir().as_local_hir_id(impl_.def_id).unwrap();
|
|
|
|
let impl_item = tcx.hir().expect_impl_item(impl_hir_id);
|
2019-12-22 23:42:04 +01:00
|
|
|
let impl_item_impl_trait_spans: Vec<Span> = impl_item
|
|
|
|
.generics
|
|
|
|
.params
|
|
|
|
.iter()
|
2019-06-02 00:39:12 +02:00
|
|
|
.filter_map(|p| match p.kind {
|
|
|
|
GenericParamKind::Type {
|
2019-12-22 23:42:04 +01:00
|
|
|
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
|
|
|
|
..
|
2019-06-02 00:39:12 +02:00
|
|
|
} => Some(p.span),
|
|
|
|
_ => None,
|
2019-12-22 23:42:04 +01:00
|
|
|
})
|
|
|
|
.collect();
|
2019-06-01 19:35:31 +02:00
|
|
|
let spans = impl_item.generics.spans();
|
|
|
|
let span = spans.primary_span();
|
|
|
|
|
2019-02-20 02:16:52 +01:00
|
|
|
let mut err = tcx.sess.struct_span_err_with_code(
|
2019-06-01 19:35:31 +02:00
|
|
|
spans,
|
2019-02-20 02:16:52 +01:00
|
|
|
&format!(
|
2019-12-08 17:58:22 +01:00
|
|
|
"{} `{}` has {} {kind} parameter{} but its trait \
|
2019-02-20 02:16:52 +01:00
|
|
|
declaration has {} {kind} parameter{}",
|
2019-12-08 17:58:22 +01:00
|
|
|
item_kind,
|
2019-02-20 02:16:52 +01:00
|
|
|
trait_.ident,
|
|
|
|
impl_count,
|
2019-11-05 21:10:24 +01:00
|
|
|
pluralize!(impl_count),
|
2019-02-20 02:16:52 +01:00
|
|
|
trait_count,
|
2019-11-05 21:10:24 +01:00
|
|
|
pluralize!(trait_count),
|
2019-02-20 02:16:52 +01:00
|
|
|
kind = kind,
|
|
|
|
),
|
|
|
|
DiagnosticId::Error("E0049".into()),
|
|
|
|
);
|
2016-08-22 10:59:18 +02:00
|
|
|
|
2019-02-20 02:16:52 +01:00
|
|
|
let mut suffix = None;
|
2016-10-19 16:45:49 +02:00
|
|
|
|
2019-06-01 19:35:31 +02:00
|
|
|
if let Some(spans) = trait_spans {
|
|
|
|
let mut spans = spans.iter();
|
|
|
|
if let Some(span) = spans.next() {
|
2019-12-22 23:42:04 +01:00
|
|
|
err.span_label(
|
|
|
|
*span,
|
|
|
|
format!(
|
|
|
|
"expected {} {} parameter{}",
|
|
|
|
trait_count,
|
|
|
|
kind,
|
|
|
|
pluralize!(trait_count),
|
|
|
|
),
|
|
|
|
);
|
2019-06-01 19:35:31 +02:00
|
|
|
}
|
|
|
|
for span in spans {
|
|
|
|
err.span_label(*span, "");
|
|
|
|
}
|
2019-02-20 02:16:52 +01:00
|
|
|
} else {
|
|
|
|
suffix = Some(format!(", expected {}", trait_count));
|
|
|
|
}
|
2016-10-19 16:45:49 +02:00
|
|
|
|
2019-06-01 19:35:31 +02:00
|
|
|
if let Some(span) = span {
|
2019-12-22 23:42:04 +01:00
|
|
|
err.span_label(
|
|
|
|
span,
|
|
|
|
format!(
|
|
|
|
"found {} {} parameter{}{}",
|
|
|
|
impl_count,
|
|
|
|
kind,
|
|
|
|
pluralize!(impl_count),
|
|
|
|
suffix.unwrap_or_else(|| String::new()),
|
|
|
|
),
|
|
|
|
);
|
2019-06-01 19:35:31 +02:00
|
|
|
}
|
2016-10-19 16:45:49 +02:00
|
|
|
|
2019-06-01 20:33:38 +02:00
|
|
|
for span in impl_trait_spans.iter().chain(impl_item_impl_trait_spans.iter()) {
|
|
|
|
err.span_label(*span, "`impl Trait` introduces an implicit type parameter");
|
|
|
|
}
|
|
|
|
|
2019-02-20 02:16:52 +01:00
|
|
|
err.emit();
|
|
|
|
}
|
2016-10-19 16:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
if err_occurred { Err(ErrorReported) } else { Ok(()) }
|
2016-10-19 16:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 23:11:55 +02:00
|
|
|
fn compare_number_of_method_arguments<'tcx>(
|
2019-06-13 23:48:52 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 23:11:55 +02:00
|
|
|
impl_m: &ty::AssocItem,
|
|
|
|
impl_m_span: Span,
|
|
|
|
trait_m: &ty::AssocItem,
|
|
|
|
trait_item_span: Option<Span>,
|
|
|
|
) -> Result<(), ErrorReported> {
|
2017-05-13 16:11:52 +02:00
|
|
|
let impl_m_fty = tcx.fn_sig(impl_m.def_id);
|
|
|
|
let trait_m_fty = tcx.fn_sig(trait_m.def_id);
|
2017-02-13 09:51:06 +01:00
|
|
|
let trait_number_args = trait_m_fty.inputs().skip_binder().len();
|
|
|
|
let impl_number_args = impl_m_fty.inputs().skip_binder().len();
|
2016-11-29 03:35:38 +01:00
|
|
|
if trait_number_args != impl_number_args {
|
2019-03-04 09:00:30 +01:00
|
|
|
let trait_m_hir_id = tcx.hir().as_local_hir_id(trait_m.def_id);
|
|
|
|
let trait_span = if let Some(trait_id) = trait_m_hir_id {
|
2019-09-26 18:07:54 +02:00
|
|
|
match tcx.hir().expect_trait_item(trait_id).kind {
|
2016-12-04 03:21:06 +01:00
|
|
|
TraitItemKind::Method(ref trait_m_sig, _) => {
|
2019-12-22 23:42:04 +01:00
|
|
|
let pos = if trait_number_args > 0 { trait_number_args - 1 } else { 0 };
|
2018-12-08 20:04:14 +01:00
|
|
|
if let Some(arg) = trait_m_sig.decl.inputs.get(pos) {
|
|
|
|
Some(if pos == 0 {
|
|
|
|
arg.span
|
|
|
|
} else {
|
2019-12-22 23:42:04 +01:00
|
|
|
Span::new(
|
|
|
|
trait_m_sig.decl.inputs[0].span.lo(),
|
|
|
|
arg.span.hi(),
|
|
|
|
arg.span.ctxt(),
|
|
|
|
)
|
2018-12-08 20:04:14 +01:00
|
|
|
})
|
2016-10-19 16:45:49 +02:00
|
|
|
} else {
|
|
|
|
trait_item_span
|
|
|
|
}
|
2016-08-22 10:59:18 +02:00
|
|
|
}
|
2016-10-19 16:45:49 +02:00
|
|
|
_ => bug!("{:?} is not a method", impl_m),
|
2016-08-22 10:59:18 +02:00
|
|
|
}
|
2016-10-19 16:45:49 +02:00
|
|
|
} else {
|
|
|
|
trait_item_span
|
|
|
|
};
|
2019-03-04 09:00:30 +01:00
|
|
|
let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id).unwrap();
|
2019-09-26 17:38:13 +02:00
|
|
|
let impl_span = match tcx.hir().expect_impl_item(impl_m_hir_id).kind {
|
2016-10-19 16:45:49 +02:00
|
|
|
ImplItemKind::Method(ref impl_m_sig, _) => {
|
2019-12-22 23:42:04 +01:00
|
|
|
let pos = if impl_number_args > 0 { impl_number_args - 1 } else { 0 };
|
2018-12-08 20:04:14 +01:00
|
|
|
if let Some(arg) = impl_m_sig.decl.inputs.get(pos) {
|
|
|
|
if pos == 0 {
|
|
|
|
arg.span
|
|
|
|
} else {
|
2019-12-22 23:42:04 +01:00
|
|
|
Span::new(
|
|
|
|
impl_m_sig.decl.inputs[0].span.lo(),
|
|
|
|
arg.span.hi(),
|
|
|
|
arg.span.ctxt(),
|
|
|
|
)
|
2018-12-08 20:04:14 +01:00
|
|
|
}
|
2016-10-19 16:45:49 +02:00
|
|
|
} else {
|
|
|
|
impl_m_span
|
2016-08-22 10:59:18 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-19 16:45:49 +02:00
|
|
|
_ => bug!("{:?} is not a method", impl_m),
|
|
|
|
};
|
2019-12-22 23:42:04 +01:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
impl_span,
|
|
|
|
E0050,
|
|
|
|
"method `{}` has {} but the declaration in \
|
2016-10-19 16:45:49 +02:00
|
|
|
trait `{}` has {}",
|
2019-12-22 23:42:04 +01:00
|
|
|
trait_m.ident,
|
|
|
|
potentially_plural_count(impl_number_args, "parameter"),
|
|
|
|
tcx.def_path_str(trait_m.def_id),
|
|
|
|
trait_number_args
|
|
|
|
);
|
2016-10-19 16:45:49 +02:00
|
|
|
if let Some(trait_span) = trait_span {
|
2019-12-22 23:42:04 +01:00
|
|
|
err.span_label(
|
|
|
|
trait_span,
|
|
|
|
format!(
|
|
|
|
"trait requires {}",
|
|
|
|
potentially_plural_count(trait_number_args, "parameter")
|
|
|
|
),
|
|
|
|
);
|
Show trait method signature when impl differs
When the trait's span is available, it is already being used, add a
`note` for the cases where the span isn't available:
```
error[E0053]: method `fmt` has an incompatible type for trait
--> $DIR/trait_type.rs:17:4
|
17 | fn fmt(&self, x: &str) -> () { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ in mutability
|
= note: expected type `fn(&MyType, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
found type `fn(&MyType, &str)`
error[E0050]: method `fmt` has 1 parameter but the declaration in trait `std::fmt::Display::fmt` has 2
--> $DIR/trait_type.rs:21:11
|
21 | fn fmt(&self) -> () { }
| ^^^^^ expected 2 parameters, found 1
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
error[E0186]: method `fmt` has a `&self` declaration in the trait, but not in the impl
--> $DIR/trait_type.rs:25:4
|
25 | fn fmt() -> () { }
| ^^^^^^^^^^^^^^^^^^ expected `&self` in impl
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
error[E0046]: not all trait items implemented, missing: `fmt`
--> $DIR/trait_type.rs:28:1
|
28 | impl std::fmt::Display for MyType4 {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `fmt` in implementation
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
```
2017-06-01 08:14:43 +02:00
|
|
|
} else {
|
2019-12-22 23:42:04 +01:00
|
|
|
err.note_trait_signature(trait_m.ident.to_string(), trait_m.signature(tcx));
|
2016-08-22 10:59:18 +02:00
|
|
|
}
|
2019-12-22 23:42:04 +01:00
|
|
|
err.span_label(
|
|
|
|
impl_span,
|
|
|
|
format!(
|
|
|
|
"expected {}, found {}",
|
|
|
|
potentially_plural_count(trait_number_args, "parameter"),
|
|
|
|
impl_number_args
|
|
|
|
),
|
|
|
|
);
|
2016-10-19 16:45:49 +02:00
|
|
|
err.emit();
|
|
|
|
return Err(ErrorReported);
|
2016-08-22 10:59:18 +02:00
|
|
|
}
|
2016-10-19 16:45:49 +02:00
|
|
|
|
|
|
|
Ok(())
|
2015-01-14 22:43:17 +01:00
|
|
|
}
|
2015-03-16 02:35:25 +01:00
|
|
|
|
2019-06-11 23:11:55 +02:00
|
|
|
fn compare_synthetic_generics<'tcx>(
|
2019-06-13 23:48:52 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 23:11:55 +02:00
|
|
|
impl_m: &ty::AssocItem,
|
|
|
|
trait_m: &ty::AssocItem,
|
|
|
|
) -> Result<(), ErrorReported> {
|
2017-11-10 18:47:33 +01:00
|
|
|
// FIXME(chrisvittal) Clean up this function, list of FIXME items:
|
2018-05-21 18:06:28 +02:00
|
|
|
// 1. Better messages for the span labels
|
2017-11-10 18:47:33 +01:00
|
|
|
// 2. Explanation as to what is going on
|
|
|
|
// If we get here, we already have the same number of generics, so the zip will
|
|
|
|
// be okay.
|
|
|
|
let mut error_found = false;
|
|
|
|
let impl_m_generics = tcx.generics_of(impl_m.def_id);
|
|
|
|
let trait_m_generics = tcx.generics_of(trait_m.def_id);
|
2018-05-27 02:43:03 +02:00
|
|
|
let impl_m_type_params = impl_m_generics.params.iter().filter_map(|param| match param.kind {
|
|
|
|
GenericParamDefKind::Type { synthetic, .. } => Some((param.def_id, synthetic)),
|
2019-02-20 02:16:52 +01:00
|
|
|
GenericParamDefKind::Lifetime | GenericParamDefKind::Const => None,
|
2018-04-18 18:54:05 +02:00
|
|
|
});
|
2019-12-22 23:42:04 +01:00
|
|
|
let trait_m_type_params = trait_m_generics.params.iter().filter_map(|param| match param.kind {
|
|
|
|
GenericParamDefKind::Type { synthetic, .. } => Some((param.def_id, synthetic)),
|
|
|
|
GenericParamDefKind::Lifetime | GenericParamDefKind::Const => None,
|
2018-04-18 18:54:05 +02:00
|
|
|
});
|
2019-12-22 23:42:04 +01:00
|
|
|
for ((impl_def_id, impl_synthetic), (trait_def_id, trait_synthetic)) in
|
|
|
|
impl_m_type_params.zip(trait_m_type_params)
|
2018-09-19 13:51:51 +02:00
|
|
|
{
|
2018-05-11 01:29:38 +02:00
|
|
|
if impl_synthetic != trait_synthetic {
|
2019-02-04 16:45:04 +01:00
|
|
|
let impl_hir_id = tcx.hir().as_local_hir_id(impl_def_id).unwrap();
|
2019-06-14 18:58:55 +02:00
|
|
|
let impl_span = tcx.hir().span(impl_hir_id);
|
2018-05-11 01:29:38 +02:00
|
|
|
let trait_span = tcx.def_span(trait_def_id);
|
2019-12-22 23:42:04 +01:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
impl_span,
|
|
|
|
E0643,
|
|
|
|
"method `{}` has incompatible signature for trait",
|
|
|
|
trait_m.ident
|
|
|
|
);
|
2018-05-21 18:06:28 +02:00
|
|
|
err.span_label(trait_span, "declaration in trait here");
|
|
|
|
match (impl_synthetic, trait_synthetic) {
|
|
|
|
// The case where the impl method uses `impl Trait` but the trait method uses
|
|
|
|
// explicit generics
|
|
|
|
(Some(hir::SyntheticTyParamKind::ImplTrait), None) => {
|
|
|
|
err.span_label(impl_span, "expected generic parameter, found `impl Trait`");
|
|
|
|
(|| {
|
|
|
|
// try taking the name from the trait impl
|
|
|
|
// FIXME: this is obviously suboptimal since the name can already be used
|
|
|
|
// as another generic argument
|
2019-12-22 23:42:04 +01:00
|
|
|
let new_name = tcx.sess.source_map().span_to_snippet(trait_span).ok()?;
|
2019-03-01 10:28:13 +01:00
|
|
|
let trait_m = tcx.hir().as_local_hir_id(trait_m.def_id)?;
|
|
|
|
let trait_m = tcx.hir().trait_item(hir::TraitItemId { hir_id: trait_m });
|
2018-05-21 18:06:28 +02:00
|
|
|
|
2019-03-01 10:28:13 +01:00
|
|
|
let impl_m = tcx.hir().as_local_hir_id(impl_m.def_id)?;
|
|
|
|
let impl_m = tcx.hir().impl_item(hir::ImplItemId { hir_id: impl_m });
|
2018-05-21 18:06:28 +02:00
|
|
|
|
|
|
|
// in case there are no generics, take the spot between the function name
|
|
|
|
// and the opening paren of the argument list
|
2019-12-22 23:42:04 +01:00
|
|
|
let new_generics_span =
|
|
|
|
tcx.sess.source_map().generate_fn_name_span(impl_span)?.shrink_to_hi();
|
2018-05-21 18:06:28 +02:00
|
|
|
// in case there are generics, just replace them
|
2019-12-22 23:42:04 +01:00
|
|
|
let generics_span =
|
|
|
|
impl_m.generics.span.substitute_dummy(new_generics_span);
|
2018-05-21 18:06:28 +02:00
|
|
|
// replace with the generics from the trait
|
2019-12-22 23:42:04 +01:00
|
|
|
let new_generics =
|
|
|
|
tcx.sess.source_map().span_to_snippet(trait_m.generics.span).ok()?;
|
2018-05-21 18:06:28 +02:00
|
|
|
|
2019-01-25 22:03:27 +01:00
|
|
|
err.multipart_suggestion(
|
2018-05-21 18:06:28 +02:00
|
|
|
"try changing the `impl Trait` argument to a generic parameter",
|
|
|
|
vec![
|
|
|
|
// replace `impl Trait` with `T`
|
|
|
|
(impl_span, new_name),
|
|
|
|
// replace impl method generics with trait method generics
|
|
|
|
// This isn't quite right, as users might have changed the names
|
|
|
|
// of the generics, but it works for the common case
|
|
|
|
(generics_span, new_generics),
|
|
|
|
],
|
2018-09-17 02:16:08 +02:00
|
|
|
Applicability::MaybeIncorrect,
|
2018-05-21 18:06:28 +02:00
|
|
|
);
|
|
|
|
Some(())
|
|
|
|
})();
|
2019-12-22 23:42:04 +01:00
|
|
|
}
|
2018-05-21 18:06:28 +02:00
|
|
|
// The case where the trait method uses `impl Trait`, but the impl method uses
|
|
|
|
// explicit generics.
|
|
|
|
(None, Some(hir::SyntheticTyParamKind::ImplTrait)) => {
|
|
|
|
err.span_label(impl_span, "expected `impl Trait`, found generic parameter");
|
|
|
|
(|| {
|
2019-03-01 10:28:13 +01:00
|
|
|
let impl_m = tcx.hir().as_local_hir_id(impl_m.def_id)?;
|
|
|
|
let impl_m = tcx.hir().impl_item(hir::ImplItemId { hir_id: impl_m });
|
2019-09-26 17:38:13 +02:00
|
|
|
let input_tys = match impl_m.kind {
|
2019-12-01 16:08:58 +01:00
|
|
|
hir::ImplItemKind::Method(ref sig, _) => sig.decl.inputs,
|
2018-05-21 18:06:28 +02:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
struct Visitor(Option<Span>, hir::def_id::DefId);
|
2020-01-05 02:37:57 +01:00
|
|
|
impl<'v> intravisit::Visitor<'v> for Visitor {
|
2019-12-01 16:08:58 +01:00
|
|
|
fn visit_ty(&mut self, ty: &'v hir::Ty<'v>) {
|
2020-01-05 02:37:57 +01:00
|
|
|
intravisit::walk_ty(self, ty);
|
2019-12-22 23:42:04 +01:00
|
|
|
if let hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) =
|
|
|
|
ty.kind
|
2018-09-19 13:51:51 +02:00
|
|
|
{
|
2019-04-20 18:36:05 +02:00
|
|
|
if let Res::Def(DefKind::TyParam, def_id) = path.res {
|
2018-09-19 13:51:51 +02:00
|
|
|
if def_id == self.1 {
|
|
|
|
self.0 = Some(ty.span);
|
2018-05-21 18:06:28 +02:00
|
|
|
}
|
2018-09-19 13:51:51 +02:00
|
|
|
}
|
2018-05-21 18:06:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn nested_visit_map<'this>(
|
2019-12-22 23:42:04 +01:00
|
|
|
&'this mut self,
|
2020-01-05 02:37:57 +01:00
|
|
|
) -> intravisit::NestedVisitorMap<'this, 'v>
|
2019-12-22 23:42:04 +01:00
|
|
|
{
|
2020-01-05 02:37:57 +01:00
|
|
|
intravisit::NestedVisitorMap::None
|
2018-05-21 18:06:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut visitor = Visitor(None, impl_def_id);
|
|
|
|
for ty in input_tys {
|
2020-01-05 02:37:57 +01:00
|
|
|
intravisit::Visitor::visit_ty(&mut visitor, ty);
|
2018-05-21 18:06:28 +02:00
|
|
|
}
|
|
|
|
let span = visitor.0?;
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
let bounds =
|
|
|
|
impl_m.generics.params.iter().find_map(|param| match param.kind {
|
2018-05-26 01:27:54 +02:00
|
|
|
GenericParamKind::Lifetime { .. } => None,
|
2019-12-22 23:42:04 +01:00
|
|
|
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
|
2019-02-16 00:34:04 +01:00
|
|
|
if param.hir_id == impl_hir_id {
|
2018-05-28 14:33:28 +02:00
|
|
|
Some(¶m.bounds)
|
2018-05-21 18:06:28 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2018-05-26 01:27:54 +02:00
|
|
|
}
|
2019-12-22 23:42:04 +01:00
|
|
|
})?;
|
2018-05-26 01:27:54 +02:00
|
|
|
let bounds = bounds.first()?.span().to(bounds.last()?.span());
|
2019-12-22 23:42:04 +01:00
|
|
|
let bounds = tcx.sess.source_map().span_to_snippet(bounds).ok()?;
|
2018-05-21 18:06:28 +02:00
|
|
|
|
2019-01-25 22:03:27 +01:00
|
|
|
err.multipart_suggestion(
|
2018-05-21 18:06:28 +02:00
|
|
|
"try removing the generic parameter and using `impl Trait` instead",
|
|
|
|
vec![
|
|
|
|
// delete generic parameters
|
|
|
|
(impl_m.generics.span, String::new()),
|
|
|
|
// replace param usage with `impl Trait`
|
|
|
|
(span, format!("impl {}", bounds)),
|
|
|
|
],
|
2018-09-17 02:16:08 +02:00
|
|
|
Applicability::MaybeIncorrect,
|
2018-05-21 18:06:28 +02:00
|
|
|
);
|
|
|
|
Some(())
|
|
|
|
})();
|
2019-12-22 23:42:04 +01:00
|
|
|
}
|
2018-05-21 18:06:28 +02:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2017-11-10 18:47:33 +01:00
|
|
|
err.emit();
|
|
|
|
error_found = true;
|
|
|
|
}
|
|
|
|
}
|
2019-12-22 23:42:04 +01:00
|
|
|
if error_found { Err(ErrorReported) } else { Ok(()) }
|
2017-11-10 18:47:33 +01:00
|
|
|
}
|
|
|
|
|
2019-12-08 17:58:22 +01:00
|
|
|
crate fn compare_const_impl<'tcx>(
|
2019-06-13 23:48:52 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 23:11:55 +02:00
|
|
|
impl_c: &ty::AssocItem,
|
|
|
|
impl_c_span: Span,
|
|
|
|
trait_c: &ty::AssocItem,
|
|
|
|
impl_trait_ref: ty::TraitRef<'tcx>,
|
|
|
|
) {
|
2016-09-26 05:41:27 +02:00
|
|
|
debug!("compare_const_impl(impl_trait_ref={:?})", impl_trait_ref);
|
2015-03-16 02:35:25 +01:00
|
|
|
|
2017-06-09 09:55:16 +02:00
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
2019-03-11 03:18:38 +01:00
|
|
|
let param_env = tcx.param_env(impl_c.def_id);
|
2017-05-07 18:57:51 +02:00
|
|
|
let inh = Inherited::new(infcx, impl_c.def_id);
|
2017-04-15 22:58:50 +02:00
|
|
|
let infcx = &inh.infcx;
|
2016-03-25 04:22:44 +01:00
|
|
|
|
|
|
|
// The below is for the most part highly similar to the procedure
|
|
|
|
// for methods above. It is simpler in many respects, especially
|
|
|
|
// because we shouldn't really have to deal with lifetimes or
|
|
|
|
// predicates. In fact some of this should probably be put into
|
|
|
|
// shared functions because of DRY violations...
|
2016-11-10 01:06:34 +01:00
|
|
|
let trait_to_impl_substs = impl_trait_ref.substs;
|
2016-03-25 04:22:44 +01:00
|
|
|
|
|
|
|
// Create a parameter environment that represents the implementation's
|
|
|
|
// method.
|
2019-03-04 09:00:30 +01:00
|
|
|
let impl_c_hir_id = tcx.hir().as_local_hir_id(impl_c.def_id).unwrap();
|
2016-03-25 04:22:44 +01:00
|
|
|
|
2018-09-07 15:46:53 +02:00
|
|
|
// Compute placeholder form of impl and trait const tys.
|
2017-05-11 14:05:00 +02:00
|
|
|
let impl_ty = tcx.type_of(impl_c.def_id);
|
|
|
|
let trait_ty = tcx.type_of(trait_c.def_id).subst(tcx, trait_to_impl_substs);
|
2019-02-04 20:01:14 +01:00
|
|
|
let mut cause = ObligationCause::misc(impl_c_span, impl_c_hir_id);
|
2016-03-25 04:22:44 +01:00
|
|
|
|
2017-04-15 22:58:50 +02:00
|
|
|
// There is no "body" here, so just pass dummy id.
|
2019-12-22 23:42:04 +01:00
|
|
|
let impl_ty =
|
|
|
|
inh.normalize_associated_types_in(impl_c_span, impl_c_hir_id, param_env, &impl_ty);
|
2016-09-26 05:41:27 +02:00
|
|
|
|
2017-04-15 22:58:50 +02:00
|
|
|
debug!("compare_const_impl: impl_ty={:?}", impl_ty);
|
2016-09-26 05:41:27 +02:00
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
let trait_ty =
|
|
|
|
inh.normalize_associated_types_in(impl_c_span, impl_c_hir_id, param_env, &trait_ty);
|
2016-09-26 05:41:27 +02:00
|
|
|
|
2017-04-15 22:58:50 +02:00
|
|
|
debug!("compare_const_impl: trait_ty={:?}", trait_ty);
|
2016-03-25 04:22:44 +01:00
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
let err = infcx
|
|
|
|
.at(&cause, param_env)
|
|
|
|
.sup(trait_ty, impl_ty)
|
|
|
|
.map(|ok| inh.register_infer_ok_obligations(ok));
|
2015-03-16 02:35:25 +01:00
|
|
|
|
2016-03-25 04:22:44 +01:00
|
|
|
if let Err(terr) = err {
|
2019-12-22 23:42:04 +01:00
|
|
|
debug!(
|
|
|
|
"checking associated const for compatibility: impl ty {:?}, trait ty {:?}",
|
|
|
|
impl_ty, trait_ty
|
|
|
|
);
|
2016-08-05 20:05:57 +02:00
|
|
|
|
|
|
|
// Locate the Span containing just the type of the offending impl
|
2019-09-26 17:38:13 +02:00
|
|
|
match tcx.hir().expect_impl_item(impl_c_hir_id).kind {
|
2016-11-07 19:25:06 +01:00
|
|
|
ImplItemKind::Const(ref ty, _) => cause.span = ty.span,
|
2016-09-26 05:41:27 +02:00
|
|
|
_ => bug!("{:?} is not a impl const", impl_c),
|
2016-08-05 20:05:57 +02:00
|
|
|
}
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
let mut diag = struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
cause.span,
|
|
|
|
E0326,
|
|
|
|
"implemented const `{}` has an incompatible type for \
|
2016-09-26 05:41:27 +02:00
|
|
|
trait",
|
2019-12-22 23:42:04 +01:00
|
|
|
trait_c.ident
|
|
|
|
);
|
2016-08-05 20:05:57 +02:00
|
|
|
|
2019-03-04 09:00:30 +01:00
|
|
|
let trait_c_hir_id = tcx.hir().as_local_hir_id(trait_c.def_id);
|
|
|
|
let trait_c_span = trait_c_hir_id.map(|trait_c_hir_id| {
|
2017-04-27 19:06:48 +02:00
|
|
|
// Add a label to the Span containing just the type of the const
|
2019-09-26 18:07:54 +02:00
|
|
|
match tcx.hir().expect_trait_item(trait_c_hir_id).kind {
|
2017-04-27 19:06:48 +02:00
|
|
|
TraitItemKind::Const(ref ty, _) => ty.span,
|
|
|
|
_ => bug!("{:?} is not a trait const", trait_c),
|
|
|
|
}
|
|
|
|
});
|
2016-08-05 20:05:57 +02:00
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
infcx.note_type_err(
|
|
|
|
&mut diag,
|
|
|
|
&cause,
|
|
|
|
trait_c_span.map(|span| (span, "type in trait".to_owned())),
|
|
|
|
Some(infer::ValuePairs::Types(ExpectedFound {
|
|
|
|
expected: trait_ty,
|
|
|
|
found: impl_ty,
|
|
|
|
})),
|
|
|
|
&terr,
|
|
|
|
);
|
2016-07-21 19:12:30 +02:00
|
|
|
diag.emit();
|
2015-03-16 02:35:25 +01:00
|
|
|
}
|
2017-04-15 22:58:50 +02:00
|
|
|
|
2017-05-31 07:21:00 +02:00
|
|
|
// Check that all obligations are satisfied by the implementation's
|
|
|
|
// version.
|
|
|
|
if let Err(ref errors) = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx) {
|
2018-03-12 05:15:06 +01:00
|
|
|
infcx.report_fulfillment_errors(errors, None, false);
|
2017-05-31 07:21:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-04 20:01:14 +01:00
|
|
|
let fcx = FnCtxt::new(&inh, param_env, impl_c_hir_id);
|
|
|
|
fcx.regionck_item(impl_c_hir_id, impl_c_span, &[]);
|
2016-03-25 04:22:44 +01:00
|
|
|
});
|
2015-03-16 02:35:25 +01:00
|
|
|
}
|
2019-12-08 17:58:22 +01:00
|
|
|
|
|
|
|
crate fn compare_ty_impl<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
impl_ty: &ty::AssocItem,
|
|
|
|
impl_ty_span: Span,
|
|
|
|
trait_ty: &ty::AssocItem,
|
|
|
|
impl_trait_ref: ty::TraitRef<'tcx>,
|
|
|
|
trait_item_span: Option<Span>,
|
|
|
|
) {
|
|
|
|
debug!("compare_impl_type(impl_trait_ref={:?})", impl_trait_ref);
|
|
|
|
|
|
|
|
let _: Result<(), ErrorReported> = (|| {
|
|
|
|
compare_number_of_generics(tcx, impl_ty, impl_ty_span, trait_ty, trait_item_span)?;
|
|
|
|
|
|
|
|
compare_type_predicate_entailment(tcx, impl_ty, impl_ty_span, trait_ty, impl_trait_ref)
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The equivalent of [compare_predicate_entailment], but for associated types
|
|
|
|
/// instead of associated functions.
|
|
|
|
fn compare_type_predicate_entailment(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
impl_ty: &ty::AssocItem,
|
|
|
|
impl_ty_span: Span,
|
|
|
|
trait_ty: &ty::AssocItem,
|
|
|
|
impl_trait_ref: ty::TraitRef<'tcx>,
|
|
|
|
) -> Result<(), ErrorReported> {
|
|
|
|
let impl_substs = InternalSubsts::identity_for_item(tcx, impl_ty.def_id);
|
2019-12-22 23:42:04 +01:00
|
|
|
let trait_to_impl_substs =
|
|
|
|
impl_substs.rebase_onto(tcx, impl_ty.container.id(), impl_trait_ref.substs);
|
2019-12-08 17:58:22 +01:00
|
|
|
|
|
|
|
let impl_ty_generics = tcx.generics_of(impl_ty.def_id);
|
|
|
|
let trait_ty_generics = tcx.generics_of(trait_ty.def_id);
|
|
|
|
let impl_ty_predicates = tcx.predicates_of(impl_ty.def_id);
|
|
|
|
let trait_ty_predicates = tcx.predicates_of(trait_ty.def_id);
|
|
|
|
|
|
|
|
check_region_bounds_on_impl_item(
|
|
|
|
tcx,
|
|
|
|
impl_ty_span,
|
|
|
|
impl_ty,
|
|
|
|
trait_ty,
|
|
|
|
&trait_ty_generics,
|
|
|
|
&impl_ty_generics,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let impl_ty_own_bounds = impl_ty_predicates.instantiate_own(tcx, impl_substs);
|
|
|
|
|
|
|
|
if impl_ty_own_bounds.is_empty() {
|
|
|
|
// Nothing to check.
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This `HirId` should be used for the `body_id` field on each
|
|
|
|
// `ObligationCause` (and the `FnCtxt`). This is what
|
|
|
|
// `regionck_item` expects.
|
|
|
|
let impl_ty_hir_id = tcx.hir().as_local_hir_id(impl_ty.def_id).unwrap();
|
|
|
|
let cause = ObligationCause {
|
|
|
|
span: impl_ty_span,
|
|
|
|
body_id: impl_ty_hir_id,
|
|
|
|
code: ObligationCauseCode::CompareImplTypeObligation {
|
|
|
|
item_name: impl_ty.ident.name,
|
|
|
|
impl_item_def_id: impl_ty.def_id,
|
|
|
|
trait_item_def_id: trait_ty.def_id,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!("compare_type_predicate_entailment: trait_to_impl_substs={:?}", trait_to_impl_substs);
|
|
|
|
|
|
|
|
// The predicates declared by the impl definition, the trait and the
|
|
|
|
// associated type in the trait are assumed.
|
|
|
|
let impl_predicates = tcx.predicates_of(impl_ty_predicates.parent.unwrap());
|
|
|
|
let mut hybrid_preds = impl_predicates.instantiate_identity(tcx);
|
2019-12-22 23:42:04 +01:00
|
|
|
hybrid_preds
|
|
|
|
.predicates
|
|
|
|
.extend(trait_ty_predicates.instantiate_own(tcx, trait_to_impl_substs).predicates);
|
2019-12-08 17:58:22 +01:00
|
|
|
|
|
|
|
debug!("compare_type_predicate_entailment: bounds={:?}", hybrid_preds);
|
|
|
|
|
|
|
|
let normalize_cause = traits::ObligationCause::misc(impl_ty_span, impl_ty_hir_id);
|
|
|
|
let param_env = ty::ParamEnv::new(
|
|
|
|
tcx.intern_predicates(&hybrid_preds.predicates),
|
|
|
|
Reveal::UserFacing,
|
2019-12-22 23:42:04 +01:00
|
|
|
None,
|
|
|
|
);
|
|
|
|
let param_env = traits::normalize_param_env_or_error(
|
|
|
|
tcx,
|
|
|
|
impl_ty.def_id,
|
|
|
|
param_env,
|
|
|
|
normalize_cause.clone(),
|
2019-12-08 17:58:22 +01:00
|
|
|
);
|
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
|
|
|
let inh = Inherited::new(infcx, impl_ty.def_id);
|
|
|
|
let infcx = &inh.infcx;
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
debug!("compare_type_predicate_entailment: caller_bounds={:?}", param_env.caller_bounds);
|
2019-12-08 17:58:22 +01:00
|
|
|
|
|
|
|
let mut selcx = traits::SelectionContext::new(&infcx);
|
|
|
|
|
|
|
|
for predicate in impl_ty_own_bounds.predicates {
|
|
|
|
let traits::Normalized { value: predicate, obligations } =
|
|
|
|
traits::normalize(&mut selcx, param_env, normalize_cause.clone(), &predicate);
|
|
|
|
|
|
|
|
inh.register_predicates(obligations);
|
|
|
|
inh.register_predicate(traits::Obligation::new(cause.clone(), param_env, predicate));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that all obligations are satisfied by the implementation's
|
|
|
|
// version.
|
|
|
|
if let Err(ref errors) = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx) {
|
|
|
|
infcx.report_fulfillment_errors(errors, None, false);
|
|
|
|
return Err(ErrorReported);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, resolve all regions. This catches wily misuses of
|
|
|
|
// lifetime parameters.
|
|
|
|
let fcx = FnCtxt::new(&inh, param_env, impl_ty_hir_id);
|
|
|
|
fcx.regionck_item(impl_ty_hir_id, impl_ty_span, &[]);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assoc_item_kind_str(impl_item: &ty::AssocItem) -> &'static str {
|
|
|
|
match impl_item.kind {
|
|
|
|
ty::AssocKind::Const => "const",
|
|
|
|
ty::AssocKind::Method => "method",
|
|
|
|
ty::AssocKind::Type | ty::AssocKind::OpaqueTy => "type",
|
|
|
|
}
|
|
|
|
}
|