|
| 1 | +//! Test that mutually recursive type definitions are properly handled by the compiler. |
| 2 | +//! This checks that types can reference each other in their definitions through |
| 3 | +//! `Box` indirection, creating cycles in the type dependency graph. |
| 4 | +
|
1 | 5 | //@ run-pass |
2 | 6 |
|
3 | | -#![allow(non_camel_case_types)] |
4 | | -#![allow(dead_code)] |
| 7 | +#[derive(Debug, PartialEq)] |
| 8 | +enum Colour { |
| 9 | + Red, |
| 10 | + Green, |
| 11 | + Blue, |
| 12 | +} |
| 13 | + |
| 14 | +#[derive(Debug, PartialEq)] |
| 15 | +enum Tree { |
| 16 | + Children(Box<List>), |
| 17 | + Leaf(Colour), |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Debug, PartialEq)] |
| 21 | +enum List { |
| 22 | + Cons(Box<Tree>, Box<List>), |
| 23 | + Nil, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Debug, PartialEq)] |
| 27 | +enum SmallList { |
| 28 | + Kons(isize, Box<SmallList>), |
| 29 | + Neel, |
| 30 | +} |
| 31 | + |
| 32 | +pub fn main() { |
| 33 | + // Construct and test all variants of Colour |
| 34 | + let leaf_red = Tree::Leaf(Colour::Red); |
| 35 | + assert_eq!(leaf_red, Tree::Leaf(Colour::Red)); |
5 | 36 |
|
| 37 | + let leaf_green = Tree::Leaf(Colour::Green); |
| 38 | + assert_eq!(leaf_green, Tree::Leaf(Colour::Green)); |
6 | 39 |
|
7 | | -enum colour { red, green, blue, } |
| 40 | + let leaf_blue = Tree::Leaf(Colour::Blue); |
| 41 | + assert_eq!(leaf_blue, Tree::Leaf(Colour::Blue)); |
8 | 42 |
|
9 | | -enum tree { children(Box<list>), leaf(colour), } |
| 43 | + let empty_list = List::Nil; |
| 44 | + assert_eq!(empty_list, List::Nil); |
10 | 45 |
|
11 | | -enum list { cons(Box<tree>, Box<list>), nil, } |
| 46 | + let tree_with_children = Tree::Children(Box::new(List::Nil)); |
| 47 | + assert_eq!(tree_with_children, Tree::Children(Box::new(List::Nil))); |
12 | 48 |
|
13 | | -enum small_list { kons(isize, Box<small_list>), neel, } |
| 49 | + let list_with_tree = List::Cons(Box::new(Tree::Leaf(Colour::Blue)), Box::new(List::Nil)); |
| 50 | + assert_eq!(list_with_tree, List::Cons(Box::new(Tree::Leaf(Colour::Blue)), Box::new(List::Nil))); |
14 | 51 |
|
15 | | -pub fn main() { } |
| 52 | + let small_list = SmallList::Kons(42, Box::new(SmallList::Neel)); |
| 53 | + assert_eq!(small_list, SmallList::Kons(42, Box::new(SmallList::Neel))); |
| 54 | +} |
0 commit comments