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

cgo: use true instead of 0 == 0 in the generated code #31109

Closed
ainar-g opened this issue Mar 28, 2019 · 4 comments
Closed

cgo: use true instead of 0 == 0 in the generated code #31109

ainar-g opened this issue Mar 28, 2019 · 4 comments
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.

Comments

@ainar-g
Copy link
Contributor

ainar-g commented Mar 28, 2019

Inspired by dominikh/go-tools#430.

CL 142883 made it so that the code generated by cgo contains an always–true conditional 0 == 0 instead of the predefined identifier true. According to the comment, it is made to account for the case when true is redefined. I don't think that it was reasonable. Firstly, it still doesn't account for all cases of “stupid shadowing”, for example I can still define

_cgoCheckPointer := 0

in my code and break cgo. Secondly, do we really need to account for that? Wouldn't a message like “identifier foo must not be shadowed in cgo code” do the job?

@ainar-g
Copy link
Contributor Author

ainar-g commented Mar 28, 2019

An interesting way to combat shadowing would be a synthetic package:

package builtinbackup

const (
	True  = true
	False = false
)

func CGoCheckPointer( /* … */ ) { /* … */ }

It could then be used in the generated code like this:

builtinbackup.CGoCheckPointer(builtinbackup.CGoBase0, builtinbackup.True)

@ALTree ALTree added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Mar 28, 2019
@bradfitz
Copy link
Contributor

Why go through all that work either with a new package or a new warning message when 0 == 0 does the trick?

@ianlancetaylor
Copy link
Member

I can't see any reason to change working code to make it work less well.

There is no reason to run a static checker on cgo output.

If I could change cgo to avoid names like _cgoCheckPointer, I would. But I don't see a way to do that. And it seems more reasonable for cgo to reserve names that start with _cgo than for it to reserve the name true.

@dominikh
Copy link
Member

There is no reason to run a static checker on cgo output.

That isn't quite correct. We definitely want to check code that uses cgo, and we can't check cgo input since it isn't valid Go. So we do have to check the output.

It is, however, much simpler to add a special case to the check than to change how cgo generation works.

@golang golang locked and limited conversation to collaborators Mar 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

6 participants