@@ -128,30 +128,32 @@ impl<'s, N: Eq + Ord + Clone + 's, E: Default + Clone + 's> Graph<N, E> {
128
128
{
129
129
let mut back_link = BTreeMap :: new ( ) ;
130
130
let mut queue = VecDeque :: from ( [ pkg] ) ;
131
- let mut bottom = None ;
131
+ let mut last = pkg ;
132
132
133
133
while let Some ( p) = queue. pop_front ( ) {
134
- bottom = Some ( p) ;
134
+ last = p;
135
+ let mut out_edges = true ;
135
136
for ( child, edge) in fn_edge ( & self , p) {
136
- bottom = None ;
137
+ out_edges = false ;
137
138
back_link. entry ( child) . or_insert_with ( || {
138
139
queue. push_back ( child) ;
139
140
( p, edge)
140
141
} ) ;
141
142
}
142
- if bottom . is_some ( ) {
143
+ if out_edges {
143
144
break ;
144
145
}
145
146
}
146
147
147
148
let mut result = Vec :: new ( ) ;
148
- let mut next =
149
- bottom. expect ( "the only path was a cycle, no dependency graph has this shape" ) ;
149
+ let mut next = last;
150
150
while let Some ( ( p, e) ) = back_link. remove ( & next) {
151
151
result. push ( ( next, Some ( e) ) ) ;
152
152
next = p;
153
153
}
154
- result. push ( ( next, None ) ) ;
154
+ if result. iter ( ) . all ( |( n, _) | n != & next) {
155
+ result. push ( ( next, None ) ) ;
156
+ }
155
157
result. reverse ( ) ;
156
158
#[ cfg( debug_assertions) ]
157
159
{
@@ -197,7 +199,7 @@ fn path_to_self() {
197
199
// Extracted from #12941
198
200
let mut new: Graph < i32 , ( ) > = Graph :: new ( ) ;
199
201
new. link ( 0 , 0 ) ;
200
- assert_eq ! ( new. path_to_bottom( & 0 ) , vec![ ( & 0 , None ) ] ) ;
202
+ assert_eq ! ( new. path_to_bottom( & 0 ) , vec![ ( & 0 , Some ( & ( ) ) ) ] ) ;
201
203
}
202
204
203
205
impl < N : Eq + Ord + Clone , E : Default + Clone > Default for Graph < N , E > {
0 commit comments