marker.rs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. use proc_macro2::{
  2. Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree,
  3. };
  4. macro_rules! assert_impl {
  5. ($ty:ident is $($marker:ident) and +) => {
  6. #[test]
  7. #[allow(non_snake_case)]
  8. fn $ty() {
  9. fn assert_implemented<T: $($marker +)+>() {}
  10. assert_implemented::<$ty>();
  11. }
  12. };
  13. ($ty:ident is not $($marker:ident) or +) => {
  14. #[test]
  15. #[allow(non_snake_case)]
  16. fn $ty() {
  17. $(
  18. {
  19. // Implemented for types that implement $marker.
  20. trait IsNotImplemented {
  21. fn assert_not_implemented() {}
  22. }
  23. impl<T: $marker> IsNotImplemented for T {}
  24. // Implemented for the type being tested.
  25. trait IsImplemented {
  26. fn assert_not_implemented() {}
  27. }
  28. impl IsImplemented for $ty {}
  29. // If $ty does not implement $marker, there is no ambiguity
  30. // in the following trait method call.
  31. <$ty>::assert_not_implemented();
  32. }
  33. )+
  34. }
  35. };
  36. }
  37. assert_impl!(Delimiter is Send and Sync);
  38. assert_impl!(Spacing is Send and Sync);
  39. assert_impl!(Group is not Send or Sync);
  40. assert_impl!(Ident is not Send or Sync);
  41. assert_impl!(LexError is not Send or Sync);
  42. assert_impl!(Literal is not Send or Sync);
  43. assert_impl!(Punct is not Send or Sync);
  44. assert_impl!(Span is not Send or Sync);
  45. assert_impl!(TokenStream is not Send or Sync);
  46. assert_impl!(TokenTree is not Send or Sync);
  47. #[cfg(procmacro2_semver_exempt)]
  48. mod semver_exempt {
  49. use proc_macro2::{LineColumn, SourceFile};
  50. assert_impl!(LineColumn is Send and Sync);
  51. assert_impl!(SourceFile is not Send or Sync);
  52. }
  53. #[cfg(not(no_libprocmacro_unwind_safe))]
  54. mod unwind_safe {
  55. use proc_macro2::{
  56. Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree,
  57. };
  58. #[cfg(procmacro2_semver_exempt)]
  59. use proc_macro2::{LineColumn, SourceFile};
  60. use std::panic::{RefUnwindSafe, UnwindSafe};
  61. macro_rules! assert_unwind_safe {
  62. ($($types:ident)*) => {
  63. $(
  64. assert_impl!($types is UnwindSafe and RefUnwindSafe);
  65. )*
  66. };
  67. }
  68. assert_unwind_safe! {
  69. Delimiter
  70. Group
  71. Ident
  72. LexError
  73. Literal
  74. Punct
  75. Spacing
  76. Span
  77. TokenStream
  78. TokenTree
  79. }
  80. #[cfg(procmacro2_semver_exempt)]
  81. assert_unwind_safe! {
  82. LineColumn
  83. SourceFile
  84. }
  85. }