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.
|
|
|
|
|
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;
|
2024-07-19 14:21:30 +00:00
|
|
|
use rustc_errors::codes::*;
|
2022-04-29 11:57:01 -04:00
|
|
|
use rustc_hir::def::DefKind;
|
2024-02-15 17:12:05 +00:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
|
2024-07-19 14:21:30 +00:00
|
|
|
use rustc_span::ErrorGuaranteed;
|
2020-02-08 20:14:02 +00:00
|
|
|
|
2024-07-19 14:21:30 +00:00
|
|
|
use crate::constrained_generic_params as cgp;
|
|
|
|
use crate::errors::UnconstrainedGenericParameter;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
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
|
|
|
/// ```
|
2024-02-15 17:12:05 +00:00
|
|
|
pub fn check_impl_wf(tcx: TyCtxt<'_>, impl_def_id: LocalDefId) -> Result<(), ErrorGuaranteed> {
|
2020-02-08 20:14:02 +00:00
|
|
|
let min_specialization = tcx.features().min_specialization;
|
2024-01-11 22:13:39 +00:00
|
|
|
let mut res = Ok(());
|
2024-02-15 17:12:05 +00:00
|
|
|
debug_assert!(matches!(tcx.def_kind(impl_def_id), DefKind::Impl { .. }));
|
|
|
|
res = res.and(enforce_impl_params_are_constrained(tcx, impl_def_id));
|
|
|
|
if min_specialization {
|
|
|
|
res = res.and(check_min_specialization(tcx, impl_def_id));
|
2022-04-29 11:57:01 -04:00
|
|
|
}
|
2019-01-26 12:18:32 +01:00
|
|
|
|
2024-02-15 17:12:05 +00:00
|
|
|
res
|
2016-11-11 09:52:46 -05:00
|
|
|
}
|
|
|
|
|
2024-01-11 22:13:39 +00:00
|
|
|
fn enforce_impl_params_are_constrained(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
impl_def_id: LocalDefId,
|
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
2016-11-08 19:23:09 -05:00
|
|
|
// Every lifetime used in an associated type must be constrained.
|
2023-07-11 22:35:29 +01:00
|
|
|
let impl_self_ty = tcx.type_of(impl_def_id).instantiate_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)
|
2023-12-18 22:21:37 +11:00
|
|
|
tcx.dcx().span_delayed_bug(
|
2019-09-03 08:33:06 -07:00
|
|
|
tcx.def_span(impl_def_id),
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
format!(
|
2023-07-25 23:17:39 +02:00
|
|
|
"potentially unconstrained type parameters weren't evaluated: {impl_self_ty:?}",
|
2020-01-05 23:00:47 +00:00
|
|
|
),
|
2019-09-03 08:33:06 -07:00
|
|
|
);
|
2024-01-11 22:13:39 +00:00
|
|
|
// This is super fishy, but our current `rustc_hir_analysis::check_crate` pipeline depends on
|
|
|
|
// `type_of` having been called much earlier, and thus this value being read from cache.
|
|
|
|
// Compilation must continue in order for other important diagnostics to keep showing up.
|
|
|
|
return Ok(());
|
2019-09-02 18:03:54 -07:00
|
|
|
}
|
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-07-11 22:35:29 +01:00
|
|
|
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).map(ty::EarlyBinder::instantiate_identity);
|
2016-11-08 19:23:09 -05:00
|
|
|
|
2024-05-27 09:45:40 +00:00
|
|
|
impl_trait_ref.error_reported()?;
|
|
|
|
|
2024-02-08 14:08:58 +01:00
|
|
|
let mut input_parameters = cgp::parameters_for_impl(tcx, 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() {
|
2024-02-20 14:29:50 +01:00
|
|
|
cgp::parameters_for(tcx, tcx.type_of(def_id).instantiate_identity(), true)
|
2019-12-26 23:41:32 +00:00
|
|
|
} else {
|
2023-06-14 05:20:31 +00:00
|
|
|
vec![]
|
2019-12-26 23:41:32 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-24 00:00:08 -03:00
|
|
|
ty::AssocKind::Fn | ty::AssocKind::Const => vec![],
|
2019-12-26 23:41:32 +00:00
|
|
|
}
|
2016-11-08 19:23:09 -05:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2024-01-11 22:13:39 +00:00
|
|
|
let mut res = Ok(());
|
2024-05-09 20:56:44 -04:00
|
|
|
for param in &impl_generics.own_params {
|
2024-07-19 14:21:30 +00:00
|
|
|
let err = 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);
|
2024-07-19 14:21:30 +00:00
|
|
|
!input_parameters.contains(&cgp::Parameter::from(param_ty))
|
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());
|
2024-07-19 14:21:30 +00:00
|
|
|
lifetimes_in_associated_types.contains(¶m_lt) && // (*)
|
2018-05-11 01:56:05 +01:00
|
|
|
!input_parameters.contains(¶m_lt)
|
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);
|
2024-07-19 14:21:30 +00:00
|
|
|
!input_parameters.contains(&cgp::Parameter::from(param_ct))
|
2019-02-20 01:19:42 +00:00
|
|
|
}
|
2024-07-19 14:21:30 +00:00
|
|
|
};
|
|
|
|
if err {
|
|
|
|
let const_param_note =
|
|
|
|
matches!(param.kind, ty::GenericParamDefKind::Const { .. }).then_some(());
|
|
|
|
let mut diag = tcx.dcx().create_err(UnconstrainedGenericParameter {
|
|
|
|
span: tcx.def_span(param.def_id),
|
|
|
|
param_name: param.name,
|
|
|
|
param_def_kind: tcx.def_descr(param.def_id),
|
|
|
|
const_param_note,
|
|
|
|
const_param_note2: const_param_note,
|
|
|
|
});
|
|
|
|
diag.code(E0207);
|
|
|
|
res = Err(diag.emit());
|
2016-11-08 19:23:09 -05:00
|
|
|
}
|
|
|
|
}
|
2024-01-11 22:13:39 +00:00
|
|
|
res
|
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.
|
|
|
|
}
|