1//! # Minimal Specialization
2//!
3//! This module contains the checks for sound specialization used when the
4//! `min_specialization` feature is enabled. This requires that the impl is
5//! *always applicable*.
6//!
7//! If `impl1` specializes `impl2` then `impl1` is always applicable if we know
8//! that all the bounds of `impl2` are satisfied, and all of the bounds of
9//! `impl1` are satisfied for some choice of lifetimes then we know that
10//! `impl1` applies for any choice of lifetimes.
11//!
12//! ## Basic approach
13//!
14//! To enforce this requirement on specializations we take the following
15//! approach:
16//!
17//! 1. Match up the args for `impl2` so that the implemented trait and
18//! self-type match those for `impl1`.
19//! 2. Check for any direct use of `'static` in the args of `impl2`.
20//! 3. Check that all of the generic parameters of `impl1` occur at most once
21//! in the *unconstrained* args for `impl2`. A parameter is constrained if
22//! its value is completely determined by an associated type projection
23//! predicate.
24//! 4. Check that all predicates on `impl1` either exist on `impl2` (after
25//! matching args), or are well-formed predicates for the trait's type
26//! arguments.
27//!
28//! ## Example
29//!
30//! Suppose we have the following always applicable impl:
31//!
32//! ```ignore (illustrative)
33//! impl<T> SpecExtend<T> for std::vec::IntoIter<T> { /* specialized impl */ }
34//! impl<T, I: Iterator<Item=T>> SpecExtend<T> for I { /* default impl */ }
35//! ```
36//!
37//! We get that the generic parameters for `impl2` are `[T, std::vec::IntoIter<T>]`.
38//! `T` is constrained to be `<I as Iterator>::Item`, so we check only
39//! `std::vec::IntoIter<T>` for repeated parameters, which it doesn't have. The
40//! predicates of `impl1` are only `T: Sized`, which is also a predicate of
41//! `impl2`. So this specialization is sound.
42//!
43//! ## Extensions
44//!
45//! Unfortunately not all specializations in the standard library are allowed
46//! by this. So there are two extensions to these rules that allow specializing
47//! on some traits: that is, using them as bounds on the specializing impl,
48//! even when they don't occur in the base impl.
49//!
50//! ### rustc_specialization_trait
51//!
52//! If a trait is always applicable, then it's sound to specialize on it. We
53//! check trait is always applicable in the same way as impls, except that step
54//! 4 is now "all predicates on `impl1` are always applicable". We require that
55//! `specialization` or `min_specialization` is enabled to implement these
56//! traits.
57//!
58//! ### rustc_unsafe_specialization_marker
59//!
60//! There are also some specialization on traits with no methods, including the
61//! stable `FusedIterator` trait. We allow marking marker traits with an
62//! unstable attribute that means we ignore them in point 3 of the checks
63//! above. This is unsound, in the sense that the specialized impl may be used
64//! when it doesn't apply, but we allow it in the short term since it can't
65//! cause use after frees with purely safe code in the same way as specializing
66//! on traits with methods can.
6768use rustc_data_structures::fx::FxHashSet;
69use rustc_hir::def_id::{DefId, LocalDefId};
70use rustc_infer::infer::TyCtxtInferExt;
71use rustc_infer::traits::ObligationCause;
72use rustc_infer::traits::specialization_graph::Node;
73use rustc_middle::ty::trait_def::TraitSpecializationKind;
74use rustc_middle::ty::{
75self, GenericArg, GenericArgs, GenericArgsRef, TyCtxt, TypeVisitableExt, TypingMode,
76};
77use rustc_span::{ErrorGuaranteed, Span};
78use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
79use rustc_trait_selection::traits::{self, ObligationCtxt, translate_args_with_cause, wf};
80use tracing::{debug, instrument};
8182use crate::errors::GenericArgsOnOverriddenImpl;
83use crate::{constrained_generic_paramsas cgp, errors};
8485pub(super) fn check_min_specialization(
86 tcx: TyCtxt<'_>,
87 impl_def_id: LocalDefId,
88) -> Result<(), ErrorGuaranteed> {
89if let Some(node) = parent_specialization_node(tcx, impl_def_id) {
90check_always_applicable(tcx, impl_def_id, node)?;
91 }
92Ok(())
93}
9495fn parent_specialization_node(tcx: TyCtxt<'_>, impl1_def_id: LocalDefId) -> Option<Node> {
96let trait_ref = tcx.impl_trait_ref(impl1_def_id);
97let trait_def = tcx.trait_def(trait_ref.skip_binder().def_id);
9899let impl2_node = trait_def.ancestors(tcx, impl1_def_id.to_def_id()).ok()?.nth(1)?;
100101let always_applicable_trait =
102#[allow(non_exhaustive_omitted_patterns)] match trait_def.specialization_kind
{
TraitSpecializationKind::AlwaysApplicable => true,
_ => false,
}matches!(trait_def.specialization_kind, TraitSpecializationKind::AlwaysApplicable);
103if impl2_node.is_from_trait() && !always_applicable_trait {
104// Implementing a normal trait isn't a specialization.
105return None;
106 }
107if trait_def.is_marker {
108// Overlapping marker implementations are not really specializations.
109return None;
110 }
111Some(impl2_node)
112}
113114/// Check that `impl1` is a sound specialization
115#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("check_always_applicable",
"rustc_hir_analysis::impl_wf_check::min_specialization",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs"),
::tracing_core::__macro_support::Option::Some(115u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::impl_wf_check::min_specialization"),
::tracing_core::field::FieldSet::new(&["impl1_def_id",
"impl2_node"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&impl1_def_id)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&impl2_node)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Result<(), ErrorGuaranteed> =
loop {};
return __tracing_attr_fake_return;
}
{
let span = tcx.def_span(impl1_def_id);
let (impl1_args, impl2_args) =
get_impl_args(tcx, impl1_def_id, impl2_node)?;
let impl2_def_id = impl2_node.def_id();
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs:125",
"rustc_hir_analysis::impl_wf_check::min_specialization",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs"),
::tracing_core::__macro_support::Option::Some(125u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::impl_wf_check::min_specialization"),
::tracing_core::field::FieldSet::new(&["impl2_def_id",
"impl2_args"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&impl2_def_id)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&impl2_args)
as &dyn Value))])
});
} else { ; }
};
let parent_args =
if impl2_node.is_from_trait() {
impl2_args.to_vec()
} else {
unconstrained_parent_impl_args(tcx, impl2_def_id,
impl2_args)
};
check_has_items(tcx, impl1_def_id, impl2_node,
span).and(check_static_lifetimes(tcx, &parent_args,
span)).and(check_duplicate_params(tcx, impl1_args,
parent_args,
span)).and(check_predicates(tcx, impl1_def_id, impl1_args,
impl2_node, impl2_args, span))
}
}
}#[instrument(level = "debug", skip(tcx))]116fn check_always_applicable(
117 tcx: TyCtxt<'_>,
118 impl1_def_id: LocalDefId,
119 impl2_node: Node,
120) -> Result<(), ErrorGuaranteed> {
121let span = tcx.def_span(impl1_def_id);
122123let (impl1_args, impl2_args) = get_impl_args(tcx, impl1_def_id, impl2_node)?;
124let impl2_def_id = impl2_node.def_id();
125debug!(?impl2_def_id, ?impl2_args);
126127let parent_args = if impl2_node.is_from_trait() {
128 impl2_args.to_vec()
129 } else {
130 unconstrained_parent_impl_args(tcx, impl2_def_id, impl2_args)
131 };
132133 check_has_items(tcx, impl1_def_id, impl2_node, span)
134 .and(check_static_lifetimes(tcx, &parent_args, span))
135 .and(check_duplicate_params(tcx, impl1_args, parent_args, span))
136 .and(check_predicates(tcx, impl1_def_id, impl1_args, impl2_node, impl2_args, span))
137}
138139fn check_has_items(
140 tcx: TyCtxt<'_>,
141 impl1_def_id: LocalDefId,
142 impl2_node: Node,
143 span: Span,
144) -> Result<(), ErrorGuaranteed> {
145if let Node::Impl(impl2_id) = impl2_node146 && tcx.associated_item_def_ids(impl1_def_id).is_empty()
147 {
148let base_impl_span = tcx.def_span(impl2_id);
149return Err(tcx.dcx().emit_err(errors::EmptySpecialization { span, base_impl_span }));
150 }
151Ok(())
152}
153154/// Given a specializing impl `impl1`, and the base impl `impl2`, returns two
155/// generic parameters `(S1, S2)` that equate their trait references.
156/// The returned types are expressed in terms of the generics of `impl1`.
157///
158/// Example
159///
160/// ```ignore (illustrative)
161/// impl<A, B> Foo<A> for B { /* impl2 */ }
162/// impl<C> Foo<Vec<C>> for C { /* impl1 */ }
163/// ```
164///
165/// Would return `S1 = [C]` and `S2 = [Vec<C>, C]`.
166fn get_impl_args(
167 tcx: TyCtxt<'_>,
168 impl1_def_id: LocalDefId,
169 impl2_node: Node,
170) -> Result<(GenericArgsRef<'_>, GenericArgsRef<'_>), ErrorGuaranteed> {
171let infcx = &tcx.infer_ctxt().build(TypingMode::non_body_analysis());
172let ocx = ObligationCtxt::new_with_diagnostics(infcx);
173let param_env = tcx.param_env(impl1_def_id);
174let impl1_span = tcx.def_span(impl1_def_id);
175176let impl1_args = GenericArgs::identity_for_item(tcx, impl1_def_id);
177let impl2_args = translate_args_with_cause(
178infcx,
179param_env,
180impl1_def_id.to_def_id(),
181impl1_args,
182impl2_node,
183&ObligationCause::misc(impl1_span, impl1_def_id),
184 );
185186let errors = ocx.evaluate_obligations_error_on_ambiguity();
187if !errors.is_empty() {
188let guar = ocx.infcx.err_ctxt().report_fulfillment_errors(errors);
189return Err(guar);
190 }
191192let assumed_wf_types = ocx.assumed_wf_types_and_report_errors(param_env, impl1_def_id)?;
193let _ = ocx.resolve_regions_and_report_errors(impl1_def_id, param_env, assumed_wf_types);
194let Ok(impl2_args) = infcx.fully_resolve(impl2_args) else {
195let span = tcx.def_span(impl1_def_id);
196let guar = tcx.dcx().emit_err(GenericArgsOnOverriddenImpl { span });
197return Err(guar);
198 };
199Ok((impl1_args, impl2_args))
200}
201202/// Returns a list of all of the unconstrained generic parameters of the given impl.
203///
204/// For example given the impl:
205///
206/// impl<'a, T, I> ... where &'a I: IntoIterator<Item=&'a T>
207///
208/// This would return the args corresponding to `['a, I]`, because knowing
209/// `'a` and `I` determines the value of `T`.
210fn unconstrained_parent_impl_args<'tcx>(
211 tcx: TyCtxt<'tcx>,
212 impl_def_id: DefId,
213 impl_args: GenericArgsRef<'tcx>,
214) -> Vec<GenericArg<'tcx>> {
215let impl_generic_predicates = tcx.predicates_of(impl_def_id);
216let mut unconstrained_parameters = FxHashSet::default();
217let mut constrained_params = FxHashSet::default();
218let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).instantiate_identity();
219220// Unfortunately the functions in `constrained_generic_parameters` don't do
221 // what we want here. We want only a list of constrained parameters while
222 // the functions in `cgp` add the constrained parameters to a list of
223 // unconstrained parameters.
224for (clause, _) in impl_generic_predicates.predicates.iter() {
225if let ty::ClauseKind::Projection(proj) = clause.kind().skip_binder() {
226let unbound_trait_ref = proj.projection_term.trait_ref(tcx);
227if unbound_trait_ref == impl_trait_ref {
228continue;
229 }
230231 unconstrained_parameters.extend(cgp::parameters_for(tcx, proj.projection_term, true));
232233for param in cgp::parameters_for(tcx, proj.term, false) {
234if !unconstrained_parameters.contains(¶m) {
235 constrained_params.insert(param.0);
236 }
237 }
238239 unconstrained_parameters.extend(cgp::parameters_for(tcx, proj.term, true));
240 }
241 }
242243impl_args244 .iter()
245 .enumerate()
246 .filter(|&(idx, _)| !constrained_params.contains(&(idxas u32)))
247 .map(|(_, arg)| arg)
248 .collect()
249}
250251/// Check that parameters of the derived impl don't occur more than once in the
252/// equated args of the base impl.
253///
254/// For example forbid the following:
255///
256/// ```ignore (illustrative)
257/// impl<A> Tr for A { }
258/// impl<B> Tr for (B, B) { }
259/// ```
260///
261/// Note that only consider the unconstrained parameters of the base impl:
262///
263/// ```ignore (illustrative)
264/// impl<S, I: IntoIterator<Item = S>> Tr<S> for I { }
265/// impl<T> Tr<T> for Vec<T> { }
266/// ```
267///
268/// The args for the parent impl here are `[T, Vec<T>]`, which repeats `T`,
269/// but `S` is constrained in the parent impl, so `parent_args` is only
270/// `[Vec<T>]`. This means we allow this impl.
271fn check_duplicate_params<'tcx>(
272 tcx: TyCtxt<'tcx>,
273 impl1_args: GenericArgsRef<'tcx>,
274 parent_args: Vec<GenericArg<'tcx>>,
275 span: Span,
276) -> Result<(), ErrorGuaranteed> {
277let mut base_params = cgp::parameters_for(tcx, parent_args, true);
278base_params.sort_unstable();
279if let (_, [duplicate, ..]) = base_params.partition_dedup() {
280let param = impl1_args[duplicate.0 as usize];
281return Err(tcx282 .dcx()
283 .struct_span_err(span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("specializing impl repeats parameter `{0}`",
param))
})format!("specializing impl repeats parameter `{param}`"))
284 .emit());
285 }
286Ok(())
287}
288289/// Check that `'static` lifetimes are not introduced by the specializing impl.
290///
291/// For example forbid the following:
292///
293/// ```ignore (illustrative)
294/// impl<A> Tr for A { }
295/// impl Tr for &'static i32 { }
296/// ```
297fn check_static_lifetimes<'tcx>(
298 tcx: TyCtxt<'tcx>,
299 parent_args: &Vec<GenericArg<'tcx>>,
300 span: Span,
301) -> Result<(), ErrorGuaranteed> {
302if tcx.any_free_region_meets(parent_args, |r| r.is_static()) {
303return Err(tcx.dcx().emit_err(errors::StaticSpecialize { span }));
304 }
305Ok(())
306}
307308/// Check whether predicates on the specializing impl (`impl1`) are allowed.
309///
310/// Each predicate `P` must be one of:
311///
312/// * Global (not reference any parameters).
313/// * A `T: Tr` predicate where `Tr` is an always-applicable trait.
314/// * Present on the base impl `impl2`.
315/// * This check is done using the `trait_predicates_eq` function below.
316/// * A well-formed predicate of a type argument of the trait being implemented,
317/// including the `Self`-type.
318#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("check_predicates",
"rustc_hir_analysis::impl_wf_check::min_specialization",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs"),
::tracing_core::__macro_support::Option::Some(318u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::impl_wf_check::min_specialization"),
::tracing_core::field::FieldSet::new(&["impl1_def_id",
"impl1_args", "impl2_node", "impl2_args", "span"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&impl1_def_id)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&impl1_args)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&impl2_node)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&impl2_args)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Result<(), ErrorGuaranteed> =
loop {};
return __tracing_attr_fake_return;
}
{
let impl1_predicates: Vec<_> =
traits::elaborate(tcx,
tcx.predicates_of(impl1_def_id).instantiate(tcx,
impl1_args).into_iter()).collect();
let mut impl2_predicates =
if impl2_node.is_from_trait() {
Vec::new()
} else {
traits::elaborate(tcx,
tcx.predicates_of(impl2_node.def_id()).instantiate(tcx,
impl2_args).into_iter().map(|(c, _s)|
c.as_predicate())).collect()
};
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs:347",
"rustc_hir_analysis::impl_wf_check::min_specialization",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs"),
::tracing_core::__macro_support::Option::Some(347u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::impl_wf_check::min_specialization"),
::tracing_core::field::FieldSet::new(&["impl1_predicates",
"impl2_predicates"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&impl1_predicates)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&impl2_predicates)
as &dyn Value))])
});
} else { ; }
};
let always_applicable_traits =
impl1_predicates.iter().copied().filter(|&(clause, _span)|
{
#[allow(non_exhaustive_omitted_patterns)]
match trait_specialization_kind(tcx, clause) {
Some(TraitSpecializationKind::AlwaysApplicable) => true,
_ => false,
}
}).map(|(c, _span)| c.as_predicate());
for arg in
tcx.impl_trait_ref(impl1_def_id).instantiate_identity().args {
let Some(term) = arg.as_term() else { continue; };
let infcx =
&tcx.infer_ctxt().build(TypingMode::non_body_analysis());
let obligations =
wf::obligations(infcx, tcx.param_env(impl1_def_id),
impl1_def_id, 0, term, span).unwrap();
if !!obligations.has_infer() {
::core::panicking::panic("assertion failed: !obligations.has_infer()")
};
impl2_predicates.extend(traits::elaborate(tcx,
obligations).map(|obligation| obligation.predicate))
}
impl2_predicates.extend(traits::elaborate(tcx,
always_applicable_traits));
let mut res = Ok(());
for (clause, span) in impl1_predicates {
if !impl2_predicates.iter().any(|&pred2|
clause.as_predicate() == pred2) {
res = res.and(check_specialization_on(tcx, clause, span))
}
}
res
}
}
}#[instrument(level = "debug", skip(tcx))]319fn check_predicates<'tcx>(
320 tcx: TyCtxt<'tcx>,
321 impl1_def_id: LocalDefId,
322 impl1_args: GenericArgsRef<'tcx>,
323 impl2_node: Node,
324 impl2_args: GenericArgsRef<'tcx>,
325 span: Span,
326) -> Result<(), ErrorGuaranteed> {
327let impl1_predicates: Vec<_> = traits::elaborate(
328 tcx,
329 tcx.predicates_of(impl1_def_id).instantiate(tcx, impl1_args).into_iter(),
330 )
331 .collect();
332333let mut impl2_predicates = if impl2_node.is_from_trait() {
334// Always applicable traits have to be always applicable without any
335 // assumptions.
336Vec::new()
337 } else {
338 traits::elaborate(
339 tcx,
340 tcx.predicates_of(impl2_node.def_id())
341 .instantiate(tcx, impl2_args)
342 .into_iter()
343 .map(|(c, _s)| c.as_predicate()),
344 )
345 .collect()
346 };
347debug!(?impl1_predicates, ?impl2_predicates);
348349// Since impls of always applicable traits don't get to assume anything, we
350 // can also assume their supertraits apply.
351 //
352 // For example, we allow:
353 //
354 // #[rustc_specialization_trait]
355 // trait AlwaysApplicable: Debug { }
356 //
357 // impl<T> Tr for T { }
358 // impl<T: AlwaysApplicable> Tr for T { }
359 //
360 // Specializing on `AlwaysApplicable` allows also specializing on `Debug`
361 // which is sound because we forbid impls like the following
362 //
363 // impl<D: Debug> AlwaysApplicable for D { }
364let always_applicable_traits = impl1_predicates
365 .iter()
366 .copied()
367 .filter(|&(clause, _span)| {
368matches!(
369 trait_specialization_kind(tcx, clause),
370Some(TraitSpecializationKind::AlwaysApplicable)
371 )
372 })
373 .map(|(c, _span)| c.as_predicate());
374375// Include the well-formed predicates of the type parameters of the impl.
376for arg in tcx.impl_trait_ref(impl1_def_id).instantiate_identity().args {
377let Some(term) = arg.as_term() else {
378continue;
379 };
380let infcx = &tcx.infer_ctxt().build(TypingMode::non_body_analysis());
381let obligations =
382 wf::obligations(infcx, tcx.param_env(impl1_def_id), impl1_def_id, 0, term, span)
383 .unwrap();
384385assert!(!obligations.has_infer());
386 impl2_predicates
387 .extend(traits::elaborate(tcx, obligations).map(|obligation| obligation.predicate))
388 }
389 impl2_predicates.extend(traits::elaborate(tcx, always_applicable_traits));
390391let mut res = Ok(());
392for (clause, span) in impl1_predicates {
393if !impl2_predicates.iter().any(|&pred2| clause.as_predicate() == pred2) {
394 res = res.and(check_specialization_on(tcx, clause, span))
395 }
396 }
397 res
398}
399400#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("check_specialization_on",
"rustc_hir_analysis::impl_wf_check::min_specialization",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs"),
::tracing_core::__macro_support::Option::Some(400u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::impl_wf_check::min_specialization"),
::tracing_core::field::FieldSet::new(&["clause", "span"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&clause)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Result<(), ErrorGuaranteed> =
loop {};
return __tracing_attr_fake_return;
}
{
match clause.kind().skip_binder() {
_ if clause.is_global() => Ok(()),
ty::ClauseKind::Trait(ty::TraitPredicate {
trait_ref, polarity: _ }) => {
if #[allow(non_exhaustive_omitted_patterns)] match trait_specialization_kind(tcx,
clause) {
Some(TraitSpecializationKind::Marker) => true,
_ => false,
} {
Ok(())
} else {
Err(tcx.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot specialize on trait `{0}`",
tcx.def_path_str(trait_ref.def_id)))
})).emit())
}
}
ty::ClauseKind::Projection(ty::ProjectionPredicate {
projection_term, term }) =>
Err(tcx.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot specialize on associated type `{0} == {1}`",
projection_term, term))
})).emit()),
ty::ClauseKind::ConstArgHasType(..) => { Ok(()) }
_ =>
Err(tcx.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot specialize on predicate `{0}`",
clause))
})).emit()),
}
}
}
}#[instrument(level = "debug", skip(tcx))]401fn check_specialization_on<'tcx>(
402 tcx: TyCtxt<'tcx>,
403 clause: ty::Clause<'tcx>,
404 span: Span,
405) -> Result<(), ErrorGuaranteed> {
406match clause.kind().skip_binder() {
407// Global predicates are either always true or always false, so we
408 // are fine to specialize on.
409_ if clause.is_global() => Ok(()),
410// We allow specializing on explicitly marked traits with no associated
411 // items.
412ty::ClauseKind::Trait(ty::TraitPredicate { trait_ref, polarity: _ }) => {
413if matches!(
414 trait_specialization_kind(tcx, clause),
415Some(TraitSpecializationKind::Marker)
416 ) {
417Ok(())
418 } else {
419Err(tcx
420 .dcx()
421 .struct_span_err(
422 span,
423format!(
424"cannot specialize on trait `{}`",
425 tcx.def_path_str(trait_ref.def_id),
426 ),
427 )
428 .emit())
429 }
430 }
431 ty::ClauseKind::Projection(ty::ProjectionPredicate { projection_term, term }) => Err(tcx
432 .dcx()
433 .struct_span_err(
434 span,
435format!("cannot specialize on associated type `{projection_term} == {term}`",),
436 )
437 .emit()),
438 ty::ClauseKind::ConstArgHasType(..) => {
439// FIXME(min_specialization), FIXME(const_generics):
440 // It probably isn't right to allow _every_ `ConstArgHasType` but I am somewhat unsure
441 // about the actual rules that would be sound. Can't just always error here because otherwise
442 // std/core doesn't even compile as they have `const N: usize` in some specializing impls.
443 //
444 // While we do not support constructs like `<T, const N: T>` there is probably no risk of
445 // soundness bugs, but when we support generic const parameter types this will need to be
446 // revisited.
447Ok(())
448 }
449_ => Err(tcx
450 .dcx()
451 .struct_span_err(span, format!("cannot specialize on predicate `{clause}`"))
452 .emit()),
453 }
454}
455456fn trait_specialization_kind<'tcx>(
457 tcx: TyCtxt<'tcx>,
458 clause: ty::Clause<'tcx>,
459) -> Option<TraitSpecializationKind> {
460match clause.kind().skip_binder() {
461 ty::ClauseKind::Trait(ty::TraitPredicate { trait_ref, polarity: _ }) => {
462Some(tcx.trait_def(trait_ref.def_id).specialization_kind)
463 }
464 ty::ClauseKind::RegionOutlives(_)
465 | ty::ClauseKind::TypeOutlives(_)
466 | ty::ClauseKind::Projection(_)
467 | ty::ClauseKind::ConstArgHasType(..)
468 | ty::ClauseKind::WellFormed(_)
469 | ty::ClauseKind::ConstEvaluatable(..)
470 | ty::ClauseKind::UnstableFeature(_)
471 | ty::ClauseKind::HostEffect(..) => None,
472 }
473}