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::ty::layout::{LayoutError, SizeSkeleton};
9use rustc_middle::ty::{self, Ty, TyCtxt, Unnormalized};
10use rustc_span::def_id::LocalDefId;
11use rustc_span::{ErrorGuaranteed, bug};
12use tracing::trace;
13
14fn 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).skip_norm_wip();
35 }
36 }
37
38 ty
39}
40
41fn 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 bug_impl(None, format_args!("{0:?} overflow for u128", size),
Location::caller())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: Unnormalized<'tcx, Ty<'tcx>>,
73 to: Unnormalized<'tcx, Ty<'tcx>>,
74 hir_id: HirId,
75) -> Result<(), ErrorGuaranteed> {
76 let span = tcx.hir_span(hir_id);
77 let normalize = |ty: Unnormalized<'tcx, Ty<'tcx>>| -> Result<Ty<'tcx>, ErrorGuaranteed> {
78 tcx.try_normalize_erasing_regions(typing_env, ty).map_err(|err| {
79 let err = LayoutError::NormalizationFailure(ty.skip_normalization(), err);
80 tcx.dcx().span_err(span, err.to_string())
81 })
82 };
83
84 let from = normalize(from)?;
85 let to = normalize(to)?;
86 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_hir_typeck/src/intrinsicck.rs:86",
"rustc_hir_typeck::intrinsicck", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_hir_typeck/src/intrinsicck.rs"),
::tracing_core::__macro_support::Option::Some(86u32),
::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);
87
88 if from == to {
90 return Ok(());
91 }
92
93 let sk_from = SizeSkeleton::compute(from, tcx, typing_env, span);
94 let sk_to = SizeSkeleton::compute(to, tcx, typing_env, span);
95 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_hir_typeck/src/intrinsicck.rs:95",
"rustc_hir_typeck::intrinsicck", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_hir_typeck/src/intrinsicck.rs"),
::tracing_core::__macro_support::Option::Some(95u32),
::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);
96
97 if let Ok(sk_from) = sk_from
99 && let Ok(sk_to) = sk_to
100 {
101 if sk_from.same_size(sk_to) {
102 return Ok(());
103 }
104
105 let from = unpack_option_like(tcx, from);
108 if let ty::FnDef(..) = from.kind()
109 && let SizeSkeleton::Known(size_to, _) = sk_to
110 && size_to == Pointer(tcx.data_layout.instruction_address_space).size(&tcx)
111 {
112 {
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")
113 .with_note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("source type: {0}", from))
})format!("source type: {from}"))
114 .with_note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("target type: {0}", to))
})format!("target type: {to}"))
115 .with_help("cast with `as` to a pointer instead")
116 .emit();
117 return Ok(());
118 }
119 }
120
121 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!(
122 tcx.sess.dcx(),
123 span,
124 E0512,
125 "cannot transmute between types of different sizes, or dependently-sized types"
126 );
127 if from == to {
128 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"));
129 Err(err.emit())
130 } else {
131 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)));
132 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)));
133 Err(err.emit())
134 }
135}
136
137fn check_offload<'tcx>(
138 tcx: TyCtxt<'tcx>,
139 typing_env: ty::TypingEnv<'tcx>,
140 kernel_ty: Ty<'tcx>,
141 args_ty: Ty<'tcx>,
142 ret_ty: Ty<'tcx>,
143 hir_id: HirId,
144) -> Result<(), ErrorGuaranteed> {
145 let span = tcx.hir_span(hir_id);
146 let ty::FnDef(kernel_def_id, kernel_args) = *kernel_ty.kind() else {
147 let err = tcx
148 .sess
149 .dcx()
150 .struct_span_err(
151 span,
152 ::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),
153 )
154 .emit();
155 return Err(err);
156 };
157
158 let kernel_sig =
159 tcx.fn_sig(kernel_def_id).instantiate(tcx, kernel_args.skip_binder()).skip_norm_wip();
160 let kernel_sig = tcx.instantiate_bound_regions_with_erased(kernel_sig);
161
162 let ty::Tuple(tuple_fields) = *args_ty.kind() else {
163 let err = tcx
164 .sess
165 .dcx()
166 .struct_span_err(
167 span,
168 ::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),
169 )
170 .emit();
171 return Err(err);
172 };
173
174 if kernel_sig.inputs().len() != tuple_fields.len() {
175 let err = tcx
176 .sess
177 .dcx()
178 .struct_span_err(
179 span,
180 ::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!(
181 "offload kernel expects {} arguments, but {} arguments were provided",
182 kernel_sig.inputs().len(),
183 tuple_fields.len()
184 ),
185 )
186 .emit();
187 return Err(err);
188 }
189
190 let normalize = |ty| {
191 if let Ok(ty) = tcx.try_normalize_erasing_regions(typing_env, Unnormalized::new_wip(ty)) {
192 ty
193 } else {
194 Ty::new_error_with_message(
195 tcx,
196 span,
197 ::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"),
198 )
199 }
200 };
201
202 let mut result = Ok(());
203
204 for (i, (&input_ty, arg_ty)) in kernel_sig.inputs().iter().zip(tuple_fields.iter()).enumerate()
205 {
206 let norm_input_ty = normalize(input_ty);
207 let norm_arg_ty = normalize(arg_ty);
208 if norm_input_ty != norm_arg_ty {
209 let err = tcx
210 .sess
211 .dcx()
212 .struct_span_err(
213 span,
214 ::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!(
215 "type mismatch in offload kernel argument {}: expected `{}`, found `{}`",
216 i, norm_input_ty, norm_arg_ty
217 ),
218 )
219 .emit();
220 result = Err(err);
221 }
222 }
223
224 let norm_kernel_ret = normalize(kernel_sig.output());
225 let norm_offload_ret = normalize(ret_ty);
226 if norm_kernel_ret != norm_offload_ret {
227 let err = tcx.sess.dcx().struct_span_err(
228 span,
229 ::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!(
230 "offload kernel return type mismatch: kernel returns `{}`, but offload call expects `{}`",
231 norm_kernel_ret, norm_offload_ret
232 )
233 ).emit();
234 result = Err(err);
235 }
236
237 result
238}
239
240pub(crate) fn check_transmutes(tcx: TyCtxt<'_>, owner: LocalDefId) -> Result<(), ErrorGuaranteed> {
241 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()));
242 let typeck_results = tcx.typeck(owner);
243 if let Some(e) = typeck_results.tainted_by_errors {
244 return Err(e);
245 };
246
247 let typing_env = ty::TypingEnv::codegen(tcx, owner);
248 let mut result = Ok(());
249 for &(from, to, hir_id) in &typeck_results.transmutes_to_check {
250 let (to, from) = ty::set_aliases_to_non_rigid(tcx, (to, from)).unzip();
251 result = result.and(check_transmute(tcx, typing_env, from, to, hir_id));
252 }
253 result
254}
255
256pub(crate) fn check_offloads(tcx: TyCtxt<'_>, owner: LocalDefId) -> Result<(), ErrorGuaranteed> {
257 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()));
258 let typeck_results = tcx.typeck(owner);
259 if let Some(e) = typeck_results.tainted_by_errors {
260 return Err(e);
261 };
262
263 let typing_env = ty::TypingEnv::codegen(tcx, owner);
264 let mut result = Ok(());
265 for &(kernel_ty, args_ty, ret_ty, hir_id) in &typeck_results.offloads_to_check {
266 result = result.and(check_offload(tcx, typing_env, kernel_ty, args_ty, ret_ty, hir_id));
267 }
268 result
269}