Skip to content

Commit c2b754e

Browse files
authored
fix: finalize_imports node ordering (#329)
1 parent 7508cda commit c2b754e

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

crates/compilers/src/compilers/multi.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ pub enum MultiCompilerParsedSource {
145145
Vyper(VyperParsedSource),
146146
}
147147

148+
impl From<SolData> for MultiCompilerParsedSource {
149+
fn from(data: SolData) -> Self {
150+
Self::Solc(data)
151+
}
152+
}
153+
154+
impl From<VyperParsedSource> for MultiCompilerParsedSource {
155+
fn from(data: VyperParsedSource) -> Self {
156+
Self::Vyper(data)
157+
}
158+
}
159+
148160
impl MultiCompilerParsedSource {
149161
fn solc(&self) -> Option<&SolData> {
150162
match self {
@@ -418,34 +430,46 @@ impl SourceParser for MultiCompilerParser {
418430
all_nodes: &mut Vec<crate::resolver::Node<Self::ParsedSource>>,
419431
include_paths: &BTreeSet<PathBuf>,
420432
) -> Result<()> {
433+
// Must maintain original order.
421434
let mut solc_nodes = Vec::new();
422435
let mut vyper_nodes = Vec::new();
436+
let mut order = Vec::new();
423437
for node in std::mem::take(all_nodes) {
438+
order.push(node.data.language());
424439
match node.data {
425440
MultiCompilerParsedSource::Solc(_) => {
426441
solc_nodes.push(node.map_data(|data| match data {
427442
MultiCompilerParsedSource::Solc(data) => data,
428443
_ => unreachable!(),
429-
}))
444+
}));
430445
}
431446
MultiCompilerParsedSource::Vyper(_) => {
432447
vyper_nodes.push(node.map_data(|data| match data {
433448
MultiCompilerParsedSource::Vyper(data) => data,
434449
_ => unreachable!(),
435-
}))
450+
}));
436451
}
437452
}
438453
}
439454

440455
self.solc.finalize_imports(&mut solc_nodes, include_paths)?;
441456
self.vyper.finalize_imports(&mut vyper_nodes, include_paths)?;
442457

443-
all_nodes.extend(
444-
solc_nodes.into_iter().map(|node| node.map_data(MultiCompilerParsedSource::Solc)),
445-
);
446-
all_nodes.extend(
447-
vyper_nodes.into_iter().map(|node| node.map_data(MultiCompilerParsedSource::Vyper)),
448-
);
458+
// Assume that the order was not changed by the parsers.
459+
let mut solc_nodes = solc_nodes.into_iter();
460+
let mut vyper_nodes = vyper_nodes.into_iter();
461+
for lang in order {
462+
match lang {
463+
MultiCompilerLanguage::Solc(_) => {
464+
all_nodes.push(solc_nodes.next().unwrap().map_data(Into::into));
465+
}
466+
MultiCompilerLanguage::Vyper(_) => {
467+
all_nodes.push(vyper_nodes.next().unwrap().map_data(Into::into));
468+
}
469+
}
470+
}
471+
assert!(solc_nodes.next().is_none());
472+
assert!(vyper_nodes.next().is_none());
449473

450474
Ok(())
451475
}

0 commit comments

Comments
 (0)