• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use rustc_errors::ErrorGuaranteed;
2 use rustc_hir::def::DefKind;
3 use rustc_hir::def_id::LocalDefId;
4 use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
5 use rustc_middle::query::Providers;
6 use rustc_middle::thir::visit;
7 use rustc_middle::thir::visit::Visitor;
8 use rustc_middle::ty::abstract_const::CastKind;
9 use rustc_middle::ty::{self, Expr, TyCtxt, TypeVisitableExt};
10 use rustc_middle::{mir, thir};
11 use rustc_span::Span;
12 use rustc_target::abi::{VariantIdx, FIRST_VARIANT};
13 
14 use std::iter;
15 
16 use crate::errors::{GenericConstantTooComplex, GenericConstantTooComplexSub};
17 
18 /// Destructures array, ADT or tuple constants into the constants
19 /// of their fields.
destructure_const<'tcx>( tcx: TyCtxt<'tcx>, const_: ty::Const<'tcx>, ) -> ty::DestructuredConst<'tcx>20 pub(crate) fn destructure_const<'tcx>(
21     tcx: TyCtxt<'tcx>,
22     const_: ty::Const<'tcx>,
23 ) -> ty::DestructuredConst<'tcx> {
24     let ty::ConstKind::Value(valtree) = const_.kind() else {
25         bug!("cannot destructure constant {:?}", const_)
26     };
27 
28     let branches = match valtree {
29         ty::ValTree::Branch(b) => b,
30         _ => bug!("cannot destructure constant {:?}", const_),
31     };
32 
33     let (fields, variant) = match const_.ty().kind() {
34         ty::Array(inner_ty, _) | ty::Slice(inner_ty) => {
35             // construct the consts for the elements of the array/slice
36             let field_consts = branches
37                 .iter()
38                 .map(|b| ty::Const::new_value(tcx, *b, *inner_ty))
39                 .collect::<Vec<_>>();
40             debug!(?field_consts);
41 
42             (field_consts, None)
43         }
44         ty::Adt(def, _) if def.variants().is_empty() => bug!("unreachable"),
45         ty::Adt(def, substs) => {
46             let (variant_idx, branches) = if def.is_enum() {
47                 let (head, rest) = branches.split_first().unwrap();
48                 (VariantIdx::from_u32(head.unwrap_leaf().try_to_u32().unwrap()), rest)
49             } else {
50                 (FIRST_VARIANT, branches)
51             };
52             let fields = &def.variant(variant_idx).fields;
53             let mut field_consts = Vec::with_capacity(fields.len());
54 
55             for (field, field_valtree) in iter::zip(fields, branches) {
56                 let field_ty = field.ty(tcx, substs);
57                 let field_const = ty::Const::new_value(tcx, *field_valtree, field_ty);
58                 field_consts.push(field_const);
59             }
60             debug!(?field_consts);
61 
62             (field_consts, Some(variant_idx))
63         }
64         ty::Tuple(elem_tys) => {
65             let fields = iter::zip(*elem_tys, branches)
66                 .map(|(elem_ty, elem_valtree)| ty::Const::new_value(tcx, *elem_valtree, elem_ty))
67                 .collect::<Vec<_>>();
68 
69             (fields, None)
70         }
71         _ => bug!("cannot destructure constant {:?}", const_),
72     };
73 
74     let fields = tcx.arena.alloc_from_iter(fields.into_iter());
75 
76     ty::DestructuredConst { variant, fields }
77 }
78 
79 /// We do not allow all binary operations in abstract consts, so filter disallowed ones.
check_binop(op: mir::BinOp) -> bool80 fn check_binop(op: mir::BinOp) -> bool {
81     use mir::BinOp::*;
82     match op {
83         Add | AddUnchecked | Sub | SubUnchecked | Mul | MulUnchecked | Div | Rem | BitXor
84         | BitAnd | BitOr | Shl | ShlUnchecked | Shr | ShrUnchecked | Eq | Lt | Le | Ne | Ge
85         | Gt => true,
86         Offset => false,
87     }
88 }
89 
90 /// While we currently allow all unary operations, we still want to explicitly guard against
91 /// future changes here.
check_unop(op: mir::UnOp) -> bool92 fn check_unop(op: mir::UnOp) -> bool {
93     use mir::UnOp::*;
94     match op {
95         Not | Neg => true,
96     }
97 }
98 
recurse_build<'tcx>( tcx: TyCtxt<'tcx>, body: &thir::Thir<'tcx>, node: thir::ExprId, root_span: Span, ) -> Result<ty::Const<'tcx>, ErrorGuaranteed>99 fn recurse_build<'tcx>(
100     tcx: TyCtxt<'tcx>,
101     body: &thir::Thir<'tcx>,
102     node: thir::ExprId,
103     root_span: Span,
104 ) -> Result<ty::Const<'tcx>, ErrorGuaranteed> {
105     use thir::ExprKind;
106     let node = &body.exprs[node];
107 
108     let maybe_supported_error = |a| maybe_supported_error(tcx, a, root_span);
109     let error = |a| error(tcx, a, root_span);
110 
111     Ok(match &node.kind {
112         // I dont know if handling of these 3 is correct
113         &ExprKind::Scope { value, .. } => recurse_build(tcx, body, value, root_span)?,
114         &ExprKind::PlaceTypeAscription { source, .. }
115         | &ExprKind::ValueTypeAscription { source, .. } => {
116             recurse_build(tcx, body, source, root_span)?
117         }
118         &ExprKind::Literal { lit, neg } => {
119             let sp = node.span;
120             match tcx.at(sp).lit_to_const(LitToConstInput { lit: &lit.node, ty: node.ty, neg }) {
121                 Ok(c) => c,
122                 Err(LitToConstError::Reported(guar)) => ty::Const::new_error(tcx, guar, node.ty),
123                 Err(LitToConstError::TypeError) => {
124                     bug!("encountered type error in lit_to_const")
125                 }
126             }
127         }
128         &ExprKind::NonHirLiteral { lit, user_ty: _ } => {
129             let val = ty::ValTree::from_scalar_int(lit);
130             ty::Const::new_value(tcx, val, node.ty)
131         }
132         &ExprKind::ZstLiteral { user_ty: _ } => {
133             let val = ty::ValTree::zst();
134             ty::Const::new_value(tcx, val, node.ty)
135         }
136         &ExprKind::NamedConst { def_id, substs, user_ty: _ } => {
137             let uneval = ty::UnevaluatedConst::new(def_id, substs);
138             ty::Const::new_unevaluated(tcx, uneval, node.ty)
139         }
140         ExprKind::ConstParam { param, .. } => ty::Const::new_param(tcx, *param, node.ty),
141 
142         ExprKind::Call { fun, args, .. } => {
143             let fun = recurse_build(tcx, body, *fun, root_span)?;
144 
145             let mut new_args = Vec::<ty::Const<'tcx>>::with_capacity(args.len());
146             for &id in args.iter() {
147                 new_args.push(recurse_build(tcx, body, id, root_span)?);
148             }
149             let new_args = tcx.mk_const_list(&new_args);
150             ty::Const::new_expr(tcx, Expr::FunctionCall(fun, new_args), node.ty)
151         }
152         &ExprKind::Binary { op, lhs, rhs } if check_binop(op) => {
153             let lhs = recurse_build(tcx, body, lhs, root_span)?;
154             let rhs = recurse_build(tcx, body, rhs, root_span)?;
155             ty::Const::new_expr(tcx, Expr::Binop(op, lhs, rhs), node.ty)
156         }
157         &ExprKind::Unary { op, arg } if check_unop(op) => {
158             let arg = recurse_build(tcx, body, arg, root_span)?;
159             ty::Const::new_expr(tcx, Expr::UnOp(op, arg), node.ty)
160         }
161         // This is necessary so that the following compiles:
162         //
163         // ```
164         // fn foo<const N: usize>(a: [(); N + 1]) {
165         //     bar::<{ N + 1 }>();
166         // }
167         // ```
168         ExprKind::Block { block } => {
169             if let thir::Block { stmts: box [], expr: Some(e), .. } = &body.blocks[*block] {
170                 recurse_build(tcx, body, *e, root_span)?
171             } else {
172                 maybe_supported_error(GenericConstantTooComplexSub::BlockNotSupported(node.span))?
173             }
174         }
175         // `ExprKind::Use` happens when a `hir::ExprKind::Cast` is a
176         // "coercion cast" i.e. using a coercion or is a no-op.
177         // This is important so that `N as usize as usize` doesnt unify with `N as usize`. (untested)
178         &ExprKind::Use { source } => {
179             let arg = recurse_build(tcx, body, source, root_span)?;
180             ty::Const::new_expr(tcx, Expr::Cast(CastKind::Use, arg, node.ty), node.ty)
181         }
182         &ExprKind::Cast { source } => {
183             let arg = recurse_build(tcx, body, source, root_span)?;
184             ty::Const::new_expr(tcx, Expr::Cast(CastKind::As, arg, node.ty), node.ty)
185         }
186         ExprKind::Borrow { arg, .. } => {
187             let arg_node = &body.exprs[*arg];
188 
189             // Skip reborrows for now until we allow Deref/Borrow/AddressOf
190             // expressions.
191             // FIXME(generic_const_exprs): Verify/explain why this is sound
192             if let ExprKind::Deref { arg } = arg_node.kind {
193                 recurse_build(tcx, body, arg, root_span)?
194             } else {
195                 maybe_supported_error(GenericConstantTooComplexSub::BorrowNotSupported(node.span))?
196             }
197         }
198         // FIXME(generic_const_exprs): We may want to support these.
199         ExprKind::AddressOf { .. } | ExprKind::Deref { .. } => maybe_supported_error(
200             GenericConstantTooComplexSub::AddressAndDerefNotSupported(node.span),
201         )?,
202         ExprKind::Repeat { .. } | ExprKind::Array { .. } => {
203             maybe_supported_error(GenericConstantTooComplexSub::ArrayNotSupported(node.span))?
204         }
205         ExprKind::NeverToAny { .. } => {
206             maybe_supported_error(GenericConstantTooComplexSub::NeverToAnyNotSupported(node.span))?
207         }
208         ExprKind::Tuple { .. } => {
209             maybe_supported_error(GenericConstantTooComplexSub::TupleNotSupported(node.span))?
210         }
211         ExprKind::Index { .. } => {
212             maybe_supported_error(GenericConstantTooComplexSub::IndexNotSupported(node.span))?
213         }
214         ExprKind::Field { .. } => {
215             maybe_supported_error(GenericConstantTooComplexSub::FieldNotSupported(node.span))?
216         }
217         ExprKind::ConstBlock { .. } => {
218             maybe_supported_error(GenericConstantTooComplexSub::ConstBlockNotSupported(node.span))?
219         }
220         ExprKind::Adt(_) => {
221             maybe_supported_error(GenericConstantTooComplexSub::AdtNotSupported(node.span))?
222         }
223         // dont know if this is correct
224         ExprKind::PointerCoercion { .. } => {
225             error(GenericConstantTooComplexSub::PointerNotSupported(node.span))?
226         }
227         ExprKind::Yield { .. } => {
228             error(GenericConstantTooComplexSub::YieldNotSupported(node.span))?
229         }
230         ExprKind::Continue { .. } | ExprKind::Break { .. } | ExprKind::Loop { .. } => {
231             error(GenericConstantTooComplexSub::LoopNotSupported(node.span))?
232         }
233         ExprKind::Box { .. } => error(GenericConstantTooComplexSub::BoxNotSupported(node.span))?,
234 
235         ExprKind::Unary { .. } => unreachable!(),
236         // we handle valid unary/binary ops above
237         ExprKind::Binary { .. } => {
238             error(GenericConstantTooComplexSub::BinaryNotSupported(node.span))?
239         }
240         ExprKind::LogicalOp { .. } => {
241             error(GenericConstantTooComplexSub::LogicalOpNotSupported(node.span))?
242         }
243         ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
244             error(GenericConstantTooComplexSub::AssignNotSupported(node.span))?
245         }
246         // FIXME(explicit_tail_calls): maybe get `become` a new error
247         ExprKind::Closure { .. } | ExprKind::Return { .. } | ExprKind::Become { .. } => {
248             error(GenericConstantTooComplexSub::ClosureAndReturnNotSupported(node.span))?
249         }
250         // let expressions imply control flow
251         ExprKind::Match { .. } | ExprKind::If { .. } | ExprKind::Let { .. } => {
252             error(GenericConstantTooComplexSub::ControlFlowNotSupported(node.span))?
253         }
254         ExprKind::InlineAsm { .. } => {
255             error(GenericConstantTooComplexSub::InlineAsmNotSupported(node.span))?
256         }
257 
258         // we dont permit let stmts so `VarRef` and `UpvarRef` cant happen
259         ExprKind::VarRef { .. }
260         | ExprKind::UpvarRef { .. }
261         | ExprKind::StaticRef { .. }
262         | ExprKind::OffsetOf { .. }
263         | ExprKind::ThreadLocalRef(_) => {
264             error(GenericConstantTooComplexSub::OperationNotSupported(node.span))?
265         }
266     })
267 }
268 
269 struct IsThirPolymorphic<'a, 'tcx> {
270     is_poly: bool,
271     thir: &'a thir::Thir<'tcx>,
272 }
273 
error( tcx: TyCtxt<'_>, sub: GenericConstantTooComplexSub, root_span: Span, ) -> Result<!, ErrorGuaranteed>274 fn error(
275     tcx: TyCtxt<'_>,
276     sub: GenericConstantTooComplexSub,
277     root_span: Span,
278 ) -> Result<!, ErrorGuaranteed> {
279     let reported = tcx.sess.emit_err(GenericConstantTooComplex {
280         span: root_span,
281         maybe_supported: None,
282         sub,
283     });
284 
285     Err(reported)
286 }
287 
maybe_supported_error( tcx: TyCtxt<'_>, sub: GenericConstantTooComplexSub, root_span: Span, ) -> Result<!, ErrorGuaranteed>288 fn maybe_supported_error(
289     tcx: TyCtxt<'_>,
290     sub: GenericConstantTooComplexSub,
291     root_span: Span,
292 ) -> Result<!, ErrorGuaranteed> {
293     let reported = tcx.sess.emit_err(GenericConstantTooComplex {
294         span: root_span,
295         maybe_supported: Some(()),
296         sub,
297     });
298 
299     Err(reported)
300 }
301 
302 impl<'a, 'tcx> IsThirPolymorphic<'a, 'tcx> {
expr_is_poly(&mut self, expr: &thir::Expr<'tcx>) -> bool303     fn expr_is_poly(&mut self, expr: &thir::Expr<'tcx>) -> bool {
304         if expr.ty.has_non_region_param() {
305             return true;
306         }
307 
308         match expr.kind {
309             thir::ExprKind::NamedConst { substs, .. }
310             | thir::ExprKind::ConstBlock { substs, .. } => substs.has_non_region_param(),
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::AddressOf { .. }
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::Closure(_)
350             | thir::ExprKind::Literal { .. }
351             | thir::ExprKind::NonHirLiteral { .. }
352             | thir::ExprKind::ZstLiteral { .. }
353             | thir::ExprKind::StaticRef { .. }
354             | thir::ExprKind::InlineAsm(_)
355             | thir::ExprKind::OffsetOf { .. }
356             | thir::ExprKind::ThreadLocalRef(_)
357             | thir::ExprKind::Yield { .. } => false,
358         }
359     }
pat_is_poly(&mut self, pat: &thir::Pat<'tcx>) -> bool360     fn pat_is_poly(&mut self, pat: &thir::Pat<'tcx>) -> bool {
361         if pat.ty.has_non_region_param() {
362             return true;
363         }
364 
365         match pat.kind {
366             thir::PatKind::Constant { value } => value.has_non_region_param(),
367             thir::PatKind::Range(box thir::PatRange { lo, hi, .. }) => {
368                 lo.has_non_region_param() || hi.has_non_region_param()
369             }
370             _ => false,
371         }
372     }
373 }
374 
375 impl<'a, 'tcx> visit::Visitor<'a, 'tcx> for IsThirPolymorphic<'a, 'tcx> {
thir(&self) -> &'a thir::Thir<'tcx>376     fn thir(&self) -> &'a thir::Thir<'tcx> {
377         &self.thir
378     }
379 
380     #[instrument(skip(self), level = "debug")]
visit_expr(&mut self, expr: &thir::Expr<'tcx>)381     fn visit_expr(&mut self, expr: &thir::Expr<'tcx>) {
382         self.is_poly |= self.expr_is_poly(expr);
383         if !self.is_poly {
384             visit::walk_expr(self, expr)
385         }
386     }
387 
388     #[instrument(skip(self), level = "debug")]
visit_pat(&mut self, pat: &thir::Pat<'tcx>)389     fn visit_pat(&mut self, pat: &thir::Pat<'tcx>) {
390         self.is_poly |= self.pat_is_poly(pat);
391         if !self.is_poly {
392             visit::walk_pat(self, pat);
393         }
394     }
395 }
396 
397 /// Builds an abstract const, do not use this directly, but use `AbstractConst::new` instead.
thir_abstract_const( tcx: TyCtxt<'_>, def: LocalDefId, ) -> Result<Option<ty::EarlyBinder<ty::Const<'_>>>, ErrorGuaranteed>398 pub fn thir_abstract_const(
399     tcx: TyCtxt<'_>,
400     def: LocalDefId,
401 ) -> Result<Option<ty::EarlyBinder<ty::Const<'_>>>, ErrorGuaranteed> {
402     if !tcx.features().generic_const_exprs {
403         return Ok(None);
404     }
405 
406     match tcx.def_kind(def) {
407         // FIXME(generic_const_exprs): We currently only do this for anonymous constants,
408         // meaning that we do not look into associated constants. I(@lcnr) am not yet sure whether
409         // we want to look into them or treat them as opaque projections.
410         //
411         // Right now we do neither of that and simply always fail to unify them.
412         DefKind::AnonConst | DefKind::InlineConst => (),
413         _ => return Ok(None),
414     }
415 
416     let body = tcx.thir_body(def)?;
417     let (body, body_id) = (&*body.0.borrow(), body.1);
418 
419     let mut is_poly_vis = IsThirPolymorphic { is_poly: false, thir: body };
420     visit::walk_expr(&mut is_poly_vis, &body[body_id]);
421     if !is_poly_vis.is_poly {
422         return Ok(None);
423     }
424 
425     let root_span = body.exprs[body_id].span;
426 
427     Ok(Some(ty::EarlyBinder::bind(recurse_build(tcx, body, body_id, root_span)?)))
428 }
429 
provide(providers: &mut Providers)430 pub fn provide(providers: &mut Providers) {
431     *providers = Providers { destructure_const, thir_abstract_const, ..*providers };
432 }
433