-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace the obligation forest with a graph #33491
Changes from 1 commit
957500b
5c39a2a
f0f5ef5
5458d8b
65ad935
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Fixes #33344
- 1.85.1
- 1.85.0
- 1.84.1
- 1.84.0
- 1.83.0
- 1.82.0
- 1.81.0
- 1.80.1
- 1.80.0
- 1.79.0
- 1.78.0
- 1.77.2
- 1.77.1
- 1.77.0
- 1.76.0
- 1.75.0
- 1.74.1
- 1.74.0
- 1.73.0
- 1.72.1
- 1.72.0
- 1.71.1
- 1.71.0
- 1.70.0
- 1.69.0
- 1.68.2
- 1.68.1
- 1.68.0
- 1.67.1
- 1.67.0
- 1.66.1
- 1.66.0
- 1.65.0
- 1.64.0
- 1.63.0
- 1.62.1
- 1.62.0
- 1.61.0
- 1.60.0
- 1.59.0
- 1.58.1
- 1.58.0
- 1.57.0
- 1.56.1
- 1.56.0
- 1.55.0
- 1.54.0
- 1.53.0
- 1.52.1
- 1.52.0
- 1.51.0
- 1.50.0
- 1.49.0
- 1.48.0
- 1.47.0
- 1.46.0
- 1.45.2
- 1.45.1
- 1.45.0
- 1.44.1
- 1.44.0
- 1.43.1
- 1.43.0
- 1.42.0
- 1.41.1
- 1.41.0
- 1.40.0
- 1.39.0
- 1.38.0
- 1.37.0
- 1.36.0
- 1.35.0
- 1.34.2
- 1.34.1
- 1.34.0
- 1.33.0
- 1.32.0
- 1.31.1
- 1.31.0
- 1.30.1
- 1.30.0
- 1.29.2
- 1.29.1
- 1.29.0
- 1.28.0
- 1.27.2
- 1.27.1
- 1.27.0
- 1.26.2
- 1.26.1
- 1.26.0
- 1.25.0
- 1.24.1
- 1.24.0
- 1.23.0
- 1.22.1
- 1.22.0
- 1.21.0
- 1.20.0
- 1.19.0
- 1.18.0
- 1.17.0
- 1.16.0
- 1.15.1
- 1.15.0
- 1.14.0
- 1.13.0
- 1.12.1
- 1.12.0
- 1.11.0
- 1.10.0
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
use fnv::{FnvHashMap, FnvHashSet}; | ||
|
||
use std::cell::Cell; | ||
use std::collections::hash_map::Entry; | ||
use std::fmt::Debug; | ||
use std::hash; | ||
|
@@ -41,7 +42,9 @@ pub trait ObligationProcessor { | |
obligation: &mut Self::Obligation) | ||
-> Result<Option<Vec<Self::Obligation>>, Self::Error>; | ||
|
||
fn process_backedge(&mut self, cycle: &[Self::Obligation]); | ||
// FIXME: crazy lifetime troubles | ||
fn process_backedge<I>(&mut self, cycle: I) | ||
where I: Clone + Iterator<Item=*const Self::Obligation>; | ||
} | ||
|
||
struct SnapshotData { | ||
|
@@ -77,7 +80,7 @@ pub struct Snapshot { | |
#[derive(Debug)] | ||
struct Node<O> { | ||
obligation: O, | ||
state: NodeState, | ||
state: Cell<NodeState>, | ||
|
||
// these both go *in the same direction*. | ||
parent: Option<NodeIndex>, | ||
|
@@ -87,14 +90,17 @@ struct Node<O> { | |
/// The state of one node in some tree within the forest. This | ||
/// represents the current state of processing for the obligation (of | ||
/// type `O`) associated with this node. | ||
#[derive(Debug, PartialEq, Eq)] | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq)] | ||
enum NodeState { | ||
/// Obligation not yet resolved to success or error. | ||
Pending, | ||
|
||
/// Used before garbage collection | ||
Success, | ||
|
||
/// Used in DFS loops | ||
InLoop, | ||
|
||
/// Obligation resolved to success; `num_incomplete_children` | ||
/// indicates the number of children still in an "incomplete" | ||
/// state. Incomplete means that either the child is still | ||
|
@@ -225,7 +231,7 @@ impl<O: Debug + ForestObligation> ObligationForest<O> { | |
let mut errors = vec![]; | ||
for index in 0..self.nodes.len() { | ||
debug_assert!(!self.nodes[index].is_popped()); | ||
if let NodeState::Pending = self.nodes[index].state { | ||
if let NodeState::Pending = self.nodes[index].state.get() { | ||
let backtrace = self.error_at(index); | ||
errors.push(Error { | ||
error: error.clone(), | ||
|
@@ -244,7 +250,7 @@ impl<O: Debug + ForestObligation> ObligationForest<O> { | |
{ | ||
self.nodes | ||
.iter() | ||
.filter(|n| n.state == NodeState::Pending) | ||
.filter(|n| n.state.get() == NodeState::Pending) | ||
.map(|n| n.obligation.clone()) | ||
.collect() | ||
} | ||
|
@@ -270,7 +276,9 @@ impl<O: Debug + ForestObligation> ObligationForest<O> { | |
self.nodes[index]); | ||
|
||
let result = match self.nodes[index] { | ||
Node { state: NodeState::Pending, ref mut obligation, .. } => { | ||
Node { state: ref _state, ref mut obligation, .. } | ||
if _state.get() == NodeState::Pending => | ||
{ | ||
processor.process_obligation(obligation) | ||
} | ||
_ => continue | ||
|
@@ -292,7 +300,7 @@ impl<O: Debug + ForestObligation> ObligationForest<O> { | |
Some(NodeIndex::new(index))); | ||
} | ||
|
||
self.nodes[index].state = NodeState::Success; | ||
self.nodes[index].state.set(NodeState::Success); | ||
} | ||
Err(err) => { | ||
let backtrace = self.error_at(index); | ||
|
@@ -319,29 +327,69 @@ impl<O: Debug + ForestObligation> ObligationForest<O> { | |
} | ||
} | ||
|
||
pub fn process_cycles<P>(&mut self, _processor: &mut P) | ||
pub fn process_cycles<P>(&mut self, processor: &mut P) | ||
where P: ObligationProcessor<Obligation=O> | ||
{ | ||
// TODO: implement | ||
for node in &mut self.nodes { | ||
if node.state == NodeState::Success { | ||
node.state = NodeState::Done; | ||
} | ||
let mut stack = self.scratch.take().unwrap(); | ||
|
||
for node in 0..self.nodes.len() { | ||
self.visit_node(&mut stack, processor, node); | ||
} | ||
|
||
self.scratch = Some(stack); | ||
} | ||
|
||
fn visit_node<P>(&self, stack: &mut Vec<usize>, processor: &mut P, index: usize) | ||
where P: ObligationProcessor<Obligation=O> | ||
{ | ||
let node = &self.nodes[index]; | ||
let state = node.state.get(); | ||
match state { | ||
NodeState::InLoop => { | ||
let index = | ||
stack.iter().rposition(|n| *n == index).unwrap(); | ||
// I need a Clone closure | ||
#[derive(Clone)] | ||
struct GetObligation<'a, O: 'a>(&'a [Node<O>]); | ||
impl<'a, 'b, O> FnOnce<(&'b usize,)> for GetObligation<'a, O> { | ||
type Output = *const O; | ||
extern "rust-call" fn call_once(self, args: (&'b usize,)) -> *const O { | ||
&self.0[*args.0].obligation | ||
} | ||
} | ||
impl<'a, 'b, O> FnMut<(&'b usize,)> for GetObligation<'a, O> { | ||
extern "rust-call" fn call_mut(&mut self, args: (&'b usize,)) -> *const O { | ||
&self.0[*args.0].obligation | ||
} | ||
} | ||
|
||
processor.process_backedge(stack[index..].iter().map(GetObligation(&self.nodes))); | ||
} | ||
NodeState::Success => { | ||
node.state.set(NodeState::InLoop); | ||
stack.push(index); | ||
if let Some(parent) = node.parent { | ||
self.visit_node(stack, processor, parent.get()); | ||
} | ||
for dependant in &node.dependants { | ||
self.visit_node(stack, processor, dependant.get()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: indentation |
||
} | ||
stack.pop(); | ||
node.state.set(NodeState::Done); | ||
}, | ||
_ => return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you enumerate the other states here instead of using |
||
}; | ||
} | ||
|
||
/// Returns a vector of obligations for `p` and all of its | ||
/// ancestors, putting them into the error state in the process. | ||
/// The fact that the root is now marked as an error is used by | ||
/// `inherit_error` above to propagate the error state to the | ||
/// remainder of the tree. | ||
fn error_at(&mut self, p: usize) -> Vec<O> { | ||
let mut error_stack = self.scratch.take().unwrap(); | ||
let mut trace = vec![]; | ||
|
||
let mut n = p; | ||
loop { | ||
self.nodes[n].state = NodeState::Error; | ||
self.nodes[n].state.set(NodeState::Error); | ||
trace.push(self.nodes[n].obligation.clone()); | ||
error_stack.extend(self.nodes[n].dependants.iter().map(|x| x.get())); | ||
|
||
|
@@ -359,12 +407,13 @@ impl<O: Debug + ForestObligation> ObligationForest<O> { | |
None => break | ||
}; | ||
|
||
match self.nodes[i].state { | ||
let node = &self.nodes[i]; | ||
|
||
match node.state.get() { | ||
NodeState::Error => continue, | ||
ref mut s => *s = NodeState::Error | ||
_ => node.state.set(NodeState::Error) | ||
} | ||
|
||
let node = &self.nodes[i]; | ||
error_stack.extend( | ||
node.dependants.iter().cloned().chain(node.parent).map(|x| x.get()) | ||
); | ||
|
@@ -374,41 +423,37 @@ impl<O: Debug + ForestObligation> ObligationForest<O> { | |
trace | ||
} | ||
|
||
fn mark_as_waiting(&mut self) { | ||
for node in &mut self.nodes { | ||
if node.state == NodeState::Waiting { | ||
node.state = NodeState::Success; | ||
/// Marks all nodes that depend on a pending node as "waiting". | ||
fn mark_as_waiting(&self) { | ||
for node in &self.nodes { | ||
if node.state.get() == NodeState::Waiting { | ||
node.state.set(NodeState::Success); | ||
} | ||
} | ||
|
||
let mut undone_stack = self.scratch.take().unwrap(); | ||
undone_stack.extend( | ||
self.nodes.iter().enumerate() | ||
.filter(|&(_i, n)| n.state == NodeState::Pending) | ||
.map(|(i, _n)| i)); | ||
|
||
loop { | ||
// non-standard `while let` to bypass #6393 | ||
let i = match undone_stack.pop() { | ||
Some(i) => i, | ||
None => break | ||
}; | ||
for node in &self.nodes { | ||
if node.state.get() == NodeState::Pending { | ||
self.mark_as_waiting_from(node) | ||
} | ||
} | ||
} | ||
|
||
match self.nodes[i].state { | ||
NodeState::Pending | NodeState::Done => {}, | ||
NodeState::Waiting | NodeState::Error => continue, | ||
ref mut s @ NodeState::Success => { | ||
*s = NodeState::Waiting; | ||
} | ||
fn mark_as_waiting_from(&self, node: &Node<O>) { | ||
match node.state.get() { | ||
NodeState::Pending | NodeState::Done => {}, | ||
NodeState::Waiting | NodeState::Error | NodeState::InLoop => return, | ||
NodeState::Success => { | ||
node.state.set(NodeState::Waiting); | ||
} | ||
} | ||
|
||
let node = &self.nodes[i]; | ||
undone_stack.extend( | ||
node.dependants.iter().cloned().chain(node.parent).map(|x| x.get()) | ||
); | ||
if let Some(parent) = node.parent { | ||
self.mark_as_waiting_from(&self.nodes[parent.get()]); | ||
} | ||
|
||
self.scratch = Some(undone_stack); | ||
for dependant in &node.dependants { | ||
self.mark_as_waiting_from(&self.nodes[dependant.get()]); | ||
} | ||
} | ||
|
||
/// Compresses the vector, removing all popped nodes. This adjusts | ||
|
@@ -433,12 +478,22 @@ impl<O: Debug + ForestObligation> ObligationForest<O> { | |
// self.nodes[i - dead_nodes..i] are all dead | ||
// self.nodes[i..] are unchanged | ||
for i in 0..self.nodes.len() { | ||
if let NodeState::Done = self.nodes[i].state { | ||
self.done_cache.insert(self.nodes[i].obligation.as_predicate().clone()); | ||
match self.nodes[i].state.get() { | ||
NodeState::Done => { | ||
self.waiting_cache.remove(self.nodes[i].obligation.as_predicate()); | ||
// FIXME(HashMap): why can't I get my key back? | ||
self.done_cache.insert(self.nodes[i].obligation.as_predicate().clone()); | ||
} | ||
NodeState::Error => { | ||
// We *intentionally* remove the node from the cache at this point. Otherwise | ||
// tests must come up with a different type on every type error they | ||
// check against. | ||
self.waiting_cache.remove(self.nodes[i].obligation.as_predicate()); | ||
} | ||
_ => {} | ||
} | ||
|
||
if self.nodes[i].is_popped() { | ||
self.waiting_cache.remove(self.nodes[i].obligation.as_predicate()); | ||
node_rewrites[i] = nodes_len; | ||
dead_nodes += 1; | ||
} else { | ||
|
@@ -461,7 +516,7 @@ impl<O: Debug + ForestObligation> ObligationForest<O> { | |
let successful = (0..dead_nodes) | ||
.map(|_| self.nodes.pop().unwrap()) | ||
.flat_map(|node| { | ||
match node.state { | ||
match node.state.get() { | ||
NodeState::Error => None, | ||
NodeState::Done => Some(node.obligation), | ||
_ => unreachable!() | ||
|
@@ -521,15 +576,15 @@ impl<O> Node<O> { | |
Node { | ||
obligation: obligation, | ||
parent: parent, | ||
state: NodeState::Pending, | ||
state: Cell::new(NodeState::Pending), | ||
dependants: vec![], | ||
} | ||
} | ||
|
||
fn is_popped(&self) -> bool { | ||
match self.state { | ||
match self.state.get() { | ||
NodeState::Pending | NodeState::Success | NodeState::Waiting => false, | ||
NodeState::Error | NodeState::Done => true, | ||
NodeState::Error | NodeState::Done | NodeState::InLoop => true, | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// Regression test for #33344, initial version. This example allowed | ||
// arbitrary trait bounds to be synthesized. | ||
|
||
trait Tweedledum: IntoIterator {} | ||
trait Tweedledee: IntoIterator {} | ||
|
||
impl<T: Tweedledum> Tweedledee for T {} | ||
impl<T: Tweedledee> Tweedledum for T {} | ||
|
||
trait Combo: IntoIterator {} | ||
impl<T: Tweedledee + Tweedledum> Combo for T {} | ||
|
||
fn is_ee<T: Combo>(t: T) { | ||
t.into_iter(); | ||
} | ||
|
||
fn main() { | ||
is_ee(4); | ||
//~^ ERROR overflow evaluating the requirement `_: Tweedle | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you visiting the parent here? This surprises me a bit. It seems like you're just doing a DFS here, so I'd expect you to only traverse edges in one direction...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parent and dependants go in the same direction.