2022-07-09 04:35:06 -05:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
|
|
|
use rustc_hir::def::DefKind;
|
|
|
|
use rustc_hir::def_id::LocalDefId;
|
|
|
|
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
|
2022-07-27 02:27:52 -05:00
|
|
|
use rustc_middle::thir::visit;
|
|
|
|
use rustc_middle::thir::visit::Visitor;
|
|
|
|
use rustc_middle::ty::abstract_const::CastKind;
|
2022-11-28 06:28:32 -06:00
|
|
|
use rustc_middle::ty::{self, Expr, TyCtxt, TypeVisitable};
|
2022-07-09 04:35:06 -05:00
|
|
|
use rustc_middle::{mir, thir};
|
|
|
|
use rustc_span::Span;
|
2022-06-28 15:35:48 -05:00
|
|
|
use rustc_target::abi::VariantIdx;
|
|
|
|
|
2022-06-28 16:26:54 -05:00
|
|
|
use std::iter;
|
|
|
|
|
2022-08-18 18:04:31 -05:00
|
|
|
use crate::errors::{GenericConstantTooComplex, GenericConstantTooComplexSub};
|
|
|
|
|
2022-06-29 03:30:47 -05:00
|
|
|
/// Destructures array, ADT or tuple constants into the constants
|
|
|
|
/// of their fields.
|
2022-06-28 15:35:48 -05:00
|
|
|
pub(crate) fn destructure_const<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
const_: ty::Const<'tcx>,
|
|
|
|
) -> ty::DestructuredConst<'tcx> {
|
2022-06-28 16:26:54 -05:00
|
|
|
let ty::ConstKind::Value(valtree) = const_.kind() else {
|
|
|
|
bug!("cannot destructure constant {:?}", const_)
|
|
|
|
};
|
2022-06-28 15:35:48 -05:00
|
|
|
|
2022-06-28 16:26:54 -05:00
|
|
|
let branches = match valtree {
|
|
|
|
ty::ValTree::Branch(b) => b,
|
|
|
|
_ => bug!("cannot destructure constant {:?}", const_),
|
|
|
|
};
|
2022-06-28 15:35:48 -05:00
|
|
|
|
2022-06-28 16:26:54 -05:00
|
|
|
let (fields, variant) = match const_.ty().kind() {
|
|
|
|
ty::Array(inner_ty, _) | ty::Slice(inner_ty) => {
|
|
|
|
// construct the consts for the elements of the array/slice
|
2022-11-28 06:28:32 -06:00
|
|
|
let field_consts =
|
|
|
|
branches.iter().map(|b| tcx.mk_const(*b, *inner_ty)).collect::<Vec<_>>();
|
2022-06-28 16:26:54 -05:00
|
|
|
debug!(?field_consts);
|
2022-06-28 15:35:48 -05:00
|
|
|
|
2022-06-28 16:26:54 -05:00
|
|
|
(field_consts, None)
|
|
|
|
}
|
|
|
|
ty::Adt(def, _) if def.variants().is_empty() => bug!("unreachable"),
|
|
|
|
ty::Adt(def, substs) => {
|
|
|
|
let (variant_idx, branches) = if def.is_enum() {
|
|
|
|
let (head, rest) = branches.split_first().unwrap();
|
|
|
|
(VariantIdx::from_u32(head.unwrap_leaf().try_to_u32().unwrap()), rest)
|
|
|
|
} else {
|
|
|
|
(VariantIdx::from_u32(0), branches)
|
|
|
|
};
|
|
|
|
let fields = &def.variant(variant_idx).fields;
|
|
|
|
let mut field_consts = Vec::with_capacity(fields.len());
|
2022-06-28 15:35:48 -05:00
|
|
|
|
2022-06-28 16:26:54 -05:00
|
|
|
for (field, field_valtree) in iter::zip(fields, branches) {
|
|
|
|
let field_ty = field.ty(tcx, substs);
|
2022-11-28 06:28:32 -06:00
|
|
|
let field_const = tcx.mk_const(*field_valtree, field_ty);
|
2022-06-28 16:26:54 -05:00
|
|
|
field_consts.push(field_const);
|
2022-06-28 15:35:48 -05:00
|
|
|
}
|
2022-06-28 16:26:54 -05:00
|
|
|
debug!(?field_consts);
|
|
|
|
|
|
|
|
(field_consts, Some(variant_idx))
|
|
|
|
}
|
|
|
|
ty::Tuple(elem_tys) => {
|
|
|
|
let fields = iter::zip(*elem_tys, branches)
|
2022-11-28 06:28:32 -06:00
|
|
|
.map(|(elem_ty, elem_valtree)| tcx.mk_const(*elem_valtree, elem_ty))
|
2022-06-28 16:26:54 -05:00
|
|
|
.collect::<Vec<_>>();
|
2022-06-28 15:35:48 -05:00
|
|
|
|
2022-06-28 16:26:54 -05:00
|
|
|
(fields, None)
|
|
|
|
}
|
|
|
|
_ => bug!("cannot destructure constant {:?}", const_),
|
|
|
|
};
|
2022-06-28 15:35:48 -05:00
|
|
|
|
2022-06-28 16:26:54 -05:00
|
|
|
let fields = tcx.arena.alloc_from_iter(fields.into_iter());
|
2022-06-28 15:35:48 -05:00
|
|
|
|
2022-06-28 16:26:54 -05:00
|
|
|
ty::DestructuredConst { variant, fields }
|
2022-06-28 15:35:48 -05:00
|
|
|
}
|
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
/// We do not allow all binary operations in abstract consts, so filter disallowed ones.
|
|
|
|
fn check_binop(op: mir::BinOp) -> bool {
|
|
|
|
use mir::BinOp::*;
|
|
|
|
match op {
|
|
|
|
Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr | Shl | Shr | Eq | Lt | Le | Ne
|
|
|
|
| Ge | Gt => true,
|
|
|
|
Offset => false,
|
2022-07-09 04:35:06 -05:00
|
|
|
}
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
2022-08-18 18:04:31 -05:00
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
/// While we currently allow all unary operations, we still want to explicitly guard against
|
|
|
|
/// future changes here.
|
|
|
|
fn check_unop(op: mir::UnOp) -> bool {
|
|
|
|
use mir::UnOp::*;
|
|
|
|
match op {
|
|
|
|
Not | Neg => true,
|
2022-07-09 04:35:06 -05:00
|
|
|
}
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
2022-07-09 04:35:06 -05:00
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
fn recurse_build<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
body: &thir::Thir<'tcx>,
|
|
|
|
node: thir::ExprId,
|
|
|
|
root_span: Span,
|
|
|
|
) -> Result<ty::Const<'tcx>, ErrorGuaranteed> {
|
|
|
|
use thir::ExprKind;
|
|
|
|
let node = &body.exprs[node];
|
2022-11-15 17:23:31 -06:00
|
|
|
|
|
|
|
let maybe_supported_error = |a| maybe_supported_error(tcx, a, root_span);
|
|
|
|
let error = |a| error(tcx, a, root_span);
|
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
Ok(match &node.kind {
|
|
|
|
// I dont know if handling of these 3 is correct
|
|
|
|
&ExprKind::Scope { value, .. } => recurse_build(tcx, body, value, root_span)?,
|
|
|
|
&ExprKind::PlaceTypeAscription { source, .. }
|
|
|
|
| &ExprKind::ValueTypeAscription { source, .. } => {
|
|
|
|
recurse_build(tcx, body, source, root_span)?
|
2022-07-09 04:35:06 -05:00
|
|
|
}
|
2022-07-27 02:27:52 -05:00
|
|
|
&ExprKind::Literal { lit, neg } => {
|
|
|
|
let sp = node.span;
|
|
|
|
match tcx.at(sp).lit_to_const(LitToConstInput { lit: &lit.node, ty: node.ty, neg }) {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(LitToConstError::Reported(guar)) => {
|
|
|
|
tcx.const_error_with_guaranteed(node.ty, guar)
|
2022-07-09 04:35:06 -05:00
|
|
|
}
|
2022-07-27 02:27:52 -05:00
|
|
|
Err(LitToConstError::TypeError) => {
|
|
|
|
bug!("encountered type error in lit_to_const")
|
2022-07-09 04:35:06 -05:00
|
|
|
}
|
|
|
|
}
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
|
|
|
&ExprKind::NonHirLiteral { lit, user_ty: _ } => {
|
|
|
|
let val = ty::ValTree::from_scalar_int(lit);
|
2022-11-28 06:38:15 -06:00
|
|
|
tcx.mk_const(val, node.ty)
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
|
|
|
&ExprKind::ZstLiteral { user_ty: _ } => {
|
|
|
|
let val = ty::ValTree::zst();
|
2022-11-28 06:38:15 -06:00
|
|
|
tcx.mk_const(val, node.ty)
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
|
|
|
&ExprKind::NamedConst { def_id, substs, user_ty: _ } => {
|
|
|
|
let uneval = ty::UnevaluatedConst::new(ty::WithOptConstParam::unknown(def_id), substs);
|
2022-11-28 06:28:32 -06:00
|
|
|
tcx.mk_const(uneval, node.ty)
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
2022-11-28 06:28:32 -06:00
|
|
|
ExprKind::ConstParam { param, .. } => tcx.mk_const(*param, node.ty),
|
2022-07-09 04:35:06 -05:00
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
ExprKind::Call { fun, args, .. } => {
|
|
|
|
let fun = recurse_build(tcx, body, *fun, root_span)?;
|
2022-07-09 04:35:06 -05:00
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
let mut new_args = Vec::<ty::Const<'tcx>>::with_capacity(args.len());
|
|
|
|
for &id in args.iter() {
|
|
|
|
new_args.push(recurse_build(tcx, body, id, root_span)?);
|
2022-07-09 04:35:06 -05:00
|
|
|
}
|
2022-07-27 02:27:52 -05:00
|
|
|
let new_args = tcx.mk_const_list(new_args.iter());
|
2022-11-28 06:28:32 -06:00
|
|
|
tcx.mk_const(Expr::FunctionCall(fun, new_args), node.ty)
|
2022-07-09 04:35:06 -05:00
|
|
|
}
|
2022-07-27 02:27:52 -05:00
|
|
|
&ExprKind::Binary { op, lhs, rhs } if check_binop(op) => {
|
|
|
|
let lhs = recurse_build(tcx, body, lhs, root_span)?;
|
|
|
|
let rhs = recurse_build(tcx, body, rhs, root_span)?;
|
2022-11-28 06:28:32 -06:00
|
|
|
tcx.mk_const(Expr::Binop(op, lhs, rhs), node.ty)
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
|
|
|
&ExprKind::Unary { op, arg } if check_unop(op) => {
|
|
|
|
let arg = recurse_build(tcx, body, arg, root_span)?;
|
2022-11-28 06:28:32 -06:00
|
|
|
tcx.mk_const(Expr::UnOp(op, arg), node.ty)
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
|
|
|
// This is necessary so that the following compiles:
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn foo<const N: usize>(a: [(); N + 1]) {
|
|
|
|
// bar::<{ N + 1 }>();
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
ExprKind::Block { block } => {
|
|
|
|
if let thir::Block { stmts: box [], expr: Some(e), .. } = &body.blocks[*block] {
|
|
|
|
recurse_build(tcx, body, *e, root_span)?
|
|
|
|
} else {
|
2022-11-15 17:23:31 -06:00
|
|
|
maybe_supported_error(GenericConstantTooComplexSub::BlockNotSupported(node.span))?
|
2022-07-09 04:35:06 -05:00
|
|
|
}
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
|
|
|
// `ExprKind::Use` happens when a `hir::ExprKind::Cast` is a
|
|
|
|
// "coercion cast" i.e. using a coercion or is a no-op.
|
|
|
|
// This is important so that `N as usize as usize` doesnt unify with `N as usize`. (untested)
|
|
|
|
&ExprKind::Use { source } => {
|
|
|
|
let arg = recurse_build(tcx, body, source, root_span)?;
|
2022-11-28 06:28:32 -06:00
|
|
|
tcx.mk_const(Expr::Cast(CastKind::Use, arg, node.ty), node.ty)
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
|
|
|
&ExprKind::Cast { source } => {
|
|
|
|
let arg = recurse_build(tcx, body, source, root_span)?;
|
2022-11-28 06:28:32 -06:00
|
|
|
tcx.mk_const(Expr::Cast(CastKind::As, arg, node.ty), node.ty)
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
|
|
|
ExprKind::Borrow { arg, .. } => {
|
|
|
|
let arg_node = &body.exprs[*arg];
|
|
|
|
|
|
|
|
// Skip reborrows for now until we allow Deref/Borrow/AddressOf
|
|
|
|
// expressions.
|
|
|
|
// FIXME(generic_const_exprs): Verify/explain why this is sound
|
|
|
|
if let ExprKind::Deref { arg } = arg_node.kind {
|
|
|
|
recurse_build(tcx, body, arg, root_span)?
|
|
|
|
} else {
|
2022-11-15 17:23:31 -06:00
|
|
|
maybe_supported_error(GenericConstantTooComplexSub::BorrowNotSupported(node.span))?
|
2022-07-09 04:35:06 -05:00
|
|
|
}
|
|
|
|
}
|
2022-07-27 02:27:52 -05:00
|
|
|
// FIXME(generic_const_exprs): We may want to support these.
|
|
|
|
ExprKind::AddressOf { .. } | ExprKind::Deref { .. } => maybe_supported_error(
|
|
|
|
GenericConstantTooComplexSub::AddressAndDerefNotSupported(node.span),
|
|
|
|
)?,
|
2022-11-15 17:23:31 -06:00
|
|
|
ExprKind::Repeat { .. } | ExprKind::Array { .. } => {
|
|
|
|
maybe_supported_error(GenericConstantTooComplexSub::ArrayNotSupported(node.span))?
|
|
|
|
}
|
|
|
|
ExprKind::NeverToAny { .. } => {
|
|
|
|
maybe_supported_error(GenericConstantTooComplexSub::NeverToAnyNotSupported(node.span))?
|
|
|
|
}
|
|
|
|
ExprKind::Tuple { .. } => {
|
|
|
|
maybe_supported_error(GenericConstantTooComplexSub::TupleNotSupported(node.span))?
|
|
|
|
}
|
|
|
|
ExprKind::Index { .. } => {
|
|
|
|
maybe_supported_error(GenericConstantTooComplexSub::IndexNotSupported(node.span))?
|
|
|
|
}
|
|
|
|
ExprKind::Field { .. } => {
|
|
|
|
maybe_supported_error(GenericConstantTooComplexSub::FieldNotSupported(node.span))?
|
|
|
|
}
|
|
|
|
ExprKind::ConstBlock { .. } => {
|
|
|
|
maybe_supported_error(GenericConstantTooComplexSub::ConstBlockNotSupported(node.span))?
|
|
|
|
}
|
|
|
|
ExprKind::Adt(_) => {
|
|
|
|
maybe_supported_error(GenericConstantTooComplexSub::AdtNotSupported(node.span))?
|
|
|
|
}
|
2022-07-27 02:27:52 -05:00
|
|
|
// dont know if this is correct
|
|
|
|
ExprKind::Pointer { .. } => {
|
2022-11-15 17:23:31 -06:00
|
|
|
error(GenericConstantTooComplexSub::PointerNotSupported(node.span))?
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
|
|
|
ExprKind::Yield { .. } => {
|
2022-11-15 17:23:31 -06:00
|
|
|
error(GenericConstantTooComplexSub::YieldNotSupported(node.span))?
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
|
|
|
ExprKind::Continue { .. } | ExprKind::Break { .. } | ExprKind::Loop { .. } => {
|
2022-11-15 17:23:31 -06:00
|
|
|
error(GenericConstantTooComplexSub::LoopNotSupported(node.span))?
|
2022-07-09 04:35:06 -05:00
|
|
|
}
|
2022-11-15 17:23:31 -06:00
|
|
|
ExprKind::Box { .. } => error(GenericConstantTooComplexSub::BoxNotSupported(node.span))?,
|
2022-07-09 04:35:06 -05:00
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
ExprKind::Unary { .. } => unreachable!(),
|
|
|
|
// we handle valid unary/binary ops above
|
|
|
|
ExprKind::Binary { .. } => {
|
2022-11-15 17:23:31 -06:00
|
|
|
error(GenericConstantTooComplexSub::BinaryNotSupported(node.span))?
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
|
|
|
ExprKind::LogicalOp { .. } => {
|
2022-11-15 17:23:31 -06:00
|
|
|
error(GenericConstantTooComplexSub::LogicalOpNotSupported(node.span))?
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
|
|
|
ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
|
2022-11-15 17:23:31 -06:00
|
|
|
error(GenericConstantTooComplexSub::AssignNotSupported(node.span))?
|
|
|
|
}
|
|
|
|
ExprKind::Closure { .. } | ExprKind::Return { .. } => {
|
|
|
|
error(GenericConstantTooComplexSub::ClosureAndReturnNotSupported(node.span))?
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
|
|
|
// let expressions imply control flow
|
|
|
|
ExprKind::Match { .. } | ExprKind::If { .. } | ExprKind::Let { .. } => {
|
2022-11-15 17:23:31 -06:00
|
|
|
error(GenericConstantTooComplexSub::ControlFlowNotSupported(node.span))?
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
|
|
|
ExprKind::InlineAsm { .. } => {
|
2022-11-15 17:23:31 -06:00
|
|
|
error(GenericConstantTooComplexSub::InlineAsmNotSupported(node.span))?
|
2022-07-09 04:35:06 -05:00
|
|
|
}
|
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
// we dont permit let stmts so `VarRef` and `UpvarRef` cant happen
|
|
|
|
ExprKind::VarRef { .. }
|
|
|
|
| ExprKind::UpvarRef { .. }
|
|
|
|
| ExprKind::StaticRef { .. }
|
|
|
|
| ExprKind::ThreadLocalRef(_) => {
|
2022-11-15 17:23:31 -06:00
|
|
|
error(GenericConstantTooComplexSub::OperationNotSupported(node.span))?
|
2022-07-09 04:35:06 -05:00
|
|
|
}
|
2022-07-27 02:27:52 -05:00
|
|
|
})
|
|
|
|
}
|
2022-07-09 04:35:06 -05:00
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
struct IsThirPolymorphic<'a, 'tcx> {
|
|
|
|
is_poly: bool,
|
|
|
|
thir: &'a thir::Thir<'tcx>,
|
|
|
|
}
|
2022-07-09 04:35:06 -05:00
|
|
|
|
2022-12-20 15:10:40 -06:00
|
|
|
fn error(
|
|
|
|
tcx: TyCtxt<'_>,
|
2022-07-27 02:27:52 -05:00
|
|
|
sub: GenericConstantTooComplexSub,
|
|
|
|
root_span: Span,
|
|
|
|
) -> Result<!, ErrorGuaranteed> {
|
|
|
|
let reported = tcx.sess.emit_err(GenericConstantTooComplex {
|
|
|
|
span: root_span,
|
|
|
|
maybe_supported: None,
|
|
|
|
sub,
|
|
|
|
});
|
|
|
|
|
|
|
|
Err(reported)
|
|
|
|
}
|
2022-07-09 04:35:06 -05:00
|
|
|
|
2022-12-20 15:10:40 -06:00
|
|
|
fn maybe_supported_error(
|
|
|
|
tcx: TyCtxt<'_>,
|
2022-07-27 02:27:52 -05:00
|
|
|
sub: GenericConstantTooComplexSub,
|
|
|
|
root_span: Span,
|
|
|
|
) -> Result<!, ErrorGuaranteed> {
|
|
|
|
let reported = tcx.sess.emit_err(GenericConstantTooComplex {
|
|
|
|
span: root_span,
|
|
|
|
maybe_supported: Some(()),
|
|
|
|
sub,
|
|
|
|
});
|
|
|
|
|
|
|
|
Err(reported)
|
|
|
|
}
|
2022-07-09 04:35:06 -05:00
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
impl<'a, 'tcx> IsThirPolymorphic<'a, 'tcx> {
|
|
|
|
fn expr_is_poly(&mut self, expr: &thir::Expr<'tcx>) -> bool {
|
|
|
|
if expr.ty.has_non_region_param() {
|
|
|
|
return true;
|
|
|
|
}
|
2022-07-09 04:35:06 -05:00
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
match expr.kind {
|
2023-01-16 13:55:20 -06:00
|
|
|
thir::ExprKind::NamedConst { substs, .. }
|
|
|
|
| thir::ExprKind::ConstBlock { substs, .. } => substs.has_non_region_param(),
|
2022-07-27 02:27:52 -05:00
|
|
|
thir::ExprKind::ConstParam { .. } => true,
|
|
|
|
thir::ExprKind::Repeat { value, count } => {
|
|
|
|
self.visit_expr(&self.thir()[value]);
|
|
|
|
count.has_non_region_param()
|
2022-07-09 04:35:06 -05:00
|
|
|
}
|
2023-01-16 13:55:20 -06:00
|
|
|
thir::ExprKind::Scope { .. }
|
|
|
|
| thir::ExprKind::Box { .. }
|
|
|
|
| thir::ExprKind::If { .. }
|
|
|
|
| thir::ExprKind::Call { .. }
|
|
|
|
| thir::ExprKind::Deref { .. }
|
|
|
|
| thir::ExprKind::Binary { .. }
|
|
|
|
| thir::ExprKind::LogicalOp { .. }
|
|
|
|
| thir::ExprKind::Unary { .. }
|
|
|
|
| thir::ExprKind::Cast { .. }
|
|
|
|
| thir::ExprKind::Use { .. }
|
|
|
|
| thir::ExprKind::NeverToAny { .. }
|
|
|
|
| thir::ExprKind::Pointer { .. }
|
|
|
|
| thir::ExprKind::Loop { .. }
|
|
|
|
| thir::ExprKind::Let { .. }
|
|
|
|
| thir::ExprKind::Match { .. }
|
|
|
|
| thir::ExprKind::Block { .. }
|
|
|
|
| thir::ExprKind::Assign { .. }
|
|
|
|
| thir::ExprKind::AssignOp { .. }
|
|
|
|
| thir::ExprKind::Field { .. }
|
|
|
|
| thir::ExprKind::Index { .. }
|
|
|
|
| thir::ExprKind::VarRef { .. }
|
|
|
|
| thir::ExprKind::UpvarRef { .. }
|
|
|
|
| thir::ExprKind::Borrow { .. }
|
|
|
|
| thir::ExprKind::AddressOf { .. }
|
|
|
|
| thir::ExprKind::Break { .. }
|
|
|
|
| thir::ExprKind::Continue { .. }
|
|
|
|
| thir::ExprKind::Return { .. }
|
|
|
|
| thir::ExprKind::Array { .. }
|
|
|
|
| thir::ExprKind::Tuple { .. }
|
|
|
|
| thir::ExprKind::Adt(_)
|
|
|
|
| thir::ExprKind::PlaceTypeAscription { .. }
|
|
|
|
| thir::ExprKind::ValueTypeAscription { .. }
|
|
|
|
| thir::ExprKind::Closure(_)
|
|
|
|
| thir::ExprKind::Literal { .. }
|
|
|
|
| thir::ExprKind::NonHirLiteral { .. }
|
|
|
|
| thir::ExprKind::ZstLiteral { .. }
|
|
|
|
| thir::ExprKind::StaticRef { .. }
|
|
|
|
| thir::ExprKind::InlineAsm(_)
|
|
|
|
| thir::ExprKind::ThreadLocalRef(_)
|
|
|
|
| thir::ExprKind::Yield { .. } => false,
|
2022-07-27 02:27:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn pat_is_poly(&mut self, pat: &thir::Pat<'tcx>) -> bool {
|
|
|
|
if pat.ty.has_non_region_param() {
|
|
|
|
return true;
|
|
|
|
}
|
2022-07-09 04:35:06 -05:00
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
match pat.kind {
|
|
|
|
thir::PatKind::Constant { value } => value.has_non_region_param(),
|
|
|
|
thir::PatKind::Range(box thir::PatRange { lo, hi, .. }) => {
|
|
|
|
lo.has_non_region_param() || hi.has_non_region_param()
|
2022-07-09 04:35:06 -05:00
|
|
|
}
|
2022-07-27 02:27:52 -05:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-09 04:35:06 -05:00
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
impl<'a, 'tcx> visit::Visitor<'a, 'tcx> for IsThirPolymorphic<'a, 'tcx> {
|
|
|
|
fn thir(&self) -> &'a thir::Thir<'tcx> {
|
|
|
|
&self.thir
|
|
|
|
}
|
2022-07-09 04:35:06 -05:00
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
#[instrument(skip(self), level = "debug")]
|
|
|
|
fn visit_expr(&mut self, expr: &thir::Expr<'tcx>) {
|
|
|
|
self.is_poly |= self.expr_is_poly(expr);
|
|
|
|
if !self.is_poly {
|
|
|
|
visit::walk_expr(self, expr)
|
|
|
|
}
|
|
|
|
}
|
2022-07-09 04:35:06 -05:00
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
#[instrument(skip(self), level = "debug")]
|
|
|
|
fn visit_pat(&mut self, pat: &thir::Pat<'tcx>) {
|
|
|
|
self.is_poly |= self.pat_is_poly(pat);
|
|
|
|
if !self.is_poly {
|
|
|
|
visit::walk_pat(self, pat);
|
|
|
|
}
|
2022-07-09 04:35:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds an abstract const, do not use this directly, but use `AbstractConst::new` instead.
|
2022-12-20 15:10:40 -06:00
|
|
|
pub fn thir_abstract_const(
|
|
|
|
tcx: TyCtxt<'_>,
|
2022-07-09 04:35:06 -05:00
|
|
|
def: ty::WithOptConstParam<LocalDefId>,
|
2022-12-20 15:10:40 -06:00
|
|
|
) -> Result<Option<ty::Const<'_>>, ErrorGuaranteed> {
|
2022-07-09 04:35:06 -05:00
|
|
|
if tcx.features().generic_const_exprs {
|
|
|
|
match tcx.def_kind(def.did) {
|
|
|
|
// FIXME(generic_const_exprs): We currently only do this for anonymous constants,
|
|
|
|
// meaning that we do not look into associated constants. I(@lcnr) am not yet sure whether
|
|
|
|
// we want to look into them or treat them as opaque projections.
|
|
|
|
//
|
|
|
|
// Right now we do neither of that and simply always fail to unify them.
|
|
|
|
DefKind::AnonConst | DefKind::InlineConst => (),
|
|
|
|
_ => return Ok(None),
|
|
|
|
}
|
|
|
|
|
|
|
|
let body = tcx.thir_body(def)?;
|
2022-07-27 02:27:52 -05:00
|
|
|
let (body, body_id) = (&*body.0.borrow(), body.1);
|
|
|
|
|
|
|
|
let mut is_poly_vis = IsThirPolymorphic { is_poly: false, thir: body };
|
|
|
|
visit::walk_expr(&mut is_poly_vis, &body[body_id]);
|
|
|
|
if !is_poly_vis.is_poly {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
let root_span = body.exprs[body_id].span;
|
2022-07-09 04:35:06 -05:00
|
|
|
|
2022-07-27 02:27:52 -05:00
|
|
|
Some(recurse_build(tcx, body, body_id, root_span)).transpose()
|
2022-07-09 04:35:06 -05:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 15:35:48 -05:00
|
|
|
pub fn provide(providers: &mut ty::query::Providers) {
|
2022-07-09 04:35:06 -05:00
|
|
|
*providers = ty::query::Providers {
|
|
|
|
destructure_const,
|
|
|
|
thir_abstract_const: |tcx, def_id| {
|
|
|
|
let def_id = def_id.expect_local();
|
|
|
|
if let Some(def) = ty::WithOptConstParam::try_lookup(def_id, tcx) {
|
|
|
|
tcx.thir_abstract_const_of_const_arg(def)
|
|
|
|
} else {
|
|
|
|
thir_abstract_const(tcx, ty::WithOptConstParam::unknown(def_id))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
thir_abstract_const_of_const_arg: |tcx, (did, param_did)| {
|
|
|
|
thir_abstract_const(
|
|
|
|
tcx,
|
|
|
|
ty::WithOptConstParam { did, const_param_did: Some(param_did) },
|
|
|
|
)
|
|
|
|
},
|
|
|
|
..*providers
|
|
|
|
};
|
2022-06-28 15:35:48 -05:00
|
|
|
}
|