1use hir::{ConstContext, LangItem};
4use rustc_errors::codes::*;
5use rustc_errors::{Applicability, Diag, MultiSpan};
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, fluent_generated};
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 {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Symbol>;
let _: ::core::cmp::AssertParamIsEq<bool>;
}
}Eq)]
29pub enum Status {
30 Unstable {
31 gate: Symbol,
33 gate_already_checked: bool,
36 safe_to_expose_on_stable: bool,
39 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 Primary,
50
51 Secondary,
53}
54
55pub trait NonConstOp<'tcx>: std::fmt::Debug {
57 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#[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#[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)]
80pub(crate) struct ConditionallyConstCall<'tcx> {
81 pub callee: DefId,
82 pub args: GenericArgsRef<'tcx>,
83 pub span: Span,
84 pub call_source: CallSource,
85}
86
87impl<'tcx> NonConstOp<'tcx> for ConditionallyConstCall<'tcx> {
88 fn status_in_item(&self, _ccx: &ConstCx<'_, 'tcx>) -> Status {
89 Status::Unstable {
91 gate: sym::const_trait_impl,
92 gate_already_checked: false,
93 safe_to_expose_on_stable: false,
94 is_function_call: false,
96 }
97 }
98
99 fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, _: Span) -> Diag<'tcx> {
100 let mut diag = build_error_for_const_call(
101 ccx,
102 self.callee,
103 self.args,
104 self.span,
105 self.call_source,
106 "conditionally",
107 |_, _, _| {},
108 );
109
110 diag.code(E0658);
112 add_feature_diagnostics(&mut diag, ccx.tcx.sess, sym::const_trait_impl);
113
114 diag
115 }
116}
117
118#[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)]
120pub(crate) struct FnCallNonConst<'tcx> {
121 pub callee: DefId,
122 pub args: GenericArgsRef<'tcx>,
123 pub span: Span,
124 pub call_source: CallSource,
125}
126
127impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
128 fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, _: Span) -> Diag<'tcx> {
129 let tcx = ccx.tcx;
130 let caller = ccx.def_id();
131
132 let mut err = build_error_for_const_call(
133 ccx,
134 self.callee,
135 self.args,
136 self.span,
137 self.call_source,
138 "non",
139 |err, self_ty, trait_id| {
140 let trait_ref = TraitRef::from_assoc(tcx, trait_id, self.args);
143
144 match self_ty.kind() {
145 Param(param_ty) => {
146 {
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:146",
"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(146u32),
::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(¶m_ty)
as &dyn Value))])
});
} else { ; }
};debug!(?param_ty);
147 if let Some(generics) = tcx.hir_node_by_def_id(caller).generics() {
148 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!(
149 "[const] {}",
150 trait_ref.print_trait_sugared(),
151 ));
152 suggest_constraining_type_param(
153 tcx,
154 generics,
155 err,
156 param_ty.name.as_str(),
157 &constraint,
158 Some(trait_ref.def_id),
159 None,
160 );
161 }
162 }
163 ty::Adt(..) => {
164 let (infcx, param_env) =
165 tcx.infer_ctxt().build_with_typing_env(ccx.typing_env);
166 let obligation =
167 Obligation::new(tcx, ObligationCause::dummy(), param_env, trait_ref);
168 let mut selcx = SelectionContext::new(&infcx);
169 let implsrc = selcx.select(&obligation);
170 if let Ok(Some(ImplSource::UserDefined(data))) = implsrc {
171 if !tcx.is_const_trait_impl(data.impl_def_id) {
173 let span = tcx.def_span(data.impl_def_id);
174 err.subdiagnostic(errors::NonConstImplNote { span });
175 }
176 }
177 }
178 _ => {}
179 }
180 },
181 );
182
183 if let ConstContext::Static(_) = ccx.const_kind() {
184 err.note(fluent_generated::const_eval_lazy_lock);
185 }
186
187 err
188 }
189}
190
191fn build_error_for_const_call<'tcx>(
196 ccx: &ConstCx<'_, 'tcx>,
197 callee: DefId,
198 args: ty::GenericArgsRef<'tcx>,
199 span: Span,
200 call_source: CallSource,
201 non_or_conditionally: &'static str,
202 note_trait_if_possible: impl FnOnce(&mut Diag<'tcx>, Ty<'tcx>, DefId),
203) -> Diag<'tcx> {
204 let tcx = ccx.tcx;
205
206 let call_kind =
207 call_kind(tcx, ccx.typing_env, callee, args, span, call_source.from_hir_call(), None);
208
209 {
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:209",
"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(209u32),
::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);
210
211 let mut err = match call_kind {
212 CallKind::Normal { desugaring: Some((kind, self_ty)), .. } => {
213 macro_rules! error {
214 ($err:ident) => {
215 tcx.dcx().create_err(errors::$err {
216 span,
217 ty: self_ty,
218 kind: ccx.const_kind(),
219 non_or_conditionally,
220 })
221 };
222 }
223
224 match kind {
227 CallDesugaringKind::ForLoopIntoIter | CallDesugaringKind::ForLoopNext => {
228 tcx.dcx().create_err(errors::NonConstForLoopIntoIter {
span,
ty: self_ty,
kind: ccx.const_kind(),
non_or_conditionally,
})error!(NonConstForLoopIntoIter)
229 }
230 CallDesugaringKind::QuestionBranch => {
231 tcx.dcx().create_err(errors::NonConstQuestionBranch {
span,
ty: self_ty,
kind: ccx.const_kind(),
non_or_conditionally,
})error!(NonConstQuestionBranch)
232 }
233 CallDesugaringKind::QuestionFromResidual => {
234 tcx.dcx().create_err(errors::NonConstQuestionFromResidual {
span,
ty: self_ty,
kind: ccx.const_kind(),
non_or_conditionally,
})error!(NonConstQuestionFromResidual)
235 }
236 CallDesugaringKind::TryBlockFromOutput => {
237 tcx.dcx().create_err(errors::NonConstTryBlockFromOutput {
span,
ty: self_ty,
kind: ccx.const_kind(),
non_or_conditionally,
})error!(NonConstTryBlockFromOutput)
238 }
239 CallDesugaringKind::Await => {
240 tcx.dcx().create_err(errors::NonConstAwait {
span,
ty: self_ty,
kind: ccx.const_kind(),
non_or_conditionally,
})error!(NonConstAwait)
241 }
242 }
243 }
244 CallKind::FnCall { fn_trait_id, self_ty } => {
245 let note = match self_ty.kind() {
246 FnDef(def_id, ..) => {
247 let span = tcx.def_span(*def_id);
248 if ccx.tcx.is_const_fn(*def_id) {
249 ::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");
250 }
251
252 Some(errors::NonConstClosureNote::FnDef { span })
253 }
254 FnPtr(..) => Some(errors::NonConstClosureNote::FnPtr),
255 Closure(..) => Some(errors::NonConstClosureNote::Closure),
256 _ => None,
257 };
258
259 let mut err = tcx.dcx().create_err(errors::NonConstClosure {
260 span,
261 kind: ccx.const_kind(),
262 note,
263 non_or_conditionally,
264 });
265
266 note_trait_if_possible(&mut err, self_ty, fn_trait_id);
267 err
268 }
269 CallKind::Operator { trait_id, self_ty, .. } => {
270 let mut err = if let CallSource::MatchCmp = call_source {
271 tcx.dcx().create_err(errors::NonConstMatchEq {
272 span,
273 kind: ccx.const_kind(),
274 ty: self_ty,
275 non_or_conditionally,
276 })
277 } else {
278 let mut sugg = None;
279
280 if ccx.tcx.is_lang_item(trait_id, LangItem::PartialEq) {
281 match (args[0].kind(), args[1].kind()) {
282 (GenericArgKind::Type(self_ty), GenericArgKind::Type(rhs_ty))
283 if self_ty == rhs_ty
284 && self_ty.is_ref()
285 && self_ty.peel_refs().is_primitive() =>
286 {
287 let mut num_refs = 0;
288 let mut tmp_ty = self_ty;
289 while let rustc_middle::ty::Ref(_, inner_ty, _) = tmp_ty.kind() {
290 num_refs += 1;
291 tmp_ty = *inner_ty;
292 }
293 let deref = "*".repeat(num_refs);
294
295 if let Ok(call_str) = ccx.tcx.sess.source_map().span_to_snippet(span)
296 && let Some(eq_idx) = call_str.find("==")
297 && let Some(rhs_idx) =
298 call_str[(eq_idx + 2)..].find(|c: char| !c.is_whitespace())
299 {
300 let rhs_pos = span.lo() + BytePos::from_usize(eq_idx + 2 + rhs_idx);
301 let rhs_span = span.with_lo(rhs_pos).with_hi(rhs_pos);
302 sugg = Some(errors::ConsiderDereferencing {
303 deref,
304 span: span.shrink_to_lo(),
305 rhs_span,
306 });
307 }
308 }
309 _ => {}
310 }
311 }
312 tcx.dcx().create_err(errors::NonConstOperator {
313 span,
314 kind: ccx.const_kind(),
315 sugg,
316 non_or_conditionally,
317 })
318 };
319
320 note_trait_if_possible(&mut err, self_ty, trait_id);
321 err
322 }
323 CallKind::DerefCoercion { deref_target_span, deref_target_ty, self_ty } => {
324 let target = if let Some(deref_target_span) = deref_target_span
326 && tcx.sess.source_map().is_span_accessible(deref_target_span)
327 {
328 Some(deref_target_span)
329 } else {
330 None
331 };
332
333 let mut err = tcx.dcx().create_err(errors::NonConstDerefCoercion {
334 span,
335 ty: self_ty,
336 kind: ccx.const_kind(),
337 target_ty: deref_target_ty,
338 deref_target: target,
339 non_or_conditionally,
340 });
341
342 note_trait_if_possible(&mut err, self_ty, tcx.require_lang_item(LangItem::Deref, span));
343 err
344 }
345 _ if tcx.opt_parent(callee) == tcx.get_diagnostic_item(sym::FmtArgumentsNew) => {
346 ccx.dcx().create_err(errors::NonConstFmtMacroCall {
347 span,
348 kind: ccx.const_kind(),
349 non_or_conditionally,
350 })
351 }
352 _ => {
353 let def_descr = ccx.tcx.def_descr(callee);
354 let mut err = ccx.dcx().create_err(errors::NonConstFnCall {
355 span,
356 def_descr,
357 def_path_str: ccx.tcx.def_path_str_with_args(callee, args),
358 kind: ccx.const_kind(),
359 non_or_conditionally,
360 });
361 if let Some(item) = ccx.tcx.opt_associated_item(callee) {
362 if let AssocContainer::Trait = item.container
363 && let parent = item.container_id(ccx.tcx)
364 && !ccx.tcx.is_const_trait(parent)
365 {
366 let assoc_span = ccx.tcx.def_span(callee);
367 let assoc_name = ccx.tcx.item_name(callee);
368 let mut span: MultiSpan = ccx.tcx.def_span(parent).into();
369 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"));
370 let trait_descr = ccx.tcx.def_descr(parent);
371 let trait_span = ccx.tcx.def_span(parent);
372 let trait_name = ccx.tcx.item_name(parent);
373 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"));
374 err.span_note(
375 span,
376 ::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!(
377 "{def_descr} `{assoc_name}` is not const because {trait_descr} \
378 `{trait_name}` is not const",
379 ),
380 );
381 if let Some(parent) = parent.as_local()
382 && ccx.tcx.sess.is_nightly_build()
383 {
384 if !ccx.tcx.features().const_trait_impl() {
385 err.help(
386 "add `#![feature(const_trait_impl)]` to the crate attributes to \
387 enable const traits",
388 );
389 }
390 let span = ccx.tcx.hir_expect_item(parent).vis_span;
391 let span = ccx.tcx.sess.source_map().span_extend_while_whitespace(span);
392 err.span_suggestion_verbose(
393 span.shrink_to_hi(),
394 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider making trait `{0}` const",
trait_name))
})format!("consider making trait `{trait_name}` const"),
395 "const ".to_owned(),
396 Applicability::MaybeIncorrect,
397 );
398 } else if !ccx.tcx.sess.is_nightly_build() {
399 err.help("const traits are not yet supported on stable Rust");
400 }
401 }
402 } else if ccx.tcx.constness(callee) != hir::Constness::Const {
403 let name = ccx.tcx.item_name(callee);
404 err.span_note(
405 ccx.tcx.def_span(callee),
406 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} `{1}` is not const", def_descr,
name))
})format!("{def_descr} `{name}` is not const"),
407 );
408 }
409 err
410 }
411 };
412
413 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!(
414 "calls in {}s are limited to constant functions, tuple structs and tuple variants",
415 ccx.const_kind(),
416 ));
417
418 err
419}
420
421#[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)]
425pub(crate) struct CallUnstable {
426 pub def_id: DefId,
427 pub feature: Symbol,
428 pub feature_enabled: bool,
431 pub safe_to_expose_on_stable: bool,
432 pub is_function_call: bool,
434}
435
436impl<'tcx> NonConstOp<'tcx> for CallUnstable {
437 fn status_in_item(&self, _ccx: &ConstCx<'_, 'tcx>) -> Status {
438 Status::Unstable {
439 gate: self.feature,
440 gate_already_checked: self.feature_enabled,
441 safe_to_expose_on_stable: self.safe_to_expose_on_stable,
442 is_function_call: self.is_function_call,
443 }
444 }
445
446 fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
447 if !!self.feature_enabled {
::core::panicking::panic("assertion failed: !self.feature_enabled")
};assert!(!self.feature_enabled);
448 let mut err = if self.is_function_call {
449 ccx.dcx().create_err(errors::UnstableConstFn {
450 span,
451 def_path: ccx.tcx.def_path_str(self.def_id),
452 })
453 } else {
454 ccx.dcx().create_err(errors::UnstableConstTrait {
455 span,
456 def_path: ccx.tcx.def_path_str(self.def_id),
457 })
458 };
459 ccx.tcx.disabled_nightly_features(&mut err, [(String::new(), self.feature)]);
460 err
461 }
462}
463
464#[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)]
466pub(crate) struct IntrinsicNonConst {
467 pub name: Symbol,
468}
469
470impl<'tcx> NonConstOp<'tcx> for IntrinsicNonConst {
471 fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
472 ccx.dcx().create_err(errors::NonConstIntrinsic {
473 span,
474 name: self.name,
475 kind: ccx.const_kind(),
476 })
477 }
478}
479
480#[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)]
482pub(crate) struct IntrinsicUnstable {
483 pub name: Symbol,
484 pub feature: Symbol,
485 pub const_stable_indirect: bool,
486}
487
488impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable {
489 fn status_in_item(&self, _ccx: &ConstCx<'_, 'tcx>) -> Status {
490 Status::Unstable {
491 gate: self.feature,
492 gate_already_checked: false,
493 safe_to_expose_on_stable: self.const_stable_indirect,
494 is_function_call: false,
497 }
498 }
499
500 fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
501 ccx.dcx().create_err(errors::UnstableIntrinsic {
502 span,
503 name: self.name,
504 feature: self.feature,
505 suggestion: ccx.tcx.crate_level_attribute_injection_span(),
506 })
507 }
508}
509
510#[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)]
511pub(crate) struct Coroutine(pub hir::CoroutineKind);
512impl<'tcx> NonConstOp<'tcx> for Coroutine {
513 fn status_in_item(&self, _: &ConstCx<'_, 'tcx>) -> Status {
514 match self.0 {
515 hir::CoroutineKind::Desugared(
516 hir::CoroutineDesugaring::Async,
517 hir::CoroutineSource::Block,
518 )
519 | hir::CoroutineKind::Coroutine(_) => Status::Unstable {
522 gate: sym::const_async_blocks,
523 gate_already_checked: false,
524 safe_to_expose_on_stable: false,
525 is_function_call: false,
526 },
527 _ => Status::Forbidden,
528 }
529 }
530
531 fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
532 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());
533 if let Status::Unstable { gate, .. } = self.status_in_item(ccx) {
534 ccx.tcx.sess.create_feature_err(errors::UnallowedOpInConstContext { span, msg }, gate)
535 } else {
536 ccx.dcx().create_err(errors::UnallowedOpInConstContext { span, msg })
537 }
538 }
539}
540
541#[derive(#[automatically_derived]
impl ::core::fmt::Debug for HeapAllocation {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "HeapAllocation")
}
}Debug)]
542pub(crate) struct HeapAllocation;
543impl<'tcx> NonConstOp<'tcx> for HeapAllocation {
544 fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
545 ccx.dcx().create_err(errors::UnallowedHeapAllocations {
546 span,
547 kind: ccx.const_kind(),
548 teach: ccx.tcx.sess.teach(E0010),
549 })
550 }
551}
552
553#[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)]
554pub(crate) struct InlineAsm;
555impl<'tcx> NonConstOp<'tcx> for InlineAsm {
556 fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
557 ccx.dcx().create_err(errors::UnallowedInlineAsm { span, kind: ccx.const_kind() })
558 }
559}
560
561#[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)]
562pub(crate) struct LiveDrop<'tcx> {
563 pub dropped_at: Span,
564 pub dropped_ty: Ty<'tcx>,
565 pub needs_non_const_drop: bool,
566}
567impl<'tcx> NonConstOp<'tcx> for LiveDrop<'tcx> {
568 fn status_in_item(&self, _ccx: &ConstCx<'_, 'tcx>) -> Status {
569 if self.needs_non_const_drop {
570 Status::Forbidden
571 } else {
572 Status::Unstable {
573 gate: sym::const_destruct,
574 gate_already_checked: false,
575 safe_to_expose_on_stable: false,
576 is_function_call: false,
577 }
578 }
579 }
580
581 fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
582 if self.needs_non_const_drop {
583 ccx.dcx().create_err(errors::LiveDrop {
584 span,
585 dropped_ty: self.dropped_ty,
586 kind: ccx.const_kind(),
587 dropped_at: self.dropped_at,
588 })
589 } else {
590 ccx.tcx.sess.create_feature_err(
591 errors::LiveDrop {
592 span,
593 dropped_ty: self.dropped_ty,
594 kind: ccx.const_kind(),
595 dropped_at: self.dropped_at,
596 },
597 sym::const_destruct,
598 )
599 }
600 }
601}
602
603#[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)]
604pub(crate) struct EscapingCellBorrow;
608impl<'tcx> NonConstOp<'tcx> for EscapingCellBorrow {
609 fn importance(&self) -> DiagImportance {
610 DiagImportance::Secondary
613 }
614 fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
615 ccx.dcx().create_err(errors::InteriorMutableBorrowEscaping { span, kind: ccx.const_kind() })
616 }
617}
618
619#[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)]
620pub(crate) struct EscapingMutBorrow;
624
625impl<'tcx> NonConstOp<'tcx> for EscapingMutBorrow {
626 fn status_in_item(&self, _ccx: &ConstCx<'_, 'tcx>) -> Status {
627 Status::Forbidden
628 }
629
630 fn importance(&self) -> DiagImportance {
631 DiagImportance::Secondary
634 }
635
636 fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
637 ccx.dcx().create_err(errors::MutableBorrowEscaping { span, kind: ccx.const_kind() })
638 }
639}
640
641#[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)]
643pub(crate) struct PanicNonStr;
644impl<'tcx> NonConstOp<'tcx> for PanicNonStr {
645 fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
646 ccx.dcx().create_err(errors::PanicNonStrErr { span })
647 }
648}
649
650#[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)]
654pub(crate) struct RawPtrComparison;
655impl<'tcx> NonConstOp<'tcx> for RawPtrComparison {
656 fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
657 ccx.dcx().create_err(errors::RawPtrComparisonErr { span })
659 }
660}
661
662#[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)]
666pub(crate) struct RawPtrToIntCast;
667impl<'tcx> NonConstOp<'tcx> for RawPtrToIntCast {
668 fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
669 ccx.dcx().create_err(errors::RawPtrToIntErr { span })
670 }
671}
672
673#[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)]
675pub(crate) struct ThreadLocalAccess;
676impl<'tcx> NonConstOp<'tcx> for ThreadLocalAccess {
677 fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
678 ccx.dcx().create_err(errors::ThreadLocalAccessErr { span })
679 }
680}