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
go101 opened this issue Oct 3, 2019 · 4 comments

Comments

@go101
Copy link

@go101 go101 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.

@go101

This comment has been minimized.

Copy link
Author

@go101 go101 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
}
@go101

This comment has been minimized.

Copy link
Author

@go101 go101 commented Oct 3, 2019

for both gc and gccgo.

@go101

This comment has been minimized.

Copy link
Author

@go101 go101 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.

@go101

This comment has been minimized.

Copy link
Author

@go101 go101 commented Oct 3, 2019

closed. No bugs here. :)

@go101 go101 closed this Oct 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.