|
| 1 | +//! Useful macros for writing unit tests. They let you gin up dummy types and things. |
| 2 | +
|
| 3 | +macro_rules! ty { |
| 4 | + (apply $n:tt $($arg:tt)*) => { |
| 5 | + chalk_ir::TyData::Apply(ApplicationTy { |
| 6 | + name: ty_name!($n), |
| 7 | + substitution: chalk_ir::Substitution::from( |
| 8 | + &chalk_integration::interner::ChalkIr, |
| 9 | + vec![$(arg!($arg)),*] as Vec<chalk_ir::Parameter<_>> |
| 10 | + ), |
| 11 | + }).intern(&chalk_integration::interner::ChalkIr) |
| 12 | + }; |
| 13 | + |
| 14 | + (function $n:tt $($arg:tt)*) => { |
| 15 | + chalk_ir::TyData::Function(Fn { |
| 16 | + num_binders: $n, |
| 17 | + substitution: chalk_ir::Substitution::from( |
| 18 | + &chalk_integration::interner::ChalkIr, |
| 19 | + vec![$(arg!($arg)),*] as Vec<chalk_ir::Parameter<_>> |
| 20 | + ), |
| 21 | + }).intern(&chalk_integration::interner::ChalkIr) |
| 22 | + }; |
| 23 | + |
| 24 | + (placeholder $n:expr) => { |
| 25 | + chalk_ir::TyData::Placeholder(PlaceholderIndex { |
| 26 | + ui: UniverseIndex { counter: $n }, |
| 27 | + idx: 0, |
| 28 | + }).intern(&chalk_integration::interner::ChalkIr) |
| 29 | + }; |
| 30 | + |
| 31 | + (projection (item $n:tt) $($arg:tt)*) => { |
| 32 | + chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { |
| 33 | + associated_ty_id: AssocTypeId(chalk_integration::interner::RawId { index: $n }), |
| 34 | + substitution: chalk_ir::Substitution::from( |
| 35 | + &chalk_integration::interner::ChalkIr, |
| 36 | + vec![$(arg!($arg)),*] as Vec<chalk_ir::Parameter<_>> |
| 37 | + ), |
| 38 | + }).intern(&chalk_integration::interner::ChalkIr) |
| 39 | + }; |
| 40 | + |
| 41 | + (infer $b:expr) => { |
| 42 | + chalk_ir::TyData::InferenceVar(chalk_ir::InferenceVar::from($b)) |
| 43 | + .intern(&chalk_integration::interner::ChalkIr) |
| 44 | + }; |
| 45 | + |
| 46 | + (bound $d:tt $b:tt) => { |
| 47 | + chalk_ir::TyData::BoundVar(chalk_ir::BoundVar::new(chalk_ir::DebruijnIndex::new($d), $b)) |
| 48 | + .intern(&chalk_integration::interner::ChalkIr) |
| 49 | + }; |
| 50 | + |
| 51 | + (bound $b:expr) => { |
| 52 | + chalk_ir::TyData::BoundVar(chalk_ir::BoundVar::new(chalk_ir::DebruijnIndex::INNERMOST, $b)) |
| 53 | + .intern(&chalk_integration::interner::ChalkIr) |
| 54 | + }; |
| 55 | + |
| 56 | + (expr $b:expr) => { |
| 57 | + $b.clone() |
| 58 | + }; |
| 59 | + |
| 60 | + (($($b:tt)*)) => { |
| 61 | + ty!($($b)*) |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +macro_rules! arg { |
| 66 | + ((lifetime $b:tt)) => { |
| 67 | + chalk_ir::Parameter::new( |
| 68 | + &chalk_integration::interner::ChalkIr, |
| 69 | + chalk_ir::ParameterKind::Lifetime(lifetime!($b)), |
| 70 | + ) |
| 71 | + }; |
| 72 | + |
| 73 | + ($arg:tt) => { |
| 74 | + chalk_ir::Parameter::new( |
| 75 | + &chalk_integration::interner::ChalkIr, |
| 76 | + chalk_ir::ParameterKind::Ty(ty!($arg)), |
| 77 | + ) |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +macro_rules! lifetime { |
| 82 | + (infer $b:expr) => { |
| 83 | + chalk_ir::LifetimeData::InferenceVar(chalk_ir::InferenceVar::from($b)) |
| 84 | + .intern(&chalk_integration::interner::ChalkIr) |
| 85 | + }; |
| 86 | + |
| 87 | + (bound $d:tt $b:tt) => { |
| 88 | + chalk_ir::LifetimeData::BoundVar(chalk_ir::BoundVar::new(chalk_ir::DebruijnIndex::new($d), $b)) |
| 89 | + .intern(&chalk_integration::interner::ChalkIr) |
| 90 | + }; |
| 91 | + |
| 92 | + (bound $b:expr) => { |
| 93 | + chalk_ir::LifetimeData::BoundVar(chalk_ir::BoundVar::new(chalk_ir::DebruijnIndex::INNERMOST, $b)) |
| 94 | + .intern(&chalk_integration::interner::ChalkIr) |
| 95 | + }; |
| 96 | + |
| 97 | + (placeholder $b:expr) => { |
| 98 | + chalk_ir::LifetimeData::Placeholder(PlaceholderIndex { ui: UniverseIndex { counter: $b }, idx: 0}) |
| 99 | + .intern(&chalk_integration::interner::ChalkIr) |
| 100 | + }; |
| 101 | + |
| 102 | + (expr $b:expr) => { |
| 103 | + $b.clone() |
| 104 | + }; |
| 105 | + |
| 106 | + (($($b:tt)*)) => { |
| 107 | + lifetime!($($b)*) |
| 108 | + }; |
| 109 | +} |
| 110 | + |
| 111 | +macro_rules! ty_name { |
| 112 | + ((item $n:expr)) => { |
| 113 | + chalk_ir::TypeName::Struct(StructId(chalk_integration::interner::RawId { index: $n })) |
| 114 | + }; |
| 115 | +} |
0 commit comments