Skip to content

Commit 2360154

Browse files
add loads of comments
1 parent d37fd99 commit 2360154

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

compiler-core/src/error.rs

+46-19
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ fn derivation_tree_to_pretty_error_message(
412412
mut derivation_tree: DerivationTree<String, hexpm::version::Version>,
413413
) -> String {
414414
derivation_tree.collapse_no_versions();
415-
let derivation_tree = simplify_error(derivation_tree);
415+
let derivation_tree = simplify_derivation_tree(derivation_tree);
416416

417417
pprint_error(0, &derivation_tree);
418418
// TODO))
@@ -570,7 +570,26 @@ fn collect_conflicting_packages<'dt, P: Package, V: Version>(
570570
}
571571
}
572572

573-
fn simplify_error<'dt, P: Package, V: Version>(
573+
/// This function collapses adjacent levels of a derivation tree that are all
574+
/// relative to the same dependency.
575+
///
576+
/// By default a derivation tree might have many nodes for a specific package,
577+
/// each node referring to a specific version range. For example:
578+
///
579+
/// - package_wibble `>= 1.0.0 and < 1.1.0` requires package_wobble `>= 1.1.0`
580+
/// - package_wibble `>= 1.1.0 and < 1.2.0` requires package_wobble `>= 1.2.0`
581+
/// - package_wibble `1.1.0` requires package_wobble `>= 1.1.0`
582+
///
583+
/// This level of fine-grained detail would be quite overwhelming in the vast
584+
/// majority of cases so we're fine with collapsing all these details into a
585+
/// single node taking the union of all the ranges that are there:
586+
///
587+
/// - package_wibble `>= 1.0.0 and < 1.2.0` requires package_wobble `>= 1.1.0`
588+
///
589+
/// This way we can print an error message that is way more concise and still
590+
/// informative about what went wrong.
591+
///
592+
fn simplify_derivation_tree<'dt, P: Package, V: Version>(
574593
derivation_tree: DerivationTree<P, V>,
575594
) -> DerivationTree<P, V> {
576595
match derivation_tree {
@@ -580,44 +599,52 @@ fn simplify_error<'dt, P: Package, V: Version>(
580599
cause2,
581600
terms,
582601
shared_id,
583-
}) => merge_trees_step(
584-
simplify_error(*cause1),
585-
simplify_error(*cause2),
602+
}) => simplify_outer_derivation_tree(
603+
simplify_derivation_tree(*cause1),
604+
simplify_derivation_tree(*cause2),
586605
terms,
587606
shared_id,
588607
),
589608
}
590609
}
591610

592-
fn merge_trees_step<'dt, P: Package, V: Version>(
611+
fn simplify_outer_derivation_tree<'dt, P: Package, V: Version>(
593612
one: DerivationTree<P, V>,
594613
other: DerivationTree<P, V>,
595614
terms: pubgrub::type_aliases::Map<P, pubgrub::term::Term<V>>,
596615
shared_id: Option<usize>,
597616
) -> DerivationTree<P, V> {
598617
match (one, other) {
599618
(
619+
// The way an External::FromDependencyOf is read is: `package` in
620+
// range `package_range` requires `required_package` to be in range
621+
// `required_package_range`...
600622
DerivationTree::External(External::FromDependencyOf(
601-
cause1_p1,
602-
cause1_r1,
603-
cause1_p2,
604-
cause1_r2,
623+
package,
624+
package_range,
625+
required_package,
626+
required_package_range,
605627
)),
606628
DerivationTree::External(External::FromDependencyOf(
607-
cause2_p1,
608-
cause2_r1,
609-
cause2_p2,
610-
cause2_r2,
629+
maybe_package,
630+
other_package_range,
631+
maybe_required_package,
632+
other_required_package_range,
611633
)),
612-
) if cause1_p1 == cause2_p1 && cause1_p2 == cause2_p2 => {
634+
)
635+
// ...So we can simplify two adjacent dependency failures if they are
636+
// talking about the same pair of packages by taking the union of the
637+
// respsective ranges.
638+
if package == maybe_package && required_package == maybe_required_package => {
613639
DerivationTree::External(External::FromDependencyOf(
614-
cause1_p1,
615-
cause1_r1.union(&cause2_r1),
616-
cause1_p2,
617-
cause1_r2.union(&cause2_r2),
640+
package,
641+
package_range.union(&other_package_range),
642+
required_package,
643+
required_package_range.union(&other_required_package_range),
618644
))
619645
}
620646

647+
// Otherwise there's no way to simplify it!
621648
(cause1, cause2) => DerivationTree::Derived(Derived {
622649
cause1: Box::new(cause1),
623650
cause2: Box::new(cause2),

0 commit comments

Comments
 (0)