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

result should be true, with autofree result is false #7337

Closed
danieldaeschle opened this issue Dec 15, 2020 · 12 comments · Fixed by #11182
Closed

result should be true, with autofree result is false #7337

danieldaeschle opened this issue Dec 15, 2020 · 12 comments · Fixed by #11182
Labels
Bug This tag is applied to issues which reports bugs. Unit: Memory Management Bugs/feature requests, that are related to the memory management of the compiler.

Comments

@danieldaeschle
Copy link
Member

What did you do?

fn main() {
	mut a := true
	b := a && if true {
		a = false
		true
	} else {
		false
	}
	println(b)
}

What did you expect to see?
true

What did you see instead?

➜  v git:(master) ✗ v run main.v
true
➜  v git:(master) ✗ v -autofree run main.v
false
@danieldaeschle danieldaeschle added the Bug This tag is applied to issues which reports bugs. label Dec 15, 2020
@danieldaeschle
Copy link
Member Author

Related to: #4401

@danieldaeschle danieldaeschle added the Unit: Memory Management Bugs/feature requests, that are related to the memory management of the compiler. label Jan 8, 2021
@danieldaeschle
Copy link
Member Author

This might be a solution:

#include <stdio.h>

#define bool int
#define false 0
#define true 1

int main(int argc, char argv[]) {
    bool a = true;
    bool _tmp_a = a;
    bool _tmp_c;
    if (true) {
        a = false;
        _tmp_c = true;
    } else {
        _tmp_c = false;
    }
    bool b = _tmp_a && _tmp_c;
    printf("%s\n", b ? "true" : "false");
    return 0;
}

@danieldaeschle
Copy link
Member Author

@yuyi98 Do you want to solve this? We also can get rid of the ternary generation completely if this works.

@yuyi98
Copy link
Member

yuyi98 commented Aug 7, 2021

@danieldaeschle Thanks! I'll try. But we cannot get rid of the ternary generation completely, because temporary variables generated before the if_expr statement can sometimes cause logic errors.

@danieldaeschle
Copy link
Member Author

The ternary stuff is not able to handle all cases, so we need to get rid of it. I think there are solutions to the temporary variable stuff but it's very hard to implement.

@yuyi98
Copy link
Member

yuyi98 commented Aug 7, 2021

@danieldaeschle The solution you give will be logic error in the following cases.

fn main() {
	mut a := true
	b := if true {
		a = false
		true
	} else {
		false
	} && a
	println(b)
}

@yuyi98
Copy link
Member

yuyi98 commented Aug 7, 2021

But this is really an effective idea that needs to be perfected.

@yuyi98
Copy link
Member

yuyi98 commented Aug 8, 2021

@danieldaeschle Can we disable this usage?
if_expr (is_expr) can only be used in a single statement.

@danieldaeschle
Copy link
Member Author

To me, disabling this usage would be a bad solution because in other languages it works.

@yuyi98
Copy link
Member

yuyi98 commented Aug 8, 2021

Ok, I see.

@yuyi98
Copy link
Member

yuyi98 commented Aug 8, 2021

@danieldaeschle Your solution can work for it.

#include <stdio.h>

#define bool int
#define false 0
#define true 1

int main(int argc, char argv[]) {
    bool a = true;
    bool _tmp_c;
    if (true) {
        a = false;
        _tmp_c = true;
    } else {
        _tmp_c = false;
    }
    bool _tmp_a = a;
    bool b =  _tmp_c && _tmp_a;
    printf("%s\n", b ? "true" : "false");
    return 0;
}

@danieldaeschle
Copy link
Member Author

Yes, but this needs a lot of forelooking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: Memory Management Bugs/feature requests, that are related to the memory management of the compiler.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants