[dev.typeparams] cmd/compile: simplify formatting of defined types

The existing code for deciding how to format defined type names is
incredibly convoluted and difficult to follow. In particular, I'm
looking at changing how Vargen works, and I couldn't tell from the
existing code whether my idea was viable.

This CL overhauls the logic to be much simpler with fewer special
cases, while overall behaving the same. A few notable intentional
differences from how the current code works:

1. The old code replaced the 'S' verb for fmtTypeGo and fmtTypeDebug
to 'v', whereas the new code leaves it alone. There's currently no
code that actually uses 'S' with these modes anyway, so it doesn't
seem important to maintain this special case. If future code wants 'v'
formatting, it should just use 'v' instead of 'S'.

2. The old code included Vargen for fmtTypeIDName mode with the 'S'
verb; but again, this functionality isn't actually used. I think it
would make sense for fmtTypeIDName to include Vargen like fmtTypeID
does (Vargen is logically part of the type's identity after all), but
that breaks tests and toolstash -cmp. So for now, this is left as a
TODO to investigate in the future.

3. The old code only added Vargen for fmtTypeID in 'v' mode when
printing types from the local package. But because we don't currently
support exporting function-scoped defined types anyway, this is again
irrelevant. In fact, once we *do* support exporting function-scoped
defined types, we'll need to include Vargen to generate the linker
symbols correctly.

Passes toolstash -cmp.

Change-Id: I4e481276bc4dc8d5b17eebf597b612737f26be5b
Reviewed-on: https://go-review.googlesource.com/c/go/+/323709
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
Matthew Dempsky 2021-05-30 18:13:36 -07:00
parent 22f5ece3b1
commit f32f4f58d9

View file

@ -319,31 +319,20 @@ func tconv2(b *bytes.Buffer, t *Type, verb rune, mode fmtMode, visited map[*Type
// Unless the 'L' flag was specified, if the type has a name, just print that name.
if verb != 'L' && t.Sym() != nil && t != Types[t.Kind()] {
switch mode {
case fmtTypeID, fmtTypeIDName:
if verb == 'S' {
if t.Vargen != 0 {
sconv2(b, t.Sym(), 'S', mode)
// Default to 'v' if verb is invalid.
if verb != 'S' {
verb = 'v'
}
sconv2(b, t.Sym(), verb, mode)
// TODO(mdempsky): Investigate including Vargen in fmtTypeIDName
// output too. It seems like it should, but that mode is currently
// used in string representation used by reflection, which is
// user-visible and doesn't expect this.
if mode == fmtTypeID && t.Vargen != 0 {
fmt.Fprintf(b, "·%d", t.Vargen)
return
}
sconv2(b, t.Sym(), 'S', mode)
return
}
if mode == fmtTypeIDName {
sconv2(b, t.Sym(), 'v', fmtTypeIDName)
return
}
if t.Sym().Pkg == LocalPkg && t.Vargen != 0 {
sconv2(b, t.Sym(), 'v', mode)
fmt.Fprintf(b, "·%d", t.Vargen)
return
}
}
sconv2(b, t.Sym(), 'v', mode)
return
}