-
Notifications
You must be signed in to change notification settings - Fork 181
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
Remove Ord
from chalk_ir::interner::DefId
#740
Conversation
Ord
from chalk_ir::interner::DefId
(second attempt)Ord
from chalk_ir::interner::DefId
@rustbot assign @jackh726 cc @nikomatsakis |
Interesting...I forgot that auto traits are sorted in |
This comment has been minimized.
This comment has been minimized.
Error: The feature Please let |
chalk-solve/src/coherence.rs
Outdated
|
||
// Find all specializations (implemented in coherence/solve) | ||
// Record them in the forest by adding an edge from the less special | ||
// to the more special. | ||
self.visit_specializations_of_trait(|less_special, more_special| { | ||
forest.add_edge(less_special, more_special, ()); | ||
// Check so that we never add multiple nodes with the same ImplId. | ||
let l = forest.add_node(less_special); |
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.
If we didn't add the nodes before, why do we need to add them now? update_edge doesn't do anything different versus add_edge with respect to nodes.
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.
The nodes must be added because petgraph::graphmap::GraphMap::add_edge
can take a generic type N
(that must implement Ord
), where petgraph::graph::Graph::update_edge
takes a NodeIndex
.
We need to use Graph
since this PR removes Ord
from ImplId
.
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.
Ah, I was looking at Graph::add_edge.
Can this create duplicate nodes?
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.
petgraph::graph::Graph::add_node
does create multiple nodes when given the same value. (Playground example.)
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.
I'm going to see if I can handle this logic with a match statement:
let node_impls: Vec<ImplId<_>> = forest.raw_nodes().iter().map(|x| x.weight).collect();
self.visit_specializations_of_trait(|less_special, more_special| {
match (
node_impls.contains(&less_special),
node_impls.contains(&more_special),
) {
(true, true) => todo!(),
(true, false) => todo!(),
(false, true) => todo!(),
(false, false) => todo!(),
}
}
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.
I see "Add or update an edge from a to b." - So it's basically add_edge
without allowing duplicates.
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.
Seems like it might make sense to keep around a Map<ImplId, NodeIndex>
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.
Before visit_specialization_of_trait
, forest
will be empty...
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.
I'm trying to reason this out. The logic below seems right, but I'm not sure how to get the indices for existing nodes...
// Find all specializations. (This is implemented in coherence/solve.)
// Record them in the forest by adding an edge from the less special
// to the more special.
self.visit_specializations_of_trait(|less_special, more_special| {
match (
node_impls.contains(&less_special),
node_impls.contains(&more_special),
) {
(true, true) => {
// but how do we get indices for l and m?
forest.add_edge(l, m, ());
}
(true, false) => {
let m = forest.add_node(more_special);
// but how do we get index for l?
forest.add_edge(l, m, ());
}
(false, true) => {
let l = forest.add_node(less_special);
// but how do we get index for m?
forest.add_edge(l, m, ());
}
(false, false) => {
// add L and M
let l = forest.add_node(less_special);
let m = forest.add_node(more_special);
forest.add_edge(l, m, ());
}
}
})?;
Can this even be done correctly without GraphMap? 😬
Update: Ah. node_indices
might work...
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.
I'm thinking something like
let mut forest = DiGraph::new();
let mut node_map = FxHashMap::new();
self.visit_specializations_of_trait(|less_special, more_special| {
let less_special_node = *node_map.entry(less_special).or_insert_with(|| forest.add_node(less_special));
let more_special_node = *node_map.entry(more_special).or_insert_with(|| forest.add_node(more_special));
forest.update_edge(less_special_noe, more_special_node, ());
}
57b4e5d
to
2e038de
Compare
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.
Nits but LGTM otherwise
Use `indexmap` to obviate need for ordering `DefId`s. In specialization forest, use a map of ImplIds to node indices to avoid duplicate nodes.
@bors r+ |
📌 Commit 29c1d17 has been approved by |
☀️ Test successful - checks-actions |
⬆ chalk to 0.76.0 This update contains rust-lang/chalk#740, which is needed for work on rust-lang#90317.
This change is required for rust-lang/rust#90749.
For background on the initiative of removing ordering traits from items like
DefId
inrustc
, see rust-lang/rust#90317.