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 (enum $enum_name:path {
67 // this incorrectly allows specifying both tuple-like and struct-like fields, as in `Variant(a,b){c,d}`,
68 // when it should be only one or the other
69 $( ( $($field:ident $(-> $delegate:tt)?),* ) )?
70 $( { $($named_field:ident $(-> $named_delegate:tt)?),* } )?
73 impl_stable_hash_for!(
74 impl<> for enum $enum_name [ $enum_name ] { $( $variant
75 $( ( $($field $(-> $delegate)?),* ) )?
76 $( { $($named_field $(-> $named_delegate)?),* } )?
80 // We want to use the enum name both in the `impl ... for $enum_name` as well as for
81 // importing all the variants. Unfortunately it seems we have to take the name
82 // twice for this purpose
83 (impl<$($T:ident),* $(,)?>
84 for enum $enum_name:path
88 // this incorrectly allows specifying both tuple-like and struct-like fields, as in `Variant(a,b){c,d}`,
89 // when it should be only one or the other
90 $( ( $($field:ident $(-> $delegate:tt)?),* ) )?
91 $( { $($named_field:ident $(-> $named_delegate:tt)?),* } )?
95 ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>
97 where $($T: ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>),*
100 fn hash_stable(&self,
101 __ctx: &mut $crate::ich::StableHashingContext<'a>,
102 __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) {
104 ::std::mem::discriminant(self).hash_stable(__ctx, __hasher);
108 $variant $( ( $(ref $field),* ) )? $( { $(ref $named_field),* } )? => {
109 $($( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)?) );*)?
110 $($( __impl_stable_hash_field!($named_field, __ctx, __hasher $(, $named_delegate)?) );*)?
118 (struct $struct_name:path { $($field:ident $(-> $delegate:tt)?),* $(,)? }) => {
119 impl_stable_hash_for!(
120 impl<> for struct $struct_name { $($field $(-> $delegate)?),* }
123 (impl<$($T:ident),* $(,)?> for struct $struct_name:path {
124 $($field:ident $(-> $delegate:tt)?),* $(,)?
127 ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $struct_name
128 where $($T: ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>),*
131 fn hash_stable(&self,
132 __ctx: &mut $crate::ich::StableHashingContext<'a>,
133 __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) {
138 $( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)?) );*
143 // We cannot use normal parentheses here, the parser won't allow it
144 (tuple_struct $struct_name:path { $($field:ident $(-> $delegate:tt)?),* $(,)? }) => {
145 impl_stable_hash_for!(
146 impl<> for tuple_struct $struct_name { $($field $(-> $delegate)?),* }
149 (impl<$($T:ident),* $(,)?>
150 for tuple_struct $struct_name:path { $($field:ident $(-> $delegate:tt)?),* $(,)? }) => {
152 ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $struct_name
153 where $($T: ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>),*
156 fn hash_stable(&self,
157 __ctx: &mut $crate::ich::StableHashingContext<'a>,
158 __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) {
163 $( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)?) );*
170 macro_rules! impl_stable_hash_for_spanned {
173 impl HashStable<StableHashingContext<'a>> for ::syntax::source_map::Spanned<$T>
176 fn hash_stable(&self,
177 hcx: &mut StableHashingContext<'a>,
178 hasher: &mut StableHasher) {
179 self.node.hash_stable(hcx, hasher);
180 self.span.hash_stable(hcx, hasher);
186 ///////////////////////////////////////////////////////////////////////////
187 // Lift and TypeFoldable macros
189 // When possible, use one of these (relatively) convenient macros to write
190 // the impls for you.
193 macro_rules! CloneLiftImpls {
194 (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
196 impl<$tcx> $crate::ty::Lift<$tcx> for $ty {
198 fn lift_to_tcx(&self, _: $crate::ty::TyCtxt<$tcx>) -> Option<Self> {
199 Some(Clone::clone(self))
214 /// Used for types that are `Copy` and which **do not care arena
215 /// allocated data** (i.e., don't need to be folded).
217 macro_rules! CloneTypeFoldableImpls {
218 (for <$tcx:lifetime> { $($ty:ty,)+ }) => {
220 impl<$tcx> $crate::ty::fold::TypeFoldable<$tcx> for $ty {
221 fn super_fold_with<F: $crate::ty::fold::TypeFolder<$tcx>>(
228 fn super_visit_with<F: $crate::ty::fold::TypeVisitor<$tcx>>(
240 CloneTypeFoldableImpls! {
249 macro_rules! CloneTypeFoldableAndLiftImpls {
251 CloneTypeFoldableImpls! { $($t)* }
252 CloneLiftImpls! { $($t)* }
257 macro_rules! BraceStructLiftImpl {
258 (impl<$($p:tt),*> Lift<$tcx:tt> for $s:path {
259 type Lifted = $lifted:ty;
260 $($field:ident),* $(,)?
261 } $(where $($wc:tt)*)*) => {
262 impl<$($p),*> $crate::ty::Lift<$tcx> for $s
265 type Lifted = $lifted;
267 fn lift_to_tcx(&self, tcx: TyCtxt<$tcx>) -> Option<$lifted> {
268 $(let $field = tcx.lift(&self.$field)?;)*
269 Some(Self::Lifted { $($field),* })
276 macro_rules! EnumLiftImpl {
277 (impl<$($p:tt),*> Lift<$tcx:tt> for $s:path {
278 type Lifted = $lifted:ty;
280 } $(where $($wc:tt)*)*) => {
281 impl<$($p),*> $crate::ty::Lift<$tcx> for $s
284 type Lifted = $lifted;
286 fn lift_to_tcx(&self, tcx: TyCtxt<$tcx>) -> Option<$lifted> {
287 EnumLiftImpl!(@Variants(self, tcx) input($($variants)*) output())
292 (@Variants($this:expr, $tcx:expr) input() output($($output:tt)*)) => {
298 (@Variants($this:expr, $tcx:expr)
299 input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
300 output( $($output:tt)*) ) => {
302 @Variants($this, $tcx)
305 $variant ( $($variant_arg),* ) => {
306 Some($variant ( $($tcx.lift($variant_arg)?),* ))
313 (@Variants($this:expr, $tcx:expr)
314 input( ($variant:path), $($input:tt)*)
315 output( $($output:tt)*) ) => {
317 @Variants($this, $tcx)
320 $variant => { Some($variant) }
328 macro_rules! BraceStructTypeFoldableImpl {
329 (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
330 $($field:ident),* $(,)?
331 } $(where $($wc:tt)*)*) => {
332 impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
335 fn super_fold_with<V: $crate::ty::fold::TypeFolder<$tcx>>(
339 let $s { $($field,)* } = self;
340 $s { $($field: $crate::ty::fold::TypeFoldable::fold_with($field, folder),)* }
343 fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
347 let $s { $($field,)* } = self;
348 false $(|| $crate::ty::fold::TypeFoldable::visit_with($field, visitor))*
355 macro_rules! TupleStructTypeFoldableImpl {
356 (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
357 $($field:ident),* $(,)?
358 } $(where $($wc:tt)*)*) => {
359 impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
362 fn super_fold_with<V: $crate::ty::fold::TypeFolder<$tcx>>(
366 let $s($($field,)*)= self;
367 $s($($crate::ty::fold::TypeFoldable::fold_with($field, folder),)*)
370 fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
374 let $s($($field,)*) = self;
375 false $(|| $crate::ty::fold::TypeFoldable::visit_with($field, visitor))*
382 macro_rules! EnumTypeFoldableImpl {
383 (impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
385 } $(where $($wc:tt)*)*) => {
386 impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
389 fn super_fold_with<V: $crate::ty::fold::TypeFolder<$tcx>>(
393 EnumTypeFoldableImpl!(@FoldVariants(self, folder) input($($variants)*) output())
396 fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
400 EnumTypeFoldableImpl!(@VisitVariants(self, visitor) input($($variants)*) output())
405 (@FoldVariants($this:expr, $folder:expr) input() output($($output:tt)*)) => {
411 (@FoldVariants($this:expr, $folder:expr)
412 input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
413 output( $($output:tt)*) ) => {
414 EnumTypeFoldableImpl!(
415 @FoldVariants($this, $folder)
418 $variant ( $($variant_arg),* ) => {
420 $($crate::ty::fold::TypeFoldable::fold_with($variant_arg, $folder)),*
428 (@FoldVariants($this:expr, $folder:expr)
429 input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
430 output( $($output:tt)*) ) => {
431 EnumTypeFoldableImpl!(
432 @FoldVariants($this, $folder)
435 $variant { $($variant_arg),* } => {
437 $($variant_arg: $crate::ty::fold::TypeFoldable::fold_with(
438 $variant_arg, $folder
446 (@FoldVariants($this:expr, $folder:expr)
447 input( ($variant:path), $($input:tt)*)
448 output( $($output:tt)*) ) => {
449 EnumTypeFoldableImpl!(
450 @FoldVariants($this, $folder)
453 $variant => { $variant }
459 (@VisitVariants($this:expr, $visitor:expr) input() output($($output:tt)*)) => {
465 (@VisitVariants($this:expr, $visitor:expr)
466 input( ($variant:path) ( $($variant_arg:ident),* ) , $($input:tt)*)
467 output( $($output:tt)*) ) => {
468 EnumTypeFoldableImpl!(
469 @VisitVariants($this, $visitor)
472 $variant ( $($variant_arg),* ) => {
473 false $(|| $crate::ty::fold::TypeFoldable::visit_with(
474 $variant_arg, $visitor
482 (@VisitVariants($this:expr, $visitor:expr)
483 input( ($variant:path) { $($variant_arg:ident),* $(,)? } , $($input:tt)*)
484 output( $($output:tt)*) ) => {
485 EnumTypeFoldableImpl!(
486 @VisitVariants($this, $visitor)
489 $variant { $($variant_arg),* } => {
490 false $(|| $crate::ty::fold::TypeFoldable::visit_with(
491 $variant_arg, $visitor
499 (@VisitVariants($this:expr, $visitor:expr)
500 input( ($variant:path), $($input:tt)*)
501 output( $($output:tt)*) ) => {
502 EnumTypeFoldableImpl!(
503 @VisitVariants($this, $visitor)
506 $variant => { false }