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

Incorrect bitmask parsing or handling behaviour #1591

Open
zoola969 opened this issue Sep 4, 2023 · 3 comments
Open

Incorrect bitmask parsing or handling behaviour #1591

zoola969 opened this issue Sep 4, 2023 · 3 comments

Comments

@zoola969
Copy link

zoola969 commented Sep 4, 2023

The following program sample.go triggers an unexpected result

package main

import (
	"encoding/json"
)

type BitFlag uint8

const (
	A BitFlag = 1 << iota
	B
)

type Flag struct {
	Flag BitFlag `json:"flag"`
}

func main() {
	json_str := "{\"flag\": 1}"
	var d Flag
	json.Unmarshal([]byte(json_str), &d)
	println(
		d.Flag,
		d.Flag&A == A,
		BitFlag(1)&A == A,
	)
}

Expected result

$ go run ./sample.go 
1 true true

Got

$ yaegi ./sample.go  
1 false true

Yaegi Version

v0.15.1

Additional Notes

No response

@zoola969 zoola969 changed the title Incorrect bitmask parsing or handling behaivour Incorrect bitmask parsing or handling behaviour Sep 4, 2023
@dennwc
Copy link
Contributor

dennwc commented Sep 26, 2023

Confirm that it still happens on master.

I actually hit something very similar recently, but with a regular iota and typed constants:

type Enum int
const (
   A = Enum(iota)
   B
   C
)

This produced A=0, B=2, C=4 for unknown reason, breaking the code that assumed it's sequential.

I will try to make a similar small reproducer. For now I was unable to, so it might be caused by something other code in the package. Probably something interferes with these const definitions.

@mvertes Could you please tag the issue? Looks important. I will try to fix it.

@dennwc
Copy link
Contributor

dennwc commented Sep 26, 2023

Actually, here's a small reproducer for my case:

package main

const (
	A = Enum(iota)
	B
	C
)

type Enum int

func main() {
	println(A, B, C)
}

This should produce 0 1 2, but produces 0 2 4 instead. Moving type Enum int before const fixes the problem.

@dennwc
Copy link
Contributor

dennwc commented Sep 26, 2023

Sorry for the noise, my issue appears to be different. Opened #1596.

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

No branches or pull requests

2 participants