1use rustc_data_structures::fx::FxIndexSet;
5use rustc_errors::ErrorGuaranteed;
6use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, TyCtxtInferExt};
7use rustc_lint_defs::builtin::UNCOVERED_PARAM_IN_PROJECTION;
8use rustc_middle::ty::{
9 self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode,
10 Unnormalized,
11};
12use rustc_span::def_id::{DefId, LocalDefId};
13use rustc_span::{bug, span_bug};
14use rustc_trait_selection::traits::{
15 self, IsFirstInputType, OrphanCheckErr, OrphanCheckMode, UncoveredTyParams,
16};
17use tracing::{debug, instrument};
18
19use crate::diagnostics;
20
21{}
#[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("orphan_check_impl",
"rustc_hir_analysis::coherence::orphan",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/064405c5f7fbc7d41aa30b45da1e45a85777bd58/compiler/rustc_hir_analysis/src/coherence/orphan.rs"),
::tracing_core::__macro_support::Option::Some(21u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::orphan"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("impl_def_id")
}> =
::tracing::__macro_support::FieldName::new("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(&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: Result<(), ErrorGuaranteed> =
loop {};
return __tracing_attr_fake_return;
}
{
let trait_ref =
tcx.impl_trait_ref(impl_def_id).instantiate_identity().skip_norm_wip();
trait_ref.error_reported()?;
match orphan_check(tcx, impl_def_id, OrphanCheckMode::Proper) {
Ok(()) => {}
Err(err) =>
match orphan_check(tcx, impl_def_id,
OrphanCheckMode::Compat) {
Ok(()) =>
match err {
OrphanCheckErr::UncoveredTyParams(uncovered_ty_params) => {
let hir_id = tcx.local_def_id_to_hir_id(impl_def_id);
for param_def_id in uncovered_ty_params.uncovered {
let ident = tcx.item_ident(param_def_id);
tcx.emit_node_span_lint(UNCOVERED_PARAM_IN_PROJECTION,
hir_id, ident.span,
diagnostics::UncoveredTyParam {
param: ident,
local_ty: uncovered_ty_params.local_ty,
});
}
}
OrphanCheckErr::NonLocalInputType(_) => {
::rustc_span::macros::bug_impl(None,
format_args!("orphanck: shouldn\'t\'ve gotten non-local input tys in compat mode"),
Location::caller())
}
},
Err(err) =>
return Err(emit_orphan_check_error(tcx, trait_ref,
impl_def_id, err)),
},
}
let trait_def_id = trait_ref.def_id;
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/064405c5f7fbc7d41aa30b45da1e45a85777bd58/compiler/rustc_hir_analysis/src/coherence/orphan.rs:92",
"rustc_hir_analysis::coherence::orphan",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/064405c5f7fbc7d41aa30b45da1e45a85777bd58/compiler/rustc_hir_analysis/src/coherence/orphan.rs"),
::tracing_core::__macro_support::Option::Some(92u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::orphan"),
::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!("trait_ref={0:?} trait_def_id={1:?} trait_is_auto={2}",
trait_ref, trait_def_id, tcx.trait_is_auto(trait_def_id)) as
&dyn ::tracing::field::Value))])
});
} else { ; }
};
if tcx.trait_is_auto(trait_def_id) {
let self_ty = tcx.expand_free_alias_tys(trait_ref.self_ty());
enum LocalImpl {
Allow,
Disallow {
problematic_kind: &'static str,
},
}
enum NonlocalImpl {
Allow,
DisallowBecauseNonlocal,
DisallowOther,
}
let (local_impl, nonlocal_impl) =
match self_ty.kind() {
ty::Adt(self_def, _) =>
(LocalImpl::Allow,
if self_def.did().is_local() {
NonlocalImpl::Allow
} else { NonlocalImpl::DisallowBecauseNonlocal }),
ty::Foreign(did) =>
(LocalImpl::Allow,
if did.is_local() {
NonlocalImpl::Allow
} else { NonlocalImpl::DisallowBecauseNonlocal }),
ty::Dynamic(..) =>
(LocalImpl::Disallow { problematic_kind: "trait object" },
NonlocalImpl::DisallowOther),
ty::Param(..) =>
(if self_ty.is_sized(tcx,
ty::TypingEnv::non_body_analysis(tcx, impl_def_id)) {
LocalImpl::Allow
} else {
LocalImpl::Disallow { problematic_kind: "generic type" }
}, NonlocalImpl::DisallowOther),
ty::Alias(_, ty::AliasTy { kind, .. }) => {
let problematic_kind =
match kind {
ty::Projection { .. } => "associated type",
ty::Free { .. } => "type alias",
ty::Opaque { .. } => "opaque type",
ty::Inherent { .. } => "associated type",
};
(LocalImpl::Disallow { problematic_kind },
NonlocalImpl::DisallowOther)
}
ty::Bool | ty::Pat(..) | ty::Char | ty::Int(..) |
ty::Uint(..) | ty::Float(..) | ty::Str | ty::Array(..) |
ty::Slice(..) | ty::RawPtr(..) | ty::Ref(..) | ty::FnPtr(..)
| ty::Never | ty::Tuple(..) | ty::UnsafeBinder(_) =>
(LocalImpl::Allow, NonlocalImpl::DisallowOther),
ty::FnDef(..) | ty::Closure(..) | ty::CoroutineClosure(..) |
ty::Coroutine(..) | ty::CoroutineWitness(..) | ty::Bound(..)
| ty::Placeholder(..) | ty::Infer(..) => {
let sp = tcx.def_span(impl_def_id);
::rustc_span::macros::bug_impl(Some(sp),
format_args!("weird self type for autotrait impl"),
Location::caller())
}
ty::Error(..) => (LocalImpl::Allow, NonlocalImpl::Allow),
ty::View(..) =>
::core::panicking::unimplemented_default_msg(),
};
if trait_def_id.is_local() {
match local_impl {
LocalImpl::Allow => {}
LocalImpl::Disallow { problematic_kind } => {
return Err(tcx.dcx().emit_err(diagnostics::TraitsWithDefaultImpl {
span: tcx.def_span(impl_def_id),
traits: tcx.def_path_str(trait_def_id),
problematic_kind,
self_ty,
}));
}
}
} else {
match nonlocal_impl {
NonlocalImpl::Allow => {}
NonlocalImpl::DisallowBecauseNonlocal => {
return Err(tcx.dcx().emit_err(diagnostics::CrossCrateTraitsDefined {
span: tcx.def_span(impl_def_id),
traits: tcx.def_path_str(trait_def_id),
}));
}
NonlocalImpl::DisallowOther => {
return Err(tcx.dcx().emit_err(diagnostics::CrossCrateTraits {
span: tcx.def_span(impl_def_id),
traits: tcx.def_path_str(trait_def_id),
self_ty,
}));
}
}
}
}
Ok(())
}
}
}#[instrument(level = "debug", skip(tcx))]
22pub(crate) fn orphan_check_impl(
23 tcx: TyCtxt<'_>,
24 impl_def_id: LocalDefId,
25) -> Result<(), ErrorGuaranteed> {
26 let trait_ref = tcx.impl_trait_ref(impl_def_id).instantiate_identity().skip_norm_wip();
27 trait_ref.error_reported()?;
28
29 match orphan_check(tcx, impl_def_id, OrphanCheckMode::Proper) {
30 Ok(()) => {}
31 Err(err) => match orphan_check(tcx, impl_def_id, OrphanCheckMode::Compat) {
32 Ok(()) => match err {
33 OrphanCheckErr::UncoveredTyParams(uncovered_ty_params) => {
34 let hir_id = tcx.local_def_id_to_hir_id(impl_def_id);
35
36 for param_def_id in uncovered_ty_params.uncovered {
37 let ident = tcx.item_ident(param_def_id);
38
39 tcx.emit_node_span_lint(
40 UNCOVERED_PARAM_IN_PROJECTION,
41 hir_id,
42 ident.span,
43 diagnostics::UncoveredTyParam {
44 param: ident,
45 local_ty: uncovered_ty_params.local_ty,
46 },
47 );
48 }
49 }
50 OrphanCheckErr::NonLocalInputType(_) => {
51 bug!("orphanck: shouldn't've gotten non-local input tys in compat mode")
52 }
53 },
54 Err(err) => return Err(emit_orphan_check_error(tcx, trait_ref, impl_def_id, err)),
55 },
56 }
57
58 let trait_def_id = trait_ref.def_id;
59
60 debug!(
93 "trait_ref={:?} trait_def_id={:?} trait_is_auto={}",
94 trait_ref,
95 trait_def_id,
96 tcx.trait_is_auto(trait_def_id)
97 );
98
99 if tcx.trait_is_auto(trait_def_id) {
100 let self_ty = tcx.expand_free_alias_tys(trait_ref.self_ty());
101
102 enum LocalImpl {
138 Allow,
139 Disallow { problematic_kind: &'static str },
140 }
141
142 enum NonlocalImpl {
150 Allow,
151 DisallowBecauseNonlocal,
152 DisallowOther,
153 }
154
155 let (local_impl, nonlocal_impl) = match self_ty.kind() {
158 ty::Adt(self_def, _) => (
161 LocalImpl::Allow,
162 if self_def.did().is_local() {
163 NonlocalImpl::Allow
164 } else {
165 NonlocalImpl::DisallowBecauseNonlocal
166 },
167 ),
168
169 ty::Foreign(did) => (
172 LocalImpl::Allow,
173 if did.is_local() {
174 NonlocalImpl::Allow
175 } else {
176 NonlocalImpl::DisallowBecauseNonlocal
177 },
178 ),
179
180 ty::Dynamic(..) => (
182 LocalImpl::Disallow { problematic_kind: "trait object" },
183 NonlocalImpl::DisallowOther,
184 ),
185
186 ty::Param(..) => (
189 if self_ty.is_sized(tcx, ty::TypingEnv::non_body_analysis(tcx, impl_def_id)) {
190 LocalImpl::Allow
191 } else {
192 LocalImpl::Disallow { problematic_kind: "generic type" }
193 },
194 NonlocalImpl::DisallowOther,
195 ),
196
197 ty::Alias(_, ty::AliasTy { kind, .. }) => {
198 let problematic_kind = match kind {
199 ty::Projection { .. } => "associated type",
205 ty::Free { .. } => "type alias",
208 ty::Opaque { .. } => "opaque type",
211 ty::Inherent { .. } => "associated type",
220 };
221 (LocalImpl::Disallow { problematic_kind }, NonlocalImpl::DisallowOther)
222 }
223
224 ty::Bool
225 | ty::Pat(..)
226 | ty::Char
227 | ty::Int(..)
228 | ty::Uint(..)
229 | ty::Float(..)
230 | ty::Str
231 | ty::Array(..)
232 | ty::Slice(..)
233 | ty::RawPtr(..)
234 | ty::Ref(..)
235 | ty::FnPtr(..)
236 | ty::Never
237 | ty::Tuple(..)
238 | ty::UnsafeBinder(_) => (LocalImpl::Allow, NonlocalImpl::DisallowOther),
239
240 ty::FnDef(..)
241 | ty::Closure(..)
242 | ty::CoroutineClosure(..)
243 | ty::Coroutine(..)
244 | ty::CoroutineWitness(..)
245 | ty::Bound(..)
246 | ty::Placeholder(..)
247 | ty::Infer(..) => {
248 let sp = tcx.def_span(impl_def_id);
249 span_bug!(sp, "weird self type for autotrait impl")
250 }
251
252 ty::Error(..) => (LocalImpl::Allow, NonlocalImpl::Allow),
253
254 ty::View(..) => unimplemented!(),
256 };
257
258 if trait_def_id.is_local() {
259 match local_impl {
260 LocalImpl::Allow => {}
261 LocalImpl::Disallow { problematic_kind } => {
262 return Err(tcx.dcx().emit_err(diagnostics::TraitsWithDefaultImpl {
263 span: tcx.def_span(impl_def_id),
264 traits: tcx.def_path_str(trait_def_id),
265 problematic_kind,
266 self_ty,
267 }));
268 }
269 }
270 } else {
271 match nonlocal_impl {
272 NonlocalImpl::Allow => {}
273 NonlocalImpl::DisallowBecauseNonlocal => {
274 return Err(tcx.dcx().emit_err(diagnostics::CrossCrateTraitsDefined {
275 span: tcx.def_span(impl_def_id),
276 traits: tcx.def_path_str(trait_def_id),
277 }));
278 }
279 NonlocalImpl::DisallowOther => {
280 return Err(tcx.dcx().emit_err(diagnostics::CrossCrateTraits {
281 span: tcx.def_span(impl_def_id),
282 traits: tcx.def_path_str(trait_def_id),
283 self_ty,
284 }));
285 }
286 }
287 }
288 }
289
290 Ok(())
291}
292
293{}
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("orphan_check",
"rustc_hir_analysis::coherence::orphan",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/064405c5f7fbc7d41aa30b45da1e45a85777bd58/compiler/rustc_hir_analysis/src/coherence/orphan.rs"),
::tracing_core::__macro_support::Option::Some(301u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::orphan"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("impl_def_id")
}> =
::tracing::__macro_support::FieldName::new("impl_def_id");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("mode")
}> =
::tracing::__macro_support::FieldName::new("mode");
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(&impl_def_id)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&mode)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[allow(clippy :: redundant_closure_call)]
let x =
(move ||
{
#[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<(),
OrphanCheckErr<TyCtxt<'tcx>, FxIndexSet<DefId>>> = loop {};
return __tracing_attr_fake_return;
}
{
let trait_ref = tcx.impl_trait_ref(impl_def_id);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/064405c5f7fbc7d41aa30b45da1e45a85777bd58/compiler/rustc_hir_analysis/src/coherence/orphan.rs:310",
"rustc_hir_analysis::coherence::orphan",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/064405c5f7fbc7d41aa30b45da1e45a85777bd58/compiler/rustc_hir_analysis/src/coherence/orphan.rs"),
::tracing_core::__macro_support::Option::Some(310u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::orphan"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("trait_ref")
}> =
::tracing::__macro_support::FieldName::new("trait_ref");
NAME.as_str()
}], ::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(&::tracing::field::debug(&trait_ref.skip_binder())
as &dyn ::tracing::field::Value))])
});
} else { ; }
};
if let Some(def_id) =
trait_ref.skip_binder().def_id.as_local() {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/064405c5f7fbc7d41aa30b45da1e45a85777bd58/compiler/rustc_hir_analysis/src/coherence/orphan.rs:314",
"rustc_hir_analysis::coherence::orphan",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/064405c5f7fbc7d41aa30b45da1e45a85777bd58/compiler/rustc_hir_analysis/src/coherence/orphan.rs"),
::tracing_core::__macro_support::Option::Some(314u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::orphan"),
::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!("trait {0:?} is local to current crate",
def_id) as &dyn ::tracing::field::Value))])
});
} else { ; }
};
return Ok(());
}
let infcx =
tcx.infer_ctxt().with_next_trait_solver(true).enable_next_solver_overflow_fcw(false).build(TypingMode::Coherence);
let cause = traits::ObligationCause::dummy();
let args =
infcx.fresh_args_for_item(cause.span,
impl_def_id.to_def_id());
let trait_ref =
trait_ref.instantiate(tcx, args).skip_norm_wip();
let lazily_normalize_ty =
|user_ty: Ty<'tcx>|
{
let ty::Alias(..) =
user_ty.kind() else { return Ok(user_ty) };
let ocx = traits::ObligationCtxt::new(&infcx);
let ty =
ocx.normalize(&cause, ty::ParamEnv::empty(),
Unnormalized::new_wip(user_ty));
let ty = infcx.deeply_resolve_ignoring_regions(ty);
let errors = ocx.try_evaluate_obligations();
if !errors.no_errors() { return Ok(user_ty); }
Ok::<_, !>(ty)
};
let result =
traits::orphan_check_trait_ref(&infcx, trait_ref,
traits::InCrate::Local { mode },
lazily_normalize_ty).into_ok();
result.map_err(|err|
match err {
OrphanCheckErr::UncoveredTyParams(UncoveredTyParams {
uncovered, local_ty }) => {
let mut collector =
UncoveredTyParamCollector {
infcx: &infcx,
uncovered_params: Default::default(),
};
uncovered.visit_with(&mut collector);
if true {
if !!collector.uncovered_params.is_empty() {
::core::panicking::panic("assertion failed: !collector.uncovered_params.is_empty()")
};
};
OrphanCheckErr::UncoveredTyParams(UncoveredTyParams {
uncovered: collector.uncovered_params,
local_ty,
})
}
OrphanCheckErr::NonLocalInputType(tys) => {
let tys =
infcx.probe(|_|
{
for (arg, id_arg) in
std::iter::zip(args,
ty::GenericArgs::identity_for_item(tcx, impl_def_id)) {
let _ =
infcx.eq_raw(&cause, ty::ParamEnv::empty(),
DefineOpaqueTypes::No, arg, id_arg);
}
infcx.deeply_resolve_ignoring_regions(tys)
});
OrphanCheckErr::NonLocalInputType(tys)
}
})
}
})();
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/064405c5f7fbc7d41aa30b45da1e45a85777bd58/compiler/rustc_hir_analysis/src/coherence/orphan.rs:301",
"rustc_hir_analysis::coherence::orphan",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/064405c5f7fbc7d41aa30b45da1e45a85777bd58/compiler/rustc_hir_analysis/src/coherence/orphan.rs"),
::tracing_core::__macro_support::Option::Some(301u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::orphan"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("return")
}> =
::tracing::__macro_support::FieldName::new("return");
NAME.as_str()
}], ::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(&::tracing::field::debug(&x)
as &dyn ::tracing::field::Value))])
});
} else { ; }
};
x;#[instrument(level = "debug", skip(tcx), ret)]
302fn orphan_check<'tcx>(
303 tcx: TyCtxt<'tcx>,
304 impl_def_id: LocalDefId,
305 mode: OrphanCheckMode,
306) -> Result<(), OrphanCheckErr<TyCtxt<'tcx>, FxIndexSet<DefId>>> {
307 let trait_ref = tcx.impl_trait_ref(impl_def_id);
310 debug!(trait_ref = ?trait_ref.skip_binder());
311
312 if let Some(def_id) = trait_ref.skip_binder().def_id.as_local() {
314 debug!("trait {def_id:?} is local to current crate");
315 return Ok(());
316 }
317
318 let infcx = tcx
320 .infer_ctxt()
321 .with_next_trait_solver(true)
322 .enable_next_solver_overflow_fcw(false)
323 .build(TypingMode::Coherence);
324 let cause = traits::ObligationCause::dummy();
325 let args = infcx.fresh_args_for_item(cause.span, impl_def_id.to_def_id());
326 let trait_ref = trait_ref.instantiate(tcx, args).skip_norm_wip();
327
328 let lazily_normalize_ty = |user_ty: Ty<'tcx>| {
329 let ty::Alias(..) = user_ty.kind() else { return Ok(user_ty) };
330
331 let ocx = traits::ObligationCtxt::new(&infcx);
332 let ty = ocx.normalize(&cause, ty::ParamEnv::empty(), Unnormalized::new_wip(user_ty));
333 let ty = infcx.deeply_resolve_ignoring_regions(ty);
334 let errors = ocx.try_evaluate_obligations();
335 if !errors.no_errors() {
336 return Ok(user_ty);
337 }
338
339 Ok::<_, !>(ty)
340 };
341
342 let result = traits::orphan_check_trait_ref(
343 &infcx,
344 trait_ref,
345 traits::InCrate::Local { mode },
346 lazily_normalize_ty,
347 )
348 .into_ok();
349
350 result.map_err(|err| match err {
352 OrphanCheckErr::UncoveredTyParams(UncoveredTyParams { uncovered, local_ty }) => {
353 let mut collector =
354 UncoveredTyParamCollector { infcx: &infcx, uncovered_params: Default::default() };
355 uncovered.visit_with(&mut collector);
356 debug_assert!(!collector.uncovered_params.is_empty());
358
359 OrphanCheckErr::UncoveredTyParams(UncoveredTyParams {
360 uncovered: collector.uncovered_params,
361 local_ty,
362 })
363 }
364 OrphanCheckErr::NonLocalInputType(tys) => {
365 let tys = infcx.probe(|_| {
366 for (arg, id_arg) in
369 std::iter::zip(args, ty::GenericArgs::identity_for_item(tcx, impl_def_id))
370 {
371 let _ = infcx.eq_raw(
372 &cause,
373 ty::ParamEnv::empty(),
374 DefineOpaqueTypes::No,
375 arg,
376 id_arg,
377 );
378 }
379 infcx.deeply_resolve_ignoring_regions(tys)
380 });
381 OrphanCheckErr::NonLocalInputType(tys)
382 }
383 })
384}
385
386fn emit_orphan_check_error<'tcx>(
387 tcx: TyCtxt<'tcx>,
388 trait_ref: ty::TraitRef<'tcx>,
389 impl_def_id: LocalDefId,
390 err: traits::OrphanCheckErr<TyCtxt<'tcx>, FxIndexSet<DefId>>,
391) -> ErrorGuaranteed {
392 match err {
393 traits::OrphanCheckErr::NonLocalInputType(tys) => {
394 let item = tcx.hir_expect_item(impl_def_id);
395 let impl_ = item.expect_impl();
396 let of_trait = impl_.of_trait.unwrap();
397
398 let span = tcx.def_span(impl_def_id);
399 let mut diag = tcx.dcx().create_err(match trait_ref.self_ty().kind() {
400 ty::Adt(..) => diagnostics::OnlyCurrentTraits::Outside { span, note: () },
401 _ if trait_ref.self_ty().is_primitive() => {
402 diagnostics::OnlyCurrentTraits::Primitive { span, note: () }
403 }
404 _ => diagnostics::OnlyCurrentTraits::Arbitrary { span, note: () },
405 });
406
407 for &(mut ty, is_target_ty) in &tys {
408 let span = if #[allow(non_exhaustive_omitted_patterns)] match is_target_ty {
IsFirstInputType::Yes => true,
_ => false,
}matches!(is_target_ty, IsFirstInputType::Yes) {
409 impl_.self_ty.span
411 } else {
412 of_trait.trait_ref.path.span
414 };
415
416 ty = tcx.erase_and_anonymize_regions(ty);
417
418 let is_foreign =
419 !trait_ref.def_id.is_local() && #[allow(non_exhaustive_omitted_patterns)] match is_target_ty {
IsFirstInputType::No => true,
_ => false,
}matches!(is_target_ty, IsFirstInputType::No);
420
421 match *ty.kind() {
422 ty::Slice(_) => {
423 if is_foreign {
424 diag.subdiagnostic(diagnostics::OnlyCurrentTraitsForeign { span });
425 } else {
426 diag.subdiagnostic(diagnostics::OnlyCurrentTraitsName {
427 span,
428 name: "slices",
429 });
430 }
431 }
432 ty::Array(..) => {
433 if is_foreign {
434 diag.subdiagnostic(diagnostics::OnlyCurrentTraitsForeign { span });
435 } else {
436 diag.subdiagnostic(diagnostics::OnlyCurrentTraitsName {
437 span,
438 name: "arrays",
439 });
440 }
441 }
442 ty::Tuple(..) => {
443 if is_foreign {
444 diag.subdiagnostic(diagnostics::OnlyCurrentTraitsForeign { span });
445 } else {
446 diag.subdiagnostic(diagnostics::OnlyCurrentTraitsName {
447 span,
448 name: "tuples",
449 });
450 }
451 }
452 ty::Alias(_, ty::AliasTy { kind: ty::Opaque { .. }, .. }) => {
453 diag.subdiagnostic(diagnostics::OnlyCurrentTraitsOpaque { span });
454 }
455 ty::RawPtr(ptr_ty, mutbl) => {
456 if !trait_ref.self_ty().has_param() {
457 diag.subdiagnostic(diagnostics::OnlyCurrentTraitsPointerSugg {
458 wrapper_span: impl_.self_ty.span,
459 struct_span: item.span.shrink_to_lo(),
460 mut_key: mutbl.prefix_str(),
461 ptr_ty,
462 });
463 }
464 diag.subdiagnostic(diagnostics::OnlyCurrentTraitsPointer {
465 span,
466 pointer: ty,
467 });
468 }
469 ty::Adt(adt_def, _) => {
470 if is_foreign {
471 diag.subdiagnostic(diagnostics::OnlyCurrentTraitsForeign { span });
472 } else {
473 diag.subdiagnostic(diagnostics::OnlyCurrentTraitsAdt {
474 span,
475 name: tcx.def_path_str(adt_def.did()),
476 });
477 }
478 }
479 _ => {
480 if is_foreign {
481 diag.subdiagnostic(diagnostics::OnlyCurrentTraitsForeign { span });
482 } else {
483 diag.subdiagnostic(diagnostics::OnlyCurrentTraitsTy { span, ty });
484 }
485 }
486 }
487 }
488
489 diag.emit_err()
490 }
491 traits::OrphanCheckErr::UncoveredTyParams(UncoveredTyParams { uncovered, local_ty }) => {
492 let mut guar = None;
493 for param_def_id in uncovered {
494 guar.get_or_insert(tcx.dcx().emit_err(diagnostics::UncoveredTyParam {
495 param: tcx.item_ident(param_def_id),
496 local_ty,
497 }));
498 }
499 guar.unwrap()
500 }
501 }
502}
503
504struct UncoveredTyParamCollector<'cx, 'tcx> {
505 infcx: &'cx InferCtxt<'tcx>,
506 uncovered_params: FxIndexSet<DefId>,
507}
508
509impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for UncoveredTyParamCollector<'_, 'tcx> {
510 fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
511 if !ty.has_type_flags(ty::TypeFlags::HAS_TY_INFER) {
512 return;
513 }
514 let ty::Infer(ty::TyVar(vid)) = *ty.kind() else {
515 return ty.super_visit_with(self);
516 };
517 let origin = self.infcx.type_var_origin(vid);
518 if let Some(def_id) = origin.param_def_id {
519 self.uncovered_params.insert(def_id);
520 }
521 }
522
523 fn visit_const(&mut self, ct: ty::Const<'tcx>) -> Self::Result {
524 if ct.has_type_flags(ty::TypeFlags::HAS_TY_INFER) {
525 ct.super_visit_with(self)
526 }
527 }
528}