Skip to main content

rustc_ty_utils/
consts.rs

1use rustc_errors::ErrorGuaranteed;
2use rustc_hir::def::DefKind;
3use rustc_hir::def_id::LocalDefId;
4use rustc_middle::query::Providers;
5use rustc_middle::thir::visit;
6use rustc_middle::thir::visit::Visitor;
7use rustc_middle::ty::abstract_const::CastKind;
8use rustc_middle::ty::{self, Expr, LitToConstInput, TyCtxt, TypeVisitableExt};
9use rustc_middle::{mir, thir};
10use rustc_span::Span;
11use tracing::instrument;
12
13use crate::errors::{GenericConstantTooComplex, GenericConstantTooComplexSub};
14
15/// We do not allow all binary operations in abstract consts, so filter disallowed ones.
16fn check_binop(op: mir::BinOp) -> bool {
17    use mir::BinOp::*;
18    match op {
19        Add | AddUnchecked | AddWithOverflow | Sub | SubUnchecked | SubWithOverflow | Mul
20        | MulUnchecked | MulWithOverflow | Div | Rem | BitXor | BitAnd | BitOr | Shl
21        | ShlUnchecked | Shr | ShrUnchecked | Eq | Lt | Le | Ne | Ge | Gt | Cmp => true,
22        Offset => false,
23    }
24}
25
26/// While we currently allow all unary operations, we still want to explicitly guard against
27/// future changes here.
28fn check_unop(op: mir::UnOp) -> bool {
29    use mir::UnOp::*;
30    match op {
31        Not | Neg | PtrMetadata => true,
32    }
33}
34
35fn recurse_build<'tcx>(
36    tcx: TyCtxt<'tcx>,
37    body: &thir::Thir<'tcx>,
38    node: thir::ExprId,
39    root_span: Span,
40) -> Result<ty::Const<'tcx>, ErrorGuaranteed> {
41    use thir::ExprKind;
42    let node = &body.exprs[node];
43
44    let maybe_supported_error = |a| maybe_supported_error(tcx, a, root_span);
45    let error = |a| error(tcx, a, root_span);
46
47    Ok(match &node.kind {
48        // I dont know if handling of these 3 is correct
49        &ExprKind::Scope { value, .. } => recurse_build(tcx, body, value, root_span)?,
50        &ExprKind::PlaceTypeAscription { source, .. }
51        | &ExprKind::ValueTypeAscription { source, .. } => {
52            recurse_build(tcx, body, source, root_span)?
53        }
54        &ExprKind::PlaceUnwrapUnsafeBinder { .. }
55        | &ExprKind::ValueUnwrapUnsafeBinder { .. }
56        | &ExprKind::WrapUnsafeBinder { .. } => {
57            {
    ::core::panicking::panic_fmt(format_args!("not yet implemented: {0}",
            format_args!("FIXME(unsafe_binders)")));
}todo!("FIXME(unsafe_binders)")
58        }
59        &ExprKind::Literal { lit, neg } => {
60            let sp = node.span;
61            match tcx.at(sp).lit_to_const(LitToConstInput { lit: lit.node, ty: Some(node.ty), neg })
62            {
63                Some(value) => ty::Const::new_value(tcx, value.valtree, value.ty),
64                None => ty::Const::new_misc_error(tcx),
65            }
66        }
67        &ExprKind::NonHirLiteral { lit, user_ty: _ } => {
68            let val = ty::ValTree::from_scalar_int(tcx, lit);
69            ty::Const::new_value(tcx, val, node.ty)
70        }
71        &ExprKind::ZstLiteral { user_ty: _ } => ty::Const::zero_sized(tcx, node.ty),
72        &ExprKind::NamedConst { def_id, args, user_ty: _ } => {
73            let uneval = ty::UnevaluatedConst::new(
74                tcx,
75                ty::UnevaluatedConstKind::new_from_def_id(tcx, def_id),
76                args,
77            );
78            ty::Const::new_unevaluated(tcx, uneval)
79        }
80        ExprKind::ConstParam { param, .. } => ty::Const::new_param(tcx, *param),
81
82        ExprKind::Call { fun, args, .. } => {
83            let fun_ty = body.exprs[*fun].ty;
84            let fun = recurse_build(tcx, body, *fun, root_span)?;
85
86            let mut new_args = Vec::<ty::Const<'tcx>>::with_capacity(args.len());
87            for &id in args.iter() {
88                new_args.push(recurse_build(tcx, body, id, root_span)?);
89            }
90            ty::Const::new_expr(tcx, Expr::new_call(tcx, fun_ty, fun, new_args))
91        }
92        &ExprKind::Binary { op, lhs, rhs } if check_binop(op) => {
93            let lhs_ty = body.exprs[lhs].ty;
94            let lhs = recurse_build(tcx, body, lhs, root_span)?;
95            let rhs_ty = body.exprs[rhs].ty;
96            let rhs = recurse_build(tcx, body, rhs, root_span)?;
97            ty::Const::new_expr(tcx, Expr::new_binop(tcx, op, lhs_ty, rhs_ty, lhs, rhs))
98        }
99        &ExprKind::Unary { op, arg } if check_unop(op) => {
100            let arg_ty = body.exprs[arg].ty;
101            let arg = recurse_build(tcx, body, arg, root_span)?;
102            ty::Const::new_expr(tcx, Expr::new_unop(tcx, op, arg_ty, arg))
103        }
104        // This is necessary so that the following compiles:
105        //
106        // ```
107        // fn foo<const N: usize>(a: [(); N + 1]) {
108        //     bar::<{ N + 1 }>();
109        // }
110        // ```
111        ExprKind::Block { block } => {
112            if let thir::Block { stmts: [], expr: Some(e), .. } = &body.blocks[*block] {
113                recurse_build(tcx, body, *e, root_span)?
114            } else {
115                maybe_supported_error(GenericConstantTooComplexSub::BlockNotSupported(node.span))?
116            }
117        }
118        // `ExprKind::Use` happens when a `hir::ExprKind::Cast` is a
119        // "coercion cast" i.e. using a coercion or is a no-op.
120        // This is important so that `N as usize as usize` doesn't unify with `N as usize`. (untested)
121        &ExprKind::Use { source } => {
122            let value_ty = body.exprs[source].ty;
123            let value = recurse_build(tcx, body, source, root_span)?;
124            ty::Const::new_expr(tcx, Expr::new_cast(tcx, CastKind::Use, value_ty, value, node.ty))
125        }
126        &ExprKind::Cast { source } => {
127            let value_ty = body.exprs[source].ty;
128            let value = recurse_build(tcx, body, source, root_span)?;
129            ty::Const::new_expr(tcx, Expr::new_cast(tcx, CastKind::As, value_ty, value, node.ty))
130        }
131        ExprKind::Borrow { arg, .. } => {
132            let arg_node = &body.exprs[*arg];
133
134            // Skip reborrows for now until we allow Deref/Borrow/RawBorrow
135            // expressions.
136            // FIXME(generic_const_exprs): Verify/explain why this is sound
137            if let ExprKind::Deref { arg } = arg_node.kind {
138                recurse_build(tcx, body, arg, root_span)?
139            } else {
140                maybe_supported_error(GenericConstantTooComplexSub::BorrowNotSupported(node.span))?
141            }
142        }
143        // FIXME(generic_const_exprs): We may want to support these.
144        ExprKind::RawBorrow { .. } | ExprKind::Deref { .. } => maybe_supported_error(
145            GenericConstantTooComplexSub::AddressAndDerefNotSupported(node.span),
146        )?,
147        ExprKind::Repeat { .. } | ExprKind::Array { .. } => {
148            maybe_supported_error(GenericConstantTooComplexSub::ArrayNotSupported(node.span))?
149        }
150        ExprKind::NeverToAny { .. } => {
151            maybe_supported_error(GenericConstantTooComplexSub::NeverToAnyNotSupported(node.span))?
152        }
153        ExprKind::Tuple { .. } => {
154            maybe_supported_error(GenericConstantTooComplexSub::TupleNotSupported(node.span))?
155        }
156        ExprKind::Index { .. } => {
157            maybe_supported_error(GenericConstantTooComplexSub::IndexNotSupported(node.span))?
158        }
159        ExprKind::Field { .. } => {
160            maybe_supported_error(GenericConstantTooComplexSub::FieldNotSupported(node.span))?
161        }
162        ExprKind::ConstBlock { .. } => {
163            maybe_supported_error(GenericConstantTooComplexSub::ConstBlockNotSupported(node.span))?
164        }
165        ExprKind::Adt(_) => {
166            maybe_supported_error(GenericConstantTooComplexSub::AdtNotSupported(node.span))?
167        }
168        // dont know if this is correct
169        ExprKind::PointerCoercion { .. } => {
170            error(GenericConstantTooComplexSub::PointerNotSupported(node.span))?
171        }
172        ExprKind::Yield { .. } => {
173            error(GenericConstantTooComplexSub::YieldNotSupported(node.span))?
174        }
175        ExprKind::Continue { .. }
176        | ExprKind::ConstContinue { .. }
177        | ExprKind::Break { .. }
178        | ExprKind::Loop { .. }
179        | ExprKind::LoopMatch { .. } => {
180            error(GenericConstantTooComplexSub::LoopNotSupported(node.span))?
181        }
182        ExprKind::ByUse { .. } => {
183            error(GenericConstantTooComplexSub::ByUseNotSupported(node.span))?
184        }
185        ExprKind::Unary { .. } => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
186        // we handle valid unary/binary ops above
187        ExprKind::Binary { .. } => {
188            error(GenericConstantTooComplexSub::BinaryNotSupported(node.span))?
189        }
190        ExprKind::LogicalOp { .. } => {
191            error(GenericConstantTooComplexSub::LogicalOpNotSupported(node.span))?
192        }
193        ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
194            error(GenericConstantTooComplexSub::AssignNotSupported(node.span))?
195        }
196        // FIXME(explicit_tail_calls): maybe get `become` a new error
197        ExprKind::Closure { .. } | ExprKind::Return { .. } | ExprKind::Become { .. } => {
198            error(GenericConstantTooComplexSub::ClosureAndReturnNotSupported(node.span))?
199        }
200        // let expressions imply control flow
201        ExprKind::Match { .. } | ExprKind::If { .. } | ExprKind::Let { .. } => {
202            error(GenericConstantTooComplexSub::ControlFlowNotSupported(node.span))?
203        }
204        ExprKind::InlineAsm { .. } => {
205            error(GenericConstantTooComplexSub::InlineAsmNotSupported(node.span))?
206        }
207
208        // we dont permit let stmts so `VarRef` and `UpvarRef` cant happen
209        ExprKind::VarRef { .. }
210        | ExprKind::UpvarRef { .. }
211        | ExprKind::StaticRef { .. }
212        | ExprKind::ThreadLocalRef(_) => {
213            error(GenericConstantTooComplexSub::OperationNotSupported(node.span))?
214        }
215        ExprKind::Reborrow { .. } => {
216            ::core::panicking::panic("not yet implemented");todo!();
217        }
218    })
219}
220
221struct IsThirPolymorphic<'a, 'tcx> {
222    is_poly: bool,
223    thir: &'a thir::Thir<'tcx>,
224}
225
226fn error(
227    tcx: TyCtxt<'_>,
228    sub: GenericConstantTooComplexSub,
229    root_span: Span,
230) -> Result<!, ErrorGuaranteed> {
231    let reported = tcx.dcx().emit_err(GenericConstantTooComplex {
232        span: root_span,
233        maybe_supported: false,
234        sub,
235    });
236
237    Err(reported)
238}
239
240fn maybe_supported_error(
241    tcx: TyCtxt<'_>,
242    sub: GenericConstantTooComplexSub,
243    root_span: Span,
244) -> Result<!, ErrorGuaranteed> {
245    let reported = tcx.dcx().emit_err(GenericConstantTooComplex {
246        span: root_span,
247        maybe_supported: true,
248        sub,
249    });
250
251    Err(reported)
252}
253
254impl<'a, 'tcx> IsThirPolymorphic<'a, 'tcx> {
255    fn expr_is_poly(&mut self, expr: &thir::Expr<'tcx>) -> bool {
256        if expr.ty.has_non_region_param() {
257            return true;
258        }
259
260        match expr.kind {
261            thir::ExprKind::NamedConst { args, .. } | thir::ExprKind::ConstBlock { args, .. } => {
262                args.has_non_region_param()
263            }
264            thir::ExprKind::ConstParam { .. } => true,
265            thir::ExprKind::Repeat { value, count } => {
266                self.visit_expr(&self.thir()[value]);
267                count.has_non_region_param()
268            }
269            thir::ExprKind::Scope { .. }
270            | thir::ExprKind::If { .. }
271            | thir::ExprKind::Call { .. }
272            | thir::ExprKind::ByUse { .. }
273            | thir::ExprKind::Deref { .. }
274            | thir::ExprKind::Binary { .. }
275            | thir::ExprKind::LogicalOp { .. }
276            | thir::ExprKind::Unary { .. }
277            | thir::ExprKind::Cast { .. }
278            | thir::ExprKind::Use { .. }
279            | thir::ExprKind::NeverToAny { .. }
280            | thir::ExprKind::PointerCoercion { .. }
281            | thir::ExprKind::Loop { .. }
282            | thir::ExprKind::LoopMatch { .. }
283            | thir::ExprKind::Let { .. }
284            | thir::ExprKind::Match { .. }
285            | thir::ExprKind::Block { .. }
286            | thir::ExprKind::Assign { .. }
287            | thir::ExprKind::AssignOp { .. }
288            | thir::ExprKind::Field { .. }
289            | thir::ExprKind::Index { .. }
290            | thir::ExprKind::VarRef { .. }
291            | thir::ExprKind::UpvarRef { .. }
292            | thir::ExprKind::Borrow { .. }
293            | thir::ExprKind::RawBorrow { .. }
294            | thir::ExprKind::Break { .. }
295            | thir::ExprKind::Continue { .. }
296            | thir::ExprKind::ConstContinue { .. }
297            | thir::ExprKind::Return { .. }
298            | thir::ExprKind::Become { .. }
299            | thir::ExprKind::Array { .. }
300            | thir::ExprKind::Tuple { .. }
301            | thir::ExprKind::Adt(_)
302            | thir::ExprKind::PlaceTypeAscription { .. }
303            | thir::ExprKind::ValueTypeAscription { .. }
304            | thir::ExprKind::PlaceUnwrapUnsafeBinder { .. }
305            | thir::ExprKind::ValueUnwrapUnsafeBinder { .. }
306            | thir::ExprKind::WrapUnsafeBinder { .. }
307            | thir::ExprKind::Closure(_)
308            | thir::ExprKind::Literal { .. }
309            | thir::ExprKind::NonHirLiteral { .. }
310            | thir::ExprKind::ZstLiteral { .. }
311            | thir::ExprKind::StaticRef { .. }
312            | thir::ExprKind::InlineAsm(_)
313            | thir::ExprKind::ThreadLocalRef(_)
314            | thir::ExprKind::Yield { .. } => false,
315            thir::ExprKind::Reborrow { .. } => {
316                ::core::panicking::panic("not yet implemented");todo!();
317            }
318        }
319    }
320    fn pat_is_poly(&mut self, pat: &thir::Pat<'tcx>) -> bool {
321        if pat.ty.has_non_region_param() {
322            return true;
323        }
324
325        match pat.kind {
326            thir::PatKind::Constant { value } => value.has_non_region_param(),
327            thir::PatKind::Range(ref range) => {
328                let &thir::PatRange { lo, hi, .. } = range.as_ref();
329                lo.has_non_region_param() || hi.has_non_region_param()
330            }
331            _ => false,
332        }
333    }
334}
335
336impl<'a, 'tcx> visit::Visitor<'a, 'tcx> for IsThirPolymorphic<'a, 'tcx> {
337    fn thir(&self) -> &'a thir::Thir<'tcx> {
338        self.thir
339    }
340
341    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("visit_expr",
                                    "rustc_ty_utils::consts", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_ty_utils/src/consts.rs"),
                                    ::tracing_core::__macro_support::Option::Some(341u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_ty_utils::consts"),
                                    ::tracing_core::field::FieldSet::new(&["expr"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expr)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            self.is_poly |= self.expr_is_poly(expr);
            if !self.is_poly { visit::walk_expr(self, expr) }
        }
    }
}#[instrument(skip(self), level = "debug")]
342    fn visit_expr(&mut self, expr: &'a thir::Expr<'tcx>) {
343        self.is_poly |= self.expr_is_poly(expr);
344        if !self.is_poly {
345            visit::walk_expr(self, expr)
346        }
347    }
348
349    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("visit_pat",
                                    "rustc_ty_utils::consts", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_ty_utils/src/consts.rs"),
                                    ::tracing_core::__macro_support::Option::Some(349u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_ty_utils::consts"),
                                    ::tracing_core::field::FieldSet::new(&["pat"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&pat)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            self.is_poly |= self.pat_is_poly(pat);
            if !self.is_poly { visit::walk_pat(self, pat); }
        }
    }
}#[instrument(skip(self), level = "debug")]
350    fn visit_pat(&mut self, pat: &'a thir::Pat<'tcx>) {
351        self.is_poly |= self.pat_is_poly(pat);
352        if !self.is_poly {
353            visit::walk_pat(self, pat);
354        }
355    }
356}
357
358/// Builds an abstract const, do not use this directly, but use `AbstractConst::new` instead.
359fn thir_abstract_const<'tcx>(
360    tcx: TyCtxt<'tcx>,
361    def: LocalDefId,
362) -> Result<Option<ty::EarlyBinder<'tcx, ty::Const<'tcx>>>, ErrorGuaranteed> {
363    if !tcx.features().generic_const_exprs() {
364        return Ok(None);
365    }
366
367    match tcx.def_kind(def) {
368        // FIXME(generic_const_exprs): We currently only do this for anonymous constants,
369        // meaning that we do not look into associated constants. I(@lcnr) am not yet sure whether
370        // we want to look into them or treat them as opaque projections.
371        //
372        // Right now we do neither of that and simply always fail to unify them.
373        DefKind::AnonConst | DefKind::InlineConst => (),
374        _ => return Ok(None),
375    }
376
377    let body = tcx.thir_body(def)?;
378    let (body, body_id) = (&*body.0.borrow(), body.1);
379
380    let mut is_poly_vis = IsThirPolymorphic { is_poly: false, thir: body };
381    visit::walk_expr(&mut is_poly_vis, &body[body_id]);
382    if !is_poly_vis.is_poly {
383        return Ok(None);
384    }
385
386    let root_span = body.exprs[body_id].span;
387
388    Ok(Some(ty::EarlyBinder::bind(recurse_build(tcx, body, body_id, root_span)?)))
389}
390
391pub(crate) fn provide(providers: &mut Providers) {
392    *providers = Providers { thir_abstract_const, ..*providers };
393}