Skip to content

Commit 2a657f0

Browse files
committed
fix: assign errors once
1 parent 891ebba commit 2a657f0

File tree

4 files changed

+69
-29
lines changed

4 files changed

+69
-29
lines changed

crates/compilers/src/compilers/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ pub trait SourceParser: Clone + Debug + Send + Sync {
167167
.collect::<Result<_>>()
168168
}
169169

170-
fn finalize_imports(&mut self, _include_paths: &BTreeSet<PathBuf>) -> Result<()> {
170+
fn finalize_imports(
171+
&mut self,
172+
_nodes: &mut Vec<Node<Self::ParsedSource>>,
173+
_include_paths: &BTreeSet<PathBuf>,
174+
) -> Result<()> {
171175
Ok(())
172176
}
173177
}

crates/compilers/src/compilers/multi.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,40 @@ impl SourceParser for MultiCompilerParser {
413413
.collect())
414414
}
415415

416-
fn finalize_imports(&mut self, include_paths: &BTreeSet<PathBuf>) -> Result<()> {
417-
self.solc.finalize_imports(include_paths)?;
418-
self.vyper.finalize_imports(include_paths)?;
416+
fn finalize_imports(
417+
&mut self,
418+
all_nodes: &mut Vec<crate::resolver::Node<Self::ParsedSource>>,
419+
include_paths: &BTreeSet<PathBuf>,
420+
) -> Result<()> {
421+
let mut solc_nodes = Vec::new();
422+
let mut vyper_nodes = Vec::new();
423+
for node in std::mem::take(all_nodes) {
424+
match node.data {
425+
MultiCompilerParsedSource::Solc(_) => {
426+
solc_nodes.push(node.map_data(|data| match data {
427+
MultiCompilerParsedSource::Solc(data) => data,
428+
_ => unreachable!(),
429+
}))
430+
}
431+
MultiCompilerParsedSource::Vyper(_) => {
432+
vyper_nodes.push(node.map_data(|data| match data {
433+
MultiCompilerParsedSource::Vyper(data) => data,
434+
_ => unreachable!(),
435+
}))
436+
}
437+
}
438+
}
439+
440+
self.solc.finalize_imports(&mut solc_nodes, include_paths)?;
441+
self.vyper.finalize_imports(&mut vyper_nodes, include_paths)?;
442+
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+
);
449+
419450
Ok(())
420451
}
421452
}

crates/compilers/src/compilers/solc/mod.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ impl SourceParser for SolParser {
399399
&mut self,
400400
sources: &mut Sources,
401401
) -> Result<Vec<(PathBuf, Node<Self::ParsedSource>)>> {
402-
self.compiler_mut().enter_mut(|compiler| {
402+
self.compiler.enter_mut(|compiler| {
403403
let mut pcx = compiler.parse();
404404
pcx.set_resolve_imports(false);
405405
let files = sources
@@ -424,38 +424,43 @@ impl SourceParser for SolParser {
424424
);
425425
(path.clone(), node)
426426
});
427-
let mut parsed = parsed.collect::<Vec<_>>();
428-
429-
// Set error on the first successful source, if any. This doesn't really have to be
430-
// exact, as long as at least one source has an error set it should be enough.
431-
if let Some(Err(diag)) = compiler.gcx().sess.emitted_errors() {
432-
if let Some(idx) = parsed
433-
.iter()
434-
.position(|(_, node)| node.data.parse_result.is_ok())
435-
.or_else(|| parsed.first().map(|_| 0))
436-
{
437-
let (_, node) = &mut parsed[idx];
438-
node.data.parse_result = Err(diag.to_string());
439-
}
440-
}
441-
442-
for (path, node) in &parsed {
443-
if let Err(e) = &node.data.parse_result {
444-
debug!("failed parsing {}: {e}", path.display());
445-
}
446-
}
427+
let parsed = parsed.collect::<Vec<_>>();
447428

448429
Ok(parsed)
449430
})
450431
}
451432

452-
fn finalize_imports(&mut self, include_paths: &BTreeSet<PathBuf>) -> Result<()> {
453-
self.compiler_mut().sess_mut().opts.include_paths.extend(include_paths.iter().cloned());
454-
self.compiler_mut().enter_mut(|compiler| {
433+
fn finalize_imports(
434+
&mut self,
435+
nodes: &mut Vec<Node<Self::ParsedSource>>,
436+
include_paths: &BTreeSet<PathBuf>,
437+
) -> Result<()> {
438+
let compiler = &mut self.compiler;
439+
compiler.sess_mut().opts.include_paths.extend(include_paths.iter().cloned());
440+
compiler.enter_mut(|compiler| {
455441
let mut pcx = compiler.parse();
456442
pcx.set_resolve_imports(true);
457443
pcx.force_resolve_all_imports();
458444
});
445+
446+
// Set error on the first successful source, if any. This doesn't really have to be
447+
// exact, as long as at least one source has an error set it should be enough.
448+
if let Some(Err(diag)) = compiler.sess().emitted_errors() {
449+
if let Some(idx) = nodes
450+
.iter()
451+
.position(|node| node.data.parse_result.is_ok())
452+
.or_else(|| nodes.first().map(|_| 0))
453+
{
454+
nodes[idx].data.parse_result = Err(diag.to_string());
455+
}
456+
}
457+
458+
for node in nodes.iter() {
459+
if let Err(e) = &node.data.parse_result {
460+
debug!("failed parsing:\n{e}");
461+
}
462+
}
463+
459464
Ok(())
460465
}
461466
}

crates/compilers/src/resolver/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ impl<P: SourceParser> Graph<P> {
484484
);
485485
}
486486

487-
parser.finalize_imports(&resolved_solc_include_paths)?;
487+
parser.finalize_imports(&mut nodes, &resolved_solc_include_paths)?;
488488

489489
let edges = GraphEdges {
490490
edges,

0 commit comments

Comments
 (0)