]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: report argument type for unsafe.OffsetOf
authorRobert Griesemer <gri@golang.org>
Sun, 22 Aug 2021 22:21:29 +0000 (15:21 -0700)
committerRobert Griesemer <gri@golang.org>
Sun, 22 Aug 2021 22:30:38 +0000 (22:30 +0000)
This is a clean port of CL 344252 to go/types.

For #47895.

Change-Id: I48cbb97ec28fcfb4fdf483594be9d29426c117ac
Reviewed-on: https://go-review.googlesource.com/c/go/+/344254
Trust: Robert Griesemer <gri@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
src/go/types/api_test.go
src/go/types/builtins.go

index 1631215d15bf3fe681cefca5c7fb3a2c1a4e73e5..a49f2113b9c455ebb7f135dfb14828de5ef31080 100644 (file)
@@ -362,6 +362,9 @@ func TestTypesInfo(t *testing.T) {
 
                // issue 45096
                {genericPkg + `issue45096; func _[T interface{ ~int8 | ~int16 | ~int32  }](x T) { _ = x < 0 }`, `0`, `generic_issue45096.T₁`},
+
+               // issue 47895
+               {`package p; import "unsafe"; type S struct { f int }; var s S; var _ = unsafe.Offsetof(s.f)`, `s.f`, `int`},
        }
 
        for _, test := range tests {
index 9c772d3844285af6636ed73e17e1723759c39fd9..698435bc207be0e5c1f853bf61754f6e570f1443 100644 (file)
@@ -671,6 +671,15 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
                // TODO(gri) Should we pass x.typ instead of base (and have indirect report if derefStructPtr indirected)?
                check.recordSelection(selx, FieldVal, base, obj, index, false)
 
+               // record the selector expression (was bug - issue #47895)
+               {
+                       mode := value
+                       if x.mode == variable || indirect {
+                               mode = variable
+                       }
+                       check.record(&operand{mode, selx, obj.Type(), nil, 0})
+               }
+
                // The field offset is considered a variable even if the field is declared before
                // the part of the struct which is variable-sized. This makes both the rules
                // simpler and also permits (or at least doesn't prevent) a compiler from re-