rustc_middle/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,
8/// [`DiagCtxtHandle::span_delayed_bug`] may be useful.
9///
10/// [`DiagCtxtHandle::span_delayed_bug`]: rustc_errors::DiagCtxtHandle::span_delayed_bug
11/// [`span_bug`]: crate::span_bug
12#[macro_export]
13macro_rules! bug {
14 () => (
15 $crate::bug!("impossible case reached")
16 );
17 ($($arg:tt)+) => (
18 $crate::util::bug::bug_fmt(::std::format_args!($($arg)+))
19 );
20}
21
22/// A macro for triggering an ICE with a span.
23/// Calling `span_bug!` instead of panicking will result in a nicer error message and point
24/// at the code the compiler was compiling when it ICEd. This is the preferred way to trigger
25/// ICEs.
26///
27/// If the bug should only be emitted when compilation didn't fail,
28/// [`DiagCtxtHandle::span_delayed_bug`] may be useful.
29///
30/// [`DiagCtxtHandle::span_delayed_bug`]: rustc_errors::DiagCtxtHandle::span_delayed_bug
31#[macro_export]
32macro_rules! span_bug {
33 ($span:expr, $($arg:tt)+) => (
34 $crate::util::bug::span_bug_fmt($span, ::std::format_args!($($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
44macro_rules! TrivialLiftImpls {
45 ($($ty:ty),+ $(,)?) => {
46 $(
47 impl<'tcx> $crate::ty::Lift<$crate::ty::TyCtxt<'tcx>> for $ty {
48 type Lifted = Self;
49 fn lift_to_interner(self, _: $crate::ty::TyCtxt<'tcx>) -> Option<Self> {
50 Some(self)
51 }
52 }
53 )+
54 };
55}
56
57/// Used for types that are `Copy` and which **do not care about arena
58/// allocated data** (i.e., don't need to be folded).
59macro_rules! TrivialTypeTraversalImpls {
60 ($($ty:ty),+ $(,)?) => {
61 $(
62 impl<'tcx> $crate::ty::fold::TypeFoldable<$crate::ty::TyCtxt<'tcx>> for $ty {
63 fn try_fold_with<F: $crate::ty::fold::FallibleTypeFolder<$crate::ty::TyCtxt<'tcx>>>(
64 self,
65 _: &mut F,
66 ) -> ::std::result::Result<Self, F::Error> {
67 Ok(self)
68 }
69
70 #[inline]
71 fn fold_with<F: $crate::ty::fold::TypeFolder<$crate::ty::TyCtxt<'tcx>>>(
72 self,
73 _: &mut F,
74 ) -> Self {
75 self
76 }
77 }
78
79 impl<'tcx> $crate::ty::visit::TypeVisitable<$crate::ty::TyCtxt<'tcx>> for $ty {
80 #[inline]
81 fn visit_with<F: $crate::ty::visit::TypeVisitor<$crate::ty::TyCtxt<'tcx>>>(
82 &self,
83 _: &mut F)
84 -> F::Result
85 {
86 <F::Result as ::rustc_ast_ir::visit::VisitorResult>::output()
87 }
88 }
89 )+
90 };
91}
92
93macro_rules! TrivialTypeTraversalAndLiftImpls {
94 ($($t:tt)*) => {
95 TrivialTypeTraversalImpls! { $($t)* }
96 TrivialLiftImpls! { $($t)* }
97 }
98}