aiken-lang / aiken

A modern smart contract platform for Cardano
https://aiken-lang.org
Apache License 2.0
454 stars 84 forks source link

Invalid error reported in `expect` expression for a public non-opaque type #878

Closed mpetruska closed 5 months ago

mpetruska commented 5 months ago

What Git revision are you using?

Installed via nix shell github:aiken-lang/aiken#aiken.

daken [ master]
✖1 ❯ aiken --version
aiken v1.0.24-alpha+b09e031

What operating system are you using, and which version?

Describe what the problem is?

Invalid error reported when compiling code in repo https://github.com/Danogo2023/daken :

daken [ master]
❯ aiken build
    Compiling danogo2023/daken 1.0.2 (/home/mark/Anastasia/audited/Danogo/GitHub/daken)
    Compiling aiken-lang/stdlib 1.7.0 (/home/mark/Anastasia/audited/Danogo/GitHub/daken/build/packages/aiken-lang-stdlib)
        Error aiken::check::illegal::expect_on_opaque (link)

  × While trying to make sense of your code...
  ╰─▶ I caught an opaque type possibly breaking its abstraction boundary.

     ╭─[/home/mark/Anastasia/audited/Danogo/GitHub/daken/lib/bond/utils.ak:117:1]
 117 │                         Some(dt) -> {
 118 │                           expect escrow_dt: EscrowDatum = dt
     ·                                                           ─┬
     ·                                                            ╰── reckless opaque cast
 119 │                           if n == escrow_dt.token_name {
     ╰────
  help: This expression is trying to convert something unknown into an opaque type. An opaque type is a data-type which hides its internal details; usually because it enforces some specific invariant on its internal structure. For example, you might define
        a Natural type that holds an Integer but ensures that it never gets negative.

        A direct consequence means that it isn't generally possible, nor safe, to turn *any* value into an opaque type. Instead, use the constructors and methods provided for lifting values into that opaque type while ensuring that any structural invariant
        is checked for.

      Summary 1 error, 0 warnings

as the type EscrowDatum is not opaque:

pub type EscrowDatum {
...
}

What should be the expected behavior?

This expression should not be reported as an error.

KtorZ commented 5 months ago

Does EscrowDatum contains any opaque type? Could you provide its whole definition ?

mpetruska commented 5 months ago

It seems to me it does not. It is in a public repo: https://github.com/Danogo2023/daken/blob/master/lib%2Fbond%2Ftypes.ak#L91

KtorZ commented 5 months ago

Value is opaque. It maintains specific invariants and thus can't be cast directly from Data. So the compiler is right to prevent this.

mpetruska commented 5 months ago

I see. Thanks for pointing it out.