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

Unquoting a numeric type fails to parse #6077

Closed
Thunkar opened this issue Sep 18, 2024 · 0 comments · Fixed by #6079
Closed

Unquoting a numeric type fails to parse #6077

Thunkar opened this issue Sep 18, 2024 · 0 comments · Fixed by #6079
Labels
bug Something isn't working

Comments

@Thunkar
Copy link
Contributor

Thunkar commented Sep 18, 2024

Aim

Use array types in quoted values

global A_GLOBAL = 3;

struct WeirdStruct<T, U> {
    a: T,
    b: U,
}

#[mangle_fn]
fn my_fn() -> [u8; A_GLOBAL] {
    [0; A_GLOBAL]
}

comptime fn mangle_fn(f: FunctionDefinition) {
    let return_type = f.return_type();

    // I have to construct this as a quoted value for my use case, cannot use the type directly
    let generics = f"Field,{return_type}".quoted_contents();
    // This prints the faulty type <Field,[u8; (3: numeric Field)]>
    println(generics);
    let new_return_type = quote { WeirdStruct<$generics>}.as_type();

    let new_body = quote {
        {
            WeirdStruct { a: 1, b: [0;3] }
        }
    }.as_expr().unwrap();

    f.set_return_type(new_return_type);
    f.set_body(new_body);
}

Expected Behavior

The above example should compile

Bug

error: Failed to parse macro's token stream into a type
   ┌─ src/main.nr:19:27
   │
19 │     let new_return_type = quote { WeirdStruct<$generics>}.as_type();
   │                           -------------------------------
   │                           │
   │                           Unexpected UnquoteMarker(ExprId(Index(44777))), expected one of bool, crate, CtString, dep, Expr, Field, fn, fmtstr, FunctionDefinition, impl, Module, Quoted, str, StructDefinition, super, TopLevelItem, TraitConstraint, TraitDefinition, TraitImpl, TypedExpr, Type, unconstrained, UnresolvedType, <, &, (, [, integer type, type expression, Ident, QuotedType, InternedUnresolvedTypeData 

To Reproduce

Use the example

Workaround

Yes

Workaround Description

    let fn_return_type_quoted = if fn_return_type.as_array().is_some() {
        let (element_type, array_len) = fn_return_type.as_array().unwrap();
        let array_len = array_len.as_constant().unwrap();
        quote { [$element_type; $array_len] }
    } else {
        quote { $fn_return_type }
    };

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

@Thunkar Thunkar added the bug Something isn't working label Sep 18, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Sep 18, 2024
github-merge-queue bot pushed a commit that referenced this issue Sep 18, 2024
# Description

## Problem\*

Resolves #6077

## Summary\*

The change made to `impl Debug for Type` to display kinds differently
was breaking the use case of formatting types into a format string then
using `.quoted_contents()` to quote the contents of the string.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** 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.
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Sep 18, 2024
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
Development

Successfully merging a pull request may close this issue.

1 participant