Skip to content

Commit

Permalink
go/packages: use go115UsesCgo instead of UsesCgo
Browse files Browse the repository at this point in the history
x/tools half of golang.org/cl/237417.

Updates #16623.
Updates #39072.

Change-Id: Ic31c2cf64dc1b71b70912b9641cf468f60d116f8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237418
Run-TryBot: Matthew Dempsky <[email protected]>
Reviewed-by: Heschi Kreinick <[email protected]>
  • Loading branch information
mdempsky committed Jun 10, 2020
1 parent 3e83d1e commit 51e3d70
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
8 changes: 2 additions & 6 deletions go/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import (
"log"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"time"

"golang.org/x/tools/go/gcexportdata"
"golang.org/x/tools/internal/gocommand"
"golang.org/x/tools/internal/packagesinternal"
"golang.org/x/tools/internal/typesinternal"
)

// A LoadMode controls the amount of detail to return when loading.
Expand Down Expand Up @@ -922,17 +922,13 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
Sizes: ld.sizes,
}
if (ld.Mode & TypecheckCgo) != 0 {
// TODO: remove this when we stop supporting 1.14.
rtc := reflect.ValueOf(tc).Elem()
usesCgo := rtc.FieldByName("UsesCgo")
if !usesCgo.IsValid() {
if !typesinternal.SetUsesCgo(tc) {
appendError(Error{
Msg: "TypecheckCgo requires Go 1.15+",
Kind: ListError,
})
return
}
usesCgo.SetBool(true)
}
types.NewChecker(tc, ld.Fset, lpkg.Types, lpkg.TypesInfo).Files(lpkg.Syntax)

Expand Down
6 changes: 2 additions & 4 deletions internal/lsp/cache/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"go/token"
"go/types"
"path"
"reflect"
"sort"
"strings"
"sync"
Expand All @@ -23,6 +22,7 @@ import (
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/memoize"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/typesinternal"
errors "golang.org/x/xerrors"
)

Expand Down Expand Up @@ -404,9 +404,7 @@ func typeCheck(ctx context.Context, fset *token.FileSet, m *metadata, mode sourc
}
// We want to type check cgo code if go/types supports it.
// We passed TypecheckCgo to go/packages when we Loaded.
if usescgo := reflect.ValueOf(cfg).Elem().FieldByName("UsesCgo"); usescgo.IsValid() {
usescgo.SetBool(true)
}
typesinternal.SetUsesCgo(cfg)

check := types.NewChecker(cfg, fset, pkg.types, pkg.typesInfo)

Expand Down
4 changes: 2 additions & 2 deletions internal/lsp/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"go/types"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"sync"
Expand All @@ -24,6 +23,7 @@ import (
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/packagesinternal"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/typesinternal"
errors "golang.org/x/xerrors"
)

Expand Down Expand Up @@ -122,7 +122,7 @@ func (s *snapshot) Config(ctx context.Context) *packages.Config {
Tests: true,
}
// We want to type check cgo code if go/types supports it.
if reflect.ValueOf(&types.Config{}).Elem().FieldByName("UsesCgo").IsValid() {
if typesinternal.SetUsesCgo(&types.Config{}) {
cfg.Mode |= packages.TypecheckCgo
}
packagesinternal.SetGoCmdRunner(cfg, s.view.gocmdRunner)
Expand Down
28 changes: 28 additions & 0 deletions internal/typesinternal/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package typesinternal

import (
"go/types"
"reflect"
"unsafe"
)

func SetUsesCgo(conf *types.Config) bool {
v := reflect.ValueOf(conf).Elem()

f := v.FieldByName("go115UsesCgo")
if !f.IsValid() {
f = v.FieldByName("UsesCgo")
if !f.IsValid() {
return false
}
}

addr := unsafe.Pointer(f.UnsafeAddr())
*(*bool)(addr) = true

return true
}

0 comments on commit 51e3d70

Please sign in to comment.