Skip to content

Commit

Permalink
unused: don't crash on generic composite literals
Browse files Browse the repository at this point in the history
(cherry picked from commit 1cd2556)
  • Loading branch information
dominikh committed Apr 24, 2022
1 parent 90a3456 commit 0ccdb5c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions unused/testdata/src/typeparams/typeparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,8 @@ type foo struct{} // used
type Bar *Node[foo] // used

func (n Node[T]) anyMethod() {} // unused

func fn11[T ~struct{ Field int }]() { // unused
// don't crash because of the composite literal
_ = T{Field: 42}
}
7 changes: 6 additions & 1 deletion unused/unused.go
Original file line number Diff line number Diff line change
Expand Up @@ -1630,12 +1630,17 @@ func (g *graph) instructions(fn *ir.Function) {
}
switch instr := instr.(type) {
case *ir.Field:
// Can't access fields via generics, for now.

st := instr.X.Type().Underlying().(*types.Struct)
field := st.Field(instr.Field)
// (4.7) functions use fields they access
g.seeAndUse(field, fnObj, edgeFieldAccess)
case *ir.FieldAddr:
st := typeutil.Dereference(instr.X.Type()).Underlying().(*types.Struct)
// User code can't access fields on type parameters, but composite literals are still possible, which
// compile to FieldAddr + Store.

st := typeutil.CoreType(typeutil.Dereference(instr.X.Type())).(*types.Struct)
field := st.Field(instr.Field)
// (4.7) functions use fields they access
g.seeAndUse(field, fnObj, edgeFieldAccess)
Expand Down

0 comments on commit 0ccdb5c

Please sign in to comment.