From f32f4f58d9cd9d15371ee6198c1b222bcf2b56d9 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Sun, 30 May 2021 18:13:36 -0700 Subject: [PATCH] [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 Run-TryBot: Matthew Dempsky TryBot-Result: Go Bot Reviewed-by: Cuong Manh Le --- src/cmd/compile/internal/types/fmt.go | 35 +++++++++------------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/src/cmd/compile/internal/types/fmt.go b/src/cmd/compile/internal/types/fmt.go index b538ea8054..cecd1b3cc1 100644 --- a/src/cmd/compile/internal/types/fmt.go +++ b/src/cmd/compile/internal/types/fmt.go @@ -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) - 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 - } + // Default to 'v' if verb is invalid. + if verb != 'S' { + verb = 'v' } - sconv2(b, t.Sym(), 'v', mode) + 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 }