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

Better line indication where compiler found a channel push syntax error #23571

Open
2 tasks
jorgeluismireles opened this issue Jan 24, 2025 · 1 comment
Open
2 tasks

Comments

@jorgeluismireles
Copy link

jorgeluismireles commented Jan 24, 2025

Describe the feature

In Golang the channel push statement <- ch is valid. In V is mandatory to have _ := <-ch but unexpectedly this also is accepted in V:

_ :=


<-

ch

This assignation valid after several rows and spaces creates problems with the line the compiler indicates as error.

Use Case

Next program has a V syntax error in line 20 <-ch and the compiler notifies the error far before in line 12 thinking there is the receiver of invalid type:

fn main() {
	result := chan string{}
	spawn fn(result chan string){
		// ...
	}(result)
	spawn start(result)	
}

fn start(result chan string) {
	ok := chan bool{}
	spawn read(result, ok)
	write(5) or { // compiler says error is here
		// some
		// extra
		// comments
		// or code
		result<- 'err:${err}'
		ok<- false
	}
	<-ok // syntax error is here
}

fn read(result chan string, ok chan bool) {
	// ...
}

fn write(n int) ! {
	// ...
}

Produces

code.v:12:2: error: cannot push on non-channel `void`
   10 |     ok := chan bool{}
   11 |     spawn read(result, ok)
   12 |     write(5) or {
      |     ~~~~~~~~
   13 |         // bla
   14 |         // bla
Exited with error status 1

It takes time to realize where the problem really is.

Proposed Solution

Improve the error line like this, showing the line of the <- operator.

code.v:20:2: error: cannot push on non-channel `void`
   19 |     }
   20 |     <-ok
      |     ~~~~~
   21 |}

Then the fix is done in line 20:

_ := <-ok

Other features to consider:

Accept as valid (as in Golang)

<- ch

Reject as invalid having the receiver and producer in different lines:

_ := 


<-

 ch

Other Information

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Version used

V 0.4.9 991ec1f

Environment details (OS name and version, etc.)

https://play.vlang.io/p/e14013a25d

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.

Copy link

Connected to Huly®: V_0.6-21997

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