All files

src/compiler/intrinsic_contracts.panack

Green: covered; red: known zero; amber: unavailable. Blank lines are outside the executable-start-line denominator.

    1 pub import project/src/compiler/body_model::{BodyContext, BoundBody, BodyResult, BodyNode, BodyNode.BodyEmpty, BodyNode.BodyCall, BodyTarget.BodyCore, BodyTypeCheckEvidence, CallArgumentEvidence}
    2 pub import project/src/compiler/module_signatures::{SignatureType, SignatureTypeIdentity.SignatureHole, SignatureTypeIdentity.SignatureCore, SignatureTypeIdentity.SignatureCallable, SignatureEffect.SignaturePure}
    3 pub import project/src/compiler/types::{SourceSpan, Diagnostic}
    4 
    5 // Shared intrinsic argument checks and retained evidence. The caller supplies
    6 // value compatibility checking, keeping the dependency on body_model acyclic.
    7 pub pure body_result(
    8   node: BodyNode,
    9   span: SourceSpan,
   10   children: [BoundBody],
   11   type: Option[SignatureType]
   12 ): BodyResult {
   13   BodyResult(BoundBody(node, span, children, type, [], [], [], [], [], []), [], [])
   14 }
   15 
   16 pub pure body_failure(message: Str, span: SourceSpan): BodyResult {
   17   BodyResult(BoundBody(BodyEmpty(), span, [], None(), [], [], [], [], [], []), [Diagnostic(message, span.start)], [])
   18 }
   19 
   20 pub pure body_nat_text(value: Nat): Str {
   21   "${value}"
   22 }
   23 
   24 pub pure body_call_arity_evidence(
   25   context: BodyContext,
   26   kind: Str,
   27   span: SourceSpan,
   28   expected_count: Option[Nat],
   29   actual_count: Nat
   30 ): [BodyTypeCheckEvidence] {
   31   if !context.retain_guard_conversions {
   32     []
   33   } else {
   34     [BodyTypeCheckEvidence(
   35       kind + " call arity",
   36       span,
   37       None(),
   38       match expected_count {
   39         Some(expected) => "expected ${expected}; found ${actual_count}",
   40         None() => "expected count unavailable because the callable type is unresolved; found ${actual_count}"
   41       },
   42       None(),
   43       match expected_count {
   44         Some(expected) => Some(expected == actual_count),
   45         None() => None()
   46       }, "", None()
   47     )]
   48   }
   49 }
   50 
   51 pub pure body_intrinsic_value_type(context: BodyContext, value: BoundBody): SignatureType {
   52   match value.type {
   53     None() => SignatureType(SignatureHole(context.owner, [], 0), []),
   54     Some(type) => type
   55   }
   56 }
   57 
   58 pub pure body_intrinsic_shape(context: BodyContext, label: Str, child: BoundBody, expected: Str, valid: Bool): [BodyTypeCheckEvidence] {
   59   if !context.retain_guard_conversions { [] }
   60   else { [BodyTypeCheckEvidence(label, child.span, child.type, expected, None(), Some(valid), "", None())] }
   61 }
   62 
   63 pub pure body_intrinsic_failure(context: BodyContext, name: Str, children: [BoundBody], span: SourceSpan,
   64   count: Nat, message: Str, shapes: [BodyTypeCheckEvidence]): BodyResult {
   65   if !context.retain_guard_conversions { body_failure(message, span) }
   66   else {
   67     checks = body_call_arity_evidence(context, "core", span, Some(count), children.len()).concat(shapes)
   68     BodyResult(BoundBody(BodyCall(BodyCore(name), [], []), span, children, None(), [], [], [], [], [], checks),
   69       [Diagnostic(message, span.start)], [])
   70   }
   71 }
   72 
   73 pub pure body_checked_intrinsic_call(context: BodyContext,
   74   require_value: PureFn[BodyContext,BoundBody,SignatureType,Str,BodyResult], name: Str, children: [BoundBody], span: SourceSpan,
   75   inputs: [SignatureType], output: SignatureType, count: Nat, initial_diagnostics: [Diagnostic],
   76   initial_deferred: [SourceSpan], shapes: [BodyTypeCheckEvidence]): BodyResult {
   77   mut diagnostics = initial_diagnostics
   78   mut deferred = initial_deferred
   79   mut arguments: [CallArgumentEvidence] = []
   80   for index in 0..inputs.len() {
   81     child = children[index]
   82     expected = inputs[index]
   83     checked = require_value.call(context, child, expected, "argument to " + name)
   84     diagnostics = diagnostics.concat(checked.diagnostics)
   85     deferred = deferred.concat(checked.deferred)
   86     if context.retain_guard_conversions {
   87       accepted: Option[Bool] = if checked.diagnostics.len() > 0 { Some(false) }
   88         else if checked.deferred.len() > 0 { None() } else { Some(true) }
   89       arguments = arguments.append(CallArgumentEvidence("call argument " + body_nat_text(index + 1) + " to " + name,
   90         child.span, child.type, expected, accepted, checked.body.guard_conversions))
   91     }
   92   }
   93   checks = body_call_arity_evidence(context, "core", span, Some(count), children.len()).concat(shapes)
   94   BodyResult(BoundBody(BodyCall(BodyCore(name), [], arguments), span, children,
   95     if diagnostics.len() == 0 && deferred.len() == 0 { Some(output) } else { None() },
   96     [], [], [], [], [], checks), diagnostics, deferred)
   97 }
   98 
   99 pub pure body_check_state_core_call(context: BodyContext,
  100   require_value: PureFn[BodyContext,BoundBody,SignatureType,Str,BodyResult], name: Str, children: [BoundBody],
  101   span: SourceSpan, initial_diagnostics: [Diagnostic], initial_deferred: [SourceSpan]): BodyResult {
  102   updating = name == "state_update" || name == "state_update_async"
  103   creating = name == "state_create" || name == "state_create_async"
  104   count: Nat = if updating { 2 } else { 1 }
  105   if children.len() != count {
  106     body_intrinsic_failure(context, name, children, span, count, "invalid State argument count", [])
  107   } else {
  108     first = body_intrinsic_value_type(context, children[0])
  109     mut payload = first
  110     mut diagnostics = initial_diagnostics
  111     if !creating {
  112       if first.identity == SignatureCore("State") && first.arguments.len() == 1 {
  113         payload = first.arguments[0]
  114       } else {
  115         diagnostics = diagnostics.append(Diagnostic("State operation requires a State[T] cell", children[0].span.start))
  116       }
  117     }
  118     cell = SignatureType(SignatureCore("State"), [payload])
  119     mut inputs = [if creating { payload } else { cell }]
  120     if updating {
  121       inputs = inputs.append(SignatureType(SignatureCallable(SignaturePure()), [payload, payload]))
  122     }
  123     body_checked_intrinsic_call(context, require_value, name, children, span, inputs,
  124       if creating { cell } else { payload }, count, diagnostics, initial_deferred, [])
  125   }
  126 }
  127 

Functions

[
  {
    "id": "declaration/3",
    "name": "body_result",
    "line": 7,
    "state": "covered",
    "entries": "483270"
  },
  {
    "id": "declaration/4",
    "name": "body_failure",
    "line": 16,
    "state": "covered",
    "entries": "207"
  },
  {
    "id": "declaration/5",
    "name": "body_nat_text",
    "line": 20,
    "state": "covered",
    "entries": "3918"
  },
  {
    "id": "declaration/6",
    "name": "body_call_arity_evidence",
    "line": 24,
    "state": "covered",
    "entries": "15218"
  },
  {
    "id": "declaration/7",
    "name": "body_intrinsic_value_type",
    "line": 51,
    "state": "zero",
    "entries": "0"
  },
  {
    "id": "declaration/8",
    "name": "body_intrinsic_shape",
    "line": 58,
    "state": "zero",
    "entries": "0"
  },
  {
    "id": "declaration/9",
    "name": "body_intrinsic_failure",
    "line": 63,
    "state": "zero",
    "entries": "0"
  },
  {
    "id": "declaration/10",
    "name": "body_checked_intrinsic_call",
    "line": 73,
    "state": "zero",
    "entries": "0"
  },
  {
    "id": "declaration/11",
    "name": "body_check_state_core_call",
    "line": 99,
    "state": "zero",
    "entries": "0"
  }
]

Source branch outcomes

[
  {
    "id": "declaration/6/tail/decision/true",
    "outcome": "true",
    "line": 31,
    "state": "covered",
    "hits": "11782"
  },
  {
    "id": "declaration/6/tail/decision/false",
    "outcome": "false",
    "line": 31,
    "state": "covered",
    "hits": "3436"
  },
  {
    "id": "declaration/6/tail/false/tail/0/args/3/arm/0",
    "outcome": "Some",
    "line": 39,
    "state": "covered",
    "hits": "3436"
  },
  {
    "id": "declaration/6/tail/false/tail/0/args/3/arm/1",
    "outcome": "None",
    "line": 40,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/6/tail/false/tail/0/args/5/arm/0",
    "outcome": "Some",
    "line": 44,
    "state": "covered",
    "hits": "3436"
  },
  {
    "id": "declaration/6/tail/false/tail/0/args/5/arm/1",
    "outcome": "None",
    "line": 45,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/7/tail/arm/0",
    "outcome": "None",
    "line": 53,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/7/tail/arm/1",
    "outcome": "Some",
    "line": 54,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/8/tail/decision/true",
    "outcome": "true",
    "line": 59,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/8/tail/decision/false",
    "outcome": "false",
    "line": 59,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/9/tail/decision/true",
    "outcome": "true",
    "line": 65,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/9/tail/decision/false",
    "outcome": "false",
    "line": 65,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/10/statement/3/decision/body",
    "outcome": "body",
    "line": 80,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/10/statement/3/decision/exit",
    "outcome": "exit",
    "line": 80,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/10/statement/3/body/tail/decision/true",
    "outcome": "true",
    "line": 86,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/10/statement/3/body/tail/decision/false",
    "outcome": "false",
    "line": 86,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/10/statement/3/body/tail/true/statement/0/value/decision/true",
    "outcome": "true",
    "line": 87,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/10/statement/3/body/tail/true/statement/0/value/decision/false",
    "outcome": "false",
    "line": 87,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/10/statement/3/body/tail/true/statement/0/value/false/tail/decision/true",
    "outcome": "true",
    "line": 88,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/10/statement/3/body/tail/true/statement/0/value/false/tail/decision/false",
    "outcome": "false",
    "line": 88,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/10/tail/args/0/args/3/decision/true",
    "outcome": "true",
    "line": 95,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/10/tail/args/0/args/3/decision/false",
    "outcome": "false",
    "line": 95,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/10/tail/args/0/args/3/condition/decision/evaluate-right",
    "outcome": "evaluate-right",
    "line": 95,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/10/tail/args/0/args/3/condition/decision/skip-right",
    "outcome": "skip-right",
    "line": 95,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/statement/0/value/decision/evaluate-right",
    "outcome": "evaluate-right",
    "line": 102,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/statement/0/value/decision/skip-right",
    "outcome": "skip-right",
    "line": 102,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/statement/1/value/decision/evaluate-right",
    "outcome": "evaluate-right",
    "line": 103,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/statement/1/value/decision/skip-right",
    "outcome": "skip-right",
    "line": 103,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/statement/2/value/decision/true",
    "outcome": "true",
    "line": 104,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/statement/2/value/decision/false",
    "outcome": "false",
    "line": 104,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/tail/decision/true",
    "outcome": "true",
    "line": 105,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/tail/decision/false",
    "outcome": "false",
    "line": 105,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/tail/false/statement/3/value/decision/true",
    "outcome": "true",
    "line": 111,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/tail/false/statement/3/value/decision/false",
    "outcome": "false",
    "line": 111,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/tail/false/statement/3/value/true/tail/decision/true",
    "outcome": "true",
    "line": 112,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/tail/false/statement/3/value/true/tail/decision/false",
    "outcome": "false",
    "line": 112,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/tail/false/statement/3/value/true/tail/condition/decision/evaluate-right",
    "outcome": "evaluate-right",
    "line": 112,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/tail/false/statement/3/value/true/tail/condition/decision/skip-right",
    "outcome": "skip-right",
    "line": 112,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/tail/false/statement/5/value/0/decision/true",
    "outcome": "true",
    "line": 119,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/tail/false/statement/5/value/0/decision/false",
    "outcome": "false",
    "line": 119,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/tail/false/statement/6/value/decision/true",
    "outcome": "true",
    "line": 120,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/tail/false/statement/6/value/decision/false",
    "outcome": "false",
    "line": 120,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/tail/false/tail/args/6/decision/true",
    "outcome": "true",
    "line": 124,
    "state": "zero",
    "hits": "0"
  },
  {
    "id": "declaration/11/tail/false/tail/args/6/decision/false",
    "outcome": "false",
    "line": 124,
    "state": "zero",
    "hits": "0"
  }
]

Reviewed declaration exclusions

[
  {
    "id": "declaration/0",
    "reason": "import",
    "line": 1
  },
  {
    "id": "declaration/1",
    "reason": "import",
    "line": 2
  },
  {
    "id": "declaration/2",
    "reason": "import",
    "line": 3
  }
]