Skip to content

Commit 2d7cb29

Browse files
cuonglmrandall77
authored andcommitted
cmd/compile: clarify the difference between types.Sym and obj.LSym
Both types.Sym and obj.LSym have the field Name, and that field is widely used in compiler source. It can lead to confusion that when to use which one. So, adding documentation for clarifying the difference between them, eliminate the confusion, or at least, make the code which use them clearer for the reader. See #31252 (comment) Change-Id: I31f7fc6e4de4cf68f67ab2e3a385a7f451c796f5 Reviewed-on: https://go-review.googlesource.com/c/go/+/175019 Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent ab724d4 commit 2d7cb29

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/cmd/compile/internal/gc/syntax.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func (n *Node) isMethodExpression() bool {
280280
return n.Op == ONAME && n.Left != nil && n.Left.Op == OTYPE && n.Right != nil && n.Right.Op == ONAME
281281
}
282282

283-
// funcname returns the name of the function n.
283+
// funcname returns the name (without the package) of the function n.
284284
func (n *Node) funcname() string {
285285
if n == nil || n.Func == nil || n.Func.Nname == nil {
286286
return "<nil>"

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ import (
1111
"unicode/utf8"
1212
)
1313

14-
// Sym represents an object name. Most commonly, this is a Go identifier naming
15-
// an object declared within a package, but Syms are also used to name internal
16-
// synthesized objects.
14+
// Sym represents an object name in a segmented (pkg, name) namespace.
15+
// Most commonly, this is a Go identifier naming an object declared within a package,
16+
// but Syms are also used to name internal synthesized objects.
1717
//
1818
// As an exception, field and method names that are exported use the Sym
1919
// associated with localpkg instead of the package that declared them. This
2020
// allows using Sym pointer equality to test for Go identifier uniqueness when
2121
// handling selector expressions.
22+
//
23+
// Ideally, Sym should be used for representing Go language constructs,
24+
// while cmd/internal/obj.LSym is used for representing emitted artifacts.
25+
//
26+
// NOTE: In practice, things can be messier than the description above
27+
// for various reasons (historical, convenience).
2228
type Sym struct {
2329
Importdef *Pkg // where imported definition was found
2430
Linkname string // link name

src/cmd/internal/obj/link.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ const (
377377
)
378378

379379
// An LSym is the sort of symbol that is written to an object file.
380+
// It represents Go symbols in a flat pkg+"."+name namespace.
380381
type LSym struct {
381382
Name string
382383
Type objabi.SymKind

0 commit comments

Comments
 (0)