Skip to content

Commit b1cd2e3

Browse files
committed
Failed attempted to improve inlining
1 parent 360c9d0 commit b1cd2e3

File tree

25 files changed

+1639
-782
lines changed

25 files changed

+1639
-782
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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
},

src/import.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,11 +561,23 @@ where
561561
.spawn(Box::pin(async move {
562562
let result = {
563563
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);
564-
db.import(modulename)
564+
std::panic::AssertUnwindSafe(db.import(modulename))
565+
.catch_unwind()
565566
.await
566-
.map_err(|err| MacroError::message(err.to_string()))
567+
.map(|r| r.map_err(|err| MacroError::message(err.to_string())))
568+
.unwrap_or_else(|err| {
569+
Err(MacroError::message(
570+
err.downcast::<String>()
571+
.map(|s| *s)
572+
.or_else(|e| {
573+
e.downcast::<&str>().map(|s| String::from(&s[..]))
574+
})
575+
.unwrap_or_else(|_| "Unknown panic".to_string()),
576+
))
577+
})
567578
};
568-
drop(db); // Drop the database before sending the result, otherwise the forker may drop before the forked database
579+
// Drop the database before sending the result, otherwise the forker may drop before the forked database
580+
drop(db);
569581
let _ = tx.send(result);
570582
}))
571583
.unwrap();

std/cmp.glu

Lines changed: 21 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@NO-IMPLICIT-PRELUDE
22
//! Functionality for ordering and comparison.
33

4-
let { Bool, Ordering, Option } = import! std.types
4+
let { Bool, Ordering } = import! std.types
55
let { Semigroup } = import! std.semigroup
66
let { Monoid } = import! std.monoid
77

@@ -25,76 +25,41 @@ type Ord a = {
2525
eq : Eq a,
2626
/// Compares two values and returns wheter the first is less than, equal or greater than the second.
2727
compare : a -> a -> Ordering,
28-
(<): a -> a -> Bool,
29-
(<=): a -> a -> Bool,
30-
(>=): a -> a -> Bool,
31-
(>): a -> a -> Bool,
3228
}
3329

3430
let compare ?ord : [Ord a] -> a -> a -> Ordering = ord.compare
3531

36-
#[infix(left, 4)]
37-
let (=?) opt y : Option b -> b -> b =
38-
match opt with
39-
| Some x -> x
40-
| None -> y
41-
42-
let mk_ord builder : _ -> Ord a =
43-
let compare = builder.compare
44-
let eq = builder.eq
45-
46-
#[infix(left, 4)]
47-
let (<=) l r : a -> a -> Bool =
48-
match compare l r with
49-
| LT -> True
50-
| EQ -> True
51-
| GT -> False
52-
53-
#[infix(left, 4)]
54-
let (<) l r : a -> a -> Bool =
55-
match compare l r with
56-
| LT -> True
57-
| EQ -> False
58-
| GT -> False
59-
60-
#[infix(left, 4)]
61-
let (>) l r : a -> a -> Bool =
62-
match compare l r with
63-
| LT -> False
64-
| EQ -> False
65-
| GT -> True
66-
67-
#[infix(left, 4)]
68-
let (>=) l r : a -> a -> Bool =
69-
match compare l r with
70-
| LT -> False
71-
| EQ -> True
72-
| GT -> True
73-
74-
{
75-
eq,
76-
compare,
77-
(<) = builder.(<) =? (<),
78-
(<=) = builder.(<=) =? (<=),
79-
(>=) = builder.(>=) =? (>=),
80-
(>) = builder.(>) =? (>),
81-
}
82-
8332
/// Returns whether `l` is less than or equal to `r`.
8433
#[infix(left, 4)]
85-
let (<=) ?ord : [Ord a] -> a -> a -> Bool = ord.(<=)
34+
let (<=) l r : [Ord a] -> a -> a -> Bool =
35+
match compare l r with
36+
| LT -> True
37+
| EQ -> True
38+
| GT -> False
8639

8740
/// Returns whether `l` is less than `r`.
8841
#[infix(left, 4)]
89-
let (<) ?ord : [Ord a] -> a -> a -> Bool = ord.(<)
42+
let (<) l r : [Ord a] -> a -> a -> Bool =
43+
match compare l r with
44+
| LT -> True
45+
| EQ -> False
46+
| GT -> False
9047

9148
/// Returns whether `l` is greater than `r`.
9249
#[infix(left, 4)]
93-
let (>) ?ord : [Ord a] -> a -> a -> Bool = ord.(>)
50+
let (>) l r : [Ord a] -> a -> a -> Bool =
51+
match compare l r with
52+
| LT -> False
53+
| EQ -> False
54+
| GT -> True
9455

9556
/// Returns whether `l` is greater than or equal to `r`.
9657
#[infix(left, 4)]
97-
let (>=) ?ord : [Ord a] -> a -> a -> Bool = ord.(>=)
58+
let (>=) l r : [Ord a] -> a -> a -> Bool =
59+
match compare l r with
60+
| LT -> False
61+
| EQ -> True
62+
| GT -> True
9863

9964
let min l r : [Ord a] -> a -> a -> a =
10065
if l <= r then l
@@ -132,8 +97,6 @@ let monoid : Monoid Ordering = {
13297
min,
13398
max,
13499

135-
mk_ord,
136-
137100
Ordering,
138101

139102
semigroup,

0 commit comments

Comments
 (0)