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 prefered over `panic`/`unreachable` or others.
5 /// If you have a span available, you should use [`span_bug`] instead.
7 /// If the bug should only be emitted when compilation didn't fail, [`Session::delay_span_bug`] may be useful.
9 /// [`Session::delay_span_bug`]: rustc_session::Session::delay_span_bug
10 /// [`span_bug`]: crate::span_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)+))
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
26 /// If the bug should only be emitted when compilation didn't fail, [`Session::delay_span_bug`] may be useful.
28 /// [`Session::delay_span_bug`]: rustc_session::Session::delay_span_bug
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)+))
38 ///////////////////////////////////////////////////////////////////////////
39 // Lift and TypeFoldable/TypeVisitable macros
41 // When possible, use one of these (relatively) convenient macros to write
45 macro_rules! CloneLiftImpls {
46 (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
48 impl<$tcx> $crate::ty::Lift<$tcx> for $ty {
50 fn lift_to_tcx(self, _: $crate::ty::TyCtxt<$tcx>) -> Option<Self> {
66 /// Used for types that are `Copy` and which **do not care arena
67 /// allocated data** (i.e., don't need to be folded).
69 macro_rules! TrivialTypeTraversalImpls {
70 (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
72 impl<$tcx> $crate::ty::fold::TypeFoldable<$tcx> for $ty {
73 fn try_fold_with<F: $crate::ty::fold::FallibleTypeFolder<$tcx>>(
76 ) -> ::std::result::Result<Self, F::Error> {
81 fn fold_with<F: $crate::ty::fold::TypeFolder<$tcx>>(
89 impl<$tcx> $crate::ty::visit::TypeVisitable<$tcx> for $ty {
91 fn visit_with<F: $crate::ty::visit::TypeVisitor<$tcx>>(
94 -> ::std::ops::ControlFlow<F::BreakTy>
96 ::std::ops::ControlFlow::Continue(())
103 TrivialTypeTraversalImpls! {
112 macro_rules! TrivialTypeTraversalAndLiftImpls {
114 TrivialTypeTraversalImpls! { $($t)* }
115 CloneLiftImpls! { $($t)* }
120 macro_rules! EnumTypeTraversalImpl {
121 (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
123 } $(where $($wc:tt)*)*) => {
124 impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
127 fn try_fold_with<V: $crate::ty::fold::FallibleTypeFolder<$tcx>>(
130 ) -> ::std::result::Result<Self, V::Error> {
131 EnumTypeTraversalImpl!(@FoldVariants(self, folder) input($($variants)*) output())
136 (impl<$($p:tt),*> TypeVisitable<$tcx:tt> for $s:path {
138 } $(where $($wc:tt)*)*) => {
139 impl<$($p),*> $crate::ty::visit::TypeVisitable<$tcx> for $s
142 fn visit_with<V: $crate::ty::visit::TypeVisitor<$tcx>>(
145 ) -> ::std::ops::ControlFlow<V::BreakTy> {
146 EnumTypeTraversalImpl!(@VisitVariants(self, visitor) input($($variants)*) output())
151 (@FoldVariants($this:expr, $folder:expr) input() output($($output:tt)*)) => {
157 (@FoldVariants($this:expr, $folder:expr)
158 input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
159 output( $($output:tt)*) ) => {
160 EnumTypeTraversalImpl!(
161 @FoldVariants($this, $folder)
164 $variant ( $($variant_arg),* ) => {
166 $($crate::ty::fold::TypeFoldable::try_fold_with($variant_arg, $folder)?),*
174 (@FoldVariants($this:expr, $folder:expr)
175 input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
176 output( $($output:tt)*) ) => {
177 EnumTypeTraversalImpl!(
178 @FoldVariants($this, $folder)
181 $variant { $($variant_arg),* } => {
183 $($variant_arg: $crate::ty::fold::TypeFoldable::fold_with(
184 $variant_arg, $folder
192 (@FoldVariants($this:expr, $folder:expr)
193 input( ($variant:path), $($input:tt)*)
194 output( $($output:tt)*) ) => {
195 EnumTypeTraversalImpl!(
196 @FoldVariants($this, $folder)
199 $variant => { $variant }
205 (@VisitVariants($this:expr, $visitor:expr) input() output($($output:tt)*)) => {
211 (@VisitVariants($this:expr, $visitor:expr)
212 input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
213 output( $($output:tt)*) ) => {
214 EnumTypeTraversalImpl!(
215 @VisitVariants($this, $visitor)
218 $variant ( $($variant_arg),* ) => {
219 $($crate::ty::visit::TypeVisitable::visit_with(
220 $variant_arg, $visitor
222 ::std::ops::ControlFlow::Continue(())
229 (@VisitVariants($this:expr, $visitor:expr)
230 input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
231 output( $($output:tt)*) ) => {
232 EnumTypeTraversalImpl!(
233 @VisitVariants($this, $visitor)
236 $variant { $($variant_arg),* } => {
237 $($crate::ty::visit::TypeVisitable::visit_with(
238 $variant_arg, $visitor
240 ::std::ops::ControlFlow::Continue(())
247 (@VisitVariants($this:expr, $visitor:expr)
248 input( ($variant:path), $($input:tt)*)
249 output( $($output:tt)*) ) => {
250 EnumTypeTraversalImpl!(
251 @VisitVariants($this, $visitor)
254 $variant => { ::std::ops::ControlFlow::Continue(()) }