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

gccgo: -O1/2 generates an incorrect behavior #19806

Closed
trex58 opened this issue Mar 31, 2017 · 3 comments
Closed

gccgo: -O1/2 generates an incorrect behavior #19806

trex58 opened this issue Mar 31, 2017 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@trex58
Copy link

trex58 commented Mar 31, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

$ gccgo --version
gccgo (GCC) 7.0.1 20170309

$ rpm -qa | grep gcc-go
gcc-go-7.0.1-0.12.fc25.x86_64

What operating system and processor architecture are you using (go env)?

$ uname -a
Linux dorado-vm1 4.8.6-300.fc25.x86_64 #1 SMP Tue Nov 1 12:36:38 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

What did you do?

$ cat main.go
package main
import "fmt"

type MyType struct {
val int
}
func (t MyType) String() string {
return "foobar"
}
func main() {
fmt.Printf("%s\n", (*MyType)(nil))
}

$ gccgo -O2 main.go ; ./a.out

What did you expect to see?

Go Lang:
$ go version
go version go1.8 linux/amd64
$ go run main.go
<nil>

GccGo:
$ gccgo -O0 main.go ; ./a.out
<nil>

What did you see instead?

$ gccgo -O2 main.go ; ./a.out
foobar
$ gccgo -O1 main.go ; ./a.out
foobar

@ALTree
Copy link
Member

ALTree commented Mar 31, 2017

(I've edited your message to fix a few formatting problems).

@ALTree ALTree added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 31, 2017
@ALTree ALTree added this to the Gccgo milestone Mar 31, 2017
@ALTree ALTree changed the title gccgo -O1/2 generates an incorrect behavior gccgo: -O1/2 generates an incorrect behavior Mar 31, 2017
@ianlancetaylor
Copy link
Contributor

The problem is that we need to add an explicit nil test in the stub generated for a pointer type to call a value method. The test case fails because the value method does not refer to the receiver, so the copy from the pointer receiver to a value is eliminated when optimizing.

@gopherbot
Copy link

Change https://golang.org/cl/91275 mentions this issue: compiler: check for nil receiver in value method

hubot pushed a commit to gcc-mirror/gcc that referenced this issue Feb 1, 2018
    
    We already dereference the pointer to copy the value, but if the
    method does not use the value then the pointer dereference may be
    optimized away.  Do an explicit nil check so that we get the panic
    that is required.
    
    Fixes golang/go#19806
    
    Reviewed-on: https://go-review.googlesource.com/91275

	* go.go-torture/execute/printnil.go: New test.


git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@257280 138bc75d-0d04-0410-961f-82ee72b054a4
@golang golang locked and limited conversation to collaborators Feb 1, 2019
asiekierka pushed a commit to WonderfulToolchain/gcc-ia16 that referenced this issue May 16, 2022
    
    We already dereference the pointer to copy the value, but if the
    method does not use the value then the pointer dereference may be
    optimized away.  Do an explicit nil check so that we get the panic
    that is required.
    
    Fixes golang/go#19806
    
    Reviewed-on: https://go-review.googlesource.com/91275

	* go.go-torture/execute/printnil.go: New test.

From-SVN: r257280
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants