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: avoid node overriding and re-instantiate a same function #198

Merged
merged 4 commits into from
Oct 17, 2024

Conversation

katat
Copy link
Collaborator

@katat katat commented Oct 9, 2024

Changes:

  1. Since every functions need to be instantiated, the ctx.generic_func_scope is not necessary for determining whether it needs to generate new node id. Thus, ctx.generic_func_scope is removed to avoid unexpected behavior in terms of handling the node ids at mast phase.

  2. A fix to avoid re-instantiate a same function.

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!
Left a couple of comments as it's not clear to me the usage of the mapping

src/mast/mod.rs Outdated
..expr.clone()
};
// check if this function is already monomorphized
if ctx.functions_instantiated.contains_key(&old_qualified) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be checked to contain the new signature as a key? I think in the code below the zip function is monomorphized two times, even if the LEN parameter is the same. Though, maybe I'm missing the point of the PR 😄

fn zip(a1: [Field; LEN], a2: [Field; LEN]) -> [[Field; 2]; LEN] {
    let mut result = [[0; 2]; LEN];
    for index in 0..LEN {
        result[index] = [a1[index], a2[index]];
    }
    return result;
}

fn main(pub arr: [Field; 3]) -> Field {
    let expected = [1, 2, 3];
    for pair in zip(arr, expected) {
        assert_eq(pair[0], pair[1]);
    }
    for pair in zip(arr, expected) {
        assert_eq(pair[0], pair[1]);
    }

    return arr[0];
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh yeah, indeed. It should always check the monomorphized name.

I have just pushed a fix that also includes a refactor to better encapsulate the resolutions for the generic values and types. This refactor makes it always resolves the types so as to get the monomorphized qualified name, without having to re-instantiate a function.

src/mast/mod.rs Outdated
// check if this function is already monomorphized
if ctx
.methods_instantiated
.contains_key(&(struct_qualified.clone(), method_name.value.clone()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as before

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.

LGTM now! Also very nice refactoring 😃

@katat katat merged commit c223380 into main Oct 17, 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