2016-11-11 09:52:46 -05:00
|
|
|
//! This pass enforces various "well-formedness constraints" on impls.
|
|
|
|
//! Logically, it is part of wfcheck -- but we do it early so that we
|
|
|
|
//! can stop compilation afterwards, since part of the trait matching
|
|
|
|
//! infrastructure gets very grumpy if these conditions don't hold. In
|
|
|
|
//! particular, if there are type parameters that are not part of the
|
|
|
|
//! impl, then coherence will report strange inference ambiguity
|
|
|
|
//! errors; if impls have duplicate items, we get misleading
|
|
|
|
//! specialization errors. These things can (and probably should) be
|
|
|
|
//! fixed, but for the moment it's easier to do these checks early.
|
|
|
|
|
2019-04-22 23:07:09 +01:00
|
|
|
use crate::constrained_generic_params as cgp;
|
2020-02-08 20:14:02 +00:00
|
|
|
use min_specialization::check_min_specialization;
|
|
|
|
|
2022-08-13 12:32:01 +02:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-01-09 11:18:47 +01:00
|
|
|
use rustc_errors::struct_span_err;
|
2022-04-29 11:57:01 -04:00
|
|
|
use rustc_hir::def::DefKind;
|
2020-06-27 13:09:54 +02:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::query::Providers;
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
|
2022-07-17 04:09:20 +09:00
|
|
|
use rustc_span::{Span, Symbol};
|
2020-02-08 20:14:02 +00:00
|
|
|
|
|
|
|
mod min_specialization;
|
2016-11-08 19:23:09 -05:00
|
|
|
|
|
|
|
/// Checks that all the type/lifetime parameters on an impl also
|
2019-02-08 14:53:55 +01:00
|
|
|
/// appear in the trait ref or self type (or are constrained by a
|
2016-11-08 19:23:09 -05:00
|
|
|
/// where-clause). These rules are needed to ensure that, given a
|
|
|
|
/// trait ref like `<T as Trait<U>>`, we can derive the values of all
|
|
|
|
/// parameters on the impl (which is needed to make specialization
|
|
|
|
/// possible).
|
|
|
|
///
|
|
|
|
/// However, in the case of lifetimes, we only enforce these rules if
|
2019-02-08 14:53:55 +01:00
|
|
|
/// the lifetime parameter is used in an associated type. This is a
|
2016-11-08 19:23:09 -05:00
|
|
|
/// concession to backwards compatibility; see comment at the end of
|
|
|
|
/// the fn for details.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
2018-04-04 16:09:58 +01:00
|
|
|
/// ```rust,ignore (pseudo-Rust)
|
2016-11-08 19:23:09 -05:00
|
|
|
/// impl<T> Trait<Foo> for Bar { ... }
|
2018-04-04 16:09:58 +01:00
|
|
|
/// // ^ T does not appear in `Foo` or `Bar`, error!
|
2016-11-08 19:23:09 -05:00
|
|
|
///
|
|
|
|
/// impl<T> Trait<Foo<T>> for Bar { ... }
|
2018-04-04 16:09:58 +01:00
|
|
|
/// // ^ T appears in `Foo<T>`, ok.
|
2016-11-08 19:23:09 -05:00
|
|
|
///
|
2019-02-08 14:53:55 +01:00
|
|
|
/// impl<T> Trait<Foo> for Bar where Bar: Iterator<Item = T> { ... }
|
2018-04-04 16:09:58 +01:00
|
|
|
/// // ^ T is bound to `<Bar as Iterator>::Item`, ok.
|
2016-11-08 19:23:09 -05:00
|
|
|
///
|
|
|
|
/// impl<'a> Trait<Foo> for Bar { }
|
2018-04-04 16:09:58 +01:00
|
|
|
/// // ^ 'a is unused, but for back-compat we allow it
|
2016-11-08 19:23:09 -05:00
|
|
|
///
|
|
|
|
/// impl<'a> Trait<Foo> for Bar { type X = &'a i32; }
|
2018-04-04 16:09:58 +01:00
|
|
|
/// // ^ 'a is unused and appears in assoc type, error
|
2016-11-08 19:23:09 -05:00
|
|
|
/// ```
|
2020-06-27 13:09:54 +02:00
|
|
|
fn check_mod_impl_wf(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
2020-02-08 20:14:02 +00:00
|
|
|
let min_specialization = tcx.features().min_specialization;
|
2022-04-29 11:57:01 -04:00
|
|
|
let module = tcx.hir_module_items(module_def_id);
|
|
|
|
for id in module.items() {
|
2023-02-12 18:26:47 +00:00
|
|
|
if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. }) {
|
2022-10-27 14:02:18 +11:00
|
|
|
enforce_impl_params_are_constrained(tcx, id.owner_id.def_id);
|
2022-06-22 19:12:06 +02:00
|
|
|
if min_specialization {
|
2022-10-27 14:02:18 +11:00
|
|
|
check_min_specialization(tcx, id.owner_id.def_id);
|
2022-04-29 11:57:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-26 12:18:32 +01:00
|
|
|
}
|
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2019-01-26 12:18:32 +01:00
|
|
|
*providers = Providers { check_mod_impl_wf, ..*providers };
|
2016-11-11 09:52:46 -05:00
|
|
|
}
|
|
|
|
|
2022-06-22 19:12:06 +02:00
|
|
|
fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId) {
|
2016-11-08 19:23:09 -05:00
|
|
|
// Every lifetime used in an associated type must be constrained.
|
2023-02-07 01:29:48 -07:00
|
|
|
let impl_self_ty = tcx.type_of(impl_def_id).subst_identity();
|
2019-09-03 08:12:28 -07:00
|
|
|
if impl_self_ty.references_error() {
|
|
|
|
// Don't complain about unconstrained type params when self ty isn't known due to errors.
|
|
|
|
// (#36836)
|
2019-09-03 08:33:06 -07:00
|
|
|
tcx.sess.delay_span_bug(
|
|
|
|
tcx.def_span(impl_def_id),
|
2020-01-05 23:00:47 +00:00
|
|
|
&format!(
|
|
|
|
"potentially unconstrained type parameters weren't evaluated: {:?}",
|
|
|
|
impl_self_ty,
|
|
|
|
),
|
2019-09-03 08:33:06 -07:00
|
|
|
);
|
2019-09-02 18:03:54 -07:00
|
|
|
return;
|
|
|
|
}
|
2017-04-24 15:20:46 +03:00
|
|
|
let impl_generics = tcx.generics_of(impl_def_id);
|
|
|
|
let impl_predicates = tcx.predicates_of(impl_def_id);
|
2023-01-10 14:57:22 -07:00
|
|
|
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).map(ty::EarlyBinder::subst_identity);
|
2016-11-08 19:23:09 -05:00
|
|
|
|
2022-01-12 03:19:52 +00:00
|
|
|
let mut input_parameters = cgp::parameters_for_impl(impl_self_ty, impl_trait_ref);
|
2019-04-22 23:07:09 +01:00
|
|
|
cgp::identify_constrained_generic_params(
|
2019-10-18 03:14:57 +03:00
|
|
|
tcx,
|
|
|
|
impl_predicates,
|
|
|
|
impl_trait_ref,
|
|
|
|
&mut input_parameters,
|
|
|
|
);
|
2016-11-08 19:23:09 -05:00
|
|
|
|
|
|
|
// Disallow unconstrained lifetimes, but only if they appear in assoc types.
|
2022-06-22 19:12:06 +02:00
|
|
|
let lifetimes_in_associated_types: FxHashSet<_> = tcx
|
|
|
|
.associated_item_def_ids(impl_def_id)
|
2016-11-10 09:47:00 -05:00
|
|
|
.iter()
|
2019-12-26 23:41:32 +00:00
|
|
|
.flat_map(|def_id| {
|
2016-10-04 02:19:40 +03:00
|
|
|
let item = tcx.associated_item(def_id);
|
2019-12-26 23:41:32 +00:00
|
|
|
match item.kind {
|
|
|
|
ty::AssocKind::Type => {
|
2022-03-12 19:36:11 +01:00
|
|
|
if item.defaultness(tcx).has_value() {
|
2023-02-07 01:29:48 -07:00
|
|
|
cgp::parameters_for(&tcx.type_of(def_id).subst_identity(), true)
|
2019-12-26 23:41:32 +00:00
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 10:09:50 +08:00
|
|
|
ty::AssocKind::Fn | ty::AssocKind::Const => Vec::new(),
|
2019-12-26 23:41:32 +00:00
|
|
|
}
|
2016-11-08 19:23:09 -05:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2018-05-11 01:56:05 +01:00
|
|
|
for param in &impl_generics.params {
|
|
|
|
match param.kind {
|
2018-04-14 00:07:25 +01:00
|
|
|
// Disallow ANY unconstrained type parameters.
|
2019-02-20 01:19:42 +00:00
|
|
|
ty::GenericParamDefKind::Type { .. } => {
|
2018-05-11 01:56:05 +01:00
|
|
|
let param_ty = ty::ParamTy::for_def(param);
|
2019-04-22 23:07:09 +01:00
|
|
|
if !input_parameters.contains(&cgp::Parameter::from(param_ty)) {
|
2022-07-17 04:09:20 +09:00
|
|
|
report_unused_parameter(tcx, tcx.def_span(param.def_id), "type", param_ty.name);
|
2018-04-14 00:07:25 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-11 01:56:05 +01:00
|
|
|
ty::GenericParamDefKind::Lifetime => {
|
2019-04-22 23:07:09 +01:00
|
|
|
let param_lt = cgp::Parameter::from(param.to_early_bound_region_data());
|
2018-05-11 01:56:05 +01:00
|
|
|
if lifetimes_in_associated_types.contains(¶m_lt) && // (*)
|
|
|
|
!input_parameters.contains(¶m_lt)
|
|
|
|
{
|
|
|
|
report_unused_parameter(
|
|
|
|
tcx,
|
|
|
|
tcx.def_span(param.def_id),
|
|
|
|
"lifetime",
|
2022-07-17 04:09:20 +09:00
|
|
|
param.name,
|
2018-05-11 01:56:05 +01:00
|
|
|
);
|
2018-04-14 00:07:25 +01:00
|
|
|
}
|
|
|
|
}
|
2020-08-11 00:02:45 +00:00
|
|
|
ty::GenericParamDefKind::Const { .. } => {
|
2019-02-20 01:19:42 +00:00
|
|
|
let param_ct = ty::ParamConst::for_def(param);
|
2019-04-22 23:07:09 +01:00
|
|
|
if !input_parameters.contains(&cgp::Parameter::from(param_ct)) {
|
2019-02-20 01:19:42 +00:00
|
|
|
report_unused_parameter(
|
|
|
|
tcx,
|
|
|
|
tcx.def_span(param.def_id),
|
|
|
|
"const",
|
2022-07-17 04:09:20 +09:00
|
|
|
param_ct.name,
|
2019-02-20 01:19:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-11-08 19:23:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// (*) This is a horrible concession to reality. I think it'd be
|
2020-09-06 04:34:20 +00:00
|
|
|
// better to just ban unconstrained lifetimes outright, but in
|
2022-03-30 15:14:15 -04:00
|
|
|
// practice people do non-hygienic macros like:
|
2016-11-08 19:23:09 -05:00
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// macro_rules! __impl_slice_eq1 {
|
|
|
|
// ($Lhs: ty, $Rhs: ty, $Bound: ident) => {
|
|
|
|
// impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs where A: PartialEq<B> {
|
|
|
|
// ....
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
//
|
2018-08-19 15:30:23 +02:00
|
|
|
// In a concession to backwards compatibility, we continue to
|
2016-11-08 19:23:09 -05:00
|
|
|
// permit those, so long as the lifetimes aren't used in
|
|
|
|
// associated types. I believe this is sound, because lifetimes
|
|
|
|
// used elsewhere are not projected back out.
|
|
|
|
}
|
|
|
|
|
2022-07-17 04:09:20 +09:00
|
|
|
fn report_unused_parameter(tcx: TyCtxt<'_>, span: Span, kind: &str, name: Symbol) {
|
2020-09-06 04:34:20 +00:00
|
|
|
let mut err = struct_span_err!(
|
2016-10-04 02:19:40 +03:00
|
|
|
tcx.sess,
|
|
|
|
span,
|
|
|
|
E0207,
|
2016-11-08 19:23:09 -05:00
|
|
|
"the {} parameter `{}` is not constrained by the \
|
|
|
|
impl trait, self type, or predicates",
|
|
|
|
kind,
|
|
|
|
name
|
2020-09-06 04:34:20 +00:00
|
|
|
);
|
|
|
|
err.span_label(span, format!("unconstrained {} parameter", kind));
|
|
|
|
if kind == "const" {
|
|
|
|
err.note(
|
|
|
|
"expressions using a const parameter must map each value to a distinct output value",
|
|
|
|
);
|
|
|
|
err.note(
|
|
|
|
"proving the result of expressions other than the parameter are unique is not supported",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
err.emit();
|
2016-11-08 19:23:09 -05:00
|
|
|
}
|