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

Operator precedence is wrong when casted value is left-shifted in if/match expr #19978

Closed
lemoncmd opened this issue Nov 23, 2023 · 6 comments · Fixed by #19985
Closed

Operator precedence is wrong when casted value is left-shifted in if/match expr #19978

lemoncmd opened this issue Nov 23, 2023 · 6 comments · Fixed by #19985
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: Parser Bugs/feature requests, that are related to the V parser or syntax (*not* semantic ones).

Comments

@lemoncmd
Copy link
Contributor

lemoncmd commented Nov 23, 2023

Describe the bug

I'm so afraid of this bug that I can't even use any expression

Reproduction Steps

println(u8(1) << 1 | 1)
println(match 0 {
        0 { u8(1) << 1 | 1 }
        else { 0 }
})
println(if 0 == 0 {
        u8(1) << 1 | 1
} else {
        0
})

Expected Behavior

3
3
3

Current Behavior

3
2
2

u8(1) << (1 | 1) is calculated instead (why?)

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.3 043ebb8

Environment details (OS name and version, etc.)

OS: Arch Linux

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@lemoncmd lemoncmd added the Bug This tag is applied to issues which reports bugs. label Nov 23, 2023
@lemoncmd lemoncmd changed the title Operator precedence is wrong when casted value is left-shifted in match expr Operator precedence is wrong when casted value is left-shifted in if/match expr Nov 23, 2023
@felipensp
Copy link
Member

felipensp commented Nov 23, 2023

Code generated:

VV_LOCAL_SYMBOL void main__main(void) {
        println(u8_str(((((u8)(1)) << 1) | 1)));
        println(u8_str(((0 == (0))? ((((u8)(1)) << 1)) : (0))));
        println(u8_str((true ? ((((u8)(1)) << 1)) : (0))));
}

So it is not about precedence, but not taking care of the whole expression when parsing.

@felipensp felipensp added the Unit: cgen Bugs/feature requests, that are related to the default C generating backend. label Nov 23, 2023
@lemoncmd
Copy link
Contributor Author

lemoncmd commented Nov 23, 2023

a := 1
println(match 0 {
        0 { u8(1) << 1 | a }
        else { 0 }
})

generates

VV_LOCAL_SYMBOL void main__main(void) {
        int a = 1;
        println(u8_str(((0 == (0))? ((((u8)(1)) << (1 | a))) : (0))));
}

so it is about precedence

@felipensp
Copy link
Member

Ah right. I've checked it with:

fn main() {
	println(if false {
			u8(1) << 1 | 2
	} else {
			0
	})
}

Generates:
println(u8_str((false ? ((((u8)(1)) << 3)) : (0))));

@felipensp felipensp added Unit: Parser Bugs/feature requests, that are related to the V parser or syntax (*not* semantic ones). and removed Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Nov 23, 2023
@Krchi
Copy link

Krchi commented Nov 24, 2023

I have the same problem, I handled it locally, but it still has problem in array

fn test_test() {
	mut arr := [u8(1), 9]
	arr << 3
	t := if true {
		arr[0] << 1 | arr[1]
	} else {
		1
	}
	println(t) // print 0, should be 11
}

u8(id) is CastExpr, but arr[0] is IndexExpr, I cant get what type store in array

@lemoncmd
Copy link
Contributor Author

lemoncmd commented Nov 25, 2023

I found it radically difficult to solve this kind of problem:

mut a := [0]
b := 1
c := match 0 {
    0 {
        a << 1 | 1 // compiler have to parse it as a << (1 | 1)
        b << 1 | 1 // compiler have to parse it as (b << 1) | 1
    }
    else {
        0
    }
}

This is completely context-sensitive.

@felipensp
Copy link
Member

I found it radically difficult to solve this kind of problem:

mut a := [0]
b := 1
c := match 0 {
    0 {
        a << 1 | 1 // compiler have to parse it as a << (1 | 1)
        b << 1 | 1 // compiler have to parse it as (b << 1) | 1
    }
    else {
        0
    }
}

This is completely context-sensitive.

Yes, for Ident it is. But for the CastExpr treated here it is okay for now.

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: Parser Bugs/feature requests, that are related to the V parser or syntax (*not* semantic ones).
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants