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::ErrorGuaranteed;
12use rustc_span::def_id::LocalDefId;
13use tracing::trace;
14
15fn unpack_option_like<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
18 let ty::Adt(def, args) = *ty.kind() else { return ty };
19
20 if def.variants().len() == 2 && !def.repr().c() && def.repr().int.is_none() {
21 let data_idx;
22
23 let one = VariantIdx::new(1);
24 let zero = VariantIdx::ZERO;
25
26 if def.variant(zero).fields.is_empty() {
27 data_idx = one;
28 } else if def.variant(one).fields.is_empty() {
29 data_idx = zero;
30 } else {
31 return ty;
32 }
33
34 if def.variant(data_idx).fields.len() == 1 {
35 return def.variant(data_idx).single_field().ty(tcx, args).skip_norm_wip();
36 }
37 }
38
39 ty
40}
41
42fn skeleton_string<'tcx>(
44 ty: Ty<'tcx>,
45 sk: Result<SizeSkeleton<'tcx>, &'tcx LayoutError<'tcx>>,
46) -> String {
47 match sk {
48 Ok(SizeSkeleton::Pointer { tail, .. }) => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("pointer to `{0}`", tail))
})format!("pointer to `{tail}`"),
49 Ok(SizeSkeleton::Known(size, _)) => {
50 if let Some(v) = u128::from(size.bytes()).checked_mul(8) {
51 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} bits", v))
})format!("{v} bits")
52 } else {
53 ::rustc_middle::util::bug::bug_fmt(format_args!("{0:?} overflow for u128",
size))bug!("{:?} overflow for u128", size)
57 }
58 }
59 Err(LayoutError::TooGeneric(bad)) => {
60 if *bad == ty {
61 "this type does not have a fixed size".to_owned()
62 } else {
63 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("size can vary because of {0}",
bad))
})format!("size can vary because of {bad}")
64 }
65 }
66 Err(err) => err.to_string(),
67 }
68}
69
70fn check_transmute<'tcx>(
71 tcx: TyCtxt<'tcx>,
72 typing_env: ty::TypingEnv<'tcx>,
73 from: Unnormalized<'tcx, Ty<'tcx>>,
74 to: Unnormalized<'tcx, Ty<'tcx>>,
75 hir_id: HirId,
76) -> Result<(), ErrorGuaranteed> {
77 let span = tcx.hir_span(hir_id);
78 let normalize = |ty: Unnormalized<'tcx, Ty<'tcx>>| -> Result<Ty<'tcx>, ErrorGuaranteed> {
79 tcx.try_normalize_erasing_regions(typing_env, ty).map_err(|err| {
80 let err = LayoutError::NormalizationFailure(ty.skip_normalization(), err);
81 tcx.dcx().struct_span_err(span, err.to_string()).emit()
82 })
83 };
84
85 let from = normalize(from)?;
86 let to = normalize(to)?;
87 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/809936eac66c547a5127ce1da805f0d3a6789b98/compiler/rustc_hir_typeck/src/intrinsicck.rs:87",
"rustc_hir_typeck::intrinsicck", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/809936eac66c547a5127ce1da805f0d3a6789b98/compiler/rustc_hir_typeck/src/intrinsicck.rs"),
::tracing_core::__macro_support::Option::Some(87u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::intrinsicck"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("from")
}> =
::tracing::__macro_support::FieldName::new("from");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("to")
}> =
::tracing::__macro_support::FieldName::new("to");
NAME.as_str()
}], ::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};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&from)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&to)
as &dyn ::tracing::field::Value))])
});
} else { ; }
};trace!(?from, ?to);
88
89 if from == to {
91 return Ok(());
92 }
93
94 let sk_from = SizeSkeleton::compute(from, tcx, typing_env, span);
95 let sk_to = SizeSkeleton::compute(to, tcx, typing_env, span);
96 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/809936eac66c547a5127ce1da805f0d3a6789b98/compiler/rustc_hir_typeck/src/intrinsicck.rs:96",
"rustc_hir_typeck::intrinsicck", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/809936eac66c547a5127ce1da805f0d3a6789b98/compiler/rustc_hir_typeck/src/intrinsicck.rs"),
::tracing_core::__macro_support::Option::Some(96u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::intrinsicck"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("sk_from")
}> =
::tracing::__macro_support::FieldName::new("sk_from");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("sk_to")
}> =
::tracing::__macro_support::FieldName::new("sk_to");
NAME.as_str()
}], ::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};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&sk_from)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&sk_to)
as &dyn ::tracing::field::Value))])
});
} else { ; }
};trace!(?sk_from, ?sk_to);
97
98 if let Ok(sk_from) = sk_from
100 && let Ok(sk_to) = sk_to
101 {
102 if sk_from.same_size(sk_to) {
103 return Ok(());
104 }
105
106 let from = unpack_option_like(tcx, from);
109 if let ty::FnDef(..) = from.kind()
110 && let SizeSkeleton::Known(size_to, _) = sk_to
111 && size_to == Pointer(tcx.data_layout.instruction_address_space).size(&tcx)
112 {
113 {
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")
114 .with_note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("source type: {0}", from))
})format!("source type: {from}"))
115 .with_note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("target type: {0}", to))
})format!("target type: {to}"))
116 .with_help("cast with `as` to a pointer instead")
117 .emit();
118 return Ok(());
119 }
120 }
121
122 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!(
123 tcx.sess.dcx(),
124 span,
125 E0512,
126 "cannot transmute between types of different sizes, or dependently-sized types"
127 );
128 if from == to {
129 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"));
130 Err(err.emit())
131 } else {
132 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)));
133 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)));
134 Err(err.emit())
135 }
136}
137
138fn check_offload<'tcx>(
139 tcx: TyCtxt<'tcx>,
140 typing_env: ty::TypingEnv<'tcx>,
141 kernel_ty: Ty<'tcx>,
142 args_ty: Ty<'tcx>,
143 ret_ty: Ty<'tcx>,
144 hir_id: HirId,
145) -> Result<(), ErrorGuaranteed> {
146 let span = tcx.hir_span(hir_id);
147 let ty::FnDef(kernel_def_id, kernel_args) = *kernel_ty.kind() else {
148 let err = tcx
149 .sess
150 .dcx()
151 .struct_span_err(
152 span,
153 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected a function item for the offload kernel, found `{0}`",
kernel_ty))
})format!("expected a function item for the offload kernel, found `{}`", kernel_ty),
154 )
155 .emit();
156 return Err(err);
157 };
158
159 let kernel_sig =
160 tcx.fn_sig(kernel_def_id).instantiate(tcx, kernel_args.skip_binder()).skip_norm_wip();
161 let kernel_sig = tcx.instantiate_bound_regions_with_erased(kernel_sig);
162
163 let ty::Tuple(tuple_fields) = *args_ty.kind() else {
164 let err = tcx
165 .sess
166 .dcx()
167 .struct_span_err(
168 span,
169 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected a tuple for the offload arguments, found `{0}`",
args_ty))
})format!("expected a tuple for the offload arguments, found `{}`", args_ty),
170 )
171 .emit();
172 return Err(err);
173 };
174
175 if kernel_sig.inputs().len() != tuple_fields.len() {
176 let err = tcx
177 .sess
178 .dcx()
179 .struct_span_err(
180 span,
181 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("offload kernel expects {0} arguments, but {1} arguments were provided",
kernel_sig.inputs().len(), tuple_fields.len()))
})format!(
182 "offload kernel expects {} arguments, but {} arguments were provided",
183 kernel_sig.inputs().len(),
184 tuple_fields.len()
185 ),
186 )
187 .emit();
188 return Err(err);
189 }
190
191 let normalize = |ty| {
192 if let Ok(ty) = tcx.try_normalize_erasing_regions(typing_env, Unnormalized::new_wip(ty)) {
193 ty
194 } else {
195 Ty::new_error_with_message(
196 tcx,
197 span,
198 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("tried to normalize non-wf type {0:#?} in check_offload",
ty))
})format!("tried to normalize non-wf type {ty:#?} in check_offload"),
199 )
200 }
201 };
202
203 let mut result = Ok(());
204
205 for (i, (&input_ty, arg_ty)) in kernel_sig.inputs().iter().zip(tuple_fields.iter()).enumerate()
206 {
207 let norm_input_ty = normalize(input_ty);
208 let norm_arg_ty = normalize(arg_ty);
209 if norm_input_ty != norm_arg_ty {
210 let err = tcx
211 .sess
212 .dcx()
213 .struct_span_err(
214 span,
215 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("type mismatch in offload kernel argument {0}: expected `{1}`, found `{2}`",
i, norm_input_ty, norm_arg_ty))
})format!(
216 "type mismatch in offload kernel argument {}: expected `{}`, found `{}`",
217 i, norm_input_ty, norm_arg_ty
218 ),
219 )
220 .emit();
221 result = Err(err);
222 }
223 }
224
225 let norm_kernel_ret = normalize(kernel_sig.output());
226 let norm_offload_ret = normalize(ret_ty);
227 if norm_kernel_ret != norm_offload_ret {
228 let err = tcx.sess.dcx().struct_span_err(
229 span,
230 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("offload kernel return type mismatch: kernel returns `{0}`, but offload call expects `{1}`",
norm_kernel_ret, norm_offload_ret))
})format!(
231 "offload kernel return type mismatch: kernel returns `{}`, but offload call expects `{}`",
232 norm_kernel_ret, norm_offload_ret
233 )
234 ).emit();
235 result = Err(err);
236 }
237
238 result
239}
240
241pub(crate) fn check_transmutes(tcx: TyCtxt<'_>, owner: LocalDefId) -> Result<(), ErrorGuaranteed> {
242 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()));
243 let typeck_results = tcx.typeck(owner);
244 if let Some(e) = typeck_results.tainted_by_errors {
245 return Err(e);
246 };
247
248 let typing_env = ty::TypingEnv::codegen(tcx, owner);
249 let mut result = Ok(());
250 for &(from, to, hir_id) in &typeck_results.transmutes_to_check {
251 let (to, from) = ty::set_aliases_to_non_rigid(tcx, (to, from)).unzip();
252 result = result.and(check_transmute(tcx, typing_env, from, to, hir_id));
253 }
254 result
255}
256
257pub(crate) fn check_offloads(tcx: TyCtxt<'_>, owner: LocalDefId) -> Result<(), ErrorGuaranteed> {
258 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()));
259 let typeck_results = tcx.typeck(owner);
260 if let Some(e) = typeck_results.tainted_by_errors {
261 return Err(e);
262 };
263
264 let typing_env = ty::TypingEnv::codegen(tcx, owner);
265 let mut result = Ok(());
266 for &(kernel_ty, args_ty, ret_ty, hir_id) in &typeck_results.offloads_to_check {
267 result = result.and(check_offload(tcx, typing_env, kernel_ty, args_ty, ret_ty, hir_id));
268 }
269 result
270}