@@ -3,6 +3,15 @@ use std::{fmt::Debug, hash::Hash};
33use ena:: unify:: { InPlaceUnificationTable , UnifyKey } ;
44use rustc_hash:: FxHashMap ;
55
6+ /// Represents a definition that contains a direct reference to itself.
7+ ///
8+ /// Recursive definitions are not valid and must be reported to the user.
9+ /// It is preferable to group definitions together such that recursions
10+ /// are reported in-whole rather than separately. `RecursiveDef` can be
11+ /// used with `RecursiveDefHelper` to perform this grouping operation.
12+ ///
13+ /// The fields `from` and `to` are the relevant identifiers and `site` can
14+ /// be used to carry diagnostic information.
615#[ derive( Eq , PartialEq , Clone , Debug , Hash ) ]
716pub struct RecursiveDef < T , U >
817where
5867 let mut table = InPlaceUnificationTable :: new ( ) ;
5968 let keys: FxHashMap < _ , _ > = defs
6069 . iter ( )
61- . map ( |def : & RecursiveDef < T , U > | ( def. from , table. new_key ( ( ) ) ) )
70+ . map ( |def| ( def. from , table. new_key ( ( ) ) ) )
6271 . collect ( ) ;
6372
6473 for def in defs. iter ( ) {
6877 Self { defs, table, keys }
6978 }
7079
80+ /// Removes a disjoint set of recursive definitions from the helper
81+ /// and returns it, if one exists.
7182 pub fn remove_disjoint_set ( & mut self ) -> Option < Vec < RecursiveDef < T , U > > > {
7283 let mut disjoint_set = vec ! [ ] ;
7384 let mut remaining_set = vec ! [ ] ;
@@ -99,10 +110,7 @@ where
99110
100111#[ test]
101112fn one_recursion ( ) {
102- let defs = vec ! [
103- RecursiveDef :: new( 0 , 1 , ( ( ) , ( ) ) ) ,
104- RecursiveDef :: new( 1 , 0 , ( ( ) , ( ) ) ) ,
105- ] ;
113+ let defs = vec ! [ RecursiveDef :: new( 0 , 1 , ( ) ) , RecursiveDef :: new( 1 , 0 , ( ) ) ] ;
106114
107115 let mut helper = RecursiveDefHelper :: new ( defs) ;
108116 let disjoint_constituents = helper. remove_disjoint_set ( ) ;
@@ -114,11 +122,11 @@ fn one_recursion() {
114122#[ test]
115123fn two_recursions ( ) {
116124 let defs = vec ! [
117- RecursiveDef :: new( 0 , 1 , ( ( ) , ( ) ) ) ,
118- RecursiveDef :: new( 1 , 0 , ( ( ) , ( ) ) ) ,
119- RecursiveDef :: new( 2 , 3 , ( ( ) , ( ) ) ) ,
120- RecursiveDef :: new( 3 , 4 , ( ( ) , ( ) ) ) ,
121- RecursiveDef :: new( 4 , 2 , ( ( ) , ( ) ) ) ,
125+ RecursiveDef :: new( 0 , 1 , ( ) ) ,
126+ RecursiveDef :: new( 1 , 0 , ( ) ) ,
127+ RecursiveDef :: new( 2 , 3 , ( ) ) ,
128+ RecursiveDef :: new( 3 , 4 , ( ) ) ,
129+ RecursiveDef :: new( 4 , 2 , ( ) ) ,
122130 ] ;
123131
124132 let mut helper = RecursiveDefHelper :: new ( defs) ;
0 commit comments