Skip to content

Commit 2f41378

Browse files
committed
stick invariant <'brand> tag onto Scope and CoreExt
When we update to rust-simplicity 0.6, we will need to stick an invariant lifetime (conventionally called 'brand) onto a bunch of types to ensure that constructed nodes from different type inference contexts are not mixed. For the types we own, do this ahead of the actual update, to separate out the noise from the "real" changes.
1 parent 31c1ee8 commit 2f41378

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

src/compile/mod.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type ProgNode = Arc<named::ConstructNode<Elements>>;
3939
/// Bindings from inner scopes overwrite bindings from outer scopes.
4040
/// Bindings live as long as their scope.
4141
#[derive(Debug, Clone)]
42-
struct Scope {
42+
struct Scope<'brand> {
4343
/// For each scope, the set of assigned variables.
4444
///
4545
/// A stack of scopes. Each scope is a stack of patterns.
@@ -72,9 +72,12 @@ struct Scope {
7272
/// Values for parameters inside the SimplicityHL program.
7373
arguments: Arguments,
7474
include_debug_symbols: bool,
75+
// In the next commits, this will be dropped since the 'brand
76+
// tag will be moved into 'ctx'.
77+
phantom: std::marker::PhantomData<fn(&'brand ()) -> &'brand ()>,
7578
}
7679

77-
impl Scope {
80+
impl<'brand> Scope<'brand> {
7881
/// Create the main scope.
7982
///
8083
/// _This function should be called at the start of the compilation and then never again._
@@ -94,6 +97,7 @@ impl Scope {
9497
call_tracker,
9598
arguments,
9699
include_debug_symbols,
100+
phantom: core::marker::PhantomData,
97101
}
98102
}
99103

@@ -105,6 +109,7 @@ impl Scope {
105109
call_tracker: Arc::clone(&self.call_tracker),
106110
arguments: self.arguments.clone(),
107111
include_debug_symbols: self.include_debug_symbols,
112+
phantom: core::marker::PhantomData,
108113
}
109114
}
110115

@@ -221,9 +226,9 @@ impl Scope {
221226
}
222227
}
223228

224-
fn compile_blk(
229+
fn compile_blk<'brand>(
225230
stmts: &[Statement],
226-
scope: &mut Scope,
231+
scope: &mut Scope<'brand>,
227232
index: usize,
228233
last_expr: Option<&Expression>,
229234
) -> Result<PairBuilder<ProgNode>, RichError> {
@@ -278,7 +283,10 @@ impl Program {
278283
}
279284

280285
impl Expression {
281-
fn compile(&self, scope: &mut Scope) -> Result<PairBuilder<ProgNode>, RichError> {
286+
fn compile<'brand>(
287+
&self,
288+
scope: &mut Scope<'brand>,
289+
) -> Result<PairBuilder<ProgNode>, RichError> {
282290
match self.inner() {
283291
ExpressionInner::Block(stmts, expr) => {
284292
scope.push_scope();
@@ -292,7 +300,10 @@ impl Expression {
292300
}
293301

294302
impl SingleExpression {
295-
fn compile(&self, scope: &mut Scope) -> Result<PairBuilder<ProgNode>, RichError> {
303+
fn compile<'brand>(
304+
&self,
305+
scope: &mut Scope<'brand>,
306+
) -> Result<PairBuilder<ProgNode>, RichError> {
296307
let expr = match self.inner() {
297308
SingleExpressionInner::Constant(value) => {
298309
let value = StructuralValue::from(value);
@@ -360,7 +371,10 @@ impl SingleExpression {
360371
}
361372

362373
impl Call {
363-
fn compile(&self, scope: &mut Scope) -> Result<PairBuilder<ProgNode>, RichError> {
374+
fn compile<'brand>(
375+
&self,
376+
scope: &mut Scope<'brand>,
377+
) -> Result<PairBuilder<ProgNode>, RichError> {
364378
let args_ast = SingleExpression::tuple(self.args().clone(), *self.as_ref());
365379
let args = args_ast.compile(scope)?;
366380

@@ -626,7 +640,10 @@ fn for_while(
626640
}
627641

628642
impl Match {
629-
fn compile(&self, scope: &mut Scope) -> Result<PairBuilder<ProgNode>, RichError> {
643+
fn compile<'brand>(
644+
&self,
645+
scope: &mut Scope<'brand>,
646+
) -> Result<PairBuilder<ProgNode>, RichError> {
630647
scope.push_scope();
631648
scope.insert(
632649
self.left()

src/named.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<J: Jet> WitnessConstructible<WitnessName> for node::ConstructData<J> {
260260
}
261261

262262
/// More constructors for types that implement [`CoreConstructible`].
263-
pub trait CoreExt: CoreConstructible + Sized {
263+
pub trait CoreExt<'brand>: CoreConstructible + Sized {
264264
fn h(inference_context: &types::Context) -> PairBuilder<Self> {
265265
PairBuilder::iden(inference_context)
266266
}
@@ -339,7 +339,7 @@ pub trait CoreExt: CoreConstructible + Sized {
339339
}
340340
}
341341

342-
impl<N: CoreConstructible> CoreExt for N {}
342+
impl<'brand, N: CoreConstructible> CoreExt<'brand> for N {}
343343

344344
/// Builder of expressions that contain
345345
/// `take`, `drop` and `iden` only.
@@ -360,7 +360,7 @@ impl<P> Default for SelectorBuilder<P> {
360360
}
361361
}
362362

363-
impl<P: CoreExt> SelectorBuilder<P> {
363+
impl<'brand, P: CoreExt<'brand>> SelectorBuilder<P> {
364364
/// Select the first component '0' of the input pair.
365365
pub fn o(mut self) -> Self {
366366
self.selection.push(false);
@@ -406,7 +406,7 @@ impl<P: CoreExt> SelectorBuilder<P> {
406406
#[derive(Debug, Clone, Hash)]
407407
pub struct PairBuilder<P>(P);
408408

409-
impl<P: CoreExt> PairBuilder<P> {
409+
impl<'brand, P: CoreExt<'brand>> PairBuilder<P> {
410410
/// Create the unit expression.
411411
///
412412
/// ## Invariant

src/pattern.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,10 @@ impl BasePattern {
202202
/// The expression takes as input a value that matches the `self` pattern.
203203
///
204204
/// The expression is a sequence of `take` and `drop` followed by `iden`.
205-
fn get<P: CoreExt>(&self, identifier: &Identifier) -> Option<SelectorBuilder<P>> {
205+
fn get<'brand, P: CoreExt<'brand>>(
206+
&self,
207+
identifier: &Identifier,
208+
) -> Option<SelectorBuilder<P>> {
206209
let mut selector = SelectorBuilder::default();
207210

208211
for data in self.verbose_pre_order_iter() {
@@ -302,7 +305,7 @@ impl BasePattern {
302305
/// This means there are infinitely many translating expressions from `self` to `to`.
303306
/// For instance, `iden`, `iden & iden`, `(iden & iden) & iden`, and so on.
304307
/// We enforce a unique translation by banning ignore from the `to` pattern.
305-
pub fn translate<P: CoreExt>(
308+
pub fn translate<'brand, P: CoreExt<'brand>>(
306309
&self,
307310
ctx: &simplicity::types::Context,
308311
to: &Self,

0 commit comments

Comments
 (0)