Skip to content

Commit c473b17

Browse files
committed
types: move innards of Context into a ContextInner type
The Context object contains an Arc, which is probably the right design decision since these are supposed to be "handles" to a type inference context which can be cheaply cloned everywhere. Without this there would be a ton of Arc::new and Arc::clone noise throughout the codebase. But we want to make this structure more elaborate, which requires putting more stuff in the Arc, which requires a new struct. So do that in its own commit, to try to reduce the amount of noise in the next one.
1 parent e2d1c4c commit c473b17

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/types/context.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,23 @@ use super::{Bound, CompleteBound, Error, Final, Type, TypeInner};
3232
/// please file an issue.
3333
#[derive(Clone)]
3434
pub struct Context {
35-
slab: Arc<Mutex<Vec<Bound>>>,
35+
inner: Arc<ContextInner>,
36+
}
37+
38+
struct ContextInner {
39+
slab: Mutex<Vec<Bound>>,
3640
}
3741

3842
impl fmt::Debug for Context {
3943
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40-
let id = Arc::as_ptr(&self.slab) as usize;
44+
let id = Arc::as_ptr(&self.inner) as usize;
4145
write!(f, "inference_ctx_{:08x}", id)
4246
}
4347
}
4448

4549
impl PartialEq for Context {
4650
fn eq(&self, other: &Self) -> bool {
47-
Arc::ptr_eq(&self.slab, &other.slab)
51+
Arc::ptr_eq(&self.inner, &other.inner)
4852
}
4953
}
5054
impl Eq for Context {}
@@ -53,7 +57,9 @@ impl Context {
5357
/// Creates a new empty type inference context.
5458
pub fn new() -> Self {
5559
Context {
56-
slab: Arc::new(Mutex::new(vec![])),
60+
inner: Arc::new(ContextInner {
61+
slab: Mutex::new(vec![]),
62+
}),
5763
}
5864
}
5965

@@ -131,7 +137,7 @@ impl Context {
131137
/// the original context object, are dropped.
132138
pub fn shallow_clone(&self) -> Self {
133139
Self {
134-
slab: Arc::clone(&self.slab),
140+
inner: Arc::clone(&self.inner),
135141
}
136142
}
137143

@@ -233,15 +239,15 @@ impl Context {
233239
/// Locks the underlying slab mutex.
234240
fn lock(&self) -> LockedContext<'_> {
235241
LockedContext {
236-
context: Arc::as_ptr(&self.slab),
237-
slab: self.slab.lock().unwrap(),
242+
context: Arc::as_ptr(&self.inner),
243+
slab: self.inner.slab.lock().unwrap(),
238244
}
239245
}
240246
}
241247

242248
#[derive(Debug, Clone)]
243249
pub struct BoundRef {
244-
context: *const Mutex<Vec<Bound>>,
250+
context: *const ContextInner,
245251
index: usize,
246252
}
247253

@@ -262,7 +268,7 @@ impl BoundRef {
262268
pub fn assert_matches_context(&self, ctx: &Context) {
263269
assert_eq!(
264270
self.context,
265-
Arc::as_ptr(&ctx.slab),
271+
Arc::as_ptr(&ctx.inner),
266272
"bound was accessed from a type inference context that did not create it",
267273
);
268274
}
@@ -313,7 +319,7 @@ impl DagLike for (&'_ Context, BoundRef) {
313319

314320
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
315321
pub struct OccursCheckId {
316-
context: *const Mutex<Vec<Bound>>,
322+
context: *const ContextInner,
317323
index: usize,
318324
}
319325

@@ -327,7 +333,7 @@ struct BindError {
327333
/// This type is never exposed outside of this module and should only exist
328334
/// ephemerally within function calls into this module.
329335
struct LockedContext<'ctx> {
330-
context: *const Mutex<Vec<Bound>>,
336+
context: *const ContextInner,
331337
slab: MutexGuard<'ctx, Vec<Bound>>,
332338
}
333339

0 commit comments

Comments
 (0)