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

cmd/compile: different behavior between using composite literal and using make function to create slices #34682

Closed
zigo101 opened this issue Oct 3, 2019 · 4 comments

Comments

@zigo101
Copy link

zigo101 commented Oct 3, 2019

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

$ go version
go version go1.13.1 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

package main

import "fmt"

func f() {
	fmt.Println("============= f")
	type S []S
	var s S
	s = make(S, 1); s[0] = s // this line is different
	fmt.Printf("%p\n", &s)
	fmt.Println(len(s)) // 1
	s = s[0]
	fmt.Printf("%p\n", &s)
	fmt.Println(len(s)) // 1
	s = s[0] // ok
}

func g() {
	fmt.Println("============= g")
	type S []S
	var s S
	s = S{0: s} // this line is different
	fmt.Printf("%p\n", &s) // 0xabcd
	fmt.Println(len(s))    // 1
	s = s[0]
	fmt.Printf("%p\n", &s) // 0xabcd
	fmt.Println(len(s))    // 0
	s = s[0] // panic
}

func main() {
	f()
	g()
}

What did you expect to see?

Not crash.

What did you see instead?

Crashes.

@zigo101
Copy link
Author

zigo101 commented Oct 3, 2019

A simpler one:

package main

func main() {
	type S []S
	var s S
	s = S{0: s} // this line is different
	px := &s
	_ = (*px)[0] // ok

	s = s[0]

	_ = (*px)[0] // panic
}

@zigo101
Copy link
Author

zigo101 commented Oct 3, 2019

for both gc and gccgo.

@zigo101
Copy link
Author

zigo101 commented Oct 3, 2019

Sorry, maybe the g function in the first comment is partially normal, the f and g functions really have some differences. But there are still some weird things.

@zigo101
Copy link
Author

zigo101 commented Oct 3, 2019

closed. No bugs here. :)

@zigo101 zigo101 closed this as completed Oct 3, 2019
@golang golang locked and limited conversation to collaborators Oct 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants