From 0c84bcbe055274757fb08b1d8e5e39fa6309136d Mon Sep 17 00:00:00 2001 From: Alexander Lyon Date: Tue, 21 May 2024 15:43:08 +0200 Subject: [PATCH] fix rustc bug in turbopack-trace-server (#8158) ### 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`. Solution is to use a Boxed iterator ### Testing Instructions Existing tests OK --- crates/turbopack-trace-server/src/bottom_up.rs | 18 +++++++++++------- crates/turbopack-trace-server/src/span_ref.rs | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/crates/turbopack-trace-server/src/bottom_up.rs b/crates/turbopack-trace-server/src/bottom_up.rs index b43f70bf44e08..39c025eb8d4de 100644 --- a/crates/turbopack-trace-server/src/bottom_up.rs +++ b/crates/turbopack-trace-server/src/bottom_up.rs @@ -1,7 +1,5 @@ use std::{collections::HashMap, env, sync::Arc}; -use either::Either; - use crate::{ span::{SpanBottomUp, SpanIndex}, span_ref::SpanRef, @@ -36,16 +34,22 @@ impl SpanBottomUpBuilder { } pub fn build_bottom_up_graph<'a>( - spans: impl IntoIterator>, + spans: impl Iterator>, ) -> Vec> { 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. 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>>> = + 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() { @@ -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(); } diff --git a/crates/turbopack-trace-server/src/span_ref.rs b/crates/turbopack-trace-server/src/span_ref.rs index 3efa417b4b73b..7439ef9d128ff 100644 --- a/crates/turbopack-trace-server/src/span_ref.rs +++ b/crates/turbopack-trace-server/src/span_ref.rs @@ -332,9 +332,9 @@ impl<'a> SpanRef<'a> { pub fn bottom_up(self) -> impl Iterator> { 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, })