Skip to main content

rustc_hir_analysis/coherence/
builtin.rs

1//! Check properties that are required by built-in traits and set
2//! up data structures required by type-checking/codegen.
3
4use std::collections::BTreeMap;
5
6use rustc_data_structures::fx::FxHashSet;
7use rustc_errors::{ErrorGuaranteed, MultiSpan};
8use rustc_hir as hir;
9use rustc_hir::ItemKind;
10use rustc_hir::def_id::{DefId, LocalDefId};
11use rustc_hir::lang_items::LangItem;
12use rustc_infer::infer::{self, InferCtxt, RegionResolutionError, SubregionOrigin, TyCtxtInferExt};
13use rustc_infer::traits::{Obligation, PredicateObligations};
14use rustc_middle::ty::adjustment::CoerceUnsizedInfo;
15use rustc_middle::ty::print::PrintTraitRefExt as _;
16use rustc_middle::ty::relate::solver_relating::RelateExt;
17use rustc_middle::ty::{
18    self, Ty, TyCtxt, TypeVisitableExt, TypingMode, Unnormalized, suggest_constraining_type_params,
19};
20use rustc_span::{DUMMY_SP, Span, sym};
21use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
22use rustc_trait_selection::traits::misc::{
23    ConstParamTyImplementationError, CopyImplementationError, InfringingFieldsReason,
24    type_allowed_to_implement_const_param_ty, type_allowed_to_implement_copy,
25};
26use rustc_trait_selection::traits::{self, FulfillmentError, ObligationCause, ObligationCtxt};
27use tracing::debug;
28
29use crate::errors;
30
31pub(super) fn check_trait<'tcx>(
32    tcx: TyCtxt<'tcx>,
33    trait_def_id: DefId,
34    impl_def_id: LocalDefId,
35    impl_header: ty::ImplTraitHeader<'tcx>,
36) -> Result<(), ErrorGuaranteed> {
37    let lang_items = tcx.lang_items();
38    let checker = Checker { tcx, trait_def_id, impl_def_id, impl_header };
39    checker.check(lang_items.drop_trait(), visit_implementation_of_drop)?;
40    checker.check(lang_items.async_drop_trait(), visit_implementation_of_drop)?;
41    checker.check(lang_items.copy_trait(), visit_implementation_of_copy)?;
42    checker.check(lang_items.unpin_trait(), visit_implementation_of_unpin)?;
43    checker.check(lang_items.const_param_ty_trait(), |checker| {
44        visit_implementation_of_const_param_ty(checker)
45    })?;
46    checker.check(lang_items.coerce_unsized_trait(), visit_implementation_of_coerce_unsized)?;
47    checker.check(lang_items.reborrow(), visit_implementation_of_reborrow)?;
48    checker.check(lang_items.coerce_shared(), visit_implementation_of_coerce_shared)?;
49    checker
50        .check(lang_items.dispatch_from_dyn_trait(), visit_implementation_of_dispatch_from_dyn)?;
51    checker.check(
52        lang_items.coerce_pointee_validated_trait(),
53        visit_implementation_of_coerce_pointee_validity,
54    )?;
55    Ok(())
56}
57
58struct Checker<'tcx> {
59    tcx: TyCtxt<'tcx>,
60    trait_def_id: DefId,
61    impl_def_id: LocalDefId,
62    impl_header: ty::ImplTraitHeader<'tcx>,
63}
64
65impl<'tcx> Checker<'tcx> {
66    fn check(
67        &self,
68        trait_def_id: Option<DefId>,
69        f: impl FnOnce(&Self) -> Result<(), ErrorGuaranteed>,
70    ) -> Result<(), ErrorGuaranteed> {
71        if Some(self.trait_def_id) == trait_def_id { f(self) } else { Ok(()) }
72    }
73}
74
75fn visit_implementation_of_drop(checker: &Checker<'_>) -> Result<(), ErrorGuaranteed> {
76    let tcx = checker.tcx;
77    let impl_did = checker.impl_def_id;
78    // Destructors only work on local ADT types.
79    match checker.impl_header.trait_ref.instantiate_identity().skip_norm_wip().self_ty().kind() {
80        ty::Adt(def, _) if def.did().is_local() => return Ok(()),
81        ty::Error(_) => return Ok(()),
82        _ => {}
83    }
84
85    let impl_ = tcx.hir_expect_item(impl_did).expect_impl();
86
87    Err(tcx.dcx().emit_err(errors::DropImplOnWrongItem {
88        span: impl_.self_ty.span,
89        trait_: tcx.item_name(checker.impl_header.trait_ref.skip_binder().def_id),
90    }))
91}
92
93fn visit_implementation_of_copy(checker: &Checker<'_>) -> Result<(), ErrorGuaranteed> {
94    let tcx = checker.tcx;
95    let impl_header = checker.impl_header;
96    let impl_did = checker.impl_def_id;
97    {
    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/coherence/builtin.rs:97",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(97u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::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};
                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(&format_args!("visit_implementation_of_copy: impl_did={0:?}",
                                                    impl_did) as &dyn Value))])
            });
    } else { ; }
};debug!("visit_implementation_of_copy: impl_did={:?}", impl_did);
98
99    let self_type = impl_header.trait_ref.instantiate_identity().skip_norm_wip().self_ty();
100    {
    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/coherence/builtin.rs:100",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(100u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::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};
                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(&format_args!("visit_implementation_of_copy: self_type={0:?} (bound)",
                                                    self_type) as &dyn Value))])
            });
    } else { ; }
};debug!("visit_implementation_of_copy: self_type={:?} (bound)", self_type);
101
102    let param_env = tcx.param_env(impl_did);
103    if !!self_type.has_escaping_bound_vars() {
    ::core::panicking::panic("assertion failed: !self_type.has_escaping_bound_vars()")
};assert!(!self_type.has_escaping_bound_vars());
104
105    {
    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/coherence/builtin.rs:105",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(105u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::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};
                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(&format_args!("visit_implementation_of_copy: self_type={0:?} (free)",
                                                    self_type) as &dyn Value))])
            });
    } else { ; }
};debug!("visit_implementation_of_copy: self_type={:?} (free)", self_type);
106
107    if let ty::ImplPolarity::Negative = impl_header.polarity {
108        return Ok(());
109    }
110
111    let cause = traits::ObligationCause::misc(DUMMY_SP, impl_did);
112    match type_allowed_to_implement_copy(tcx, param_env, self_type, cause, impl_header.safety) {
113        Ok(()) => Ok(()),
114        Err(CopyImplementationError::InfringingFields(fields)) => {
115            let span = tcx.hir_expect_item(impl_did).expect_impl().self_ty.span;
116            Err(infringing_fields_error(
117                tcx,
118                fields.into_iter().map(|(field, ty, reason)| (tcx.def_span(field.did), ty, reason)),
119                LangItem::Copy,
120                impl_did,
121                span,
122            ))
123        }
124        Err(CopyImplementationError::NotAnAdt) => {
125            let span = tcx.hir_expect_item(impl_did).expect_impl().self_ty.span;
126            Err(tcx.dcx().emit_err(errors::CopyImplOnNonAdt { span }))
127        }
128        Err(CopyImplementationError::HasDestructor(did)) => {
129            let span = tcx.hir_expect_item(impl_did).expect_impl().self_ty.span;
130            let impl_ = tcx.def_span(did);
131            Err(tcx.dcx().emit_err(errors::CopyImplOnTypeWithDtor { span, impl_ }))
132        }
133        Err(CopyImplementationError::HasUnsafeFields) => {
134            let span = tcx.hir_expect_item(impl_did).expect_impl().self_ty.span;
135            Err(tcx
136                .dcx()
137                .span_delayed_bug(span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("cannot implement `Copy` for `{0}`",
                self_type))
    })format!("cannot implement `Copy` for `{}`", self_type)))
138        }
139    }
140}
141
142fn visit_implementation_of_unpin(checker: &Checker<'_>) -> Result<(), ErrorGuaranteed> {
143    let tcx = checker.tcx;
144    let impl_header = checker.impl_header;
145    let impl_did = checker.impl_def_id;
146    {
    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/coherence/builtin.rs:146",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(146u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::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};
                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(&format_args!("visit_implementation_of_unpin: impl_did={0:?}",
                                                    impl_did) as &dyn Value))])
            });
    } else { ; }
};debug!("visit_implementation_of_unpin: impl_did={:?}", impl_did);
147
148    let self_type = impl_header.trait_ref.instantiate_identity().skip_norm_wip().self_ty();
149    {
    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/coherence/builtin.rs:149",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(149u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::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};
                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(&format_args!("visit_implementation_of_unpin: self_type={0:?}",
                                                    self_type) as &dyn Value))])
            });
    } else { ; }
};debug!("visit_implementation_of_unpin: self_type={:?}", self_type);
150
151    let span = tcx.def_span(impl_did);
152
153    if tcx.features().pin_ergonomics() {
154        match self_type.kind() {
155            // Soundness concerns: a type `T` annotated with `#[pin_v2]` is allowed to project
156            // `Pin<&mut T>` to its field `Pin<&mut U>` safely (even if `U: !Unpin`).
157            // If `T` is allowed to impl `Unpin` manually (note that `Unpin` is a safe trait,
158            // which cannot carry safety properties), then `&mut U` could be obtained from
159            // `&mut T` that dereferenced by `Pin<&mut T>`, which breaks the safety contract of
160            // `Pin<&mut U>` for `U: !Unpin`.
161            ty::Adt(adt, _) if adt.is_pin_project() => {
162                return Err(tcx.dcx().emit_err(crate::errors::ImplUnpinForPinProjectedType {
163                    span,
164                    adt_span: tcx.def_span(adt.did()),
165                    adt_name: tcx.item_name(adt.did()),
166                }));
167            }
168            ty::Adt(_, _) => {}
169            _ => {
170                return Err(tcx.dcx().span_delayed_bug(span, "impl of `Unpin` for a non-adt type"));
171            }
172        };
173    }
174    Ok(())
175}
176
177fn visit_implementation_of_const_param_ty(checker: &Checker<'_>) -> Result<(), ErrorGuaranteed> {
178    let tcx = checker.tcx;
179    let header = checker.impl_header;
180    let impl_did = checker.impl_def_id;
181    let self_type = header.trait_ref.instantiate_identity().skip_norm_wip().self_ty();
182    if !!self_type.has_escaping_bound_vars() {
    ::core::panicking::panic("assertion failed: !self_type.has_escaping_bound_vars()")
};assert!(!self_type.has_escaping_bound_vars());
183
184    let param_env = tcx.param_env(impl_did);
185
186    if let ty::ImplPolarity::Negative | ty::ImplPolarity::Reservation = header.polarity {
187        return Ok(());
188    }
189
190    if tcx.features().const_param_ty_unchecked() {
191        return Ok(());
192    }
193
194    if !tcx.features().adt_const_params() {
195        match *self_type.kind() {
196            ty::Adt(adt, _) if adt.is_struct() => {
197                let struct_vis = tcx.visibility(adt.did());
198                for variant in adt.variants() {
199                    for field in &variant.fields {
200                        if struct_vis.greater_than(field.vis, tcx) {
201                            let span = tcx.hir_expect_item(impl_did).expect_impl().self_ty.span;
202                            return Err(tcx
203                                .dcx()
204                                .emit_err(errors::ConstParamTyFieldVisMismatch { span }));
205                        }
206                    }
207                }
208            }
209
210            _ => {}
211        }
212    }
213
214    let cause = traits::ObligationCause::misc(DUMMY_SP, impl_did);
215    match type_allowed_to_implement_const_param_ty(tcx, param_env, self_type, cause) {
216        Ok(()) => Ok(()),
217        Err(ConstParamTyImplementationError::InfrigingFields(fields)) => {
218            let span = tcx.hir_expect_item(impl_did).expect_impl().self_ty.span;
219            Err(infringing_fields_error(
220                tcx,
221                fields.into_iter().map(|(field, ty, reason)| (tcx.def_span(field.did), ty, reason)),
222                LangItem::ConstParamTy,
223                impl_did,
224                span,
225            ))
226        }
227        Err(ConstParamTyImplementationError::NotAnAdtOrBuiltinAllowed) => {
228            let span = tcx.hir_expect_item(impl_did).expect_impl().self_ty.span;
229            Err(tcx.dcx().emit_err(errors::ConstParamTyImplOnNonAdt { span }))
230        }
231        Err(ConstParamTyImplementationError::NonExhaustive(attr_span)) => {
232            let defn_span = tcx.hir_expect_item(impl_did).expect_impl().self_ty.span;
233            Err(tcx
234                .dcx()
235                .emit_err(errors::ConstParamTyImplOnNonExhaustive { defn_span, attr_span }))
236        }
237        Err(ConstParamTyImplementationError::InvalidInnerTyOfBuiltinTy(infringing_tys)) => {
238            let span = tcx.hir_expect_item(impl_did).expect_impl().self_ty.span;
239            Err(infringing_fields_error(
240                tcx,
241                infringing_tys.into_iter().map(|(ty, reason)| (span, ty, reason)),
242                LangItem::ConstParamTy,
243                impl_did,
244                span,
245            ))
246        }
247        Err(ConstParamTyImplementationError::UnsizedConstParamsFeatureRequired) => {
248            let span = tcx.hir_expect_item(impl_did).expect_impl().self_ty.span;
249            Err(tcx.dcx().emit_err(errors::ConstParamTyImplOnUnsized { span }))
250        }
251    }
252}
253
254fn visit_implementation_of_coerce_unsized(checker: &Checker<'_>) -> Result<(), ErrorGuaranteed> {
255    let tcx = checker.tcx;
256    let impl_did = checker.impl_def_id;
257    {
    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/coherence/builtin.rs:257",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(257u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::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};
                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(&format_args!("visit_implementation_of_coerce_unsized: impl_did={0:?}",
                                                    impl_did) as &dyn Value))])
            });
    } else { ; }
};debug!("visit_implementation_of_coerce_unsized: impl_did={:?}", impl_did);
258
259    // Just compute this for the side-effects, in particular reporting
260    // errors; other parts of the code may demand it for the info of
261    // course.
262    tcx.ensure_result().coerce_unsized_info(impl_did)
263}
264
265fn visit_implementation_of_reborrow(checker: &Checker<'_>) -> Result<(), ErrorGuaranteed> {
266    let tcx = checker.tcx;
267    let impl_did = checker.impl_def_id;
268    {
    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/coherence/builtin.rs:268",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(268u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::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};
                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(&format_args!("visit_implementation_of_reborrow: impl_did={0:?}",
                                                    impl_did) as &dyn Value))])
            });
    } else { ; }
};debug!("visit_implementation_of_reborrow: impl_did={:?}", impl_did);
269
270    // Just compute this for the side-effects, in particular reporting
271    // errors; other parts of the code may demand it for the info of
272    // course.
273    reborrow_info(tcx, impl_did)
274}
275
276fn visit_implementation_of_coerce_shared(checker: &Checker<'_>) -> Result<(), ErrorGuaranteed> {
277    let tcx = checker.tcx;
278    let impl_did = checker.impl_def_id;
279    {
    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/coherence/builtin.rs:279",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(279u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::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};
                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(&format_args!("visit_implementation_of_coerce_shared: impl_did={0:?}",
                                                    impl_did) as &dyn Value))])
            });
    } else { ; }
};debug!("visit_implementation_of_coerce_shared: impl_did={:?}", impl_did);
280
281    // Just compute this for the side-effects, in particular reporting
282    // errors; other parts of the code may demand it for the info of
283    // course.
284    coerce_shared_info(tcx, impl_did)
285}
286
287fn is_from_coerce_pointee_derive(tcx: TyCtxt<'_>, span: Span) -> bool {
288    span.ctxt()
289        .outer_expn_data()
290        .macro_def_id
291        .is_some_and(|def_id| tcx.is_diagnostic_item(sym::CoercePointee, def_id))
292}
293
294fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<(), ErrorGuaranteed> {
295    let tcx = checker.tcx;
296    let impl_did = checker.impl_def_id;
297    let trait_ref = checker.impl_header.trait_ref.instantiate_identity().skip_norm_wip();
298    {
    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/coherence/builtin.rs:298",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(298u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::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};
                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(&format_args!("visit_implementation_of_dispatch_from_dyn: impl_did={0:?}",
                                                    impl_did) as &dyn Value))])
            });
    } else { ; }
};debug!("visit_implementation_of_dispatch_from_dyn: impl_did={:?}", impl_did);
299
300    let span = tcx.def_span(impl_did);
301    let trait_name = "DispatchFromDyn";
302
303    let source = trait_ref.self_ty();
304    let target = {
305        if !tcx.is_lang_item(trait_ref.def_id, LangItem::DispatchFromDyn) {
    ::core::panicking::panic("assertion failed: tcx.is_lang_item(trait_ref.def_id, LangItem::DispatchFromDyn)")
};assert!(tcx.is_lang_item(trait_ref.def_id, LangItem::DispatchFromDyn));
306
307        trait_ref.args.type_at(1)
308    };
309
310    // Check `CoercePointee` impl is WF -- if not, then there's no reason to report
311    // redundant errors for `DispatchFromDyn`. This is best effort, though.
312    let mut res = Ok(());
313    tcx.for_each_relevant_impl(
314        tcx.require_lang_item(LangItem::CoerceUnsized, span),
315        source,
316        |impl_def_id| {
317            res = res.and(tcx.ensure_result().coerce_unsized_info(impl_def_id));
318        },
319    );
320    res?;
321
322    {
    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/coherence/builtin.rs:322",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(322u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::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};
                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(&format_args!("visit_implementation_of_dispatch_from_dyn: {0:?} -> {1:?}",
                                                    source, target) as &dyn Value))])
            });
    } else { ; }
};debug!("visit_implementation_of_dispatch_from_dyn: {:?} -> {:?}", source, target);
323
324    let param_env = tcx.param_env(impl_did);
325
326    let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
327    let cause = ObligationCause::misc(span, impl_did);
328
329    // Later parts of the compiler rely on all DispatchFromDyn types to be ABI-compatible with raw
330    // pointers. This is enforced here: we only allow impls for references, raw pointers, and things
331    // that are effectively repr(transparent) newtypes around types that already hav a
332    // DispatchedFromDyn impl. We cannot literally use repr(transparent) on those types since some
333    // of them support an allocator, but we ensure that for the cases where the type implements this
334    // trait, they *do* satisfy the repr(transparent) rules, and then we assume that everything else
335    // in the compiler (in particular, all the call ABI logic) will treat them as repr(transparent)
336    // even if they do not carry that attribute.
337    match (source.kind(), target.kind()) {
338        (&ty::Pat(_, pat_a), &ty::Pat(_, pat_b)) => {
339            if pat_a != pat_b {
340                return Err(tcx.dcx().emit_err(errors::CoerceSamePatKind {
341                    span,
342                    trait_name,
343                    pat_a: pat_a.to_string(),
344                    pat_b: pat_b.to_string(),
345                }));
346            }
347            Ok(())
348        }
349
350        (&ty::Ref(r_a, _, mutbl_a), ty::Ref(r_b, _, mutbl_b))
351            if r_a == *r_b && mutbl_a == *mutbl_b =>
352        {
353            Ok(())
354        }
355        (&ty::RawPtr(_, a_mutbl), &ty::RawPtr(_, b_mutbl)) if a_mutbl == b_mutbl => Ok(()),
356        (&ty::Adt(def_a, args_a), &ty::Adt(def_b, args_b))
357            if def_a.is_struct() && def_b.is_struct() =>
358        {
359            if def_a != def_b {
360                let source_path = tcx.def_path_str(def_a.did());
361                let target_path = tcx.def_path_str(def_b.did());
362                return Err(tcx.dcx().emit_err(errors::CoerceSameStruct {
363                    span,
364                    trait_name,
365                    note: true,
366                    source_path,
367                    target_path,
368                }));
369            }
370
371            if def_a.repr().c() || def_a.repr().packed() {
372                return Err(tcx.dcx().emit_err(errors::DispatchFromDynRepr { span }));
373            }
374
375            let fields = &def_a.non_enum_variant().fields;
376
377            let mut res = Ok(());
378            let coerced_fields = fields
379                .iter_enumerated()
380                .filter_map(|(i, field)| {
381                    // Ignore PhantomData fields
382                    let unnormalized_ty = tcx.type_of(field.did).instantiate_identity();
383                    if tcx
384                        .try_normalize_erasing_regions(
385                            ty::TypingEnv::non_body_analysis(tcx, def_a.did()),
386                            unnormalized_ty,
387                        )
388                        .unwrap_or(unnormalized_ty.skip_norm_wip())
389                        .is_phantom_data()
390                    {
391                        return None;
392                    }
393
394                    let ty_a = field.ty(tcx, args_a).skip_norm_wip();
395                    let ty_b = field.ty(tcx, args_b).skip_norm_wip();
396
397                    // FIXME: We could do normalization here, but is it really worth it?
398                    if ty_a == ty_b {
399                        // Allow 1-ZSTs that don't mention type params.
400                        //
401                        // Allowing type params here would allow us to possibly transmute
402                        // between ZSTs, which may be used to create library unsoundness.
403                        if let Ok(layout) =
404                            tcx.layout_of(infcx.typing_env(param_env).as_query_input(ty_a))
405                            && layout.is_1zst()
406                            && !ty_a.has_non_region_param()
407                        {
408                            // ignore 1-ZST fields
409                            return None;
410                        }
411
412                        res = Err(tcx.dcx().emit_err(errors::DispatchFromDynZST {
413                            span,
414                            name: field.ident(tcx),
415                            ty: ty_a,
416                        }));
417
418                        None
419                    } else {
420                        Some((i, ty_a, ty_b, tcx.def_span(field.did)))
421                    }
422                })
423                .collect::<Vec<_>>();
424            res?;
425
426            if coerced_fields.is_empty() {
427                return Err(tcx.dcx().emit_err(errors::CoerceNoField {
428                    span,
429                    trait_name,
430                    note: true,
431                }));
432            } else if let &[(_, ty_a, ty_b, field_span)] = &coerced_fields[..] {
433                let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
434                ocx.register_obligation(Obligation::new(
435                    tcx,
436                    cause.clone(),
437                    param_env,
438                    ty::TraitRef::new(tcx, trait_ref.def_id, [ty_a, ty_b]),
439                ));
440                let errors = ocx.evaluate_obligations_error_on_ambiguity();
441                if !errors.is_empty() {
442                    if is_from_coerce_pointee_derive(tcx, span) {
443                        return Err(tcx.dcx().emit_err(errors::CoerceFieldValidity {
444                            span,
445                            trait_name,
446                            ty: trait_ref.self_ty(),
447                            field_span,
448                            field_ty: ty_a,
449                        }));
450                    } else {
451                        return Err(infcx.err_ctxt().report_fulfillment_errors(errors));
452                    }
453                }
454
455                // Finally, resolve all regions.
456                ocx.resolve_regions_and_report_errors(impl_did, param_env, [])?;
457
458                Ok(())
459            } else {
460                return Err(tcx.dcx().emit_err(errors::CoerceMulti {
461                    span,
462                    trait_name,
463                    number: coerced_fields.len(),
464                    fields: coerced_fields.iter().map(|(_, _, _, s)| *s).collect::<Vec<_>>().into(),
465                }));
466            }
467        }
468        _ => Err(tcx.dcx().emit_err(errors::CoerceUnsizedNonStruct { span, trait_name })),
469    }
470}
471
472fn structurally_normalize_ty<'tcx>(
473    tcx: TyCtxt<'tcx>,
474    infcx: &InferCtxt<'tcx>,
475    impl_did: LocalDefId,
476    span: Span,
477    ty: Unnormalized<'tcx, Ty<'tcx>>,
478) -> Option<(Ty<'tcx>, PredicateObligations<'tcx>)> {
479    let ocx = ObligationCtxt::new(infcx);
480    let Ok(normalized_ty) = ocx.structurally_normalize_ty(
481        &traits::ObligationCause::misc(span, impl_did),
482        tcx.param_env(impl_did),
483        ty,
484    ) else {
485        // We shouldn't have errors here in the old solver, except for
486        // evaluate/fulfill mismatches, but that's not a reason for an ICE.
487        return None;
488    };
489    let errors = ocx.try_evaluate_obligations();
490    if !errors.is_empty() {
491        if infcx.next_trait_solver() {
492            ::core::panicking::panic("internal error: entered unreachable code");unreachable!();
493        }
494        // We shouldn't have errors here in the old solver, except for
495        // evaluate/fulfill mismatches, but that's not a reason for an ICE.
496        {
    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/coherence/builtin.rs:496",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(496u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::tracing_core::field::FieldSet::new(&["message", "errors"],
                            ::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(&format_args!("encountered errors while fulfilling")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&errors) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?errors, "encountered errors while fulfilling");
497        return None;
498    }
499
500    Some((normalized_ty, ocx.into_pending_obligations()))
501}
502
503pub(crate) fn reborrow_info<'tcx>(
504    tcx: TyCtxt<'tcx>,
505    impl_did: LocalDefId,
506) -> Result<(), ErrorGuaranteed> {
507    {
    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/coherence/builtin.rs:507",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(507u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::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};
                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(&format_args!("compute_reborrow_info(impl_did={0:?})",
                                                    impl_did) as &dyn Value))])
            });
    } else { ; }
};debug!("compute_reborrow_info(impl_did={:?})", impl_did);
508    let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
509    let span = tcx.def_span(impl_did);
510    let trait_name = "Reborrow";
511
512    let reborrow_trait = tcx.require_lang_item(LangItem::Reborrow, span);
513
514    let source = tcx.type_of(impl_did).instantiate_identity().skip_norm_wip();
515    let trait_ref = tcx.impl_trait_ref(impl_did).instantiate_identity().skip_norm_wip();
516
517    if trait_impl_lifetime_params_count(tcx, impl_did) != 1 {
518        return Err(tcx
519            .dcx()
520            .emit_err(errors::CoerceSharedNotSingleLifetimeParam { span, trait_name }));
521    }
522
523    match (&trait_ref.def_id, &reborrow_trait) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(trait_ref.def_id, reborrow_trait);
524    let param_env = tcx.param_env(impl_did);
525    if !!source.has_escaping_bound_vars() {
    ::core::panicking::panic("assertion failed: !source.has_escaping_bound_vars()")
};assert!(!source.has_escaping_bound_vars());
526
527    let (def, args) = match source.kind() {
528        &ty::Adt(def, args) if def.is_struct() => (def, args),
529        _ => {
530            // Note: reusing error here as it takes trait_name as argument.
531            return Err(tcx.dcx().emit_err(errors::CoerceUnsizedNonStruct { span, trait_name }));
532        }
533    };
534
535    let lifetimes_count = generic_lifetime_params_count(args);
536    let data_fields = collect_struct_data_fields(tcx, def, args);
537
538    if lifetimes_count != 1 {
539        let item = tcx.hir_expect_item(impl_did);
540        let _span = if let ItemKind::Impl(hir::Impl { of_trait: Some(of_trait), .. }) = &item.kind {
541            of_trait.trait_ref.path.span
542        } else {
543            tcx.def_span(impl_did)
544        };
545
546        return Err(tcx.dcx().emit_err(errors::CoerceSharedMulti { span, trait_name }));
547    }
548
549    if data_fields.is_empty() {
550        return Ok(());
551    }
552
553    // We've found some data fields. They must all be either be Copy or Reborrow.
554    for (field, span) in data_fields {
555        if assert_field_type_is_reborrow(
556            tcx,
557            &infcx,
558            reborrow_trait,
559            impl_did,
560            param_env,
561            field,
562            span,
563        )
564        .is_ok()
565        {
566            // Field implements Reborrow.
567            return Ok(());
568        }
569
570        // Field does not implement Reborrow: it must be Copy.
571        assert_field_type_is_copy(tcx, &infcx, impl_did, param_env, field, span)?;
572    }
573
574    Ok(())
575}
576
577fn assert_field_type_is_reborrow<'tcx>(
578    tcx: TyCtxt<'tcx>,
579    infcx: &InferCtxt<'tcx>,
580    reborrow_trait: DefId,
581    impl_did: LocalDefId,
582    param_env: ty::ParamEnv<'tcx>,
583    ty: Ty<'tcx>,
584    span: Span,
585) -> Result<(), Vec<FulfillmentError<'tcx>>> {
586    if ty.ref_mutability() == Some(ty::Mutability::Mut) {
587        // Mutable references are Reborrow but not really.
588        return Ok(());
589    }
590    let ocx = ObligationCtxt::new_with_diagnostics(infcx);
591    let cause = traits::ObligationCause::misc(span, impl_did);
592    let obligation =
593        Obligation::new(tcx, cause, param_env, ty::TraitRef::new(tcx, reborrow_trait, [ty]));
594    ocx.register_obligation(obligation);
595    let errors = ocx.evaluate_obligations_error_on_ambiguity();
596
597    if !errors.is_empty() { Err(errors) } else { Ok(()) }
598}
599
600pub(crate) fn coerce_shared_info<'tcx>(
601    tcx: TyCtxt<'tcx>,
602    impl_did: LocalDefId,
603) -> Result<(), ErrorGuaranteed> {
604    {
    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/coherence/builtin.rs:604",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(604u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::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};
                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(&format_args!("compute_coerce_shared_info(impl_did={0:?})",
                                                    impl_did) as &dyn Value))])
            });
    } else { ; }
};debug!("compute_coerce_shared_info(impl_did={:?})", impl_did);
605    let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
606    let span = tcx.def_span(impl_did);
607    let trait_name = "CoerceShared";
608
609    let coerce_shared_trait = tcx.require_lang_item(LangItem::CoerceShared, span);
610
611    let source = tcx.type_of(impl_did).instantiate_identity().skip_norm_wip();
612    let trait_ref = tcx.impl_trait_ref(impl_did).instantiate_identity().skip_norm_wip();
613
614    if trait_impl_lifetime_params_count(tcx, impl_did) != 1 {
615        return Err(tcx
616            .dcx()
617            .emit_err(errors::CoerceSharedNotSingleLifetimeParam { span, trait_name }));
618    }
619
620    match (&trait_ref.def_id, &coerce_shared_trait) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(trait_ref.def_id, coerce_shared_trait);
621    let Some((target, _obligations)) = structurally_normalize_ty(
622        tcx,
623        &infcx,
624        impl_did,
625        span,
626        Unnormalized::new_wip(trait_ref.args.type_at(1)),
627    ) else {
628        {
    ::core::panicking::panic_fmt(format_args!("not yet implemented: {0}",
            format_args!("something went wrong with structurally_normalize_ty")));
};todo!("something went wrong with structurally_normalize_ty");
629    };
630
631    let param_env = tcx.param_env(impl_did);
632    if !!source.has_escaping_bound_vars() {
    ::core::panicking::panic("assertion failed: !source.has_escaping_bound_vars()")
};assert!(!source.has_escaping_bound_vars());
633
634    let data = match (source.kind(), target.kind()) {
635        (&ty::Adt(def_a, args_a), &ty::Adt(def_b, args_b))
636            if def_a.is_struct() && def_b.is_struct() =>
637        {
638            // Check that both A and B have exactly one lifetime argument, and that they have the
639            // same number of data fields that is not more than 1. The eventual intention is to
640            // support multiple lifetime arguments (with the reborrowed lifetimes inferred from
641            // usage one way or another) and multiple data fields with B allowed to leave out fields
642            // from A. The current state is just the simplest choice.
643            let a_lifetimes_count = generic_lifetime_params_count(args_a);
644            let a_data_fields = collect_struct_data_fields(tcx, def_a, args_a);
645            let b_lifetimes_count = generic_lifetime_params_count(args_b);
646            let b_data_fields = collect_struct_data_fields(tcx, def_b, args_b);
647
648            if a_lifetimes_count != 1
649                || b_lifetimes_count != 1
650                || a_data_fields.len() > 1
651                || b_data_fields.len() > 1
652                || a_data_fields.len() != b_data_fields.len()
653            {
654                let item = tcx.hir_expect_item(impl_did);
655                let span = if let ItemKind::Impl(hir::Impl { of_trait: Some(of_trait), .. }) =
656                    &item.kind
657                {
658                    of_trait.trait_ref.path.span
659                } else {
660                    tcx.def_span(impl_did)
661                };
662
663                return Err(tcx.dcx().emit_err(errors::CoerceSharedMulti { span, trait_name }));
664            }
665
666            if a_data_fields.len() == 1 {
667                // We found one data field for both: we'll attempt to perform CoerceShared between
668                // them below.
669                let (a, span_a) = a_data_fields[0];
670                let (b, span_b) = b_data_fields[0];
671
672                Some((a, b, coerce_shared_trait, span_a, span_b))
673            } else {
674                // We found no data fields in either: this is a reborrowable marker type being
675                // coerced into a shared marker. That is fine too.
676                None
677            }
678        }
679
680        _ => {
681            // Note: reusing CoerceUnsizedNonStruct error as it takes trait_name as argument.
682            return Err(tcx.dcx().emit_err(errors::CoerceUnsizedNonStruct { span, trait_name }));
683        }
684    };
685
686    // We've proven that we have two types with one lifetime each and 0 or 1 data fields each.
687    if let Some((source, target, trait_def_id, source_field_span, _target_field_span)) = data {
688        // struct Source(SourceData);
689        // struct Target(TargetData);
690        //
691        // 1 data field each; they must be the same type and Copy, or relate to one another using
692        // CoerceShared.
693        if source.ref_mutability() == Some(ty::Mutability::Mut)
694            && target.ref_mutability() == Some(ty::Mutability::Not)
695            && infcx
696                .eq_structurally_relating_aliases(
697                    param_env,
698                    source.peel_refs(),
699                    target.peel_refs(),
700                    source_field_span,
701                )
702                .is_ok()
703        {
704            // &mut T implements CoerceShared to &T, except not really.
705            return Ok(());
706        }
707        if infcx
708            .eq_structurally_relating_aliases(param_env, source, target, source_field_span)
709            .is_err()
710        {
711            // The two data fields don't agree on a common type; this means
712            // that they must be `A: CoerceShared<B>`. Register an obligation
713            // for that.
714            let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
715            let cause = traits::ObligationCause::misc(span, impl_did);
716            let obligation = Obligation::new(
717                tcx,
718                cause,
719                param_env,
720                ty::TraitRef::new(tcx, trait_def_id, [source, target]),
721            );
722            ocx.register_obligation(obligation);
723            let errors = ocx.evaluate_obligations_error_on_ambiguity();
724
725            if !errors.is_empty() {
726                return Err(infcx.err_ctxt().report_fulfillment_errors(errors));
727            }
728            // Finally, resolve all regions.
729            ocx.resolve_regions_and_report_errors(impl_did, param_env, [])?;
730        } else {
731            // Types match: check that it is Copy.
732            assert_field_type_is_copy(tcx, &infcx, impl_did, param_env, source, source_field_span)?;
733        }
734    }
735
736    Ok(())
737}
738
739fn trait_impl_lifetime_params_count(tcx: TyCtxt<'_>, did: LocalDefId) -> usize {
740    tcx.generics_of(did)
741        .own_params
742        .iter()
743        .filter(|p| #[allow(non_exhaustive_omitted_patterns)] match p.kind {
    ty::GenericParamDefKind::Lifetime => true,
    _ => false,
}matches!(p.kind, ty::GenericParamDefKind::Lifetime))
744        .count()
745}
746
747fn generic_lifetime_params_count(args: &[ty::GenericArg<'_>]) -> usize {
748    args.iter().filter(|arg| arg.as_region().is_some()).count()
749}
750
751// FIXME(#155345): This should return `Unnormalized`
752fn collect_struct_data_fields<'tcx>(
753    tcx: TyCtxt<'tcx>,
754    def: ty::AdtDef<'tcx>,
755    args: ty::GenericArgsRef<'tcx>,
756) -> Vec<(Ty<'tcx>, Span)> {
757    def.non_enum_variant()
758        .fields
759        .iter()
760        .filter_map(|f| {
761            // Ignore PhantomData fields
762            let ty = f.ty(tcx, args).skip_norm_wip();
763            if ty.is_phantom_data() {
764                return None;
765            }
766            Some((ty, tcx.def_span(f.did)))
767        })
768        .collect()
769}
770
771fn assert_field_type_is_copy<'tcx>(
772    tcx: TyCtxt<'tcx>,
773    infcx: &InferCtxt<'tcx>,
774    impl_did: LocalDefId,
775    param_env: ty::ParamEnv<'tcx>,
776    ty: Ty<'tcx>,
777    span: Span,
778) -> Result<(), ErrorGuaranteed> {
779    let copy_trait = tcx.require_lang_item(LangItem::Copy, span);
780    let ocx = ObligationCtxt::new_with_diagnostics(infcx);
781    let cause = traits::ObligationCause::misc(span, impl_did);
782    let obligation =
783        Obligation::new(tcx, cause, param_env, ty::TraitRef::new(tcx, copy_trait, [ty]));
784    ocx.register_obligation(obligation);
785    let errors = ocx.evaluate_obligations_error_on_ambiguity();
786
787    if !errors.is_empty() {
788        Err(infcx.err_ctxt().report_fulfillment_errors(errors))
789    } else {
790        Ok(())
791    }
792}
793
794pub(crate) fn coerce_unsized_info<'tcx>(
795    tcx: TyCtxt<'tcx>,
796    impl_did: LocalDefId,
797) -> Result<CoerceUnsizedInfo, ErrorGuaranteed> {
798    {
    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/coherence/builtin.rs:798",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(798u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::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};
                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(&format_args!("compute_coerce_unsized_info(impl_did={0:?})",
                                                    impl_did) as &dyn Value))])
            });
    } else { ; }
};debug!("compute_coerce_unsized_info(impl_did={:?})", impl_did);
799    let span = tcx.def_span(impl_did);
800    let trait_name = "CoerceUnsized";
801
802    let coerce_unsized_trait = tcx.require_lang_item(LangItem::CoerceUnsized, span);
803    let unsize_trait = tcx.require_lang_item(LangItem::Unsize, span);
804
805    let source = tcx.type_of(impl_did).instantiate_identity().skip_norm_wip();
806    let trait_ref = tcx.impl_trait_ref(impl_did).instantiate_identity().skip_norm_wip();
807
808    match (&trait_ref.def_id, &coerce_unsized_trait) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(trait_ref.def_id, coerce_unsized_trait);
809    let target = trait_ref.args.type_at(1);
810    {
    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/coherence/builtin.rs:810",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(810u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::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};
                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(&format_args!("visit_implementation_of_coerce_unsized: {0:?} -> {1:?} (bound)",
                                                    source, target) as &dyn Value))])
            });
    } else { ; }
};debug!("visit_implementation_of_coerce_unsized: {:?} -> {:?} (bound)", source, target);
811
812    let param_env = tcx.param_env(impl_did);
813    if !!source.has_escaping_bound_vars() {
    ::core::panicking::panic("assertion failed: !source.has_escaping_bound_vars()")
};assert!(!source.has_escaping_bound_vars());
814
815    {
    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/coherence/builtin.rs:815",
                        "rustc_hir_analysis::coherence::builtin",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/coherence/builtin.rs"),
                        ::tracing_core::__macro_support::Option::Some(815u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::coherence::builtin"),
                        ::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};
                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(&format_args!("visit_implementation_of_coerce_unsized: {0:?} -> {1:?} (free)",
                                                    source, target) as &dyn Value))])
            });
    } else { ; }
};debug!("visit_implementation_of_coerce_unsized: {:?} -> {:?} (free)", source, target);
816
817    let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
818    let cause = ObligationCause::misc(span, impl_did);
819    let check_mutbl = |mt_a: ty::TypeAndMut<'tcx>,
820                       mt_b: ty::TypeAndMut<'tcx>,
821                       mk_ptr: &dyn Fn(Ty<'tcx>) -> Ty<'tcx>| {
822        if mt_a.mutbl < mt_b.mutbl {
823            infcx
824                .err_ctxt()
825                .report_mismatched_types(
826                    &cause,
827                    param_env,
828                    mk_ptr(mt_b.ty),
829                    target,
830                    ty::error::TypeError::Mutability,
831                )
832                .emit();
833        }
834        (mt_a.ty, mt_b.ty, unsize_trait, None, span)
835    };
836    let (source, target, trait_def_id, kind, field_span) = match (source.kind(), target.kind()) {
837        (&ty::Pat(ty_a, pat_a), &ty::Pat(ty_b, pat_b)) => {
838            if pat_a != pat_b {
839                return Err(tcx.dcx().emit_err(errors::CoerceSamePatKind {
840                    span,
841                    trait_name,
842                    pat_a: pat_a.to_string(),
843                    pat_b: pat_b.to_string(),
844                }));
845            }
846            (ty_a, ty_b, coerce_unsized_trait, None, span)
847        }
848
849        (&ty::Ref(r_a, ty_a, mutbl_a), &ty::Ref(r_b, ty_b, mutbl_b)) => {
850            infcx.sub_regions(
851                SubregionOrigin::RelateObjectBound(span),
852                r_b,
853                r_a,
854                ty::VisibleForLeakCheck::Yes,
855            );
856            let mt_a = ty::TypeAndMut { ty: ty_a, mutbl: mutbl_a };
857            let mt_b = ty::TypeAndMut { ty: ty_b, mutbl: mutbl_b };
858            check_mutbl(mt_a, mt_b, &|ty| Ty::new_imm_ref(tcx, r_b, ty))
859        }
860
861        (&ty::Ref(_, ty_a, mutbl_a), &ty::RawPtr(ty_b, mutbl_b))
862        | (&ty::RawPtr(ty_a, mutbl_a), &ty::RawPtr(ty_b, mutbl_b)) => {
863            let mt_a = ty::TypeAndMut { ty: ty_a, mutbl: mutbl_a };
864            let mt_b = ty::TypeAndMut { ty: ty_b, mutbl: mutbl_b };
865            check_mutbl(mt_a, mt_b, &|ty| Ty::new_imm_ptr(tcx, ty))
866        }
867
868        (&ty::Adt(def_a, args_a), &ty::Adt(def_b, args_b))
869            if def_a.is_struct() && def_b.is_struct() =>
870        {
871            if def_a != def_b {
872                let source_path = tcx.def_path_str(def_a.did());
873                let target_path = tcx.def_path_str(def_b.did());
874                return Err(tcx.dcx().emit_err(errors::CoerceSameStruct {
875                    span,
876                    trait_name,
877                    note: true,
878                    source_path,
879                    target_path,
880                }));
881            }
882
883            // Here we are considering a case of converting
884            // `S<P0...Pn>` to `S<Q0...Qn>`. As an example, let's imagine a struct `Foo<T, U>`,
885            // which acts like a pointer to `U`, but carries along some extra data of type `T`:
886            //
887            //     struct Foo<T, U> {
888            //         extra: T,
889            //         ptr: *mut U,
890            //     }
891            //
892            // We might have an impl that allows (e.g.) `Foo<T, [i32; 3]>` to be unsized
893            // to `Foo<T, [i32]>`. That impl would look like:
894            //
895            //   impl<T, U: Unsize<V>, V> CoerceUnsized<Foo<T, V>> for Foo<T, U> {}
896            //
897            // Here `U = [i32; 3]` and `V = [i32]`. At runtime,
898            // when this coercion occurs, we would be changing the
899            // field `ptr` from a thin pointer of type `*mut [i32;
900            // 3]` to a wide pointer of type `*mut [i32]` (with
901            // extra data `3`). **The purpose of this check is to
902            // make sure that we know how to do this conversion.**
903            //
904            // To check if this impl is legal, we would walk down
905            // the fields of `Foo` and consider their types with
906            // both generic parameters. We are looking to find that
907            // exactly one (non-phantom) field has changed its
908            // type, which we will expect to be the pointer that
909            // is becoming fat (we could probably generalize this
910            // to multiple thin pointers of the same type becoming
911            // fat, but we don't). In this case:
912            //
913            // - `extra` has type `T` before and type `T` after
914            // - `ptr` has type `*mut U` before and type `*mut V` after
915            //
916            // Since just one field changed, we would then check
917            // that `*mut U: CoerceUnsized<*mut V>` is implemented
918            // (in other words, that we know how to do this
919            // conversion). This will work out because `U:
920            // Unsize<V>`, and we have a builtin rule that `*mut
921            // U` can be coerced to `*mut V` if `U: Unsize<V>`.
922            let fields = &def_a.non_enum_variant().fields;
923            let diff_fields = fields
924                .iter_enumerated()
925                .filter_map(|(i, f)| {
926                    let (a, b) =
927                        (f.ty(tcx, args_a).skip_norm_wip(), f.ty(tcx, args_b).skip_norm_wip());
928
929                    // Ignore PhantomData fields
930                    let unnormalized_ty = tcx.type_of(f.did).instantiate_identity();
931                    if tcx
932                        .try_normalize_erasing_regions(
933                            ty::TypingEnv::non_body_analysis(tcx, def_a.did()),
934                            unnormalized_ty,
935                        )
936                        .unwrap_or(unnormalized_ty.skip_norm_wip())
937                        .is_phantom_data()
938                    {
939                        return None;
940                    }
941
942                    // Ignore fields that aren't changed; it may
943                    // be that we could get away with subtyping or
944                    // something more accepting, but we use
945                    // equality because we want to be able to
946                    // perform this check without computing
947                    // variance or constraining opaque types' hidden types.
948                    // (This is because we may have to evaluate constraint
949                    // expressions in the course of execution.)
950                    // See e.g., #41936.
951                    if a == b {
952                        return None;
953                    }
954
955                    // Collect up all fields that were significantly changed
956                    // i.e., those that contain T in coerce_unsized T -> U
957                    Some((i, a, b, tcx.def_span(f.did)))
958                })
959                .collect::<Vec<_>>();
960
961            if diff_fields.is_empty() {
962                return Err(tcx.dcx().emit_err(errors::CoerceNoField {
963                    span,
964                    trait_name,
965                    note: true,
966                }));
967            } else if diff_fields.len() > 1 {
968                let item = tcx.hir_expect_item(impl_did);
969                let span = if let ItemKind::Impl(hir::Impl { of_trait: Some(of_trait), .. }) =
970                    &item.kind
971                {
972                    of_trait.trait_ref.path.span
973                } else {
974                    tcx.def_span(impl_did)
975                };
976
977                return Err(tcx.dcx().emit_err(errors::CoerceMulti {
978                    span,
979                    trait_name,
980                    number: diff_fields.len(),
981                    fields: diff_fields.iter().map(|(_, _, _, s)| *s).collect::<Vec<_>>().into(),
982                }));
983            }
984
985            let (i, a, b, field_span) = diff_fields[0];
986            let kind = ty::adjustment::CustomCoerceUnsized::Struct(i);
987            (a, b, coerce_unsized_trait, Some(kind), field_span)
988        }
989
990        _ => {
991            return Err(tcx.dcx().emit_err(errors::CoerceUnsizedNonStruct { span, trait_name }));
992        }
993    };
994
995    // Register an obligation for `A: Trait<B>`.
996    let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
997    let cause = traits::ObligationCause::misc(span, impl_did);
998    let obligation = Obligation::new(
999        tcx,
1000        cause,
1001        param_env,
1002        ty::TraitRef::new(tcx, trait_def_id, [source, target]),
1003    );
1004    ocx.register_obligation(obligation);
1005    let errors = ocx.evaluate_obligations_error_on_ambiguity();
1006
1007    if !errors.is_empty() {
1008        if is_from_coerce_pointee_derive(tcx, span) {
1009            return Err(tcx.dcx().emit_err(errors::CoerceFieldValidity {
1010                span,
1011                trait_name,
1012                ty: trait_ref.self_ty(),
1013                field_span,
1014                field_ty: source,
1015            }));
1016        } else {
1017            return Err(infcx.err_ctxt().report_fulfillment_errors(errors));
1018        }
1019    }
1020
1021    // Finally, resolve all regions.
1022    ocx.resolve_regions_and_report_errors(impl_did, param_env, [])?;
1023
1024    Ok(CoerceUnsizedInfo { custom_kind: kind })
1025}
1026
1027fn infringing_fields_error<'tcx>(
1028    tcx: TyCtxt<'tcx>,
1029    infringing_tys: impl Iterator<Item = (Span, Ty<'tcx>, InfringingFieldsReason<'tcx>)>,
1030    lang_item: LangItem,
1031    impl_did: LocalDefId,
1032    impl_span: Span,
1033) -> ErrorGuaranteed {
1034    let trait_did = tcx.require_lang_item(lang_item, impl_span);
1035
1036    let trait_name = tcx.def_path_str(trait_did);
1037
1038    // We'll try to suggest constraining type parameters to fulfill the requirements of
1039    // their `Copy` implementation.
1040    let mut errors: BTreeMap<_, Vec<_>> = Default::default();
1041    let mut bounds = ::alloc::vec::Vec::new()vec![];
1042
1043    let mut seen_tys = FxHashSet::default();
1044
1045    let mut label_spans = Vec::new();
1046
1047    for (span, ty, reason) in infringing_tys {
1048        // Only report an error once per type.
1049        if !seen_tys.insert(ty) {
1050            continue;
1051        }
1052
1053        label_spans.push(span);
1054
1055        match reason {
1056            InfringingFieldsReason::Fulfill(fulfillment_errors) => {
1057                for error in fulfillment_errors {
1058                    let error_predicate = error.obligation.predicate;
1059                    // Only note if it's not the root obligation, otherwise it's trivial and
1060                    // should be self-explanatory (i.e. a field literally doesn't implement Copy).
1061
1062                    // FIXME: This error could be more descriptive, especially if the error_predicate
1063                    // contains a foreign type or if it's a deeply nested type...
1064                    if error_predicate != error.root_obligation.predicate {
1065                        errors
1066                            .entry((ty.to_string(), error_predicate.to_string()))
1067                            .or_default()
1068                            .push(error.obligation.cause.span);
1069                    }
1070                    if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(ty::TraitPredicate {
1071                        trait_ref,
1072                        polarity: ty::PredicatePolarity::Positive,
1073                        ..
1074                    })) = error_predicate.kind().skip_binder()
1075                    {
1076                        let ty = trait_ref.self_ty();
1077                        if let ty::Param(_) = ty.kind() {
1078                            bounds.push((
1079                                ::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("{0}", ty)) })format!("{ty}"),
1080                                trait_ref.print_trait_sugared().to_string(),
1081                                Some(trait_ref.def_id),
1082                            ));
1083                        }
1084                    }
1085                }
1086            }
1087            InfringingFieldsReason::Regions(region_errors) => {
1088                for error in region_errors {
1089                    let ty = ty.to_string();
1090                    match error {
1091                        RegionResolutionError::ConcreteFailure(origin, a, b) => {
1092                            let predicate = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}: {1}", b, a))
    })format!("{b}: {a}");
1093                            errors
1094                                .entry((ty.clone(), predicate.clone()))
1095                                .or_default()
1096                                .push(origin.span());
1097                            if let ty::RegionKind::ReEarlyParam(ebr) = b.kind()
1098                                && ebr.is_named()
1099                            {
1100                                bounds.push((b.to_string(), a.to_string(), None));
1101                            }
1102                        }
1103                        RegionResolutionError::GenericBoundFailure(origin, a, b) => {
1104                            let predicate = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}: {1}", a, b))
    })format!("{a}: {b}");
1105                            errors
1106                                .entry((ty.clone(), predicate.clone()))
1107                                .or_default()
1108                                .push(origin.span());
1109                            if let infer::region_constraints::GenericKind::Param(_) = a {
1110                                bounds.push((a.to_string(), b.to_string(), None));
1111                            }
1112                        }
1113                        _ => continue,
1114                    }
1115                }
1116            }
1117        }
1118    }
1119    let mut notes = Vec::new();
1120    for ((ty, error_predicate), spans) in errors {
1121        let span: MultiSpan = spans.into();
1122        notes.push(errors::ImplForTyRequires {
1123            span,
1124            error_predicate,
1125            trait_name: trait_name.clone(),
1126            ty,
1127        });
1128    }
1129
1130    let mut err = tcx.dcx().create_err(errors::TraitCannotImplForTy {
1131        span: impl_span,
1132        trait_name,
1133        label_spans,
1134        notes,
1135    });
1136
1137    suggest_constraining_type_params(
1138        tcx,
1139        tcx.hir_get_generics(impl_did).expect("impls always have generics"),
1140        &mut err,
1141        bounds
1142            .iter()
1143            .map(|(param, constraint, def_id)| (param.as_str(), constraint.as_str(), *def_id)),
1144        None,
1145    );
1146
1147    err.emit()
1148}
1149
1150fn visit_implementation_of_coerce_pointee_validity(
1151    checker: &Checker<'_>,
1152) -> Result<(), ErrorGuaranteed> {
1153    let tcx = checker.tcx;
1154    let self_ty =
1155        tcx.impl_trait_ref(checker.impl_def_id).instantiate_identity().skip_norm_wip().self_ty();
1156    let span = tcx.def_span(checker.impl_def_id);
1157    if !tcx.is_builtin_derived(checker.impl_def_id.into()) {
1158        return Err(tcx.dcx().emit_err(errors::CoercePointeeNoUserValidityAssertion { span }));
1159    }
1160    let ty::Adt(def, _args) = self_ty.kind() else {
1161        return Err(tcx.dcx().emit_err(errors::CoercePointeeNotConcreteType { span }));
1162    };
1163    let did = def.did();
1164    // Now get a more precise span of the `struct`.
1165    let span = tcx.def_span(did);
1166    if !def.is_struct() {
1167        return Err(tcx
1168            .dcx()
1169            .emit_err(errors::CoercePointeeNotStruct { span, kind: def.descr().into() }));
1170    }
1171    if !def.repr().transparent() {
1172        return Err(tcx.dcx().emit_err(errors::CoercePointeeNotTransparent { span }));
1173    }
1174    if def.all_fields().next().is_none() {
1175        return Err(tcx.dcx().emit_err(errors::CoercePointeeNoField { span }));
1176    }
1177    Ok(())
1178}