1use std::iter;
2
3use rustc_abi::{FIRST_VARIANT, VariantIdx};
4use rustc_errors::ErrorGuaranteed;
5use rustc_hir::def::DefKind;
6use rustc_hir::def_id::LocalDefId;
7use rustc_middle::mir::interpret::LitToConstInput;
8use rustc_middle::query::Providers;
9use rustc_middle::thir::visit;
10use rustc_middle::thir::visit::Visitor;
11use rustc_middle::ty::abstract_const::CastKind;
12use rustc_middle::ty::{self, Expr, TyCtxt, TypeVisitableExt};
13use rustc_middle::{bug, mir, thir};
14use rustc_span::Span;
15use tracing::{debug, instrument};
16
17use crate::errors::{GenericConstantTooComplex, GenericConstantTooComplexSub};
18
19fn destructure_const<'tcx>(
22 tcx: TyCtxt<'tcx>,
23 const_: ty::Const<'tcx>,
24) -> ty::DestructuredConst<'tcx> {
25 let ty::ConstKind::Value(cv) = const_.kind() else {
26 bug!("cannot destructure constant {:?}", const_)
27 };
28
29 let branches = cv.valtree.unwrap_branch();
30
31 let (fields, variant) = match cv.ty.kind() {
32 ty::Array(inner_ty, _) | ty::Slice(inner_ty) => {
33 let field_consts = branches
35 .iter()
36 .map(|b| ty::Const::new_value(tcx, *b, *inner_ty))
37 .collect::<Vec<_>>();
38 debug!(?field_consts);
39
40 (field_consts, None)
41 }
42 ty::Adt(def, _) if def.variants().is_empty() => bug!("unreachable"),
43 ty::Adt(def, args) => {
44 let (variant_idx, branches) = if def.is_enum() {
45 let (head, rest) = branches.split_first().unwrap();
46 (VariantIdx::from_u32(head.unwrap_leaf().to_u32()), rest)
47 } else {
48 (FIRST_VARIANT, branches)
49 };
50 let fields = &def.variant(variant_idx).fields;
51 let mut field_consts = Vec::with_capacity(fields.len());
52
53 for (field, field_valtree) in iter::zip(fields, branches) {
54 let field_ty = field.ty(tcx, args);
55 let field_const = ty::Const::new_value(tcx, *field_valtree, field_ty);
56 field_consts.push(field_const);
57 }
58 debug!(?field_consts);
59
60 (field_consts, Some(variant_idx))
61 }
62 ty::Tuple(elem_tys) => {
63 let fields = iter::zip(*elem_tys, branches)
64 .map(|(elem_ty, elem_valtree)| ty::Const::new_value(tcx, *elem_valtree, elem_ty))
65 .collect::<Vec<_>>();
66
67 (fields, None)
68 }
69 _ => bug!("cannot destructure constant {:?}", const_),
70 };
71
72 let fields = tcx.arena.alloc_from_iter(fields);
73
74 ty::DestructuredConst { variant, fields }
75}
76
77fn check_binop(op: mir::BinOp) -> bool {
79 use mir::BinOp::*;
80 match op {
81 Add | AddUnchecked | AddWithOverflow | Sub | SubUnchecked | SubWithOverflow | Mul
82 | MulUnchecked | MulWithOverflow | Div | Rem | BitXor | BitAnd | BitOr | Shl
83 | ShlUnchecked | Shr | ShrUnchecked | Eq | Lt | Le | Ne | Ge | Gt | Cmp => true,
84 Offset => false,
85 }
86}
87
88fn check_unop(op: mir::UnOp) -> bool {
91 use mir::UnOp::*;
92 match op {
93 Not | Neg | PtrMetadata => true,
94 }
95}
96
97fn recurse_build<'tcx>(
98 tcx: TyCtxt<'tcx>,
99 body: &thir::Thir<'tcx>,
100 node: thir::ExprId,
101 root_span: Span,
102) -> Result<ty::Const<'tcx>, ErrorGuaranteed> {
103 use thir::ExprKind;
104 let node = &body.exprs[node];
105
106 let maybe_supported_error = |a| maybe_supported_error(tcx, a, root_span);
107 let error = |a| error(tcx, a, root_span);
108
109 Ok(match &node.kind {
110 &ExprKind::Scope { value, .. } => recurse_build(tcx, body, value, root_span)?,
112 &ExprKind::PlaceTypeAscription { source, .. }
113 | &ExprKind::ValueTypeAscription { source, .. } => {
114 recurse_build(tcx, body, source, root_span)?
115 }
116 &ExprKind::PlaceUnwrapUnsafeBinder { .. }
117 | &ExprKind::ValueUnwrapUnsafeBinder { .. }
118 | &ExprKind::WrapUnsafeBinder { .. } => {
119 todo!("FIXME(unsafe_binders)")
120 }
121 &ExprKind::Literal { lit, neg } => {
122 let sp = node.span;
123 tcx.at(sp).lit_to_const(LitToConstInput { lit: &lit.node, ty: node.ty, neg })
124 }
125 &ExprKind::NonHirLiteral { lit, user_ty: _ } => {
126 let val = ty::ValTree::from_scalar_int(tcx, lit);
127 ty::Const::new_value(tcx, val, node.ty)
128 }
129 &ExprKind::ZstLiteral { user_ty: _ } => ty::Const::zero_sized(tcx, node.ty),
130 &ExprKind::NamedConst { def_id, args, user_ty: _ } => {
131 let uneval = ty::UnevaluatedConst::new(def_id, args);
132 ty::Const::new_unevaluated(tcx, uneval)
133 }
134 ExprKind::ConstParam { param, .. } => ty::Const::new_param(tcx, *param),
135
136 ExprKind::Call { fun, args, .. } => {
137 let fun_ty = body.exprs[*fun].ty;
138 let fun = recurse_build(tcx, body, *fun, root_span)?;
139
140 let mut new_args = Vec::<ty::Const<'tcx>>::with_capacity(args.len());
141 for &id in args.iter() {
142 new_args.push(recurse_build(tcx, body, id, root_span)?);
143 }
144 ty::Const::new_expr(tcx, Expr::new_call(tcx, fun_ty, fun, new_args))
145 }
146 &ExprKind::Binary { op, lhs, rhs } if check_binop(op) => {
147 let lhs_ty = body.exprs[lhs].ty;
148 let lhs = recurse_build(tcx, body, lhs, root_span)?;
149 let rhs_ty = body.exprs[rhs].ty;
150 let rhs = recurse_build(tcx, body, rhs, root_span)?;
151 ty::Const::new_expr(tcx, Expr::new_binop(tcx, op, lhs_ty, rhs_ty, lhs, rhs))
152 }
153 &ExprKind::Unary { op, arg } if check_unop(op) => {
154 let arg_ty = body.exprs[arg].ty;
155 let arg = recurse_build(tcx, body, arg, root_span)?;
156 ty::Const::new_expr(tcx, Expr::new_unop(tcx, op, arg_ty, arg))
157 }
158 ExprKind::Block { block } => {
166 if let thir::Block { stmts: box [], expr: Some(e), .. } = &body.blocks[*block] {
167 recurse_build(tcx, body, *e, root_span)?
168 } else {
169 maybe_supported_error(GenericConstantTooComplexSub::BlockNotSupported(node.span))?
170 }
171 }
172 &ExprKind::Use { source } => {
176 let value_ty = body.exprs[source].ty;
177 let value = recurse_build(tcx, body, source, root_span)?;
178 ty::Const::new_expr(tcx, Expr::new_cast(tcx, CastKind::Use, value_ty, value, node.ty))
179 }
180 &ExprKind::Cast { source } => {
181 let value_ty = body.exprs[source].ty;
182 let value = recurse_build(tcx, body, source, root_span)?;
183 ty::Const::new_expr(tcx, Expr::new_cast(tcx, CastKind::As, value_ty, value, node.ty))
184 }
185 ExprKind::Borrow { arg, .. } => {
186 let arg_node = &body.exprs[*arg];
187
188 if let ExprKind::Deref { arg } = arg_node.kind {
192 recurse_build(tcx, body, arg, root_span)?
193 } else {
194 maybe_supported_error(GenericConstantTooComplexSub::BorrowNotSupported(node.span))?
195 }
196 }
197 ExprKind::RawBorrow { .. } | ExprKind::Deref { .. } => maybe_supported_error(
199 GenericConstantTooComplexSub::AddressAndDerefNotSupported(node.span),
200 )?,
201 ExprKind::Repeat { .. } | ExprKind::Array { .. } => {
202 maybe_supported_error(GenericConstantTooComplexSub::ArrayNotSupported(node.span))?
203 }
204 ExprKind::NeverToAny { .. } => {
205 maybe_supported_error(GenericConstantTooComplexSub::NeverToAnyNotSupported(node.span))?
206 }
207 ExprKind::Tuple { .. } => {
208 maybe_supported_error(GenericConstantTooComplexSub::TupleNotSupported(node.span))?
209 }
210 ExprKind::Index { .. } => {
211 maybe_supported_error(GenericConstantTooComplexSub::IndexNotSupported(node.span))?
212 }
213 ExprKind::Field { .. } => {
214 maybe_supported_error(GenericConstantTooComplexSub::FieldNotSupported(node.span))?
215 }
216 ExprKind::ConstBlock { .. } => {
217 maybe_supported_error(GenericConstantTooComplexSub::ConstBlockNotSupported(node.span))?
218 }
219 ExprKind::Adt(_) => {
220 maybe_supported_error(GenericConstantTooComplexSub::AdtNotSupported(node.span))?
221 }
222 ExprKind::PointerCoercion { .. } => {
224 error(GenericConstantTooComplexSub::PointerNotSupported(node.span))?
225 }
226 ExprKind::Yield { .. } => {
227 error(GenericConstantTooComplexSub::YieldNotSupported(node.span))?
228 }
229 ExprKind::Continue { .. } | ExprKind::Break { .. } | ExprKind::Loop { .. } => {
230 error(GenericConstantTooComplexSub::LoopNotSupported(node.span))?
231 }
232 ExprKind::Box { .. } => error(GenericConstantTooComplexSub::BoxNotSupported(node.span))?,
233
234 ExprKind::Unary { .. } => unreachable!(),
235 ExprKind::Binary { .. } => {
237 error(GenericConstantTooComplexSub::BinaryNotSupported(node.span))?
238 }
239 ExprKind::LogicalOp { .. } => {
240 error(GenericConstantTooComplexSub::LogicalOpNotSupported(node.span))?
241 }
242 ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
243 error(GenericConstantTooComplexSub::AssignNotSupported(node.span))?
244 }
245 ExprKind::Closure { .. } | ExprKind::Return { .. } | ExprKind::Become { .. } => {
247 error(GenericConstantTooComplexSub::ClosureAndReturnNotSupported(node.span))?
248 }
249 ExprKind::Match { .. } | ExprKind::If { .. } | ExprKind::Let { .. } => {
251 error(GenericConstantTooComplexSub::ControlFlowNotSupported(node.span))?
252 }
253 ExprKind::InlineAsm { .. } => {
254 error(GenericConstantTooComplexSub::InlineAsmNotSupported(node.span))?
255 }
256
257 ExprKind::VarRef { .. }
259 | ExprKind::UpvarRef { .. }
260 | ExprKind::StaticRef { .. }
261 | ExprKind::OffsetOf { .. }
262 | ExprKind::ThreadLocalRef(_) => {
263 error(GenericConstantTooComplexSub::OperationNotSupported(node.span))?
264 }
265 })
266}
267
268struct IsThirPolymorphic<'a, 'tcx> {
269 is_poly: bool,
270 thir: &'a thir::Thir<'tcx>,
271}
272
273fn error(
274 tcx: TyCtxt<'_>,
275 sub: GenericConstantTooComplexSub,
276 root_span: Span,
277) -> Result<!, ErrorGuaranteed> {
278 let reported = tcx.dcx().emit_err(GenericConstantTooComplex {
279 span: root_span,
280 maybe_supported: false,
281 sub,
282 });
283
284 Err(reported)
285}
286
287fn maybe_supported_error(
288 tcx: TyCtxt<'_>,
289 sub: GenericConstantTooComplexSub,
290 root_span: Span,
291) -> Result<!, ErrorGuaranteed> {
292 let reported = tcx.dcx().emit_err(GenericConstantTooComplex {
293 span: root_span,
294 maybe_supported: true,
295 sub,
296 });
297
298 Err(reported)
299}
300
301impl<'a, 'tcx> IsThirPolymorphic<'a, 'tcx> {
302 fn expr_is_poly(&mut self, expr: &thir::Expr<'tcx>) -> bool {
303 if expr.ty.has_non_region_param() {
304 return true;
305 }
306
307 match expr.kind {
308 thir::ExprKind::NamedConst { args, .. } | thir::ExprKind::ConstBlock { args, .. } => {
309 args.has_non_region_param()
310 }
311 thir::ExprKind::ConstParam { .. } => true,
312 thir::ExprKind::Repeat { value, count } => {
313 self.visit_expr(&self.thir()[value]);
314 count.has_non_region_param()
315 }
316 thir::ExprKind::Scope { .. }
317 | thir::ExprKind::Box { .. }
318 | thir::ExprKind::If { .. }
319 | thir::ExprKind::Call { .. }
320 | thir::ExprKind::Deref { .. }
321 | thir::ExprKind::Binary { .. }
322 | thir::ExprKind::LogicalOp { .. }
323 | thir::ExprKind::Unary { .. }
324 | thir::ExprKind::Cast { .. }
325 | thir::ExprKind::Use { .. }
326 | thir::ExprKind::NeverToAny { .. }
327 | thir::ExprKind::PointerCoercion { .. }
328 | thir::ExprKind::Loop { .. }
329 | thir::ExprKind::Let { .. }
330 | thir::ExprKind::Match { .. }
331 | thir::ExprKind::Block { .. }
332 | thir::ExprKind::Assign { .. }
333 | thir::ExprKind::AssignOp { .. }
334 | thir::ExprKind::Field { .. }
335 | thir::ExprKind::Index { .. }
336 | thir::ExprKind::VarRef { .. }
337 | thir::ExprKind::UpvarRef { .. }
338 | thir::ExprKind::Borrow { .. }
339 | thir::ExprKind::RawBorrow { .. }
340 | thir::ExprKind::Break { .. }
341 | thir::ExprKind::Continue { .. }
342 | thir::ExprKind::Return { .. }
343 | thir::ExprKind::Become { .. }
344 | thir::ExprKind::Array { .. }
345 | thir::ExprKind::Tuple { .. }
346 | thir::ExprKind::Adt(_)
347 | thir::ExprKind::PlaceTypeAscription { .. }
348 | thir::ExprKind::ValueTypeAscription { .. }
349 | thir::ExprKind::PlaceUnwrapUnsafeBinder { .. }
350 | thir::ExprKind::ValueUnwrapUnsafeBinder { .. }
351 | thir::ExprKind::WrapUnsafeBinder { .. }
352 | thir::ExprKind::Closure(_)
353 | thir::ExprKind::Literal { .. }
354 | thir::ExprKind::NonHirLiteral { .. }
355 | thir::ExprKind::ZstLiteral { .. }
356 | thir::ExprKind::StaticRef { .. }
357 | thir::ExprKind::InlineAsm(_)
358 | thir::ExprKind::OffsetOf { .. }
359 | thir::ExprKind::ThreadLocalRef(_)
360 | thir::ExprKind::Yield { .. } => false,
361 }
362 }
363 fn pat_is_poly(&mut self, pat: &thir::Pat<'tcx>) -> bool {
364 if pat.ty.has_non_region_param() {
365 return true;
366 }
367
368 match pat.kind {
369 thir::PatKind::Constant { value } => value.has_non_region_param(),
370 thir::PatKind::Range(ref range) => {
371 let &thir::PatRange { lo, hi, .. } = range.as_ref();
372 lo.has_non_region_param() || hi.has_non_region_param()
373 }
374 _ => false,
375 }
376 }
377}
378
379impl<'a, 'tcx> visit::Visitor<'a, 'tcx> for IsThirPolymorphic<'a, 'tcx> {
380 fn thir(&self) -> &'a thir::Thir<'tcx> {
381 self.thir
382 }
383
384 #[instrument(skip(self), level = "debug")]
385 fn visit_expr(&mut self, expr: &'a thir::Expr<'tcx>) {
386 self.is_poly |= self.expr_is_poly(expr);
387 if !self.is_poly {
388 visit::walk_expr(self, expr)
389 }
390 }
391
392 #[instrument(skip(self), level = "debug")]
393 fn visit_pat(&mut self, pat: &'a thir::Pat<'tcx>) {
394 self.is_poly |= self.pat_is_poly(pat);
395 if !self.is_poly {
396 visit::walk_pat(self, pat);
397 }
398 }
399}
400
401fn thir_abstract_const<'tcx>(
403 tcx: TyCtxt<'tcx>,
404 def: LocalDefId,
405) -> Result<Option<ty::EarlyBinder<'tcx, ty::Const<'tcx>>>, ErrorGuaranteed> {
406 if !tcx.features().generic_const_exprs() {
407 return Ok(None);
408 }
409
410 match tcx.def_kind(def) {
411 DefKind::AnonConst | DefKind::InlineConst => (),
417 _ => return Ok(None),
418 }
419
420 let body = tcx.thir_body(def)?;
421 let (body, body_id) = (&*body.0.borrow(), body.1);
422
423 let mut is_poly_vis = IsThirPolymorphic { is_poly: false, thir: body };
424 visit::walk_expr(&mut is_poly_vis, &body[body_id]);
425 if !is_poly_vis.is_poly {
426 return Ok(None);
427 }
428
429 let root_span = body.exprs[body_id].span;
430
431 Ok(Some(ty::EarlyBinder::bind(recurse_build(tcx, body, body_id, root_span)?)))
432}
433
434pub(crate) fn provide(providers: &mut Providers) {
435 *providers = Providers { destructure_const, thir_abstract_const, ..*providers };
436}