Skip to main content

rustc_hir_typeck/
intrinsicck.rs

1use hir::HirId;
2use rustc_abi::Primitive::Pointer;
3use rustc_abi::VariantIdx;
4use rustc_errors::codes::*;
5use rustc_errors::struct_span_code_err;
6use rustc_hir as hir;
7use rustc_index::Idx;
8use rustc_middle::bug;
9use rustc_middle::ty::layout::{LayoutError, SizeSkeleton};
10use rustc_middle::ty::{self, Ty, TyCtxt, Unnormalized};
11use rustc_span::def_id::LocalDefId;
12use tracing::trace;
13
14/// If the type is `Option<T>`, it will return `T`, otherwise
15/// the type itself. Works on most `Option`-like types.
16fn unpack_option_like<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
17    let ty::Adt(def, args) = *ty.kind() else { return ty };
18
19    if def.variants().len() == 2 && !def.repr().c() && def.repr().int.is_none() {
20        let data_idx;
21
22        let one = VariantIdx::new(1);
23        let zero = VariantIdx::ZERO;
24
25        if def.variant(zero).fields.is_empty() {
26            data_idx = one;
27        } else if def.variant(one).fields.is_empty() {
28            data_idx = zero;
29        } else {
30            return ty;
31        }
32
33        if def.variant(data_idx).fields.len() == 1 {
34            return def.variant(data_idx).single_field().ty(tcx, args);
35        }
36    }
37
38    ty
39}
40
41/// Try to display a sensible error with as much information as possible.
42fn skeleton_string<'tcx>(
43    ty: Ty<'tcx>,
44    sk: Result<SizeSkeleton<'tcx>, &'tcx LayoutError<'tcx>>,
45) -> String {
46    match sk {
47        Ok(SizeSkeleton::Pointer { tail, .. }) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("pointer to `{0}`", tail))
    })format!("pointer to `{tail}`"),
48        Ok(SizeSkeleton::Known(size, _)) => {
49            if let Some(v) = u128::from(size.bytes()).checked_mul(8) {
50                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} bits", v))
    })format!("{v} bits")
51            } else {
52                // `u128` should definitely be able to hold the size of different architectures
53                // larger sizes should be reported as error `are too big for the target architecture`
54                // otherwise we have a bug somewhere
55                ::rustc_middle::util::bug::bug_fmt(format_args!("{0:?} overflow for u128",
        size))bug!("{:?} overflow for u128", size)
56            }
57        }
58        Err(LayoutError::TooGeneric(bad)) => {
59            if *bad == ty {
60                "this type does not have a fixed size".to_owned()
61            } else {
62                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("size can vary because of {0}",
                bad))
    })format!("size can vary because of {bad}")
63            }
64        }
65        Err(err) => err.to_string(),
66    }
67}
68
69fn check_transmute<'tcx>(
70    tcx: TyCtxt<'tcx>,
71    typing_env: ty::TypingEnv<'tcx>,
72    from: Ty<'tcx>,
73    to: Ty<'tcx>,
74    hir_id: HirId,
75) {
76    let span = || tcx.hir_span(hir_id);
77    let normalize = |ty| {
78        if let Ok(ty) = tcx.try_normalize_erasing_regions(typing_env, Unnormalized::new_wip(ty)) {
79            ty
80        } else {
81            Ty::new_error_with_message(
82                tcx,
83                span(),
84                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("tried to normalize non-wf type {0:#?} in check_transmute",
                ty))
    })format!("tried to normalize non-wf type {ty:#?} in check_transmute"),
85            )
86        }
87    };
88
89    let from = normalize(from);
90    let to = normalize(to);
91    {
    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_typeck/src/intrinsicck.rs:91",
                        "rustc_hir_typeck::intrinsicck", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/intrinsicck.rs"),
                        ::tracing_core::__macro_support::Option::Some(91u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::intrinsicck"),
                        ::tracing_core::field::FieldSet::new(&["from", "to"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&from) as
                                            &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&to) as
                                            &dyn Value))])
            });
    } else { ; }
};trace!(?from, ?to);
92
93    // Transmutes that are only changing lifetimes are always ok.
94    if from == to {
95        return;
96    }
97
98    let sk_from = SizeSkeleton::compute(from, tcx, typing_env);
99    let sk_to = SizeSkeleton::compute(to, tcx, typing_env);
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_typeck/src/intrinsicck.rs:100",
                        "rustc_hir_typeck::intrinsicck", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/intrinsicck.rs"),
                        ::tracing_core::__macro_support::Option::Some(100u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::intrinsicck"),
                        ::tracing_core::field::FieldSet::new(&["sk_from", "sk_to"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&sk_from) as
                                            &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&sk_to) as
                                            &dyn Value))])
            });
    } else { ; }
};trace!(?sk_from, ?sk_to);
101
102    // Check for same size using the skeletons.
103    if let Ok(sk_from) = sk_from
104        && let Ok(sk_to) = sk_to
105    {
106        if sk_from.same_size(sk_to) {
107            return;
108        }
109
110        // Special-case transmuting from `typeof(function)` and
111        // `Option<typeof(function)>` to present a clearer error.
112        let from = unpack_option_like(tcx, from);
113        if let ty::FnDef(..) = from.kind()
114            && let SizeSkeleton::Known(size_to, _) = sk_to
115            && size_to == Pointer(tcx.data_layout.instruction_address_space).size(&tcx)
116        {
117            {
    tcx.sess.dcx().struct_span_err(span(),
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("can\'t transmute zero-sized type"))
                })).with_code(E0591)
}struct_span_code_err!(tcx.sess.dcx(), span(), E0591, "can't transmute zero-sized type")
118                .with_note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("source type: {0}", from))
    })format!("source type: {from}"))
119                .with_note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("target type: {0}", to))
    })format!("target type: {to}"))
120                .with_help("cast with `as` to a pointer instead")
121                .emit();
122            return;
123        }
124    }
125
126    let mut err = {
    tcx.sess.dcx().struct_span_err(span(),
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("cannot transmute between types of different sizes, or dependently-sized types"))
                })).with_code(E0512)
}struct_span_code_err!(
127        tcx.sess.dcx(),
128        span(),
129        E0512,
130        "cannot transmute between types of different sizes, or dependently-sized types"
131    );
132    if from == to {
133        err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` does not have a fixed size",
                from))
    })format!("`{from}` does not have a fixed size"));
134        err.emit();
135    } else {
136        err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("source type: `{0}` ({1})", from,
                skeleton_string(from, sk_from)))
    })format!("source type: `{}` ({})", from, skeleton_string(from, sk_from)));
137        err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("target type: `{0}` ({1})", to,
                skeleton_string(to, sk_to)))
    })format!("target type: `{}` ({})", to, skeleton_string(to, sk_to)));
138        err.emit();
139    }
140}
141
142pub(crate) fn check_transmutes(tcx: TyCtxt<'_>, owner: LocalDefId) {
143    if !!tcx.is_typeck_child(owner.to_def_id()) {
    ::core::panicking::panic("assertion failed: !tcx.is_typeck_child(owner.to_def_id())")
};assert!(!tcx.is_typeck_child(owner.to_def_id()));
144    let typeck_results = tcx.typeck(owner);
145    let None = typeck_results.tainted_by_errors else { return };
146
147    let typing_env = ty::TypingEnv::post_analysis(tcx, owner);
148    for &(from, to, hir_id) in &typeck_results.transmutes_to_check {
149        check_transmute(tcx, typing_env, from, to, hir_id);
150    }
151}