1 // ignore-tidy-linelength
3 macro_rules! enum_from_u32 {
4 ($(#[$attr:meta])* pub enum $name:ident {
5 $($variant:ident = $e:expr,)*
13 pub fn from_u32(u: u32) -> Option<$name> {
14 $(if u == $name::$variant as u32 {
15 return Some($name::$variant)
21 ($(#[$attr:meta])* pub enum $name:ident {
30 pub fn from_u32(u: u32) -> Option<$name> {
31 $(if u == $name::$variant as u32 {
32 return Some($name::$variant)
42 () => ( bug!("impossible case reached") );
43 ($($message:tt)*) => ({
44 $crate::util::bug::bug_fmt(file!(), line!(), format_args!($($message)*))
49 macro_rules! span_bug {
50 ($span:expr, $($message:tt)*) => ({
51 $crate::util::bug::span_bug_fmt(file!(), line!(), $span, format_args!($($message)*))
56 macro_rules! __impl_stable_hash_field {
57 ($field:ident, $ctx:expr, $hasher:expr) => ($field.hash_stable($ctx, $hasher));
58 ($field:ident, $ctx:expr, $hasher:expr, _) => ({ let _ = $field; });
59 ($field:ident, $ctx:expr, $hasher:expr, $delegate:expr) => ($delegate.hash_stable($ctx, $hasher));
63 macro_rules! impl_stable_hash_for {
65 // FIXME(mark-i-m): Some of these should be `?` rather than `*`. See the git blame and change
66 // them back when `?` is supported again.
67 (enum $enum_name:path {
69 // this incorrectly allows specifying both tuple-like and struct-like fields, as in `Variant(a,b){c,d}`,
70 // when it should be only one or the other
71 $( ( $($field:ident $(-> $delegate:tt)*),* ) )*
72 $( { $($named_field:ident $(-> $named_delegate:tt)*),* } )*
75 impl_stable_hash_for!(
76 impl<> for enum $enum_name [ $enum_name ] { $( $variant
77 $( ( $($field $(-> $delegate)*),* ) )*
78 $( { $($named_field $(-> $named_delegate)*),* } )*
82 // We want to use the enum name both in the `impl ... for $enum_name` as well as for
83 // importing all the variants. Unfortunately it seems we have to take the name
84 // twice for this purpose
85 (impl<$($lt:lifetime $(: $lt_bound:lifetime)* ),* $(,)* $($T:ident),* $(,)*>
86 for enum $enum_name:path
90 // this incorrectly allows specifying both tuple-like and struct-like fields, as in `Variant(a,b){c,d}`,
91 // when it should be only one or the other
92 $( ( $($field:ident $(-> $delegate:tt)*),* ) )*
93 $( { $($named_field:ident $(-> $named_delegate:tt)*),* } )*
96 impl<'a, $($lt $(: $lt_bound)*,)* $($T,)*>
97 ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>
99 where $($T: ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>),*
102 fn hash_stable<W: ::rustc_data_structures::stable_hasher::StableHasherResult>(&self,
103 __ctx: &mut $crate::ich::StableHashingContext<'a>,
104 __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher<W>) {
106 ::std::mem::discriminant(self).hash_stable(__ctx, __hasher);
110 $variant $( ( $(ref $field),* ) )* $( { $(ref $named_field),* } )* => {
111 $($( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)*) );*)*
112 $($( __impl_stable_hash_field!($named_field, __ctx, __hasher $(, $named_delegate)*) );*)*
120 // FIXME(mark-i-m): same here.
121 (struct $struct_name:path { $($field:ident $(-> $delegate:tt)*),* $(,)* }) => {
122 impl_stable_hash_for!(
123 impl<'tcx> for struct $struct_name { $($field $(-> $delegate)*),* }
126 (impl<$($lt:lifetime $(: $lt_bound:lifetime)* ),* $(,)* $($T:ident),* $(,)*> for struct $struct_name:path {
127 $($field:ident $(-> $delegate:tt)*),* $(,)*
129 impl<'a, $($lt $(: $lt_bound)*,)* $($T,)*>
130 ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $struct_name
131 where $($T: ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>),*
134 fn hash_stable<W: ::rustc_data_structures::stable_hasher::StableHasherResult>(&self,
135 __ctx: &mut $crate::ich::StableHashingContext<'a>,
136 __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher<W>) {
141 $( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)*) );*
146 // We cannot use normale parentheses here, the parser won't allow it
147 // FIXME(mark-i-m): same here.
148 (tuple_struct $struct_name:path { $($field:ident $(-> $delegate:tt)*),* $(,)* }) => {
149 impl_stable_hash_for!(
150 impl<'tcx> for tuple_struct $struct_name { $($field $(-> $delegate)*),* }
153 (impl<$($lt:lifetime $(: $lt_bound:lifetime)* ),* $(,)* $($T:ident),* $(,)*>
154 for tuple_struct $struct_name:path { $($field:ident $(-> $delegate:tt)*),* $(,)* }) => {
155 impl<'a, $($lt $(: $lt_bound)*,)* $($T,)*>
156 ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $struct_name
157 where $($T: ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>),*
160 fn hash_stable<W: ::rustc_data_structures::stable_hasher::StableHasherResult>(&self,
161 __ctx: &mut $crate::ich::StableHashingContext<'a>,
162 __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher<W>) {
167 $( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)*) );*
174 macro_rules! impl_stable_hash_for_spanned {
177 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ::syntax::source_map::Spanned<$T>
180 fn hash_stable<W: StableHasherResult>(&self,
181 hcx: &mut StableHashingContext<'a>,
182 hasher: &mut StableHasher<W>) {
183 self.node.hash_stable(hcx, hasher);
184 self.span.hash_stable(hcx, hasher);
190 ///////////////////////////////////////////////////////////////////////////
191 // Lift and TypeFoldable macros
193 // When possible, use one of these (relatively) convenient macros to write
194 // the impls for you.
197 macro_rules! CloneLiftImpls {
198 (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
200 impl<$tcx> $crate::ty::Lift<$tcx> for $ty {
202 fn lift_to_tcx<'a, 'gcx>(&self, _: $crate::ty::TyCtxt<'a, 'gcx, $tcx>) -> Option<Self> {
203 Some(Clone::clone(self))
218 /// Used for types that are `Copy` and which **do not care arena
219 /// allocated data** (i.e., don't need to be folded).
221 macro_rules! CloneTypeFoldableImpls {
222 (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
224 impl<$tcx> $crate::ty::fold::TypeFoldable<$tcx> for $ty {
225 fn super_fold_with<'gcx: $tcx, F: $crate::ty::fold::TypeFolder<'gcx, $tcx>>(
232 fn super_visit_with<F: $crate::ty::fold::TypeVisitor<$tcx>>(
244 CloneTypeFoldableImpls! {
253 macro_rules! CloneTypeFoldableAndLiftImpls {
255 CloneTypeFoldableImpls! { $($t)* }
256 CloneLiftImpls! { $($t)* }
261 macro_rules! BraceStructLiftImpl {
262 (impl<$($p:tt),*> Lift<$tcx:tt> for $s:path {
263 type Lifted = $lifted:ty;
264 $($field:ident),* $(,)*
265 } $(where $($wc:tt)*)*) => {
266 impl<$($p),*> $crate::ty::Lift<$tcx> for $s
269 type Lifted = $lifted;
271 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<$lifted> {
272 $(let $field = tcx.lift(&self.$field)?;)*
273 Some(Self::Lifted { $($field),* })
280 macro_rules! EnumLiftImpl {
281 (impl<$($p:tt),*> Lift<$tcx:tt> for $s:path {
282 type Lifted = $lifted:ty;
284 } $(where $($wc:tt)*)*) => {
285 impl<$($p),*> $crate::ty::Lift<$tcx> for $s
288 type Lifted = $lifted;
290 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<$lifted> {
291 EnumLiftImpl!(@Variants(self, tcx) input($($variants)*) output())
296 (@Variants($this:expr, $tcx:expr) input() output($($output:tt)*)) => {
302 (@Variants($this:expr, $tcx:expr)
303 input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
304 output( $($output:tt)*) ) => {
306 @Variants($this, $tcx)
309 $variant ( $($variant_arg),* ) => {
310 Some($variant ( $($tcx.lift($variant_arg)?),* ))
317 (@Variants($this:expr, $tcx:expr)
318 input( ($variant:path), $($input:tt)*)
319 output( $($output:tt)*) ) => {
321 @Variants($this, $tcx)
324 $variant => { Some($variant) }
332 macro_rules! BraceStructTypeFoldableImpl {
333 (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
334 $($field:ident),* $(,)*
335 } $(where $($wc:tt)*)*) => {
336 impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
339 fn super_fold_with<'gcx: $tcx, V: $crate::ty::fold::TypeFolder<'gcx, $tcx>>(
343 let $s { $($field,)* } = self;
344 $s { $($field: $crate::ty::fold::TypeFoldable::fold_with($field, folder),)* }
347 fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
351 let $s { $($field,)* } = self;
352 false $(|| $crate::ty::fold::TypeFoldable::visit_with($field, visitor))*
359 macro_rules! TupleStructTypeFoldableImpl {
360 (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
361 $($field:ident),* $(,)*
362 } $(where $($wc:tt)*)*) => {
363 impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
366 fn super_fold_with<'gcx: $tcx, V: $crate::ty::fold::TypeFolder<'gcx, $tcx>>(
370 let $s($($field,)*)= self;
371 $s($($crate::ty::fold::TypeFoldable::fold_with($field, folder),)*)
374 fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
378 let $s($($field,)*) = self;
379 false $(|| $crate::ty::fold::TypeFoldable::visit_with($field, visitor))*
386 macro_rules! EnumTypeFoldableImpl {
387 (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
389 } $(where $($wc:tt)*)*) => {
390 impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
393 fn super_fold_with<'gcx: $tcx, V: $crate::ty::fold::TypeFolder<'gcx, $tcx>>(
397 EnumTypeFoldableImpl!(@FoldVariants(self, folder) input($($variants)*) output())
400 fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
404 EnumTypeFoldableImpl!(@VisitVariants(self, visitor) input($($variants)*) output())
409 (@FoldVariants($this:expr, $folder:expr) input() output($($output:tt)*)) => {
415 (@FoldVariants($this:expr, $folder:expr)
416 input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
417 output( $($output:tt)*) ) => {
418 EnumTypeFoldableImpl!(
419 @FoldVariants($this, $folder)
422 $variant ( $($variant_arg),* ) => {
424 $($crate::ty::fold::TypeFoldable::fold_with($variant_arg, $folder)),*
432 (@FoldVariants($this:expr, $folder:expr)
433 input( ($variant:path) { $($variant_arg:ident),* $(,)* } , $($input:tt)*)
434 output( $($output:tt)*) ) => {
435 EnumTypeFoldableImpl!(
436 @FoldVariants($this, $folder)
439 $variant { $($variant_arg),* } => {
441 $($variant_arg: $crate::ty::fold::TypeFoldable::fold_with(
442 $variant_arg, $folder
450 (@FoldVariants($this:expr, $folder:expr)
451 input( ($variant:path), $($input:tt)*)
452 output( $($output:tt)*) ) => {
453 EnumTypeFoldableImpl!(
454 @FoldVariants($this, $folder)
457 $variant => { $variant }
463 (@VisitVariants($this:expr, $visitor:expr) input() output($($output:tt)*)) => {
469 (@VisitVariants($this:expr, $visitor:expr)
470 input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
471 output( $($output:tt)*) ) => {
472 EnumTypeFoldableImpl!(
473 @VisitVariants($this, $visitor)
476 $variant ( $($variant_arg),* ) => {
477 false $(|| $crate::ty::fold::TypeFoldable::visit_with(
478 $variant_arg, $visitor
486 (@VisitVariants($this:expr, $visitor:expr)
487 input( ($variant:path) { $($variant_arg:ident),* $(,)* } , $($input:tt)*)
488 output( $($output:tt)*) ) => {
489 EnumTypeFoldableImpl!(
490 @VisitVariants($this, $visitor)
493 $variant { $($variant_arg),* } => {
494 false $(|| $crate::ty::fold::TypeFoldable::visit_with(
495 $variant_arg, $visitor
503 (@VisitVariants($this:expr, $visitor:expr)
504 input( ($variant:path), $($input:tt)*)
505 output( $($output:tt)*) ) => {
506 EnumTypeFoldableImpl!(
507 @VisitVariants($this, $visitor)
510 $variant => { false }