Skip to main content

rustc_const_eval/check_consts/
ops.rs

1//! Concrete error types for all operations which may be invalid in a certain const context.
2
3use hir::{ConstContext, LangItem};
4use rustc_errors::codes::*;
5use rustc_errors::{Applicability, Diag, MultiSpan, msg};
6use rustc_hir as hir;
7use rustc_hir::def_id::DefId;
8use rustc_infer::infer::TyCtxtInferExt;
9use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
10use rustc_middle::mir::CallSource;
11use rustc_middle::span_bug;
12use rustc_middle::ty::print::{PrintTraitRefExt as _, with_no_trimmed_paths};
13use rustc_middle::ty::{
14    self, AssocContainer, Closure, FnDef, FnPtr, GenericArgKind, GenericArgsRef, Param, TraitRef,
15    Ty, suggest_constraining_type_param,
16};
17use rustc_session::parse::add_feature_diagnostics;
18use rustc_span::{BytePos, Pos, Span, Symbol, sym};
19use rustc_trait_selection::error_reporting::traits::call_kind::{
20    CallDesugaringKind, CallKind, call_kind,
21};
22use rustc_trait_selection::traits::SelectionContext;
23use tracing::debug;
24
25use super::ConstCx;
26use crate::errors;
27
28#[derive(#[automatically_derived]
impl ::core::clone::Clone for Status {
    #[inline]
    fn clone(&self) -> Status {
        let _: ::core::clone::AssertParamIsClone<Symbol>;
        let _: ::core::clone::AssertParamIsClone<bool>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Status { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for Status {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Status::Unstable {
                gate: __self_0,
                gate_already_checked: __self_1,
                safe_to_expose_on_stable: __self_2,
                is_function_call: __self_3 } =>
                ::core::fmt::Formatter::debug_struct_field4_finish(f,
                    "Unstable", "gate", __self_0, "gate_already_checked",
                    __self_1, "safe_to_expose_on_stable", __self_2,
                    "is_function_call", &__self_3),
            Status::Forbidden =>
                ::core::fmt::Formatter::write_str(f, "Forbidden"),
        }
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for Status {
    #[inline]
    fn eq(&self, other: &Status) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (Status::Unstable {
                    gate: __self_0,
                    gate_already_checked: __self_1,
                    safe_to_expose_on_stable: __self_2,
                    is_function_call: __self_3 }, Status::Unstable {
                    gate: __arg1_0,
                    gate_already_checked: __arg1_1,
                    safe_to_expose_on_stable: __arg1_2,
                    is_function_call: __arg1_3 }) =>
                    __self_1 == __arg1_1 && __self_2 == __arg1_2 &&
                            __self_3 == __arg1_3 && __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Status {
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Symbol>;
        let _: ::core::cmp::AssertParamIsEq<bool>;
    }
}Eq)]
29pub enum Status {
30    Unstable {
31        /// The feature that must be enabled to use this operation.
32        gate: Symbol,
33        /// Whether the feature gate was already checked (because the logic is a bit more
34        /// complicated than just checking a single gate).
35        gate_already_checked: bool,
36        /// Whether it is allowed to use this operation from stable `const fn`.
37        /// This will usually be `false`.
38        safe_to_expose_on_stable: bool,
39        /// We indicate whether this is a function call, since we can use targeted
40        /// diagnostics for "callee is not safe to expose om stable".
41        is_function_call: bool,
42    },
43    Forbidden,
44}
45
46#[derive(#[automatically_derived]
impl ::core::clone::Clone for DiagImportance {
    #[inline]
    fn clone(&self) -> DiagImportance { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for DiagImportance { }Copy)]
47pub enum DiagImportance {
48    /// An operation that must be removed for const-checking to pass.
49    Primary,
50
51    /// An operation that causes const-checking to fail, but is usually a side-effect of a `Primary` operation elsewhere.
52    Secondary,
53}
54
55/// An operation that is *not allowed* in a const context.
56pub trait NonConstOp<'tcx>: std::fmt::Debug {
57    /// Returns an enum indicating whether this operation can be enabled with a feature gate.
58    fn status_in_item(&self, _ccx: &ConstCx<'_, 'tcx>) -> Status {
59        Status::Forbidden
60    }
61
62    fn importance(&self) -> DiagImportance {
63        DiagImportance::Primary
64    }
65
66    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx>;
67}
68
69/// A function call where the callee is a pointer.
70#[derive(#[automatically_derived]
impl ::core::fmt::Debug for FnCallIndirect {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "FnCallIndirect")
    }
}Debug)]
71pub(crate) struct FnCallIndirect;
72impl<'tcx> NonConstOp<'tcx> for FnCallIndirect {
73    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
74        ccx.dcx().create_err(errors::UnallowedFnPointerCall { span, kind: ccx.const_kind() })
75    }
76}
77
78/// A c-variadic function call.
79#[derive(#[automatically_derived]
impl ::core::fmt::Debug for FnCallCVariadic {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "FnCallCVariadic")
    }
}Debug)]
80pub(crate) struct FnCallCVariadic;
81impl<'tcx> NonConstOp<'tcx> for FnCallCVariadic {
82    fn status_in_item(&self, _ccx: &ConstCx<'_, 'tcx>) -> Status {
83        Status::Unstable {
84            gate: sym::const_c_variadic,
85            gate_already_checked: false,
86            safe_to_expose_on_stable: false,
87            is_function_call: true,
88        }
89    }
90
91    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
92        ccx.tcx.sess.create_feature_err(
93            errors::NonConstCVariadicCall { span, kind: ccx.const_kind() },
94            sym::const_c_variadic,
95        )
96    }
97}
98
99/// A call to a function that is in a trait, or has trait bounds that make it conditionally-const.
100#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for ConditionallyConstCall<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field4_finish(f,
            "ConditionallyConstCall", "callee", &self.callee, "args",
            &self.args, "span", &self.span, "call_source", &&self.call_source)
    }
}Debug)]
101pub(crate) struct ConditionallyConstCall<'tcx> {
102    pub callee: DefId,
103    pub args: GenericArgsRef<'tcx>,
104    pub span: Span,
105    pub call_source: CallSource,
106}
107
108impl<'tcx> NonConstOp<'tcx> for ConditionallyConstCall<'tcx> {
109    fn status_in_item(&self, _ccx: &ConstCx<'_, 'tcx>) -> Status {
110        // We use the `const_trait_impl` gate for all conditionally-const calls.
111        Status::Unstable {
112            gate: sym::const_trait_impl,
113            gate_already_checked: false,
114            safe_to_expose_on_stable: false,
115            // We don't want the "mark the callee as `#[rustc_const_stable_indirect]`" hint
116            is_function_call: false,
117        }
118    }
119
120    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, _: Span) -> Diag<'tcx> {
121        let mut diag = build_error_for_const_call(
122            ccx,
123            self.callee,
124            self.args,
125            self.span,
126            self.call_source,
127            "conditionally",
128            |_, _, _| {},
129        );
130
131        // Override code and mention feature.
132        diag.code(E0658);
133        add_feature_diagnostics(&mut diag, ccx.tcx.sess, sym::const_trait_impl);
134
135        diag
136    }
137}
138
139/// A function call where the callee is not marked as `const`.
140#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for FnCallNonConst<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field4_finish(f,
            "FnCallNonConst", "callee", &self.callee, "args", &self.args,
            "span", &self.span, "call_source", &&self.call_source)
    }
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for FnCallNonConst<'tcx> {
    #[inline]
    fn clone(&self) -> FnCallNonConst<'tcx> {
        let _: ::core::clone::AssertParamIsClone<DefId>;
        let _: ::core::clone::AssertParamIsClone<GenericArgsRef<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<Span>;
        let _: ::core::clone::AssertParamIsClone<CallSource>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for FnCallNonConst<'tcx> { }Copy)]
141pub(crate) struct FnCallNonConst<'tcx> {
142    pub callee: DefId,
143    pub args: GenericArgsRef<'tcx>,
144    pub span: Span,
145    pub call_source: CallSource,
146}
147
148impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
149    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, _: Span) -> Diag<'tcx> {
150        let tcx = ccx.tcx;
151        let caller = ccx.def_id();
152
153        let mut err = build_error_for_const_call(
154            ccx,
155            self.callee,
156            self.args,
157            self.span,
158            self.call_source,
159            "non",
160            |err, self_ty, trait_id| {
161                // FIXME(const_trait_impl): Do we need any of this on the non-const codepath?
162
163                let trait_ref = TraitRef::from_assoc(tcx, trait_id, self.args);
164
165                match self_ty.kind() {
166                    Param(param_ty) => {
167                        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/check_consts/ops.rs:167",
                        "rustc_const_eval::check_consts::ops",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/check_consts/ops.rs"),
                        ::tracing_core::__macro_support::Option::Some(167u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_const_eval::check_consts::ops"),
                        ::tracing_core::field::FieldSet::new(&["param_ty"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&param_ty)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?param_ty);
168                        if let Some(generics) = tcx.hir_node_by_def_id(caller).generics() {
169                            let constraint = {
    let _guard = NoTrimmedGuard::new();
    ::alloc::__export::must_use({
            ::alloc::fmt::format(format_args!("[const] {0}",
                    trait_ref.print_trait_sugared()))
        })
}with_no_trimmed_paths!(format!(
170                                "[const] {}",
171                                trait_ref.print_trait_sugared(),
172                            ));
173                            suggest_constraining_type_param(
174                                tcx,
175                                generics,
176                                err,
177                                param_ty.name.as_str(),
178                                &constraint,
179                                Some(trait_ref.def_id),
180                                None,
181                            );
182                        }
183                    }
184                    ty::Adt(..) => {
185                        let (infcx, param_env) =
186                            tcx.infer_ctxt().build_with_typing_env(ccx.typing_env);
187                        let obligation =
188                            Obligation::new(tcx, ObligationCause::dummy(), param_env, trait_ref);
189                        let mut selcx = SelectionContext::new(&infcx);
190                        let implsrc = selcx.select(&obligation);
191                        if let Ok(Some(ImplSource::UserDefined(data))) = implsrc {
192                            // FIXME(const_trait_impl) revisit this
193                            if !tcx.is_const_trait_impl(data.impl_def_id) {
194                                let span = tcx.def_span(data.impl_def_id);
195                                err.subdiagnostic(errors::NonConstImplNote { span });
196                            }
197                        }
198                    }
199                    _ => {}
200                }
201            },
202        );
203
204        if let ConstContext::Static(_) = ccx.const_kind() {
205            err.note(rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`"))msg!(
206                "consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`"
207            ));
208        }
209
210        err
211    }
212}
213
214/// Build an error message reporting that a function call is not const (or only
215/// conditionally const). In case that this call is desugared (like an operator
216/// or sugar from something like a `for` loop), try to build a better error message
217/// that doesn't call it a method.
218fn build_error_for_const_call<'tcx>(
219    ccx: &ConstCx<'_, 'tcx>,
220    callee: DefId,
221    args: ty::GenericArgsRef<'tcx>,
222    span: Span,
223    call_source: CallSource,
224    non_or_conditionally: &'static str,
225    note_trait_if_possible: impl FnOnce(&mut Diag<'tcx>, Ty<'tcx>, DefId),
226) -> Diag<'tcx> {
227    let tcx = ccx.tcx;
228
229    let call_kind =
230        call_kind(tcx, ccx.typing_env, callee, args, span, call_source.from_hir_call(), None);
231
232    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/check_consts/ops.rs:232",
                        "rustc_const_eval::check_consts::ops",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/check_consts/ops.rs"),
                        ::tracing_core::__macro_support::Option::Some(232u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_const_eval::check_consts::ops"),
                        ::tracing_core::field::FieldSet::new(&["call_kind"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&call_kind)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?call_kind);
233
234    let mut err = match call_kind {
235        CallKind::Normal { desugaring: Some((kind, self_ty)), .. } => {
236            macro_rules! error {
237                ($err:ident) => {
238                    tcx.dcx().create_err(errors::$err {
239                        span,
240                        ty: self_ty,
241                        kind: ccx.const_kind(),
242                        non_or_conditionally,
243                    })
244                };
245            }
246
247            // Don't point at the trait if this is a desugaring...
248            // FIXME(const_trait_impl): we could perhaps do this for `Iterator`.
249            match kind {
250                CallDesugaringKind::ForLoopIntoIter | CallDesugaringKind::ForLoopNext => {
251                    tcx.dcx().create_err(errors::NonConstForLoopIntoIter {
        span,
        ty: self_ty,
        kind: ccx.const_kind(),
        non_or_conditionally,
    })error!(NonConstForLoopIntoIter)
252                }
253                CallDesugaringKind::QuestionBranch => {
254                    tcx.dcx().create_err(errors::NonConstQuestionBranch {
        span,
        ty: self_ty,
        kind: ccx.const_kind(),
        non_or_conditionally,
    })error!(NonConstQuestionBranch)
255                }
256                CallDesugaringKind::QuestionFromResidual => {
257                    tcx.dcx().create_err(errors::NonConstQuestionFromResidual {
        span,
        ty: self_ty,
        kind: ccx.const_kind(),
        non_or_conditionally,
    })error!(NonConstQuestionFromResidual)
258                }
259                CallDesugaringKind::TryBlockFromOutput => {
260                    tcx.dcx().create_err(errors::NonConstTryBlockFromOutput {
        span,
        ty: self_ty,
        kind: ccx.const_kind(),
        non_or_conditionally,
    })error!(NonConstTryBlockFromOutput)
261                }
262                CallDesugaringKind::Await => {
263                    tcx.dcx().create_err(errors::NonConstAwait {
        span,
        ty: self_ty,
        kind: ccx.const_kind(),
        non_or_conditionally,
    })error!(NonConstAwait)
264                }
265            }
266        }
267        CallKind::FnCall { fn_trait_id, self_ty } => {
268            let note = match self_ty.kind() {
269                FnDef(def_id, ..) => {
270                    let span = tcx.def_span(*def_id);
271                    if ccx.tcx.is_const_fn(*def_id) {
272                        ::rustc_middle::util::bug::span_bug_fmt(span,
    format_args!("calling const FnDef errored when it shouldn\'t"));span_bug!(span, "calling const FnDef errored when it shouldn't");
273                    }
274
275                    Some(errors::NonConstClosureNote::FnDef { span })
276                }
277                FnPtr(..) => Some(errors::NonConstClosureNote::FnPtr),
278                Closure(..) => Some(errors::NonConstClosureNote::Closure),
279                _ => None,
280            };
281
282            let mut err = tcx.dcx().create_err(errors::NonConstClosure {
283                span,
284                kind: ccx.const_kind(),
285                note,
286                non_or_conditionally,
287            });
288
289            note_trait_if_possible(&mut err, self_ty, fn_trait_id);
290            err
291        }
292        CallKind::Operator { trait_id, self_ty, .. } => {
293            let mut err = if let CallSource::MatchCmp = call_source {
294                tcx.dcx().create_err(errors::NonConstMatchEq {
295                    span,
296                    kind: ccx.const_kind(),
297                    ty: self_ty,
298                    non_or_conditionally,
299                })
300            } else {
301                let mut sugg = None;
302
303                if ccx.tcx.is_lang_item(trait_id, LangItem::PartialEq) {
304                    match (args[0].kind(), args[1].kind()) {
305                        (GenericArgKind::Type(self_ty), GenericArgKind::Type(rhs_ty))
306                            if self_ty == rhs_ty
307                                && self_ty.is_ref()
308                                && self_ty.peel_refs().is_primitive() =>
309                        {
310                            let mut num_refs = 0;
311                            let mut tmp_ty = self_ty;
312                            while let rustc_middle::ty::Ref(_, inner_ty, _) = tmp_ty.kind() {
313                                num_refs += 1;
314                                tmp_ty = *inner_ty;
315                            }
316                            let deref = "*".repeat(num_refs);
317
318                            if let Ok(call_str) = ccx.tcx.sess.source_map().span_to_snippet(span)
319                                && let Some(eq_idx) = call_str.find("==")
320                                && let Some(rhs_idx) =
321                                    call_str[(eq_idx + 2)..].find(|c: char| !c.is_whitespace())
322                            {
323                                let rhs_pos = span.lo() + BytePos::from_usize(eq_idx + 2 + rhs_idx);
324                                let rhs_span = span.with_lo(rhs_pos).with_hi(rhs_pos);
325                                sugg = Some(errors::ConsiderDereferencing {
326                                    deref,
327                                    span: span.shrink_to_lo(),
328                                    rhs_span,
329                                });
330                            }
331                        }
332                        _ => {}
333                    }
334                }
335                tcx.dcx().create_err(errors::NonConstOperator {
336                    span,
337                    kind: ccx.const_kind(),
338                    sugg,
339                    non_or_conditionally,
340                })
341            };
342
343            note_trait_if_possible(&mut err, self_ty, trait_id);
344            err
345        }
346        CallKind::DerefCoercion { deref_target_span, deref_target_ty, self_ty } => {
347            // Check first whether the source is accessible (issue #87060)
348            let target = if let Some(deref_target_span) = deref_target_span
349                && tcx.sess.source_map().is_span_accessible(deref_target_span)
350            {
351                Some(deref_target_span)
352            } else {
353                None
354            };
355
356            let mut err = tcx.dcx().create_err(errors::NonConstDerefCoercion {
357                span,
358                ty: self_ty,
359                kind: ccx.const_kind(),
360                target_ty: deref_target_ty,
361                deref_target: target,
362                non_or_conditionally,
363            });
364
365            note_trait_if_possible(&mut err, self_ty, tcx.require_lang_item(LangItem::Deref, span));
366            err
367        }
368        _ if tcx.opt_parent(callee) == tcx.get_diagnostic_item(sym::FmtArgumentsNew) => {
369            ccx.dcx().create_err(errors::NonConstFmtMacroCall {
370                span,
371                kind: ccx.const_kind(),
372                non_or_conditionally,
373            })
374        }
375        _ => {
376            let def_descr = ccx.tcx.def_descr(callee);
377            let mut err = ccx.dcx().create_err(errors::NonConstFnCall {
378                span,
379                def_descr,
380                def_path_str: ccx.tcx.def_path_str_with_args(callee, args),
381                kind: ccx.const_kind(),
382                non_or_conditionally,
383            });
384            if let Some(item) = ccx.tcx.opt_associated_item(callee) {
385                if let AssocContainer::Trait = item.container
386                    && let parent = item.container_id(ccx.tcx)
387                    && !ccx.tcx.is_const_trait(parent)
388                {
389                    let assoc_span = ccx.tcx.def_span(callee);
390                    let assoc_name = ccx.tcx.item_name(callee);
391                    let mut span: MultiSpan = ccx.tcx.def_span(parent).into();
392                    span.push_span_label(assoc_span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this {0} is not const", def_descr))
    })format!("this {def_descr} is not const"));
393                    let trait_descr = ccx.tcx.def_descr(parent);
394                    let trait_span = ccx.tcx.def_span(parent);
395                    let trait_name = ccx.tcx.item_name(parent);
396                    span.push_span_label(trait_span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this {0} is not const",
                trait_descr))
    })format!("this {trait_descr} is not const"));
397                    err.span_note(
398                        span,
399                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} `{1}` is not const because {2} `{3}` is not const",
                def_descr, assoc_name, trait_descr, trait_name))
    })format!(
400                            "{def_descr} `{assoc_name}` is not const because {trait_descr} \
401                            `{trait_name}` is not const",
402                        ),
403                    );
404                    if let Some(parent) = parent.as_local()
405                        && ccx.tcx.sess.is_nightly_build()
406                    {
407                        if !ccx.tcx.features().const_trait_impl() {
408                            err.help(
409                                "add `#![feature(const_trait_impl)]` to the crate attributes to \
410                                 enable const traits",
411                            );
412                        }
413                        let span = ccx.tcx.hir_expect_item(parent).vis_span;
414                        let span = ccx.tcx.sess.source_map().span_extend_while_whitespace(span);
415                        err.span_suggestion_verbose(
416                            span.shrink_to_hi(),
417                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider making trait `{0}` const",
                trait_name))
    })format!("consider making trait `{trait_name}` const"),
418                            "const ".to_owned(),
419                            Applicability::MaybeIncorrect,
420                        );
421                    } else if !ccx.tcx.sess.is_nightly_build() {
422                        err.help("const traits are not yet supported on stable Rust");
423                    }
424                }
425            } else if ccx.tcx.constness(callee) != hir::Constness::Const {
426                let name = ccx.tcx.item_name(callee);
427                err.span_note(
428                    ccx.tcx.def_span(callee),
429                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} `{1}` is not const", def_descr,
                name))
    })format!("{def_descr} `{name}` is not const"),
430                );
431            }
432            err
433        }
434    };
435
436    err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("calls in {0}s are limited to constant functions, tuple structs and tuple variants",
                ccx.const_kind()))
    })format!(
437        "calls in {}s are limited to constant functions, tuple structs and tuple variants",
438        ccx.const_kind(),
439    ));
440
441    err
442}
443
444/// A call to an `#[unstable]` const fn, `#[rustc_const_unstable]` function or trait.
445///
446/// Contains the name of the feature that would allow the use of this function/trait.
447#[derive(#[automatically_derived]
impl ::core::fmt::Debug for CallUnstable {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field5_finish(f, "CallUnstable",
            "def_id", &self.def_id, "feature", &self.feature,
            "feature_enabled", &self.feature_enabled,
            "safe_to_expose_on_stable", &self.safe_to_expose_on_stable,
            "is_function_call", &&self.is_function_call)
    }
}Debug)]
448pub(crate) struct CallUnstable {
449    pub def_id: DefId,
450    pub feature: Symbol,
451    /// If this is true, then the feature is enabled, but we need to still check if it is safe to
452    /// expose on stable.
453    pub feature_enabled: bool,
454    pub safe_to_expose_on_stable: bool,
455    /// true if `def_id` is the function we are calling, false if `def_id` is an unstable trait.
456    pub is_function_call: bool,
457}
458
459impl<'tcx> NonConstOp<'tcx> for CallUnstable {
460    fn status_in_item(&self, _ccx: &ConstCx<'_, 'tcx>) -> Status {
461        Status::Unstable {
462            gate: self.feature,
463            gate_already_checked: self.feature_enabled,
464            safe_to_expose_on_stable: self.safe_to_expose_on_stable,
465            is_function_call: self.is_function_call,
466        }
467    }
468
469    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
470        if !!self.feature_enabled {
    ::core::panicking::panic("assertion failed: !self.feature_enabled")
};assert!(!self.feature_enabled);
471        let mut err = if self.is_function_call {
472            ccx.dcx().create_err(errors::UnstableConstFn {
473                span,
474                def_path: ccx.tcx.def_path_str(self.def_id),
475            })
476        } else {
477            ccx.dcx().create_err(errors::UnstableConstTrait {
478                span,
479                def_path: ccx.tcx.def_path_str(self.def_id),
480            })
481        };
482        ccx.tcx.disabled_nightly_features(&mut err, [(String::new(), self.feature)]);
483        err
484    }
485}
486
487/// A call to an intrinsic that is just not const-callable at all.
488#[derive(#[automatically_derived]
impl ::core::fmt::Debug for IntrinsicNonConst {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f,
            "IntrinsicNonConst", "name", &&self.name)
    }
}Debug)]
489pub(crate) struct IntrinsicNonConst {
490    pub name: Symbol,
491}
492
493impl<'tcx> NonConstOp<'tcx> for IntrinsicNonConst {
494    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
495        ccx.dcx().create_err(errors::NonConstIntrinsic {
496            span,
497            name: self.name,
498            kind: ccx.const_kind(),
499        })
500    }
501}
502
503/// A call to an intrinsic that is just not const-callable at all.
504#[derive(#[automatically_derived]
impl ::core::fmt::Debug for IntrinsicUnstable {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f,
            "IntrinsicUnstable", "name", &self.name, "feature", &self.feature,
            "const_stable_indirect", &&self.const_stable_indirect)
    }
}Debug)]
505pub(crate) struct IntrinsicUnstable {
506    pub name: Symbol,
507    pub feature: Symbol,
508    pub const_stable_indirect: bool,
509}
510
511impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable {
512    fn status_in_item(&self, _ccx: &ConstCx<'_, 'tcx>) -> Status {
513        Status::Unstable {
514            gate: self.feature,
515            gate_already_checked: false,
516            safe_to_expose_on_stable: self.const_stable_indirect,
517            // We do *not* want to suggest to mark the intrinsic as `const_stable_indirect`,
518            // that's not a trivial change!
519            is_function_call: false,
520        }
521    }
522
523    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
524        ccx.dcx().create_err(errors::UnstableIntrinsic {
525            span,
526            name: self.name,
527            feature: self.feature,
528            suggestion: ccx.tcx.crate_level_attribute_injection_span(),
529        })
530    }
531}
532
533#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Coroutine {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Coroutine",
            &&self.0)
    }
}Debug)]
534pub(crate) struct Coroutine(pub hir::CoroutineKind);
535impl<'tcx> NonConstOp<'tcx> for Coroutine {
536    fn status_in_item(&self, _: &ConstCx<'_, 'tcx>) -> Status {
537        match self.0 {
538            hir::CoroutineKind::Desugared(
539                hir::CoroutineDesugaring::Async,
540                hir::CoroutineSource::Block,
541            )
542            // FIXME(coroutines): eventually we want to gate const coroutine coroutines behind a
543            // different feature.
544            | hir::CoroutineKind::Coroutine(_) => Status::Unstable {
545                gate: sym::const_async_blocks,
546                gate_already_checked: false,
547                safe_to_expose_on_stable: false,
548                is_function_call: false,
549            },
550            _ => Status::Forbidden,
551        }
552    }
553
554    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
555        let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} are not allowed in {1}s",
                self.0.to_plural_string(), ccx.const_kind()))
    })format!("{} are not allowed in {}s", self.0.to_plural_string(), ccx.const_kind());
556        if let Status::Unstable { gate, .. } = self.status_in_item(ccx) {
557            ccx.tcx.sess.create_feature_err(errors::UnallowedOpInConstContext { span, msg }, gate)
558        } else {
559            ccx.dcx().create_err(errors::UnallowedOpInConstContext { span, msg })
560        }
561    }
562}
563
564#[derive(#[automatically_derived]
impl ::core::fmt::Debug for InlineAsm {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "InlineAsm")
    }
}Debug)]
565pub(crate) struct InlineAsm;
566impl<'tcx> NonConstOp<'tcx> for InlineAsm {
567    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
568        ccx.dcx().create_err(errors::UnallowedInlineAsm { span, kind: ccx.const_kind() })
569    }
570}
571
572#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for LiveDrop<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "LiveDrop",
            "dropped_at", &self.dropped_at, "dropped_ty", &self.dropped_ty,
            "needs_non_const_drop", &&self.needs_non_const_drop)
    }
}Debug)]
573pub(crate) struct LiveDrop<'tcx> {
574    pub dropped_at: Span,
575    pub dropped_ty: Ty<'tcx>,
576    pub needs_non_const_drop: bool,
577}
578impl<'tcx> NonConstOp<'tcx> for LiveDrop<'tcx> {
579    fn status_in_item(&self, _ccx: &ConstCx<'_, 'tcx>) -> Status {
580        if self.needs_non_const_drop {
581            Status::Forbidden
582        } else {
583            Status::Unstable {
584                gate: sym::const_destruct,
585                gate_already_checked: false,
586                safe_to_expose_on_stable: false,
587                is_function_call: false,
588            }
589        }
590    }
591
592    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
593        if self.needs_non_const_drop {
594            ccx.dcx().create_err(errors::LiveDrop {
595                span,
596                dropped_ty: self.dropped_ty,
597                kind: ccx.const_kind(),
598                dropped_at: self.dropped_at,
599            })
600        } else {
601            ccx.tcx.sess.create_feature_err(
602                errors::LiveDrop {
603                    span,
604                    dropped_ty: self.dropped_ty,
605                    kind: ccx.const_kind(),
606                    dropped_at: self.dropped_at,
607                },
608                sym::const_destruct,
609            )
610        }
611    }
612}
613
614#[derive(#[automatically_derived]
impl ::core::fmt::Debug for EscapingCellBorrow {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "EscapingCellBorrow")
    }
}Debug)]
615/// A borrow of a type that contains an `UnsafeCell` somewhere. The borrow might escape to
616/// the final value of the constant, and thus we cannot allow this (for now). We may allow
617/// it in the future for static items.
618pub(crate) struct EscapingCellBorrow;
619impl<'tcx> NonConstOp<'tcx> for EscapingCellBorrow {
620    fn importance(&self) -> DiagImportance {
621        // Most likely the code will try to do mutation with these borrows, which
622        // triggers its own errors. Only show this one if that does not happen.
623        DiagImportance::Secondary
624    }
625    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
626        ccx.dcx().create_err(errors::InteriorMutableBorrowEscaping { span, kind: ccx.const_kind() })
627    }
628}
629
630#[derive(#[automatically_derived]
impl ::core::fmt::Debug for EscapingMutBorrow {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "EscapingMutBorrow")
    }
}Debug)]
631/// This op is for `&mut` borrows in the trailing expression of a constant
632/// which uses the "enclosing scopes rule" to leak its locals into anonymous
633/// static or const items.
634pub(crate) struct EscapingMutBorrow;
635
636impl<'tcx> NonConstOp<'tcx> for EscapingMutBorrow {
637    fn status_in_item(&self, _ccx: &ConstCx<'_, 'tcx>) -> Status {
638        Status::Forbidden
639    }
640
641    fn importance(&self) -> DiagImportance {
642        // Most likely the code will try to do mutation with these borrows, which
643        // triggers its own errors. Only show this one if that does not happen.
644        DiagImportance::Secondary
645    }
646
647    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
648        ccx.dcx().create_err(errors::MutableBorrowEscaping { span, kind: ccx.const_kind() })
649    }
650}
651
652/// A call to a `panic()` lang item where the first argument is _not_ a `&str`.
653#[derive(#[automatically_derived]
impl ::core::fmt::Debug for PanicNonStr {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "PanicNonStr")
    }
}Debug)]
654pub(crate) struct PanicNonStr;
655impl<'tcx> NonConstOp<'tcx> for PanicNonStr {
656    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
657        ccx.dcx().create_err(errors::PanicNonStrErr { span })
658    }
659}
660
661/// Comparing raw pointers for equality.
662/// Not currently intended to ever be allowed, even behind a feature gate: operation depends on
663/// allocation base addresses that are not known at compile-time.
664#[derive(#[automatically_derived]
impl ::core::fmt::Debug for RawPtrComparison {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "RawPtrComparison")
    }
}Debug)]
665pub(crate) struct RawPtrComparison;
666impl<'tcx> NonConstOp<'tcx> for RawPtrComparison {
667    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
668        // FIXME(const_trait_impl): revert to span_bug?
669        ccx.dcx().create_err(errors::RawPtrComparisonErr { span })
670    }
671}
672
673/// Casting raw pointer or function pointer to an integer.
674/// Not currently intended to ever be allowed, even behind a feature gate: operation depends on
675/// allocation base addresses that are not known at compile-time.
676#[derive(#[automatically_derived]
impl ::core::fmt::Debug for RawPtrToIntCast {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "RawPtrToIntCast")
    }
}Debug)]
677pub(crate) struct RawPtrToIntCast;
678impl<'tcx> NonConstOp<'tcx> for RawPtrToIntCast {
679    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
680        ccx.dcx().create_err(errors::RawPtrToIntErr { span })
681    }
682}
683
684/// An access to a thread-local `static`.
685#[derive(#[automatically_derived]
impl ::core::fmt::Debug for ThreadLocalAccess {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "ThreadLocalAccess")
    }
}Debug)]
686pub(crate) struct ThreadLocalAccess;
687impl<'tcx> NonConstOp<'tcx> for ThreadLocalAccess {
688    fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
689        ccx.dcx().create_err(errors::ThreadLocalAccessErr { span })
690    }
691}