1pub mod specialization_graph;
13
14use rustc_data_structures::fx::FxIndexSet;
15use rustc_errors::codes::*;
16use rustc_errors::{Diag, EmissionGuarantee};
17use rustc_hir::def_id::{DefId, LocalDefId};
18use rustc_infer::traits::Obligation;
19use rustc_middle::bug;
20use rustc_middle::query::LocalCrate;
21use rustc_middle::traits::query::NoSolution;
22use rustc_middle::ty::print::PrintTraitRefExt as _;
23use rustc_middle::ty::{
24 self, GenericArgsRef, Ty, TyCtxt, TypeVisitableExt, TypingMode, Unnormalized,
25};
26use rustc_session::lint::builtin::COHERENCE_LEAK_CHECK;
27use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, sym};
28use specialization_graph::GraphExt;
29use tracing::{debug, instrument};
30
31use crate::diagnostics::NegativePositiveConflict;
32use crate::error_reporting::traits::to_pretty_impl_header;
33use crate::infer::{InferCtxt, TyCtxtInferExt};
34use crate::traits::select::IntercrateAmbiguityCause;
35use crate::traits::{
36 FutureCompatOverlapErrorKind, ObligationCause, ObligationCtxt, coherence,
37 predicates_for_generics,
38};
39
40#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for OverlapError<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let names: &'static _ =
&["with_impl", "trait_ref", "self_ty",
"intercrate_ambiguity_causes", "involves_placeholder",
"overflowing_predicates"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.with_impl, &self.trait_ref, &self.self_ty,
&self.intercrate_ambiguity_causes,
&self.involves_placeholder, &&self.overflowing_predicates];
::core::fmt::Formatter::debug_struct_fields_finish(f, "OverlapError",
names, values)
}
}Debug)]
42pub struct OverlapError<'tcx> {
43 pub with_impl: DefId,
44 pub trait_ref: ty::TraitRef<'tcx>,
45 pub self_ty: Option<Ty<'tcx>>,
46 pub intercrate_ambiguity_causes: FxIndexSet<IntercrateAmbiguityCause<'tcx>>,
47 pub involves_placeholder: bool,
48 pub overflowing_predicates: Vec<ty::Predicate<'tcx>>,
49}
50
51pub fn translate_args<'tcx>(
87 infcx: &InferCtxt<'tcx>,
88 param_env: ty::ParamEnv<'tcx>,
89 source_impl: DefId,
90 source_args: GenericArgsRef<'tcx>,
91 target_node: specialization_graph::Node,
92) -> GenericArgsRef<'tcx> {
93 translate_args_with_cause(
94 infcx,
95 param_env,
96 source_impl,
97 source_args,
98 target_node,
99 &ObligationCause::dummy(),
100 )
101}
102
103pub fn translate_args_with_cause<'tcx>(
110 infcx: &InferCtxt<'tcx>,
111 param_env: ty::ParamEnv<'tcx>,
112 source_impl: DefId,
113 source_args: GenericArgsRef<'tcx>,
114 target_node: specialization_graph::Node,
115 cause: &ObligationCause<'tcx>,
116) -> GenericArgsRef<'tcx> {
117 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/specialize/mod.rs:117",
"rustc_trait_selection::traits::specialize",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/specialize/mod.rs"),
::tracing_core::__macro_support::Option::Some(117u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::specialize"),
::tracing_core::field::FieldSet::new(&["message"],
::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};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("translate_args({0:?}, {1:?}, {2:?}, {3:?})",
param_env, source_impl, source_args, target_node) as
&dyn ::tracing::field::Value))])
});
} else { ; }
};debug!(
118 "translate_args({:?}, {:?}, {:?}, {:?})",
119 param_env, source_impl, source_args, target_node
120 );
121 let source_trait_ref =
122 infcx.tcx.impl_trait_ref(source_impl).instantiate(infcx.tcx, source_args).skip_norm_wip();
123
124 let target_args = match target_node {
127 specialization_graph::Node::Impl(target_impl) => {
128 if source_impl == target_impl {
130 return source_args;
131 }
132
133 fulfill_implication(infcx, param_env, source_trait_ref, source_impl, target_impl, cause)
134 .unwrap_or_else(|_| {
135 ::rustc_middle::util::bug::bug_fmt(format_args!("When translating generic parameters from {0:?} to {1:?}, the expected specialization failed to hold",
source_impl, target_impl))bug!(
136 "When translating generic parameters from {source_impl:?} to \
137 {target_impl:?}, the expected specialization failed to hold"
138 )
139 })
140 }
141 specialization_graph::Node::Trait(..) => source_trait_ref.args,
142 };
143
144 source_args.rebase_onto(infcx.tcx, source_impl, target_args)
146}
147
148fn fulfill_implication<'tcx>(
154 infcx: &InferCtxt<'tcx>,
155 param_env: ty::ParamEnv<'tcx>,
156 source_trait_ref: ty::TraitRef<'tcx>,
157 source_impl: DefId,
158 target_impl: DefId,
159 cause: &ObligationCause<'tcx>,
160) -> Result<GenericArgsRef<'tcx>, NoSolution> {
161 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/specialize/mod.rs:161",
"rustc_trait_selection::traits::specialize",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/specialize/mod.rs"),
::tracing_core::__macro_support::Option::Some(161u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::specialize"),
::tracing_core::field::FieldSet::new(&["message"],
::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};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("fulfill_implication({0:?}, trait_ref={1:?} |- {2:?} applies)",
param_env, source_trait_ref, target_impl) as
&dyn ::tracing::field::Value))])
});
} else { ; }
};debug!(
162 "fulfill_implication({:?}, trait_ref={:?} |- {:?} applies)",
163 param_env, source_trait_ref, target_impl
164 );
165
166 let ocx = ObligationCtxt::new(infcx);
167 let source_trait_ref = ocx.normalize(cause, param_env, Unnormalized::new_wip(source_trait_ref));
168
169 if !ocx.evaluate_obligations_error_on_ambiguity().is_empty() {
170 infcx.dcx().span_delayed_bug(
171 infcx.tcx.def_span(source_impl),
172 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("failed to fully normalize {0}",
source_trait_ref))
})format!("failed to fully normalize {source_trait_ref}"),
173 );
174 return Err(NoSolution);
175 }
176
177 let target_args = infcx.fresh_args_for_item(DUMMY_SP, target_impl);
178 let target_trait_ref = ocx.normalize(
179 cause,
180 param_env,
181 infcx.tcx.impl_trait_ref(target_impl).instantiate(infcx.tcx, target_args),
182 );
183
184 ocx.eq(cause, param_env, source_trait_ref, target_trait_ref)?;
186
187 let predicates = infcx.tcx.predicates_of(target_impl).instantiate(infcx.tcx, target_args);
191 let obligations = predicates_for_generics(
192 |_, _| cause.clone(),
193 |pred| ocx.normalize(cause, param_env, pred),
194 param_env,
195 predicates,
196 );
197 ocx.register_obligations(obligations);
198
199 let errors = ocx.evaluate_obligations_error_on_ambiguity();
200 if !errors.is_empty() {
201 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/specialize/mod.rs:202",
"rustc_trait_selection::traits::specialize",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/specialize/mod.rs"),
::tracing_core::__macro_support::Option::Some(202u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::specialize"),
::tracing_core::field::FieldSet::new(&["message"],
::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};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("fulfill_implication: for impls on {0:?} and {1:?}, could not fulfill: {2:?} given {3:?}",
source_trait_ref, target_trait_ref, errors,
param_env.caller_bounds()) as
&dyn ::tracing::field::Value))])
});
} else { ; }
};debug!(
203 "fulfill_implication: for impls on {:?} and {:?}, \
204 could not fulfill: {:?} given {:?}",
205 source_trait_ref,
206 target_trait_ref,
207 errors,
208 param_env.caller_bounds()
209 );
210 return Err(NoSolution);
211 }
212
213 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/specialize/mod.rs:213",
"rustc_trait_selection::traits::specialize",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/specialize/mod.rs"),
::tracing_core::__macro_support::Option::Some(213u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::specialize"),
::tracing_core::field::FieldSet::new(&["message"],
::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};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("fulfill_implication: an impl for {0:?} specializes {1:?}",
source_trait_ref, target_trait_ref) as
&dyn ::tracing::field::Value))])
});
} else { ; }
};debug!(
214 "fulfill_implication: an impl for {:?} specializes {:?}",
215 source_trait_ref, target_trait_ref
216 );
217
218 Ok(infcx.resolve_vars_if_possible(target_args))
221}
222
223pub(super) fn specialization_enabled_in(tcx: TyCtxt<'_>, _: LocalCrate) -> bool {
224 tcx.features().specialization() || tcx.features().min_specialization()
225}
226
227#[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("specializes",
"rustc_trait_selection::traits::specialize",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/specialize/mod.rs"),
::tracing_core::__macro_support::Option::Some(238u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::specialize"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("specializing_impl_def_id")
}> =
::tracing::__macro_support::FieldName::new("specializing_impl_def_id");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("parent_impl_def_id")
}> =
::tracing::__macro_support::FieldName::new("parent_impl_def_id");
NAME.as_str()
}], ::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};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&specializing_impl_def_id)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&parent_impl_def_id)
as &dyn ::tracing::field::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: bool = loop {};
return __tracing_attr_fake_return;
}
{
if !tcx.specialization_enabled_in(specializing_impl_def_id.krate)
{
let span = tcx.def_span(specializing_impl_def_id);
if !span.allows_unstable(sym::specialization) &&
!span.allows_unstable(sym::min_specialization) {
return false;
}
}
let specializing_impl_trait_header =
tcx.impl_trait_header(specializing_impl_def_id);
if specializing_impl_trait_header.polarity !=
tcx.impl_polarity(parent_impl_def_id) {
return false;
}
let param_env = tcx.param_env(specializing_impl_def_id);
let infcx =
tcx.infer_ctxt().build(TypingMode::non_body_analysis());
let specializing_impl_trait_ref =
specializing_impl_trait_header.trait_ref.instantiate_identity();
let cause = &ObligationCause::dummy();
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/specialize/mod.rs:287",
"rustc_trait_selection::traits::specialize",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/specialize/mod.rs"),
::tracing_core::__macro_support::Option::Some(287u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::specialize"),
::tracing_core::field::FieldSet::new(&["message"],
::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};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("fulfill_implication({0:?}, trait_ref={1:?} |- {2:?} applies)",
param_env, specializing_impl_trait_ref, parent_impl_def_id)
as &dyn ::tracing::field::Value))])
});
} else { ; }
};
let ocx = ObligationCtxt::new(&infcx);
let specializing_impl_trait_ref =
ocx.normalize(cause, param_env, specializing_impl_trait_ref);
if !ocx.evaluate_obligations_error_on_ambiguity().is_empty() {
infcx.dcx().span_delayed_bug(infcx.tcx.def_span(specializing_impl_def_id),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("failed to fully normalize {0}",
specializing_impl_trait_ref))
}));
return false;
}
let parent_args =
infcx.fresh_args_for_item(DUMMY_SP, parent_impl_def_id);
let parent_impl_trait_ref =
ocx.normalize(cause, param_env,
infcx.tcx.impl_trait_ref(parent_impl_def_id).instantiate(infcx.tcx,
parent_args));
let Ok(()) =
ocx.eq(cause, param_env, specializing_impl_trait_ref,
parent_impl_trait_ref) else { return false; };
let predicates =
infcx.tcx.predicates_of(parent_impl_def_id).instantiate(infcx.tcx,
parent_args);
let obligations =
predicates_for_generics(|_, _| cause.clone(),
|pred| ocx.normalize(cause, param_env, pred), param_env,
predicates);
ocx.register_obligations(obligations);
let errors = ocx.evaluate_obligations_error_on_ambiguity();
if !errors.is_empty() {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/specialize/mod.rs:334",
"rustc_trait_selection::traits::specialize",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/specialize/mod.rs"),
::tracing_core::__macro_support::Option::Some(334u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::specialize"),
::tracing_core::field::FieldSet::new(&["message"],
::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};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("fulfill_implication: for impls on {0:?} and {1:?}, could not fulfill: {2:?} given {3:?}",
specializing_impl_trait_ref, parent_impl_trait_ref, errors,
param_env.caller_bounds()) as
&dyn ::tracing::field::Value))])
});
} else { ; }
};
return false;
}
if tcx.is_conditionally_const(parent_impl_def_id) {
if !tcx.is_conditionally_const(specializing_impl_def_id) {
return false;
}
let const_conditions =
infcx.tcx.const_conditions(parent_impl_def_id).instantiate(infcx.tcx,
parent_args);
let const_conditions =
const_conditions.into_iter().map(|(trait_ref, span)|
(ocx.normalize(cause, param_env, trait_ref), span));
ocx.register_obligations(const_conditions.into_iter().map(|(trait_ref,
_)|
{
Obligation::new(infcx.tcx, cause.clone(), param_env,
trait_ref.to_host_effect_clause(infcx.tcx,
ty::BoundConstness::Maybe))
}));
let errors = ocx.evaluate_obligations_error_on_ambiguity();
if !errors.is_empty() {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/specialize/mod.rs:370",
"rustc_trait_selection::traits::specialize",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/specialize/mod.rs"),
::tracing_core::__macro_support::Option::Some(370u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::specialize"),
::tracing_core::field::FieldSet::new(&["message"],
::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};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("fulfill_implication: for impls on {0:?} and {1:?}, could not fulfill: {2:?} given {3:?}",
specializing_impl_trait_ref, parent_impl_trait_ref, errors,
param_env.caller_bounds()) as
&dyn ::tracing::field::Value))])
});
} else { ; }
};
return false;
}
}
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/specialize/mod.rs:382",
"rustc_trait_selection::traits::specialize",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/specialize/mod.rs"),
::tracing_core::__macro_support::Option::Some(382u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::specialize"),
::tracing_core::field::FieldSet::new(&["message"],
::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};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("fulfill_implication: an impl for {0:?} specializes {1:?}",
specializing_impl_trait_ref, parent_impl_trait_ref) as
&dyn ::tracing::field::Value))])
});
} else { ; }
};
true
}
}
}#[instrument(skip(tcx), level = "debug")]
239pub(super) fn specializes(
240 tcx: TyCtxt<'_>,
241 (specializing_impl_def_id, parent_impl_def_id): (DefId, DefId),
242) -> bool {
243 if !tcx.specialization_enabled_in(specializing_impl_def_id.krate) {
250 let span = tcx.def_span(specializing_impl_def_id);
251 if !span.allows_unstable(sym::specialization)
252 && !span.allows_unstable(sym::min_specialization)
253 {
254 return false;
255 }
256 }
257
258 let specializing_impl_trait_header = tcx.impl_trait_header(specializing_impl_def_id);
259
260 if specializing_impl_trait_header.polarity != tcx.impl_polarity(parent_impl_def_id) {
274 return false;
275 }
276
277 let param_env = tcx.param_env(specializing_impl_def_id);
280
281 let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
283
284 let specializing_impl_trait_ref =
285 specializing_impl_trait_header.trait_ref.instantiate_identity();
286 let cause = &ObligationCause::dummy();
287 debug!(
288 "fulfill_implication({:?}, trait_ref={:?} |- {:?} applies)",
289 param_env, specializing_impl_trait_ref, parent_impl_def_id
290 );
291
292 let ocx = ObligationCtxt::new(&infcx);
295 let specializing_impl_trait_ref = ocx.normalize(cause, param_env, specializing_impl_trait_ref);
296
297 if !ocx.evaluate_obligations_error_on_ambiguity().is_empty() {
298 infcx.dcx().span_delayed_bug(
299 infcx.tcx.def_span(specializing_impl_def_id),
300 format!("failed to fully normalize {specializing_impl_trait_ref}"),
301 );
302 return false;
303 }
304
305 let parent_args = infcx.fresh_args_for_item(DUMMY_SP, parent_impl_def_id);
306 let parent_impl_trait_ref = ocx.normalize(
307 cause,
308 param_env,
309 infcx.tcx.impl_trait_ref(parent_impl_def_id).instantiate(infcx.tcx, parent_args),
310 );
311
312 let Ok(()) = ocx.eq(cause, param_env, specializing_impl_trait_ref, parent_impl_trait_ref)
314 else {
315 return false;
316 };
317
318 let predicates =
322 infcx.tcx.predicates_of(parent_impl_def_id).instantiate(infcx.tcx, parent_args);
323 let obligations = predicates_for_generics(
324 |_, _| cause.clone(),
325 |pred| ocx.normalize(cause, param_env, pred),
326 param_env,
327 predicates,
328 );
329 ocx.register_obligations(obligations);
330
331 let errors = ocx.evaluate_obligations_error_on_ambiguity();
332 if !errors.is_empty() {
333 debug!(
335 "fulfill_implication: for impls on {:?} and {:?}, \
336 could not fulfill: {:?} given {:?}",
337 specializing_impl_trait_ref,
338 parent_impl_trait_ref,
339 errors,
340 param_env.caller_bounds()
341 );
342 return false;
343 }
344
345 if tcx.is_conditionally_const(parent_impl_def_id) {
349 if !tcx.is_conditionally_const(specializing_impl_def_id) {
350 return false;
351 }
352
353 let const_conditions =
354 infcx.tcx.const_conditions(parent_impl_def_id).instantiate(infcx.tcx, parent_args);
355 let const_conditions = const_conditions
356 .into_iter()
357 .map(|(trait_ref, span)| (ocx.normalize(cause, param_env, trait_ref), span));
358 ocx.register_obligations(const_conditions.into_iter().map(|(trait_ref, _)| {
359 Obligation::new(
360 infcx.tcx,
361 cause.clone(),
362 param_env,
363 trait_ref.to_host_effect_clause(infcx.tcx, ty::BoundConstness::Maybe),
364 )
365 }));
366
367 let errors = ocx.evaluate_obligations_error_on_ambiguity();
368 if !errors.is_empty() {
369 debug!(
371 "fulfill_implication: for impls on {:?} and {:?}, \
372 could not fulfill: {:?} given {:?}",
373 specializing_impl_trait_ref,
374 parent_impl_trait_ref,
375 errors,
376 param_env.caller_bounds()
377 );
378 return false;
379 }
380 }
381
382 debug!(
383 "fulfill_implication: an impl for {:?} specializes {:?}",
384 specializing_impl_trait_ref, parent_impl_trait_ref
385 );
386
387 true
388}
389
390pub(super) fn specialization_graph_provider(
392 tcx: TyCtxt<'_>,
393 trait_id: DefId,
394) -> Result<&'_ specialization_graph::Graph, ErrorGuaranteed> {
395 let mut sg = specialization_graph::Graph::new();
396 let overlap_mode = specialization_graph::OverlapMode::get(tcx, trait_id);
397
398 let mut trait_impls: Vec<_> = tcx.all_impls(trait_id).collect();
399
400 trait_impls
405 .sort_unstable_by_key(|def_id| (-(def_id.krate.as_u32() as i64), def_id.index.index()));
406
407 let mut errored = Ok(());
408
409 for impl_def_id in trait_impls {
410 if let Some(impl_def_id) = impl_def_id.as_local() {
411 let insert_result = sg.insert(tcx, impl_def_id.to_def_id(), overlap_mode);
413 let (overlap, used_to_be_allowed) = match insert_result {
415 Err(overlap) => (Some(overlap), None),
416 Ok(Some(overlap)) => (Some(overlap.error), Some(overlap.kind)),
417 Ok(None) => (None, None),
418 };
419
420 if let Some(overlap) = overlap {
421 errored = errored.and(report_overlap_conflict(
422 tcx,
423 overlap,
424 impl_def_id,
425 used_to_be_allowed,
426 ));
427 }
428 } else {
429 let parent = tcx.impl_parent(impl_def_id).unwrap_or(trait_id);
430 sg.record_impl_from_cstore(tcx, parent, impl_def_id)
431 }
432 }
433 errored?;
434
435 Ok(tcx.arena.alloc(sg))
436}
437
438#[cold]
442#[inline(never)]
443fn report_overlap_conflict<'tcx>(
444 tcx: TyCtxt<'tcx>,
445 overlap: OverlapError<'tcx>,
446 impl_def_id: LocalDefId,
447 used_to_be_allowed: Option<FutureCompatOverlapErrorKind>,
448) -> Result<(), ErrorGuaranteed> {
449 let impl_polarity = tcx.impl_polarity(impl_def_id.to_def_id());
450 let other_polarity = tcx.impl_polarity(overlap.with_impl);
451 match (impl_polarity, other_polarity) {
452 (ty::ImplPolarity::Negative, ty::ImplPolarity::Positive) => {
453 Err(report_negative_positive_conflict(
454 tcx,
455 &overlap,
456 impl_def_id,
457 impl_def_id.to_def_id(),
458 overlap.with_impl,
459 ))
460 }
461
462 (ty::ImplPolarity::Positive, ty::ImplPolarity::Negative) => {
463 Err(report_negative_positive_conflict(
464 tcx,
465 &overlap,
466 impl_def_id,
467 overlap.with_impl,
468 impl_def_id.to_def_id(),
469 ))
470 }
471
472 _ => report_conflicting_impls(tcx, overlap, impl_def_id, used_to_be_allowed),
473 }
474}
475
476fn report_negative_positive_conflict<'tcx>(
477 tcx: TyCtxt<'tcx>,
478 overlap: &OverlapError<'tcx>,
479 local_impl_def_id: LocalDefId,
480 negative_impl_def_id: DefId,
481 positive_impl_def_id: DefId,
482) -> ErrorGuaranteed {
483 let mut diag = tcx.dcx().create_err(NegativePositiveConflict {
484 impl_span: tcx.def_span(local_impl_def_id),
485 trait_desc: overlap.trait_ref,
486 self_ty: overlap.self_ty,
487 negative_impl_span: tcx.span_of_impl(negative_impl_def_id),
488 positive_impl_span: tcx.span_of_impl(positive_impl_def_id),
489 });
490
491 for cause in &overlap.intercrate_ambiguity_causes {
492 cause.add_intercrate_ambiguity_hint(&mut diag);
493 }
494
495 diag.emit()
496}
497
498fn report_conflicting_impls<'tcx>(
499 tcx: TyCtxt<'tcx>,
500 overlap: OverlapError<'tcx>,
501 impl_def_id: LocalDefId,
502 used_to_be_allowed: Option<FutureCompatOverlapErrorKind>,
503) -> Result<(), ErrorGuaranteed> {
504 let impl_span = tcx.def_span(impl_def_id);
505
506 fn decorate<'tcx, G: EmissionGuarantee>(
510 tcx: TyCtxt<'tcx>,
511 overlap: &OverlapError<'tcx>,
512 impl_span: Span,
513 err: &mut Diag<'_, G>,
514 ) {
515 match tcx.span_of_impl(overlap.with_impl) {
516 Ok(span) => {
517 err.span_label(span, "first implementation here");
518
519 err.span_label(
520 impl_span,
521 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("conflicting implementation{0}",
overlap.self_ty.map_or_else(String::new,
|ty|
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" for `{0}`", ty))
}))))
})format!(
522 "conflicting implementation{}",
523 overlap.self_ty.map_or_else(String::new, |ty| format!(" for `{ty}`"))
524 ),
525 );
526 }
527 Err(cname) => {
528 let msg = match to_pretty_impl_header(tcx, overlap.with_impl) {
529 Some(s) => {
530 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("conflicting implementation in crate `{0}`:\n- {1}",
cname, s))
})format!("conflicting implementation in crate `{cname}`:\n- {s}")
531 }
532 None => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("conflicting implementation in crate `{0}`",
cname))
})format!("conflicting implementation in crate `{cname}`"),
533 };
534 err.note(msg);
535 }
536 }
537
538 for cause in &overlap.intercrate_ambiguity_causes {
539 cause.add_intercrate_ambiguity_hint(err);
540 }
541
542 if overlap.involves_placeholder {
543 coherence::add_placeholder_note(err);
544 }
545
546 if !overlap.overflowing_predicates.is_empty() {
547 coherence::suggest_increasing_recursion_limit(
548 tcx,
549 err,
550 &overlap.overflowing_predicates,
551 );
552 }
553 }
554
555 let msg = || {
556 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("conflicting implementations of trait `{0}`{1}",
overlap.trait_ref.print_trait_sugared(),
overlap.self_ty.map_or_else(String::new,
|ty|
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" for type `{0}`", ty))
}))))
})format!(
557 "conflicting implementations of trait `{}`{}",
558 overlap.trait_ref.print_trait_sugared(),
559 overlap.self_ty.map_or_else(String::new, |ty| format!(" for type `{ty}`")),
560 )
561 };
562
563 if let Err(err) = (overlap.trait_ref, overlap.self_ty).error_reported() {
565 return Err(err);
566 }
567
568 match used_to_be_allowed {
569 None => {
570 let reported = if overlap.with_impl.is_local()
571 || tcx.ensure_result().orphan_check_impl(impl_def_id).is_ok()
572 {
573 let mut err = tcx.dcx().struct_span_err(impl_span, msg());
574 err.code(E0119);
575 decorate(tcx, &overlap, impl_span, &mut err);
576 err.emit()
577 } else {
578 tcx.dcx().span_delayed_bug(impl_span, "impl should have failed the orphan check")
579 };
580 Err(reported)
581 }
582 Some(kind) => {
583 let lint = match kind {
584 FutureCompatOverlapErrorKind::LeakCheck => COHERENCE_LEAK_CHECK,
585 };
586 tcx.emit_node_span_lint(
587 lint,
588 tcx.local_def_id_to_hir_id(impl_def_id),
589 impl_span,
590 rustc_errors::DiagDecorator(|err| {
591 err.primary_message(msg());
592 decorate(tcx, &overlap, impl_span, err);
593 }),
594 );
595 Ok(())
596 }
597 }
598}