Skip to content

Commit 29d7e54

Browse files
committed
go/types: report argument type for unsafe.OffsetOf
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 <[email protected]> Trust: Dan Scales <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Dan Scales <[email protected]>
1 parent 8fcc614 commit 29d7e54

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/go/types/api_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,9 @@ func TestTypesInfo(t *testing.T) {
362362

363363
// issue 45096
364364
{genericPkg + `issue45096; func _[T interface{ ~int8 | ~int16 | ~int32 }](x T) { _ = x < 0 }`, `0`, `generic_issue45096.T₁`},
365+
366+
// issue 47895
367+
{`package p; import "unsafe"; type S struct { f int }; var s S; var _ = unsafe.Offsetof(s.f)`, `s.f`, `int`},
365368
}
366369

367370
for _, test := range tests {

src/go/types/builtins.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,15 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
671671
// TODO(gri) Should we pass x.typ instead of base (and have indirect report if derefStructPtr indirected)?
672672
check.recordSelection(selx, FieldVal, base, obj, index, false)
673673

674+
// record the selector expression (was bug - issue #47895)
675+
{
676+
mode := value
677+
if x.mode == variable || indirect {
678+
mode = variable
679+
}
680+
check.record(&operand{mode, selx, obj.Type(), nil, 0})
681+
}
682+
674683
// The field offset is considered a variable even if the field is declared before
675684
// the part of the struct which is variable-sized. This makes both the rules
676685
// simpler and also permits (or at least doesn't prevent) a compiler from re-

0 commit comments

Comments
 (0)