Skip to content

reflect: gccgo+gotip1.6beta1 crashes on multi-line field tags #43379

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

Closed
zigo101 opened this issue Dec 25, 2020 · 8 comments
Closed

reflect: gccgo+gotip1.6beta1 crashes on multi-line field tags #43379

zigo101 opened this issue Dec 25, 2020 · 8 comments
Milestone

Comments

@zigo101
Copy link

zigo101 commented Dec 25, 2020

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

$ go version
go version go1.16beta1 linux/amd64
$ gccgo --version
gccgo (Debian 8.3.0-6) 8.3.0

Does this issue reproduce with the latest release?

No.

What did you do?

package main

import "fmt"
import "reflect"

type T struct {
	Z bool `optional:"yes" name:"zz
" default:"true"`
}

func main() {
	t := reflect.TypeOf(T{})
	z := t.Field(0).Tag
	fmt.Println(z.Lookup("optional"))
	fmt.Println(z.Lookup("default"))
	fmt.Println(z.Lookup("max"))
}

What did you expect to see?

gccgo will not crash in compiling.

And should z.Lookup("default") return "" false instead?

What did you see instead?

$ go version
go version go1.16beta1 linux/amd64
$ go run a.go
yes true
true true
 false
$ go run -compiler=gccgo a.go
# command-line-arguments
/usr/bin/ld: $WORK/b001/_pkg_.a(_go_.o): in function `main.main..init0':
/tmp/go-build/b001/_gomod_.go:5: undefined reference to `runtime.setmodinfo'
collect2: error: ld returned 1 exit status
$ go version
go version go1.15.5 linux/amd64
$ go run a.go
yes true
true true
 false
$ go run -compiler=gccgo a.go
yes true
true true
 false
@gopherbot gopherbot added this to the Gccgo milestone Dec 25, 2020
@zigo101 zigo101 changed the title reflct: gccgo+gotip1.6beta1 crashes on multi-line field tags reflect: gccgo+gotip1.6beta1 crashes on multi-line field tags Dec 25, 2020
@ianlancetaylor
Copy link
Member

Closing as a dup of #42916. I don't think this has anything to do with multi-line field tags as such.

@zigo101
Copy link
Author

zigo101 commented Dec 26, 2020

So the workable multi-line field tag workaround is unintended, but they will be kept as such for compatibility?

@ianlancetaylor
Copy link
Member

Maybe I'm misunderstanding. Are you saying that if you add or remove a multi-line field tag it changes the behavior on tip?

@zigo101
Copy link
Author

zigo101 commented Dec 27, 2020

I just asked to confirm whether or not the key-value pair default:"true" in the field tags is intended to be valid.

@ianlancetaylor
Copy link
Member

The docs for struct tags say that the value string should use Go string literal syntax, so I think that putting a bare newline in a value string does not follow the documentation.

@zigo101
Copy link
Author

zigo101 commented Dec 27, 2020

putting a bare newline in a value string does not follow the documentation.

I understand this. So I'm not surprised that the field Z doesn't have a name key in its tag.
But I'm uncertain whether or not I should be surprised that the field Z has a default key in its tag.

What is the principle difference to make the field Z has a default key in tag but the field W has not?

package main

import "fmt"
import "reflect"

type T struct {
	Z int `optional:"111" name:"22
	       2" default:"333"`
	W int `optional:"111" 
	       name:"222" default:"333"`
}

func main() {
	t := reflect.TypeOf(T{})
	
	z := t.Field(0).Tag
	fmt.Println(z.Lookup("optional")) // 111 true
	fmt.Println(z.Lookup("name"))     //  false
	fmt.Println(z.Lookup("default"))  // 333 true
	
	w := t.Field(1).Tag
	fmt.Println(w.Lookup("optional")) // 111 true
	fmt.Println(w.Lookup("name"))     //  false
	fmt.Println(w.Lookup("default"))  //  false
}

@ianlancetaylor
Copy link
Member

The reflect.StructTag.Get method is only documented as handling struct tags that fit the documented format. There is no prediction or guarantee made for struct tags that do not fit the documented format. You can look at the code to see why the two cases behave differently if you like, but there is no promise that those cases will not change in some future release. So while I'm sure your question has an answer, I don't think it's a very interesting one.

@zigo101
Copy link
Author

zigo101 commented Dec 28, 2020

Thanks for confirming this is a unspecified behavior.

@golang golang locked and limited conversation to collaborators Dec 28, 2021
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

3 participants