Skip to content

Commit

Permalink
Fix TyModule::submodules_recursive iterator (FuelLabs#3167)
Browse files Browse the repository at this point in the history
This fixes the submodule iterator added in FuelLabs#3152.

Previously this wasn't actually returning any submodules, only
traversing them! This resulted in `test_fns` method not working. This
commit fixes the behaviour, tested locally as a part of FuelLabs#2985.
  • Loading branch information
mitchmindtree authored Oct 27, 2022
1 parent 467e22c commit 4e3e44f
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions sway-core/src/language/ty/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ pub struct TySubmodule {
/// Used rather than `impl Iterator` to enable recursive submodule iteration.
pub struct SubmodulesRecursive<'module> {
submods: std::slice::Iter<'module, (DepName, TySubmodule)>,
current: Option<Box<SubmodulesRecursive<'module>>>,
current: Option<(
&'module (DepName, TySubmodule),
Box<SubmodulesRecursive<'module>>,
)>,
}

impl TyModule {
Expand Down Expand Up @@ -56,17 +59,19 @@ impl<'module> Iterator for SubmodulesRecursive<'module> {
type Item = &'module (DepName, TySubmodule);
fn next(&mut self) -> Option<Self::Item> {
loop {
self.current = match self.current.as_mut() {
self.current = match self.current.take() {
None => match self.submods.next() {
None => return None,
Some((_, submod)) => Some(Box::new(submod.module.submodules_recursive())),
Some(submod) => {
Some((submod, Box::new(submod.1.module.submodules_recursive())))
}
},
Some(submod) => match submod.next() {
Some(submod) => return Some(submod),
None => {
self.current = None;
continue;
Some((submod, mut submods)) => match submods.next() {
Some(next) => {
self.current = Some((submod, submods));
return Some(next);
}
None => return Some(submod),
},
}
}
Expand Down

0 comments on commit 4e3e44f

Please sign in to comment.