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

Fix: Allow struct fields to propagate constants #204

Merged
merged 2 commits into from
Oct 22, 2024

Conversation

katat
Copy link
Collaborator

@katat katat commented Oct 17, 2024

This is a fix to propagate the constant values from struct fields also.

This fix allows the struct fields to be passed as an constant argument for a generic function. eg:

struct Uint8 {
    inner: Field,
    bit_len: Field,
}

fn Uint8.new(val: Field) -> Uint8 {
    let bit_len = 8;

    // range check
    let ignore_ = bits::to_bits(bit_len, val);

    return Uint8 {
        inner: val,
        bit_len: bit_len
    };
}

fn Uint8.less_than(self, rhs: Uint8) -> Bool {
    // this won't work without this PR fix
    return comparator::less_than(self.bit_len, self.inner, rhs.inner);
}

@katat katat changed the base branch from main to feat/init-stdlib October 17, 2024 12:56
@katat katat requested a review from gio54321 October 17, 2024 12:57
@katat katat mentioned this pull request Oct 18, 2024
2 tasks
Copy link
Contributor

@gio54321 gio54321 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, I did not find any issues in the implementation, this is definitely a fix worth merging in the stdlib PR 😄

I'm wondering, defining Uint8 using a structure allows an user to modify the bit length, which maybe is not necessarily what we want. Perhaps it is better to define it using a constant BIT_LEN so that we ensure that it is fixed in the monomorphization phase.
Afaiu, if the user using Uint8 do modify the bit length with another constant, the monomorphization would still pass with the new bit length, but maybe I'm wrong.

@katat
Copy link
Collaborator Author

katat commented Oct 22, 2024

Indeed it is potentially an issue when it comes to the fact that the users can modify the field, such as bit_len, which supposes to be a constant and can't be changed once the struct is declared. There is an ongoing discussion on this issue: #194

@katat katat merged commit 18825a3 into feat/init-stdlib Oct 22, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants