gno icon indicating copy to clipboard operation
gno copied to clipboard

Recognition of `_` in Initial Condition of for Loop

Open notJoon opened this issue 2 years ago • 4 comments

Description

I'm not sure if this is a bug or a feature. In gno, using _ (underscore, typically for ignoring values) in the initialization of a for loop is accepted. Such usage is not permitted in Go syntax.

package main

func main() {
	i := 1
	for _; i < 5; i++ {
		println(i)
	}
}

// output: 1 2 3 4
// expect: cannot use _ as value or type

Note that, just like in standard Go, gno also allows leaving the initialization part of a for loop blank, as shown below:

package main

func main() {
	i := 1
	for ; i < 5; i++ {
		println(i)
	}
}

// output: 
1 
2 
3
4 

May related with op_exec

notJoon avatar Dec 19 '23 06:12 notJoon

This test reproduces the case where the loop's initialization condition is underscored, as shown below:

func TestForLoopIgnoredInital(t *testing.T) {
	m := NewMachine("test", nil)
	main := FuncD("main", nil, nil, Ss(
		A("i", ":=", "0"),
		For(
			X("_"),
			X("i < 5"),
			Inc("i"),
		),
	))
	m.RunDeclaration(main)
	m.RunMain()
}

However, I'm not sure how to test for an empty value in the initial condition.

notJoon avatar Dec 19 '23 12:12 notJoon

It probably shouldn't accept the syntax and have the same behaviour as Go, there's no good reason why the feature should be accepted in Gno. However, I think it's safe to say that if the only statement accepted in this context _ this can be considered low-priority :)

thehowl avatar Dec 19 '23 17:12 thehowl

@thehowl I didn't think there was anything wrong with it, but it was vague. Thanks for the feedback

notJoon avatar Dec 20 '23 11:12 notJoon

still an issue we should fix (I think more likely by not accepting all expressions as statements)

thehowl avatar Dec 20 '23 17:12 thehowl

This now fails with cannot use _ as value or type

thehowl avatar Jan 23 '25 15:01 thehowl