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 reassignment of struct members in impl self #1173

Merged
merged 2 commits into from
Apr 7, 2022

Conversation

mohammadfawaz
Copy link
Contributor

@mohammadfawaz mohammadfawaz commented Apr 7, 2022

Closes: #1169
When looking for the value to reassign, also look for it in the argument list of the function.

@mohammadfawaz mohammadfawaz requested a review from otrho April 7, 2022 00:05
@mohammadfawaz mohammadfawaz self-assigned this Apr 7, 2022
@mohammadfawaz mohammadfawaz added bug Something isn't working compiler General compiler. Should eventually become more specific as the issue is triaged labels Apr 7, 2022
entry:
v0 = const { u64 } { u64 undef }, !1
v1 = const u64 0, !2
v2 = insert_value v0, { u64 }, v1, 0, !1
Copy link
Contributor Author

@mohammadfawaz mohammadfawaz Apr 7, 2022

Choose a reason for hiding this comment

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

@otrho this looks a bit weird to me even though it's functional. I understand that this IR is before the optimizer runs, but the fact that we're inserting into a const seems odd. Is this expected?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, conceptually I was thinking functional style, so insert_value isn't modifying the original, but creating a new value. v2 is not a modified v0, it's a whole new value. But this will go away with the struct refactor and everything goes on the stack.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, yeah that makes sense. This is SSA anyways.. I don't know what I was thinking 😂

@@ -15,18 +15,18 @@ script {

while_body:
v4 = get_ptr mut ptr bool a, ptr bool, 0, !4
v5 = load ptr v4, !4
cbr v5, block0, block1, !5
v5 = get_ptr mut ptr bool a, ptr bool, 0, !5
Copy link
Contributor Author

@mohammadfawaz mohammadfawaz Apr 7, 2022

Choose a reason for hiding this comment

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

I think these changes are due to the fact that we're now creating get_ptr before compiling the RHS.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, v4 is used way down below for the store in block1, where the old get_ptr was, which is a bit unfortunate.

Copy link
Contributor

@otrho otrho left a comment

Choose a reason for hiding this comment

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

I've come across a slightly different scenario where the AST is saying that the following:

fn f(a: u64) {
    let a = a + 1;
    ...
}

Is a re-assignment, where IMO this is a new declaration which is shadowing the function argument. I didn't raise an issue at the time though...

This change is necessary, because it (e.g. self.a = 0 from the test) is a genuine re-assignment, but it'll also hide the above issue. But maybe the above issue isn't an issue.

@@ -15,18 +15,18 @@ script {

while_body:
v4 = get_ptr mut ptr bool a, ptr bool, 0, !4
v5 = load ptr v4, !4
cbr v5, block0, block1, !5
v5 = get_ptr mut ptr bool a, ptr bool, 0, !5
Copy link
Contributor

Choose a reason for hiding this comment

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

OK, v4 is used way down below for the store in block1, where the old get_ptr was, which is a bit unfortunate.

@mohammadfawaz
Copy link
Contributor Author

I've come across a slightly different scenario where the AST is saying that the following:

fn f(a: u64) {
    let a = a + 1;
    ...
}

Is a re-assignment, where IMO this is a new declaration which is shadowing the function argument. I didn't raise an issue at the time though...

This change is necessary, because it (e.g. self.a = 0 from the test) is a genuine re-assignment, but it'll also hide the above issue. But maybe the above issue isn't an issue.

You're right.. a new variable is being declared via let so this is certainly not a reassignment.

@mohammadfawaz mohammadfawaz merged commit b0ab3f8 into master Apr 7, 2022
@mohammadfawaz mohammadfawaz deleted the mohammadfawaz/1169 branch April 7, 2022 13:14
@mohammadfawaz
Copy link
Contributor Author

mohammadfawaz commented Apr 7, 2022

I've come across a slightly different scenario where the AST is saying that the following:

fn f(a: u64) {
    let a = a + 1;
    ...
}

Is a re-assignment, where IMO this is a new declaration which is shadowing the function argument. I didn't raise an issue at the time though...
This change is necessary, because it (e.g. self.a = 0 from the test) is a genuine re-assignment, but it'll also hide the above issue. But maybe the above issue isn't an issue.

You're right.. a new variable is being declared via let so this is certainly not a reassignment.

@otrho I looked at the AST of the following code:

fn foo(a: u64) {
    let a = a + 1;
}

fn main() {
    foo(5);
}

and I don't see a Reassignment in the AST 🤔 , and the IR has a new local ptr with an _ as expected:

local ptr u64 a_

@otrho
Copy link
Contributor

otrho commented Apr 7, 2022

Argh. It was something like that..! I think it was in the enum_if_let_large_types test. Doesn't matter for now, if it becomes and issue we can deal with it then, there's plenty of other stuff to do for now. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler General compiler. Should eventually become more specific as the issue is triaged
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Basic impl can't find self.
2 participants