Skip to content

Commit 11f5df2

Browse files
committed
cmd/compile: extract pkgqual from symfmt
The logic in symfmt for deciding how to package-qualify an identifier is easily refactored into a separate function, loosely similar to go/types.Qualifier's API. Passes toolstash -cmp. Updates #47087. Change-Id: Ib3e7cc35a6577dc28df8eca79ba3457c48168e86 Reviewed-on: https://go-review.googlesource.com/c/go/+/333161 Trust: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
1 parent 991fd38 commit 11f5df2

File tree

1 file changed

+20
-23
lines changed
  • src/cmd/compile/internal/types

1 file changed

+20
-23
lines changed

src/cmd/compile/internal/types/fmt.go

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -137,47 +137,44 @@ func sconv2(b *bytes.Buffer, s *Sym, verb rune, mode fmtMode) {
137137
}
138138

139139
func symfmt(b *bytes.Buffer, s *Sym, verb rune, mode fmtMode) {
140+
if q := pkgqual(s.Pkg, verb, mode); q != "" {
141+
b.WriteString(q)
142+
b.WriteByte('.')
143+
}
144+
b.WriteString(s.Name)
145+
}
146+
147+
// pkgqual returns the qualifier that should be used for printing
148+
// symbols from the given package in the given mode.
149+
// If it returns the empty string, no qualification is needed.
150+
func pkgqual(pkg *Pkg, verb rune, mode fmtMode) string {
140151
if verb != 'S' {
141152
switch mode {
142153
case fmtGo: // This is for the user
143-
if s.Pkg == BuiltinPkg || s.Pkg == LocalPkg {
144-
b.WriteString(s.Name)
145-
return
154+
if pkg == BuiltinPkg || pkg == LocalPkg {
155+
return ""
146156
}
147157

148158
// If the name was used by multiple packages, display the full path,
149-
if s.Pkg.Name != "" && NumImport[s.Pkg.Name] > 1 {
150-
fmt.Fprintf(b, "%q.%s", s.Pkg.Path, s.Name)
151-
return
159+
if pkg.Name != "" && NumImport[pkg.Name] > 1 {
160+
return strconv.Quote(pkg.Path)
152161
}
153-
b.WriteString(s.Pkg.Name)
154-
b.WriteByte('.')
155-
b.WriteString(s.Name)
156-
return
162+
return pkg.Name
157163

158164
case fmtDebug:
159-
b.WriteString(s.Pkg.Name)
160-
b.WriteByte('.')
161-
b.WriteString(s.Name)
162-
return
165+
return pkg.Name
163166

164167
case fmtTypeIDName:
165168
// dcommontype, typehash
166-
b.WriteString(s.Pkg.Name)
167-
b.WriteByte('.')
168-
b.WriteString(s.Name)
169-
return
169+
return pkg.Name
170170

171171
case fmtTypeID:
172172
// (methodsym), typesym, weaksym
173-
b.WriteString(s.Pkg.Prefix)
174-
b.WriteByte('.')
175-
b.WriteString(s.Name)
176-
return
173+
return pkg.Prefix
177174
}
178175
}
179176

180-
b.WriteString(s.Name)
177+
return ""
181178
}
182179

183180
// Type

0 commit comments

Comments
 (0)