Skip to content

Unconstrained function call only fails when input is constant #7372

@michaeljklein

Description

@michaeljklein
pub unconstrained fn foo(input: Field) -> Field {
    // moving the `!= 0` from `bar` to here fixes the panic:
    (input == 0) as Field
}

pub fn bar(input: Field) {
    // Safety: test
    let output = unsafe { foo(input) };

    // removing the empty assert message fixes the panic:
    assert(output != 0, "");
}

mod test {
    use crate::bar;

    // this panics:
    #[test]
    fn test_local() {
        bar(0);
    }

    global x_global: Field = 0;

    // this panics:
    #[test]
    fn test_global() {
        bar(x_global);
    }

}

// it works when calling or inlining `bar` in `main` (with `input = 0`)
fn main(input: Field) {
    bar(input)
}

Expected Behavior

Expected the tests to pass, as they do with nargo from noirup:

~/.nargo/bin/nargo --version
nargo version = 1.0.0-beta.2
noirc version = 1.0.0-beta.2+97afa52f5212be2d05af26b9e8dde9c3ea7a1d2e
(git version hash: 97afa52f5212be2d05af26b9e8dde9c3ea7a1d2e, is dirty: false)

❯ ~/.nargo/bin/nargo test

[regression_noir_sort] Running 2 test functions
[regression_noir_sort] Testing test::test_local ... ok
[regression_noir_sort] Testing test::test_global ... ok
[regression_noir_sort] 2 tests passed

Bug

The tests fail with overflow errors:

❯ cargo run test
   Compiling noir_debugger v1.0.0-beta.4 (/home/michael/coding/rust/noir/tooling/debugger)
   Compiling nargo_cli v1.0.0-beta.4 (/home/michael/coding/rust/noir/tooling/nargo_cli)
    Finished `dev` profile [optimized + debuginfo] target(s) in 6.15s
     Running `/home/michael/coding/rust/noir/target/debug/nargo test`
[regression_7372] Running 2 test functions
The application panicked (crashed).
Message:  attempt to subtract with overflow
Location: compiler/noirc_evaluator/src/acir/acir_context/generated_acir/mod.rs:646

This is a bug. We may have already fixed this in newer versions of Nargo so try searching for similar issues at https://github.com/noir-lang/noir/issues/.
If there isn't an open issue for this bug, consider opening one at https://github.com/noir-lang/noir/issues/new?labels=bug&template=bug_report.yml
[regression_7372] Testing test::test_local ... FAIL
attempt to subtract with overflow

The application panicked (crashed).
Message:  attempt to subtract with overflow
Location: compiler/noirc_evaluator/src/acir/acir_context/generated_acir/mod.rs:646

This is a bug. We may have already fixed this in newer versions of Nargo so try searching for similar issues at https://github.com/noir-lang/noir/issues/.
If there isn't an open issue for this bug, consider opening one at https://github.com/noir-lang/noir/issues/new?labels=bug&template=bug_report.yml
[regression_7372] Testing test::test_global ... FAIL
attempt to subtract with overflow


[regression_7372] Failures:
     test::test_local
     test::test_global

[regression_7372] 2 tests failed

Where the panicking line is in GeneratedAcir::last_acir_opcode_location:

OpcodeLocation::Acir(self.opcodes.len() - 1)

To Reproduce

Workaround

None

Workaround Description

No response

Additional Context

No response

Project Impact

None

Blocker Context

No response

Nargo Version

No response

NoirJS Version

No response

Proving Backend Tooling & Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions