Skip to content

Commit

Permalink
Merge #260: clippy fixes: make some lifetimes anonymous and add send/…
Browse files Browse the repository at this point in the history
…sync to types::BoundRef

5696096 types: implement `Send`/`Sync` for `BoundRef` (Andrew Poelstra)
9385b6d clippy: make lifetimes anonymous where possible (Andrew Poelstra)

Pull request description:

  `types::BoundRef` is an internal type which includes a sort of weak pointer to an `Arc` containing a slab allocation used by the type inference engine.

  It is `Send` and `Sync` by construction but the compiler does not recognize this because it includes a raw pointer. Add explicit implementations with a safety comment explaining why it is okay.

  Fixes #258

ACKs for top commit:
  uncomputable:
    ACK 5696096

Tree-SHA512: 4498543e98c13950444029803f7a7f016861323092bc814d418b7bda67c0ddd131556d931caff47cded3f08a236673c987ebb74da9e879f5c13969001ee5116a
  • Loading branch information
uncomputable committed Jan 12, 2025
2 parents fbe88d6 + 5696096 commit 7f14121
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/bit_encoding/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ enum DecodeNode<J: Jet> {
Word(Word),
}

impl<'d, J: Jet> DagLike for (usize, &'d [DecodeNode<J>]) {
impl<J: Jet> DagLike for (usize, &'_ [DecodeNode<J>]) {
type Node = DecodeNode<J>;

fn data(&self) -> &DecodeNode<J> {
Expand Down
4 changes: 2 additions & 2 deletions src/bit_encoding/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<'n, N: node::Marker> Disconnectable<EncodeNode<'n, N>> for EncodeNode<'n, N
}
}

impl<'n, N: node::Marker> DagLike for EncodeNode<'n, N> {
impl<N: node::Marker> DagLike for EncodeNode<'_, N> {
type Node = Self;
fn data(&self) -> &Self {
self
Expand Down Expand Up @@ -125,7 +125,7 @@ impl<N: node::Marker> Default for EncodeSharing<N> {
}
}

impl<'n, N: node::Marker> SharingTracker<EncodeNode<'n, N>> for EncodeSharing<N> {
impl<N: node::Marker> SharingTracker<EncodeNode<'_, N>> for EncodeSharing<N> {
fn record(&mut self, d: &EncodeNode<N>, index: usize) -> Option<usize> {
let id = match d {
EncodeNode::Node(n) => EncodeId::Node(n.sharing_id()?),
Expand Down
2 changes: 1 addition & 1 deletion src/bit_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl BitMachine {
}

// Not used, but useful for debugging, so keep it around
impl<'a, J: Jet> fmt::Debug for CallStack<'a, J> {
impl<J: Jet> fmt::Debug for CallStack<'_, J> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CallStack::Goto(ins) => write!(f, "goto {}", ins.inner()),
Expand Down
2 changes: 1 addition & 1 deletion src/dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ impl<D: DagLike> DagLike for SwapChildren<D> {
}
}

impl<'a, N: node::Marker> DagLike for &'a Node<N> {
impl<N: node::Marker> DagLike for &'_ Node<N> {
type Node = Node<N>;

fn data(&self) -> &Node<N> {
Expand Down
2 changes: 1 addition & 1 deletion src/human_encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl WitnessOrHole {
}
}

impl<'a> From<&'a NoWitness> for WitnessOrHole {
impl From<&'_ NoWitness> for WitnessOrHole {
fn from(_: &NoWitness) -> Self {
WitnessOrHole::Witness
}
Expand Down
2 changes: 1 addition & 1 deletion src/human_encoding/named_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<J: Jet> NamedCommitNode<J> {
phantom: PhantomData<J>,
}

impl<'a, J: Jet> Converter<Named<Commit<J>>, Witness<J>> for Populator<'a, J> {
impl<J: Jet> Converter<Named<Commit<J>>, Witness<J>> for Populator<'_, J> {
type Error = ();

fn convert_witness(
Expand Down
2 changes: 1 addition & 1 deletion src/human_encoding/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ struct ResolvedExpression<J: Jet> {
in_degree: AtomicUsize,
}

impl<'a, J: Jet> DagLike for &'a ResolvedExpression<J> {
impl<J: Jet> DagLike for &'_ ResolvedExpression<J> {
type Node = ResolvedExpression<J>;
fn data(&self) -> &ResolvedExpression<J> {
self
Expand Down
4 changes: 2 additions & 2 deletions src/node/redeem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ impl<J: Jet> RedeemNode<J> {
phantom: PhantomData<J>,
}

impl<'bits, J: Jet, I: Iterator<Item = u8>> Converter<Construct<J>, Redeem<J>>
for DecodeFinalizer<'bits, J, I>
impl<J: Jet, I: Iterator<Item = u8>> Converter<Construct<J>, Redeem<J>>
for DecodeFinalizer<'_, J, I>
{
type Error = Error;
fn convert_witness(
Expand Down
2 changes: 1 addition & 1 deletion src/policy/satisfy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ mod tests {
pub index: usize,
}

impl<'a, Pk: ToXOnlyPubkey> Satisfier<Pk> for PolicySatisfier<'a, Pk> {
impl<Pk: ToXOnlyPubkey> Satisfier<Pk> for PolicySatisfier<'_, Pk> {
fn lookup_tap_leaf_script_sig(
&self,
pk: &Pk,
Expand Down
17 changes: 15 additions & 2 deletions src/types/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,19 @@ pub struct BoundRef {
index: usize,
}

// SAFETY: The pointer inside `BoundRef` is always (eventually) constructed from Arc::as_ptr
// from the slab of a type-inference context.
//
// Arc will prevent the pointer from ever changing, except to be deallocated when the last
// Arc goes away. But this occurs only when the context itself goes away, which in turn
// happens only when every type bound referring to the context goes away.
//
// If this were untrue, our use of `BoundRef` would lead to dereferences of a dangling
// pointer, and `Send`/`Sync` would be the least of our concerns!
unsafe impl Send for BoundRef {}
// SAFETY: see comment on `Send`
unsafe impl Sync for BoundRef {}

impl BoundRef {
pub fn assert_matches_context(&self, ctx: &Context) {
assert_eq!(
Expand Down Expand Up @@ -282,7 +295,7 @@ impl super::PointerLike for BoundRef {
}
}

impl<'ctx> DagLike for (&'ctx Context, BoundRef) {
impl DagLike for (&'_ Context, BoundRef) {
type Node = BoundRef;
fn data(&self) -> &BoundRef {
&self.1
Expand Down Expand Up @@ -318,7 +331,7 @@ struct LockedContext<'ctx> {
slab: MutexGuard<'ctx, Vec<Bound>>,
}

impl<'ctx> LockedContext<'ctx> {
impl LockedContext<'_> {
fn alloc_bound(&mut self, bound: Bound) -> BoundRef {
self.slab.push(bound);
let index = self.slab.len() - 1;
Expand Down
2 changes: 1 addition & 1 deletion src/types/final_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl fmt::Display for Final {
}
}

impl<'a> DagLike for &'a Final {
impl DagLike for &'_ Final {
type Node = Final;
fn data(&self) -> &Final {
self
Expand Down
4 changes: 2 additions & 2 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ enum ValueInner {
Product(Arc<Value>, Arc<Value>),
}

impl<'a> DagLike for &'a Value {
impl DagLike for &'_ Value {
type Node = Value;

fn data(&self) -> &Value {
Expand Down Expand Up @@ -361,7 +361,7 @@ impl<'a> PaddedBitsIter<'a> {
}
}

impl<'a> Iterator for PaddedBitsIter<'a> {
impl Iterator for PaddedBitsIter<'_> {
type Item = bool;

fn next(&mut self) -> Option<Self::Item> {
Expand Down

0 comments on commit 7f14121

Please sign in to comment.