Skip to content

Commit b25023d

Browse files
committed
compiler: remove a bunch of unused error paths
It still feels like there is more to remove because we have this `type_check_common` method which sometimes takes an error-returning closure and sometimes takes an infallible one. But this at lesat gets rid of the error paths that were purely caused by Property. Again, in a separate commit to make reviewing easier.
1 parent eb854aa commit b25023d

File tree

1 file changed

+63
-81
lines changed

1 file changed

+63
-81
lines changed

src/policy/compiler.rs

+63-81
Original file line numberDiff line numberDiff line change
@@ -200,145 +200,133 @@ impl CompilerExtData {
200200

201201
fn time() -> Self { CompilerExtData { branch_prob: None, sat_cost: 0.0, dissat_cost: None } }
202202

203-
fn cast_alt(self) -> Result<Self, types::ErrorKind> {
204-
Ok(CompilerExtData {
203+
fn cast_alt(self) -> Self {
204+
CompilerExtData {
205205
branch_prob: None,
206206
sat_cost: self.sat_cost,
207207
dissat_cost: self.dissat_cost,
208-
})
208+
}
209209
}
210210

211-
fn cast_swap(self) -> Result<Self, types::ErrorKind> {
212-
Ok(CompilerExtData {
211+
fn cast_swap(self) -> Self {
212+
CompilerExtData {
213213
branch_prob: None,
214214
sat_cost: self.sat_cost,
215215
dissat_cost: self.dissat_cost,
216-
})
216+
}
217217
}
218218

219-
fn cast_check(self) -> Result<Self, types::ErrorKind> {
220-
Ok(CompilerExtData {
219+
fn cast_check(self) -> Self {
220+
CompilerExtData {
221221
branch_prob: None,
222222
sat_cost: self.sat_cost,
223223
dissat_cost: self.dissat_cost,
224-
})
224+
}
225225
}
226226

227-
fn cast_dupif(self) -> Result<Self, types::ErrorKind> {
228-
Ok(CompilerExtData {
229-
branch_prob: None,
230-
sat_cost: 2.0 + self.sat_cost,
231-
dissat_cost: Some(1.0),
232-
})
227+
fn cast_dupif(self) -> Self {
228+
CompilerExtData { branch_prob: None, sat_cost: 2.0 + self.sat_cost, dissat_cost: Some(1.0) }
233229
}
234230

235-
fn cast_verify(self) -> Result<Self, types::ErrorKind> {
236-
Ok(CompilerExtData { branch_prob: None, sat_cost: self.sat_cost, dissat_cost: None })
231+
fn cast_verify(self) -> Self {
232+
CompilerExtData { branch_prob: None, sat_cost: self.sat_cost, dissat_cost: None }
237233
}
238234

239-
fn cast_nonzero(self) -> Result<Self, types::ErrorKind> {
240-
Ok(CompilerExtData { branch_prob: None, sat_cost: self.sat_cost, dissat_cost: Some(1.0) })
235+
fn cast_nonzero(self) -> Self {
236+
CompilerExtData { branch_prob: None, sat_cost: self.sat_cost, dissat_cost: Some(1.0) }
241237
}
242238

243-
fn cast_zeronotequal(self) -> Result<Self, types::ErrorKind> {
244-
Ok(CompilerExtData {
239+
fn cast_zeronotequal(self) -> Self {
240+
CompilerExtData {
245241
branch_prob: None,
246242
sat_cost: self.sat_cost,
247243
dissat_cost: self.dissat_cost,
248-
})
244+
}
249245
}
250246

251-
fn cast_true(self) -> Result<Self, types::ErrorKind> {
252-
Ok(CompilerExtData { branch_prob: None, sat_cost: self.sat_cost, dissat_cost: None })
247+
fn cast_true(self) -> Self {
248+
CompilerExtData { branch_prob: None, sat_cost: self.sat_cost, dissat_cost: None }
253249
}
254250

255-
fn cast_unlikely(self) -> Result<Self, types::ErrorKind> {
256-
Ok(CompilerExtData {
257-
branch_prob: None,
258-
sat_cost: 2.0 + self.sat_cost,
259-
dissat_cost: Some(1.0),
260-
})
251+
fn cast_unlikely(self) -> Self {
252+
CompilerExtData { branch_prob: None, sat_cost: 2.0 + self.sat_cost, dissat_cost: Some(1.0) }
261253
}
262254

263-
fn cast_likely(self) -> Result<Self, types::ErrorKind> {
264-
Ok(CompilerExtData {
265-
branch_prob: None,
266-
sat_cost: 1.0 + self.sat_cost,
267-
dissat_cost: Some(2.0),
268-
})
255+
fn cast_likely(self) -> Self {
256+
CompilerExtData { branch_prob: None, sat_cost: 1.0 + self.sat_cost, dissat_cost: Some(2.0) }
269257
}
270258

271-
fn and_b(left: Self, right: Self) -> Result<Self, types::ErrorKind> {
272-
Ok(CompilerExtData {
259+
fn and_b(left: Self, right: Self) -> Self {
260+
CompilerExtData {
273261
branch_prob: None,
274262
sat_cost: left.sat_cost + right.sat_cost,
275263
dissat_cost: match (left.dissat_cost, right.dissat_cost) {
276264
(Some(l), Some(r)) => Some(l + r),
277265
_ => None,
278266
},
279-
})
267+
}
280268
}
281269

282-
fn and_v(left: Self, right: Self) -> Result<Self, types::ErrorKind> {
283-
Ok(CompilerExtData {
270+
fn and_v(left: Self, right: Self) -> Self {
271+
CompilerExtData {
284272
branch_prob: None,
285273
sat_cost: left.sat_cost + right.sat_cost,
286274
dissat_cost: None,
287-
})
275+
}
288276
}
289277

290-
fn or_b(l: Self, r: Self) -> Result<Self, types::ErrorKind> {
278+
fn or_b(l: Self, r: Self) -> Self {
291279
let lprob = l
292280
.branch_prob
293281
.expect("BUG: left branch prob must be set for disjunctions");
294282
let rprob = r
295283
.branch_prob
296284
.expect("BUG: right branch prob must be set for disjunctions");
297-
Ok(CompilerExtData {
285+
CompilerExtData {
298286
branch_prob: None,
299287
sat_cost: lprob * (l.sat_cost + r.dissat_cost.unwrap())
300288
+ rprob * (r.sat_cost + l.dissat_cost.unwrap()),
301289
dissat_cost: Some(l.dissat_cost.unwrap() + r.dissat_cost.unwrap()),
302-
})
290+
}
303291
}
304292

305-
fn or_d(l: Self, r: Self) -> Result<Self, types::ErrorKind> {
293+
fn or_d(l: Self, r: Self) -> Self {
306294
let lprob = l
307295
.branch_prob
308296
.expect("BUG: left branch prob must be set for disjunctions");
309297
let rprob = r
310298
.branch_prob
311299
.expect("BUG: right branch prob must be set for disjunctions");
312-
Ok(CompilerExtData {
300+
CompilerExtData {
313301
branch_prob: None,
314302
sat_cost: lprob * l.sat_cost + rprob * (r.sat_cost + l.dissat_cost.unwrap()),
315303
dissat_cost: r.dissat_cost.map(|rd| l.dissat_cost.unwrap() + rd),
316-
})
304+
}
317305
}
318306

319-
fn or_c(l: Self, r: Self) -> Result<Self, types::ErrorKind> {
307+
fn or_c(l: Self, r: Self) -> Self {
320308
let lprob = l
321309
.branch_prob
322310
.expect("BUG: left branch prob must be set for disjunctions");
323311
let rprob = r
324312
.branch_prob
325313
.expect("BUG: right branch prob must be set for disjunctions");
326-
Ok(CompilerExtData {
314+
CompilerExtData {
327315
branch_prob: None,
328316
sat_cost: lprob * l.sat_cost + rprob * (r.sat_cost + l.dissat_cost.unwrap()),
329317
dissat_cost: None,
330-
})
318+
}
331319
}
332320

333321
#[allow(clippy::manual_map)] // Complex if/let is better as is.
334-
fn or_i(l: Self, r: Self) -> Result<Self, types::ErrorKind> {
322+
fn or_i(l: Self, r: Self) -> Self {
335323
let lprob = l
336324
.branch_prob
337325
.expect("BUG: left branch prob must be set for disjunctions");
338326
let rprob = r
339327
.branch_prob
340328
.expect("BUG: right branch prob must be set for disjunctions");
341-
Ok(CompilerExtData {
329+
CompilerExtData {
342330
branch_prob: None,
343331
sat_cost: lprob * (2.0 + l.sat_cost) + rprob * (1.0 + r.sat_cost),
344332
dissat_cost: if let (Some(ldis), Some(rdis)) = (l.dissat_cost, r.dissat_cost) {
@@ -354,7 +342,7 @@ impl CompilerExtData {
354342
} else {
355343
None
356344
},
357-
})
345+
}
358346
}
359347

360348
fn and_or(a: Self, b: Self, c: Self) -> Result<Self, types::ErrorKind> {
@@ -434,11 +422,6 @@ impl CompilerExtData {
434422
Pk: MiniscriptKey,
435423
Ctx: ScriptContext,
436424
{
437-
let wrap_err = |result: Result<Self, ErrorKind>| {
438-
result
439-
.map_err(|kind| types::Error { fragment_string: fragment.to_string(), error: kind })
440-
};
441-
442425
match *fragment {
443426
Terminal::True => Ok(Self::TRUE),
444427
Terminal::False => Ok(Self::FALSE),
@@ -488,50 +471,53 @@ impl CompilerExtData {
488471
Terminal::Hash256(..) => Ok(Self::hash()),
489472
Terminal::Ripemd160(..) => Ok(Self::hash()),
490473
Terminal::Hash160(..) => Ok(Self::hash()),
491-
Terminal::Alt(ref sub) => wrap_err(Self::cast_alt(get_child(&sub.node, 0)?)),
492-
Terminal::Swap(ref sub) => wrap_err(Self::cast_swap(get_child(&sub.node, 0)?)),
493-
Terminal::Check(ref sub) => wrap_err(Self::cast_check(get_child(&sub.node, 0)?)),
494-
Terminal::DupIf(ref sub) => wrap_err(Self::cast_dupif(get_child(&sub.node, 0)?)),
495-
Terminal::Verify(ref sub) => wrap_err(Self::cast_verify(get_child(&sub.node, 0)?)),
496-
Terminal::NonZero(ref sub) => wrap_err(Self::cast_nonzero(get_child(&sub.node, 0)?)),
474+
Terminal::Alt(ref sub) => Ok(Self::cast_alt(get_child(&sub.node, 0)?)),
475+
Terminal::Swap(ref sub) => Ok(Self::cast_swap(get_child(&sub.node, 0)?)),
476+
Terminal::Check(ref sub) => Ok(Self::cast_check(get_child(&sub.node, 0)?)),
477+
Terminal::DupIf(ref sub) => Ok(Self::cast_dupif(get_child(&sub.node, 0)?)),
478+
Terminal::Verify(ref sub) => Ok(Self::cast_verify(get_child(&sub.node, 0)?)),
479+
Terminal::NonZero(ref sub) => Ok(Self::cast_nonzero(get_child(&sub.node, 0)?)),
497480
Terminal::ZeroNotEqual(ref sub) => {
498-
wrap_err(Self::cast_zeronotequal(get_child(&sub.node, 0)?))
481+
Ok(Self::cast_zeronotequal(get_child(&sub.node, 0)?))
499482
}
500483
Terminal::AndB(ref l, ref r) => {
501484
let ltype = get_child(&l.node, 0)?;
502485
let rtype = get_child(&r.node, 1)?;
503-
wrap_err(Self::and_b(ltype, rtype))
486+
Ok(Self::and_b(ltype, rtype))
504487
}
505488
Terminal::AndV(ref l, ref r) => {
506489
let ltype = get_child(&l.node, 0)?;
507490
let rtype = get_child(&r.node, 1)?;
508-
wrap_err(Self::and_v(ltype, rtype))
491+
Ok(Self::and_v(ltype, rtype))
509492
}
510493
Terminal::OrB(ref l, ref r) => {
511494
let ltype = get_child(&l.node, 0)?;
512495
let rtype = get_child(&r.node, 1)?;
513-
wrap_err(Self::or_b(ltype, rtype))
496+
Ok(Self::or_b(ltype, rtype))
514497
}
515498
Terminal::OrD(ref l, ref r) => {
516499
let ltype = get_child(&l.node, 0)?;
517500
let rtype = get_child(&r.node, 1)?;
518-
wrap_err(Self::or_d(ltype, rtype))
501+
Ok(Self::or_d(ltype, rtype))
519502
}
520503
Terminal::OrC(ref l, ref r) => {
521504
let ltype = get_child(&l.node, 0)?;
522505
let rtype = get_child(&r.node, 1)?;
523-
wrap_err(Self::or_c(ltype, rtype))
506+
Ok(Self::or_c(ltype, rtype))
524507
}
525508
Terminal::OrI(ref l, ref r) => {
526509
let ltype = get_child(&l.node, 0)?;
527510
let rtype = get_child(&r.node, 1)?;
528-
wrap_err(Self::or_i(ltype, rtype))
511+
Ok(Self::or_i(ltype, rtype))
529512
}
530513
Terminal::AndOr(ref a, ref b, ref c) => {
531514
let atype = get_child(&a.node, 0)?;
532515
let btype = get_child(&b.node, 1)?;
533516
let ctype = get_child(&c.node, 2)?;
534-
wrap_err(Self::and_or(atype, btype, ctype))
517+
Self::and_or(atype, btype, ctype).map_err(|kind| types::Error {
518+
fragment_string: fragment.to_string(),
519+
error: kind,
520+
})
535521
}
536522
Terminal::Thresh(k, ref subs) => {
537523
if k == 0 {
@@ -548,18 +534,14 @@ impl CompilerExtData {
548534
}
549535

550536
let mut last_err_frag = None;
551-
let res = Self::threshold(k, subs.len(), |n| match get_child(&subs[n].node, n) {
537+
Self::threshold(k, subs.len(), |n| match get_child(&subs[n].node, n) {
552538
Ok(x) => Ok(x),
553539
Err(e) => {
554540
last_err_frag = Some(e.fragment_string);
555541
Err(e.error)
556542
}
557-
});
558-
559-
res.map_err(|kind| types::Error {
560-
fragment_string: last_err_frag.unwrap_or_else(|| fragment.to_string()),
561-
error: kind,
562543
})
544+
.map_err(|kind| types::Error { fragment_string: fragment.to_string(), error: kind })
563545
}
564546
}
565547
}
@@ -650,7 +632,7 @@ struct Cast<Pk: MiniscriptKey, Ctx: ScriptContext> {
650632
node: fn(Arc<Miniscript<Pk, Ctx>>) -> Terminal<Pk, Ctx>,
651633
ast_type: fn(types::Type) -> Result<types::Type, ErrorKind>,
652634
ext_data: fn(types::ExtData) -> types::ExtData,
653-
comp_ext_data: fn(CompilerExtData) -> Result<CompilerExtData, types::ErrorKind>,
635+
comp_ext_data: fn(CompilerExtData) -> CompilerExtData,
654636
}
655637

656638
impl<Pk: MiniscriptKey, Ctx: ScriptContext> Cast<Pk, Ctx> {
@@ -661,7 +643,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Cast<Pk, Ctx> {
661643
(self.ast_type)(ast.ms.ty)?,
662644
(self.ext_data)(ast.ms.ext),
663645
)),
664-
comp_ext_data: (self.comp_ext_data)(ast.comp_ext_data)?,
646+
comp_ext_data: (self.comp_ext_data)(ast.comp_ext_data),
665647
})
666648
}
667649
}

0 commit comments

Comments
 (0)