-
-
Notifications
You must be signed in to change notification settings - Fork 60
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
Avoid cloning the typechecker in elaborate_statement
#543
Merged
sharkdp
merged 2 commits into
sharkdp:master
from
GuerricChupin:avoid-cloning-the-typechecker
Aug 19, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use std::hash::Hash; | ||
use std::{borrow::Borrow, collections::HashMap}; | ||
|
||
/// A stack of hash maps. All insertions affect the hash map at the top of the | ||
/// stack (which is the last element of the `stack` vector), preserving any | ||
/// entries in maps below. The `save` function can be used to push a new map on | ||
/// the top of the stack, in effect saving the current state of the map, which | ||
/// one can then restore with `restore`. | ||
/// | ||
/// The stack vector should never be empty | ||
#[derive(Debug, Clone)] | ||
pub(crate) struct MapStack<K, V> { | ||
stack: Vec<HashMap<K, V>>, | ||
} | ||
|
||
impl<K, V> Default for MapStack<K, V> { | ||
fn default() -> Self { | ||
MapStack { | ||
stack: vec![Default::default()], | ||
} | ||
} | ||
} | ||
|
||
impl<K: Hash + Eq, V> MapStack<K, V> { | ||
fn iter_dict(&self) -> impl Iterator<Item = &HashMap<K, V>> { | ||
self.stack.iter().rev() | ||
} | ||
|
||
fn iter_dict_mut(&mut self) -> impl Iterator<Item = &mut HashMap<K, V>> { | ||
self.stack.iter_mut().rev() | ||
} | ||
|
||
pub(crate) fn iter(&self) -> impl Iterator<Item = (&K, &V)> { | ||
self.iter_dict().flatten() | ||
} | ||
|
||
pub(crate) fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)> { | ||
self.iter_dict_mut().flatten() | ||
} | ||
|
||
pub(crate) fn keys(&self) -> impl Iterator<Item = &K> { | ||
self.iter_dict().map(|dict| dict.keys()).flatten() | ||
} | ||
|
||
pub(crate) fn get<Q>(&self, key: &Q) -> Option<&V> | ||
where | ||
K: Borrow<Q>, | ||
Q: Hash + Eq + ?Sized, | ||
{ | ||
self.iter_dict().filter_map(|dict| dict.get(key)).nth(0) | ||
} | ||
|
||
pub(crate) fn insert(&mut self, key: K, value: V) { | ||
let _ = self.stack.last_mut().unwrap().insert(key, value); | ||
} | ||
|
||
pub(crate) fn contains_key<Q>(&self, key: &Q) -> bool | ||
where | ||
K: Borrow<Q>, | ||
Q: Hash + Eq + ?Sized, | ||
{ | ||
self.iter_dict().any(|dict| dict.contains_key(key)) | ||
} | ||
|
||
/// Remove the top hash map from the stack, making the next one the | ||
/// current top of the stack, restoring the state of the map before the | ||
/// last call to save. | ||
pub(crate) fn restore(&mut self) { | ||
let _ = self.stack.pop(); | ||
// The stack should never be empty | ||
assert!( | ||
!self.stack.is_empty(), | ||
"Tried to restore the last saved state but nothing was saved" | ||
); | ||
} | ||
|
||
/// Save the current state of the map by making the top of the stack a | ||
/// new empty map. | ||
pub(crate) fn save(&mut self) { | ||
self.stack.push(HashMap::default()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We probably only use two layers at the moment, but it's great to have this more general stack, since we're going to need it once we add nested functions, I guess.