]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_middle/src/macros.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / compiler / rustc_middle / src / macros.rs
1 /// A macro for triggering an ICE.
2 /// Calling `bug` instead of panicking will result in a nicer error message and should
3 /// therefore be preferred over `panic`/`unreachable` or others.
4 ///
5 /// If you have a span available, you should use [`span_bug`] instead.
6 ///
7 /// If the bug should only be emitted when compilation didn't fail, [`Session::delay_span_bug`] may be useful.
8 ///
9 /// [`Session::delay_span_bug`]: rustc_session::Session::delay_span_bug
10 /// [`span_bug`]: crate::span_bug
11 #[macro_export]
12 macro_rules! bug {
13 () => ( $crate::bug!("impossible case reached") );
14 ($msg:expr) => ({ $crate::util::bug::bug_fmt(::std::format_args!($msg)) });
15 ($msg:expr,) => ({ $crate::bug!($msg) });
16 ($fmt:expr, $($arg:tt)+) => ({
17 $crate::util::bug::bug_fmt(::std::format_args!($fmt, $($arg)+))
18 });
19 }
20
21 /// A macro for triggering an ICE with a span.
22 /// Calling `span_bug!` instead of panicking will result in a nicer error message and point
23 /// at the code the compiler was compiling when it ICEd. This is the preferred way to trigger
24 /// ICEs.
25 ///
26 /// If the bug should only be emitted when compilation didn't fail, [`Session::delay_span_bug`] may be useful.
27 ///
28 /// [`Session::delay_span_bug`]: rustc_session::Session::delay_span_bug
29 #[macro_export]
30 macro_rules! span_bug {
31 ($span:expr, $msg:expr) => ({ $crate::util::bug::span_bug_fmt($span, ::std::format_args!($msg)) });
32 ($span:expr, $msg:expr,) => ({ $crate::span_bug!($span, $msg) });
33 ($span:expr, $fmt:expr, $($arg:tt)+) => ({
34 $crate::util::bug::span_bug_fmt($span, ::std::format_args!($fmt, $($arg)+))
35 });
36 }
37
38 ///////////////////////////////////////////////////////////////////////////
39 // Lift and TypeFoldable/TypeVisitable macros
40 //
41 // When possible, use one of these (relatively) convenient macros to write
42 // the impls for you.
43
44 #[macro_export]
45 macro_rules! CloneLiftImpls {
46 ($($ty:ty,)+) => {
47 $(
48 impl<'tcx> $crate::ty::Lift<'tcx> for $ty {
49 type Lifted = Self;
50 fn lift_to_tcx(self, _: $crate::ty::TyCtxt<'tcx>) -> Option<Self> {
51 Some(self)
52 }
53 }
54 )+
55 };
56 }
57
58 /// Used for types that are `Copy` and which **do not care arena
59 /// allocated data** (i.e., don't need to be folded).
60 #[macro_export]
61 macro_rules! TrivialTypeTraversalImpls {
62 ($($ty:ty,)+) => {
63 $(
64 impl<'tcx> $crate::ty::fold::TypeFoldable<$crate::ty::TyCtxt<'tcx>> for $ty {
65 fn try_fold_with<F: $crate::ty::fold::FallibleTypeFolder<$crate::ty::TyCtxt<'tcx>>>(
66 self,
67 _: &mut F,
68 ) -> ::std::result::Result<Self, F::Error> {
69 Ok(self)
70 }
71
72 #[inline]
73 fn fold_with<F: $crate::ty::fold::TypeFolder<$crate::ty::TyCtxt<'tcx>>>(
74 self,
75 _: &mut F,
76 ) -> Self {
77 self
78 }
79 }
80
81 impl<'tcx> $crate::ty::visit::TypeVisitable<$crate::ty::TyCtxt<'tcx>> for $ty {
82 #[inline]
83 fn visit_with<F: $crate::ty::visit::TypeVisitor<$crate::ty::TyCtxt<'tcx>>>(
84 &self,
85 _: &mut F)
86 -> ::std::ops::ControlFlow<F::BreakTy>
87 {
88 ::std::ops::ControlFlow::Continue(())
89 }
90 }
91 )+
92 };
93 }
94
95 #[macro_export]
96 macro_rules! TrivialTypeTraversalAndLiftImpls {
97 ($($t:tt)*) => {
98 TrivialTypeTraversalImpls! { $($t)* }
99 CloneLiftImpls! { $($t)* }
100 }
101 }