Skip to content

Commit 91444af

Browse files
committed
Refactor try_mark_previous_green.
1 parent c2c59ae commit 91444af

File tree

1 file changed

+112
-117
lines changed
  • compiler/rustc_query_system/src/dep_graph

1 file changed

+112
-117
lines changed

compiler/rustc_query_system/src/dep_graph/graph.rs

+112-117
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,117 @@ impl<K: DepKind> DepGraph<K> {
488488
}
489489
}
490490

491+
fn try_mark_parent_green<Ctxt: QueryContext<DepKind = K>>(
492+
&self,
493+
tcx: Ctxt,
494+
data: &DepGraphData<K>,
495+
parent_dep_node_index: SerializedDepNodeIndex,
496+
dep_node: &DepNode<K>,
497+
) -> Option<()> {
498+
let dep_dep_node_color = data.colors.get(parent_dep_node_index);
499+
let dep_dep_node = &data.previous.index_to_node(parent_dep_node_index);
500+
501+
match dep_dep_node_color {
502+
Some(DepNodeColor::Green(_)) => {
503+
// This dependency has been marked as green before, we are
504+
// still fine and can continue with checking the other
505+
// dependencies.
506+
debug!(
507+
"try_mark_previous_green({:?}) --- found dependency {:?} to \
508+
be immediately green",
509+
dep_node, dep_dep_node,
510+
);
511+
return Some(());
512+
}
513+
Some(DepNodeColor::Red) => {
514+
// We found a dependency the value of which has changed
515+
// compared to the previous compilation session. We cannot
516+
// mark the DepNode as green and also don't need to bother
517+
// with checking any of the other dependencies.
518+
debug!(
519+
"try_mark_previous_green({:?}) - END - dependency {:?} was immediately red",
520+
dep_node, dep_dep_node,
521+
);
522+
return None;
523+
}
524+
None => {}
525+
}
526+
527+
// We don't know the state of this dependency. If it isn't
528+
// an eval_always node, let's try to mark it green recursively.
529+
if !dep_dep_node.kind.is_eval_always() {
530+
debug!(
531+
"try_mark_previous_green({:?}) --- state of dependency {:?} ({}) \
532+
is unknown, trying to mark it green",
533+
dep_node, dep_dep_node, dep_dep_node.hash,
534+
);
535+
536+
let node_index =
537+
self.try_mark_previous_green(tcx, data, parent_dep_node_index, dep_dep_node);
538+
if node_index.is_some() {
539+
debug!(
540+
"try_mark_previous_green({:?}) --- managed to MARK dependency {:?} as green",
541+
dep_node, dep_dep_node
542+
);
543+
return Some(());
544+
}
545+
}
546+
547+
// We failed to mark it green, so we try to force the query.
548+
debug!(
549+
"try_mark_previous_green({:?}) --- trying to force dependency {:?}",
550+
dep_node, dep_dep_node
551+
);
552+
if !tcx.try_force_from_dep_node(dep_dep_node) {
553+
// The DepNode could not be forced.
554+
debug!(
555+
"try_mark_previous_green({:?}) - END - dependency {:?} could not be forced",
556+
dep_node, dep_dep_node
557+
);
558+
return None;
559+
}
560+
561+
let dep_dep_node_color = data.colors.get(parent_dep_node_index);
562+
563+
match dep_dep_node_color {
564+
Some(DepNodeColor::Green(_)) => {
565+
debug!(
566+
"try_mark_previous_green({:?}) --- managed to FORCE dependency {:?} to green",
567+
dep_node, dep_dep_node
568+
);
569+
return Some(());
570+
}
571+
Some(DepNodeColor::Red) => {
572+
debug!(
573+
"try_mark_previous_green({:?}) - END - dependency {:?} was red after forcing",
574+
dep_node, dep_dep_node
575+
);
576+
return None;
577+
}
578+
None => {}
579+
}
580+
581+
if !tcx.dep_context().sess().has_errors_or_delayed_span_bugs() {
582+
panic!("try_mark_previous_green() - Forcing the DepNode should have set its color")
583+
}
584+
585+
// If the query we just forced has resulted in
586+
// some kind of compilation error, we cannot rely on
587+
// the dep-node color having been properly updated.
588+
// This means that the query system has reached an
589+
// invalid state. We let the compiler continue (by
590+
// returning `None`) so it can emit error messages
591+
// and wind down, but rely on the fact that this
592+
// invalid state will not be persisted to the
593+
// incremental compilation cache because of
594+
// compilation errors being present.
595+
debug!(
596+
"try_mark_previous_green({:?}) - END - dependency {:?} resulted in compilation error",
597+
dep_node, dep_dep_node
598+
);
599+
return None;
600+
}
601+
491602
/// Try to mark a dep-node which existed in the previous compilation session as green.
492603
fn try_mark_previous_green<Ctxt: QueryContext<DepKind = K>>(
493604
&self,
@@ -512,123 +623,7 @@ impl<K: DepKind> DepGraph<K> {
512623
let prev_deps = data.previous.edge_targets_from(prev_dep_node_index);
513624

514625
for &dep_dep_node_index in prev_deps {
515-
let dep_dep_node_color = data.colors.get(dep_dep_node_index);
516-
517-
match dep_dep_node_color {
518-
Some(DepNodeColor::Green(_)) => {
519-
// This dependency has been marked as green before, we are
520-
// still fine and can continue with checking the other
521-
// dependencies.
522-
debug!(
523-
"try_mark_previous_green({:?}) --- found dependency {:?} to \
524-
be immediately green",
525-
dep_node,
526-
data.previous.index_to_node(dep_dep_node_index)
527-
);
528-
}
529-
Some(DepNodeColor::Red) => {
530-
// We found a dependency the value of which has changed
531-
// compared to the previous compilation session. We cannot
532-
// mark the DepNode as green and also don't need to bother
533-
// with checking any of the other dependencies.
534-
debug!(
535-
"try_mark_previous_green({:?}) - END - dependency {:?} was \
536-
immediately red",
537-
dep_node,
538-
data.previous.index_to_node(dep_dep_node_index)
539-
);
540-
return None;
541-
}
542-
None => {
543-
let dep_dep_node = &data.previous.index_to_node(dep_dep_node_index);
544-
545-
// We don't know the state of this dependency. If it isn't
546-
// an eval_always node, let's try to mark it green recursively.
547-
if !dep_dep_node.kind.is_eval_always() {
548-
debug!(
549-
"try_mark_previous_green({:?}) --- state of dependency {:?} ({}) \
550-
is unknown, trying to mark it green",
551-
dep_node, dep_dep_node, dep_dep_node.hash,
552-
);
553-
554-
let node_index = self.try_mark_previous_green(
555-
tcx,
556-
data,
557-
dep_dep_node_index,
558-
dep_dep_node,
559-
);
560-
if node_index.is_some() {
561-
debug!(
562-
"try_mark_previous_green({:?}) --- managed to MARK \
563-
dependency {:?} as green",
564-
dep_node, dep_dep_node
565-
);
566-
continue;
567-
}
568-
}
569-
570-
// We failed to mark it green, so we try to force the query.
571-
debug!(
572-
"try_mark_previous_green({:?}) --- trying to force \
573-
dependency {:?}",
574-
dep_node, dep_dep_node
575-
);
576-
if tcx.try_force_from_dep_node(dep_dep_node) {
577-
let dep_dep_node_color = data.colors.get(dep_dep_node_index);
578-
579-
match dep_dep_node_color {
580-
Some(DepNodeColor::Green(_)) => {
581-
debug!(
582-
"try_mark_previous_green({:?}) --- managed to \
583-
FORCE dependency {:?} to green",
584-
dep_node, dep_dep_node
585-
);
586-
}
587-
Some(DepNodeColor::Red) => {
588-
debug!(
589-
"try_mark_previous_green({:?}) - END - \
590-
dependency {:?} was red after forcing",
591-
dep_node, dep_dep_node
592-
);
593-
return None;
594-
}
595-
None => {
596-
if !tcx.dep_context().sess().has_errors_or_delayed_span_bugs() {
597-
panic!(
598-
"try_mark_previous_green() - Forcing the DepNode \
599-
should have set its color"
600-
)
601-
} else {
602-
// If the query we just forced has resulted in
603-
// some kind of compilation error, we cannot rely on
604-
// the dep-node color having been properly updated.
605-
// This means that the query system has reached an
606-
// invalid state. We let the compiler continue (by
607-
// returning `None`) so it can emit error messages
608-
// and wind down, but rely on the fact that this
609-
// invalid state will not be persisted to the
610-
// incremental compilation cache because of
611-
// compilation errors being present.
612-
debug!(
613-
"try_mark_previous_green({:?}) - END - \
614-
dependency {:?} resulted in compilation error",
615-
dep_node, dep_dep_node
616-
);
617-
return None;
618-
}
619-
}
620-
}
621-
} else {
622-
// The DepNode could not be forced.
623-
debug!(
624-
"try_mark_previous_green({:?}) - END - dependency {:?} \
625-
could not be forced",
626-
dep_node, dep_dep_node
627-
);
628-
return None;
629-
}
630-
}
631-
}
626+
self.try_mark_parent_green(tcx, data, dep_dep_node_index, dep_node)?
632627
}
633628

634629
// If we got here without hitting a `return` that means that all

0 commit comments

Comments
 (0)