Skip to content

Commit 6d36aae

Browse files
bors[bot]Marwes
andauthored
Merge #819
819: Remove function inlining r=Marwes a=Marwes Co-authored-by: Markus Westerlind <[email protected]>
2 parents 219201e + 908dd24 commit 6d36aae

30 files changed

+2344
-816
lines changed

base/src/merge.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ where
7474
T: Clone + 'a,
7575
R: std::iter::FromIterator<T>,
7676
{
77-
merge_collect(&mut (), types, |_, (l, r)| f(l, r), |(l, _)| l.clone())
77+
merge_collect(&mut (), types, |_, (l, r)| f(l, r), |_, (l, _)| l.clone())
7878
}
7979

8080
pub struct MergeIter<'s, I, F, G, T, S>
@@ -95,13 +95,15 @@ where
9595
S: ?Sized,
9696
I: Iterator,
9797
F: FnMut(&mut S, I::Item) -> Option<U>,
98-
G: FnMut(I::Item) -> U,
98+
G: FnMut(&mut S, I::Item) -> U,
9999
{
100100
type Item = U;
101101
fn next(&mut self) -> Option<Self::Item> {
102102
if self.clone_types > 0 {
103103
self.clone_types -= 1;
104-
self.clone_types_iter.next().map(&mut self.converter)
104+
let converter = &mut self.converter;
105+
let state = &mut self.state;
106+
self.clone_types_iter.next().map(|e| converter(state, e))
105107
} else if let Some(typ) = self.next.take() {
106108
self.clone_types_iter.next();
107109
Some(typ)
@@ -135,7 +137,7 @@ where
135137
S: ?Sized,
136138
I: ExactSizeIterator,
137139
F: FnMut(&mut S, I::Item) -> Option<U>,
138-
G: FnMut(I::Item) -> U,
140+
G: FnMut(&mut S, I::Item) -> U,
139141
{
140142
fn len(&self) -> usize {
141143
self.clone_types_iter.len()
@@ -153,7 +155,7 @@ where
153155
I: IntoIterator,
154156
I::IntoIter: FusedIterator + Clone,
155157
F: FnMut(&mut S, I::Item) -> Option<U>,
156-
G: FnMut(I::Item) -> U,
158+
G: FnMut(&mut S, I::Item) -> U,
157159
R: std::iter::FromIterator<U>,
158160
{
159161
merge_iter(state, types, action, converter).map(|iter| iter.collect())
@@ -170,7 +172,7 @@ where
170172
I: IntoIterator,
171173
I::IntoIter: FusedIterator + Clone,
172174
F: FnMut(&mut S, I::Item) -> Option<U>,
173-
G: FnMut(I::Item) -> U,
175+
G: FnMut(&mut S, I::Item) -> U,
174176
{
175177
let mut types = types.into_iter();
176178
let clone_types_iter = types.clone();

base/src/scoped_map.rs

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,29 +75,28 @@ impl<K: Eq + Hash + Clone, V> ScopedMap<K, V> {
7575
}
7676

7777
/// Removes a previously inserted value from the map.
78-
pub fn remove<Q>(&mut self, k: &Q) -> bool
78+
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
7979
where
8080
K: Borrow<Q>,
81-
Q: Eq + Hash,
81+
Q: ?Sized + Eq + Hash,
8282
K: ::std::fmt::Debug,
8383
V: ::std::fmt::Debug,
8484
{
85-
match self.map.get_mut(k).map(|x| x.pop()) {
86-
Some(..) => {
87-
let mut i = self.scopes.len() as isize - 1;
88-
while i >= 0 {
89-
if self.scopes[i as usize]
90-
.as_ref()
91-
.map_or(false, |x| x.borrow() == k)
92-
{
93-
self.scopes.remove(i as usize);
94-
}
95-
i -= 1;
96-
}
97-
true
85+
let x = self.map.get_mut(k).map(|x| x.pop())?;
86+
let mut i = self.scopes.len() as isize - 1;
87+
while i >= 0 {
88+
if self.scopes[i as usize]
89+
.as_ref()
90+
.expect("Tried to remove entry not in the current scope")
91+
.borrow()
92+
== k
93+
{
94+
self.scopes.remove(i as usize);
95+
break;
9896
}
99-
None => false,
97+
i -= 1;
10098
}
99+
x
101100
}
102101

103102
/// Returns true if the key has a value declared in the last declared scope
@@ -230,6 +229,12 @@ impl<K: Eq + Hash + Clone, V> ScopedMap<K, V> {
230229
}
231230
}
232231
}
232+
233+
pub fn into_iter(self) -> impl Iterator<Item = (K, V)> {
234+
self.map
235+
.into_iter()
236+
.filter_map(|(k, mut v)| Some((k, v.pop()?)))
237+
}
233238
}
234239

235240
impl<K: Eq + Hash, V> ScopedMap<K, V> {
@@ -427,4 +432,20 @@ mod tests {
427432
assert_eq!(map.get(&"a"), Some(&0));
428433
assert_eq!(map.get(&"c"), None);
429434
}
435+
436+
#[test]
437+
fn remove() {
438+
let mut map = ScopedMap::new();
439+
map.insert("a", 0);
440+
map.enter_scope();
441+
map.insert("a", 1);
442+
map.insert("b", 2);
443+
assert_eq!(map.get("a"), Some(&1));
444+
map.remove("a");
445+
assert_eq!(map.get("a"), Some(&0));
446+
assert_eq!(map.get("b"), Some(&2));
447+
map.remove("b");
448+
assert_eq!(map.get("b"), None);
449+
map.exit_scope();
450+
}
430451
}

base/src/symbol.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ impl From<String> for Symbol {
161161
}
162162
}
163163

164+
impl<N> From<SymbolData<N>> for Symbol
165+
where
166+
N: Into<NameBuf>,
167+
{
168+
fn from(name: SymbolData<N>) -> Symbol {
169+
Symbol(Arc::new(SymbolInner::new(name)))
170+
}
171+
}
172+
164173
impl From<&'_ str> for Symbol {
165174
fn from(name: &str) -> Symbol {
166175
Symbol(Arc::new(SymbolInner::new(SymbolData::<NameBuf>::from(
@@ -282,6 +291,10 @@ impl Symbol {
282291
.location
283292
.map_or_else(|| self.0.name.len(), |l| l as usize)]
284293
}
294+
295+
pub fn as_data(&self) -> SymbolData<&Name> {
296+
SymbolData::from(&self.0.name.0[..])
297+
}
285298
}
286299

287300
impl SymbolRef {

base/src/types/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4473,7 +4473,7 @@ where
44734473
T: Clone + 'a,
44744474
R: FromIterator<T>,
44754475
{
4476-
merge_collect(state, types, f, Clone::clone)
4476+
merge_collect(state, types, f, |_, e| e.clone())
44774477
}
44784478

44794479
pub fn translate_alias<Id, T, U, F, I>(

check/src/unify_type.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,49 @@ impl<'a, 'e> Unifier<State<'a>, RcType> for UnifierState<'a, Smaller> {
15811581
}
15821582
}
15831583

1584+
pub fn instantiation<'a>(
1585+
env: &'a (dyn TypeEnv<Type = RcType> + 'a),
1586+
f: &mut dyn FnMut(&Symbol, &RcType),
1587+
l: &RcType,
1588+
r: &RcType,
1589+
) {
1590+
let subs = Default::default();
1591+
let state = State::new(env, &subs);
1592+
let mut unifier = UnifierState {
1593+
unifier: Instantiation { consumer: f },
1594+
state,
1595+
};
1596+
1597+
unifier.try_match(l, r);
1598+
}
1599+
1600+
pub struct Instantiation<'a, 'b> {
1601+
consumer: &'a mut (dyn FnMut(&Symbol, &RcType) + 'b),
1602+
}
1603+
1604+
impl<'a> Unifier<State<'a>, RcType> for UnifierState<'a, Instantiation<'_, '_>> {
1605+
fn report_error(&mut self, _error: UnifyError<TypeError<Symbol, RcType>, RcType>) {}
1606+
1607+
fn try_match_res(
1608+
&mut self,
1609+
l: &RcType,
1610+
r: &RcType,
1611+
) -> Result<Option<RcType>, UnifyError<TypeError<Symbol, RcType>, RcType>> {
1612+
match (&**l, &**r) {
1613+
(Type::Generic(l), _) => {
1614+
(self.unifier.consumer)(&l.id, r);
1615+
Ok(None)
1616+
}
1617+
1618+
_ => l.zip_match(r, self),
1619+
}
1620+
}
1621+
1622+
fn error_type(&self) -> RcType {
1623+
RcType::error_type(&self.state)
1624+
}
1625+
}
1626+
15841627
#[cfg(test)]
15851628
mod tests {
15861629
use super::*;

check/tests/implicits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ f 42
4747
ref implicit_args, ..
4848
} => match (&implicit_args[0].value, &bind[0].name.value) {
4949
(&Expr::Ident(ref arg), &Pattern::Ident(ref bind_id)) => {
50-
assert_eq!(arg.name, bind_id.name)
50+
assert_eq!(arg.name, bind_id.name);
5151
}
5252
_ => panic!(),
5353
},

codegen/src/functor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
extern crate proc_macro;
2+
13
use proc_macro2::{Ident, Span, TokenStream};
24
use syn::{self, Data, DeriveInput, Generics};
35

repl/src/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ pub struct Opt {
152152
#[structopt(name = "FILE", help = "Executes each file as a gluon program")]
153153
input: Vec<String>,
154154

155+
#[structopt(
156+
last = true,
157+
name = "ARGS",
158+
help = "Extra arguments passed to the gluon program"
159+
)]
160+
#[allow(dead_code)]
161+
args: Vec<String>,
162+
155163
#[structopt(subcommand)]
156164
subcommand_opt: Option<SubOpt>,
157165
}

repl/src/repl.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ async fn eval_line_(vm: RootedThread, line: &str) -> gluon::Result<()> {
377377
let mut eval_expr;
378378
let value = {
379379
let mut db = vm.get_database();
380-
let mut db = gluon::salsa::OwnedDb::<dyn gluon::query::Compilation>::from(&mut db);
381380
let mut module_compiler = vm.module_compiler(&mut db);
382381
eval_expr = {
383382
let eval_expr = {

0 commit comments

Comments
 (0)