Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert statements can only accept string literals #3113

Closed
jfecher opened this issue Oct 11, 2023 · 1 comment · Fixed by #4101 or #4219
Closed

Assert statements can only accept string literals #3113

jfecher opened this issue Oct 11, 2023 · 1 comment · Fixed by #4101 or #4219
Assignees
Labels
bug Something isn't working
Milestone

Comments

@jfecher
Copy link
Contributor

jfecher commented Oct 11, 2023

Aim

Trying to write an Option::expect method:

fn expect<N>(self, message: str<N>) -> T {
    assert(self.is_some(), message);
    self._value
}

Expected Behavior

The function to typecheck and execute properly.

Bug

warning: unused variable error_message
   ┌─ std/option.nr:35:28
   │
35 │     pub fn expect<N>(self, error_message: str<N>) -> T {
   │                            ------------- unused variable 
   │

error: Assert statements can only accept string literals
   ┌─ std/option.nr:36:9
   │
36 │         assert(self._is_some, error_message);
   │         ------------------------------------
   │

Aborting due to 1 previous error

To Reproduce

Installation Method

None

Nargo Version

No response

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@jfecher jfecher added bug Something isn't working P-LOW labels Oct 11, 2023
@kevaundray kevaundray added this to the 1.0 milestone Jan 15, 2024
@kevaundray
Copy link
Contributor

@vezenovm this seems related to the runtime error messages we mentioned before

github-merge-queue bot pushed a commit that referenced this issue Feb 1, 2024
# Description

## Problem\*

Resolves #3113

## Summary\*

The first major change is a new MacroProcessor that generates an oracle
and the respective unconstrained oracle wrapper for resolving assert
message expressions. This was done as to prevent exposing the resolve
assert message funcs in the stdlib directly.

Before an assertion with a dynamic assert message we perform a foreign
call out to the user with the format-string and the values to be
formatted. The user must then return what the assert message should look
like. This is then cached on by nargo when executing the ACVM and should
the next opcode fail to execute then this error message will be returned
to the user.

We now require end-users (people running ACVM) to format their own
assert messages.

Example output for `assert_msg_runtime` test:
<img width="737" alt="Screenshot 2024-01-23 at 4 07 18 PM"
src="https://github.com/noir-lang/noir/assets/43554004/96879ab1-5192-4b01-b798-4a63631bd270">

Assert message still work in the stdlib as expected:
<img width="780" alt="Screenshot 2024-01-23 at 4 43 47 PM"
src="https://github.com/noir-lang/noir/assets/43554004/1ad0c00b-7d5f-4a3f-94d8-955589f487c2">

Output for debug logs when putting `assert(x == y, struct_string);` on
line 48 immediately following `std::println(struct_string);`:
<img width="1040" alt="Screenshot 2024-01-23 at 4 47 55 PM"
src="https://github.com/noir-lang/noir/assets/43554004/6a8220b8-dd19-4e1b-b360-976a6952a36f">


## Additional Context

## Documentation\*

Check one:
- [ ] No documentation needed.
- [x] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
Co-authored-by: Tom French <tom@tomfren.ch>
github-merge-queue bot pushed a commit that referenced this issue Feb 1, 2024
# Description

## Problem\*

#4101 resolves
#3113 which was preventing an
expect function on our Option struct.

## Summary\*

This adds an `expect` method and a test inside of `compile_failure`.
Here is the example for the test under `compile_failure/option_expect`:
<img width="716" alt="Screenshot 2024-01-31 at 11 12 51 AM"
src="https://github.com/noir-lang/noir/assets/43554004/1a84d489-bd7f-446a-96be-ebf63ae63f47">



## Additional Context



## Documentation\*

Check one:
- [ ] No documentation needed.
- [x] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
Co-authored-by: Tom French <tom@tomfren.ch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
4 participants