Skip to content

Commit

Permalink
fix rustc bug in turbopack-trace-server (#8158)
Browse files Browse the repository at this point in the history
### Description

unfortunately there is a rustc bug that crashes when the typechecking in
run in this crate during compiles for next-swc when using `Either<impl
Iterator, impl Iterator>`. Solution is to use a Boxed iterator

### Testing Instructions

Existing tests OK
  • Loading branch information
arlyon committed May 21, 2024
1 parent d52a6d2 commit 0c84bcb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
18 changes: 11 additions & 7 deletions crates/turbopack-trace-server/src/bottom_up.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::{collections::HashMap, env, sync::Arc};

use either::Either;

use crate::{
span::{SpanBottomUp, SpanIndex},
span_ref::SpanRef,
Expand Down Expand Up @@ -36,16 +34,22 @@ impl SpanBottomUpBuilder {
}

pub fn build_bottom_up_graph<'a>(
spans: impl IntoIterator<Item = SpanRef<'a>>,
spans: impl Iterator<Item = SpanRef<'a>>,
) -> Vec<Arc<SpanBottomUp>> {
let max_depth = env::var("BOTTOM_UP_DEPTH")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(usize::MAX);
let mut roots = HashMap::new();
let mut current_iterators = vec![Either::Left(
spans.into_iter().flat_map(|span| span.children()),
)];

// unfortunately there is a rustc bug that fails the typechecking here
// when using Either<impl Iterator, impl Iterator>. This error appears
// in certain cases when building next-swc.
//
// see here: https://github.com/rust-lang/rust/issues/124891
let mut current_iterators: Vec<Box<dyn Iterator<Item = SpanRef<'_>>>> =
vec![Box::new(spans.flat_map(|span| span.children()))];

let mut current_path: Vec<(&'_ str, SpanIndex)> = vec![];
while let Some(mut iter) = current_iterators.pop() {
if let Some(child) = iter.next() {
Expand Down Expand Up @@ -73,7 +77,7 @@ pub fn build_bottom_up_graph<'a>(
}

current_path.push((child.group_name(), child.index()));
current_iterators.push(Either::Right(child.children()));
current_iterators.push(Box::new(child.children()));
} else {
current_path.pop();
}
Expand Down
4 changes: 2 additions & 2 deletions crates/turbopack-trace-server/src/span_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ impl<'a> SpanRef<'a> {
pub fn bottom_up(self) -> impl Iterator<Item = SpanBottomUpRef<'a>> {
self.extra()
.bottom_up
.get_or_init(|| build_bottom_up_graph([self]))
.get_or_init(|| build_bottom_up_graph([self].into_iter()))
.iter()
.map(|bottom_up| SpanBottomUpRef {
.map(move |bottom_up| SpanBottomUpRef {
bottom_up: bottom_up.clone(),
store: self.store,
})
Expand Down

0 comments on commit 0c84bcb

Please sign in to comment.