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

Typed iota value changes depending on declaration order #1596

Open
dennwc opened this issue Sep 26, 2023 · 1 comment
Open

Typed iota value changes depending on declaration order #1596

dennwc opened this issue Sep 26, 2023 · 1 comment

Comments

@dennwc
Copy link
Contributor

dennwc commented Sep 26, 2023

The following program sample.go triggers an unexpected result

package main

const (
	A = Enum(iota)
	B
	C
)

type Enum int

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

// Output:
// 0 1 2

Expected result

$ go run ./sample.go
// 0 1 2

Got

$ yaegi ./sample.go
// 0 2 4

Yaegi Version

f5b5481

Additional Notes

Moving type Enum int before const fixes the issue.

@dennwc
Copy link
Contributor Author

dennwc commented Sep 26, 2023

I'm using this case currently to check the difference:

package main

const (
	A1 = Enum1(iota)
	A2
	A3
)

type Enum1 int

type Enum2 int

const (
	B1 = Enum2(iota)
	B2
	B3
)

func main() {
	println(A1, A2, A3)
	println(B1, B2, B3)
}

Expected output:

0 1 2
0 1 2

Actual output:

0 2 4
0 1 2

The Enum2 is correctly handled by gta, nothing interesting there. The type is registered upfront, so constant can be computed immediately.

However for Enum1 case it fails to compute the constant, thus pushing it to revisit list. Later is hits both CFG iota increment and GTA iota increment.

Actually, completely removing this GTA iota reset/increment code passes all the tests including the new one, so I'm wondering if it's needed there at all 🤔

yaegi/interp/gta.go

Lines 103 to 107 in f5b5481

if childPos(n) == len(n.anc.child)-1 {
sc.iota = 0
} else {
sc.iota++
}

@mvertes any thoughts on this?

For now I'm not sure how to prevent the second increment from running, since constant value is assigned after both CFG and GTA increments run.

Maybe we could track the last node that caused the increment in the scope? Dropping GTA increment sounds dangerous, even though the tests are good.

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

1 participant