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

Can non-nil interface fields be considered nonzero? #97

Open
ydnar opened this issue Sep 19, 2023 · 0 comments · May be fixed by #98
Open

Can non-nil interface fields be considered nonzero? #97

ydnar opened this issue Sep 19, 2023 · 0 comments · May be fixed by #98

Comments

@ydnar
Copy link

ydnar commented Sep 19, 2023

Hi there, thanks for making this package.

I observed this issue printing a complex data structure with several structs containing interface fields which could be one of many conforming types. A number of those types might be empty structs, where the type is the only relevant information (the zero value is the only value).

If the outer struct with an interface field is printed, the nonzero function returns false because the concrete value of the interface field is zero. An example playground showing this can be found here: https://go.dev/play/p/HjxnCorTQtj

For interface fields, would it be possible to mark the field as nonzero if the field is non-nil, rather than the value of what the field is pointing at?

Thanks!

package main

import (
	"fmt"

	"github.com/kr/pretty"
)

func main() {
	// This should print the zero value for A
	v := &A{Err: nil}
	pretty.Println(v)

	// This will print &main.FormatError{...}
	v.Err = &FormatError{"foo.go", 12}
	pretty.Println(v)

	// This SHOULD print &main.A{Err: main.LogicError{}}, but prints the zero value for A instead.
	v.Err = LogicError{}
	pretty.Println(v)
}

type A struct {
	Err error
}

type FormatError struct {
	File string
	Line int
}

func (err *FormatError) Error() string {
	return fmt.Sprintf("format error: %s:%d", err.File, err.Line)
}

type LogicError struct{}

func (LogicError) Error() string {
	return "logic error"
}

Output

&main.A{}
&main.A{
    Err: &main.FormatError{File:"foo.go", Line:12},
}
&main.A{}

Desired Output

&main.A{}
&main.A{
    Err: &main.FormatError{File:"foo.go", Line:12},
}
&main.A{
    Err: &main.LogicError{},
}
@ydnar ydnar changed the title False-positives for nonzero Non-nil interface fields are not nonzero Sep 19, 2023
@ydnar ydnar changed the title Non-nil interface fields are not nonzero Non-nil interface fields should be considered nonzero? Sep 19, 2023
@ydnar ydnar changed the title Non-nil interface fields should be considered nonzero? Can non-nil interface fields be considered nonzero? Sep 19, 2023
ydnar added a commit to ydnar/pretty that referenced this issue Sep 19, 2023
@ydnar ydnar linked a pull request Sep 19, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant