Skip to content

Commit

Permalink
internal/lsp/analysis/fillstruct: correct pointer to builtin values
Browse files Browse the repository at this point in the history
The current implementation correctly calls 'new' when filling a
pointer to a builtin type.

Fixes: golang/go#39854
Change-Id: I0c2b27bb57fd865c4376279059ad060608d48ba3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/239978
Run-TryBot: Josh Baum <joshbaum@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
  • Loading branch information
joshbaum authored and stamblerre committed Jun 25, 2020
1 parent aa12c9e commit 727c06e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
20 changes: 17 additions & 3 deletions internal/lsp/analysis/fillstruct/fillstruct.go
Expand Up @@ -346,9 +346,23 @@ func populateValue(fset *token.FileSet, f *ast.File, pkg *types.Package, typ typ
Body: &ast.BlockStmt{},
}
case *types.Pointer:
return &ast.UnaryExpr{
Op: token.AND,
X: populateValue(fset, f, pkg, u.Elem()),
switch u.Elem().(type) {
case *types.Basic:
return &ast.CallExpr{
Fun: &ast.Ident{
Name: "new",
},
Args: []ast.Expr{
&ast.Ident{
Name: u.Elem().String(),
},
},
}
default:
return &ast.UnaryExpr{
Op: token.AND,
X: populateValue(fset, f, pkg, u.Elem()),
}
}
case *types.Interface:
return ast.NewIdent("nil")
Expand Down
8 changes: 8 additions & 0 deletions internal/lsp/analysis/fillstruct/testdata/src/a/a.go
Expand Up @@ -89,3 +89,11 @@ type importedStruct struct {
}

var _ = importedStruct{} // want ""

type pointerBuiltinStruct struct {
b *bool
s *string
i *int
}

var _ = pointerBuiltinStruct{} // want ""
12 changes: 12 additions & 0 deletions internal/lsp/analysis/fillstruct/testdata/src/a/a.go.golden
Expand Up @@ -125,3 +125,15 @@ var _ = importedStruct{
},
st: ast.CompositeLit{},
} // want ""

type pointerBuiltinStruct struct {
b *bool
s *string
i *int
}

var _ = pointerBuiltinStruct{
b: new(bool),
s: new(string),
i: new(int),
} // want ""

0 comments on commit 727c06e

Please sign in to comment.