return n
}
+// importName is like oldname, but it reports an error if sym is from another package and not exported.
+func importName(sym *types.Sym) *Node {
+ n := oldname(sym)
+ if !types.IsExported(sym.Name) && sym.Pkg != localpkg {
+ n.SetDiag(true)
+ yyerror("cannot refer to unexported name %s.%s", sym.Pkg.Name, sym.Name)
+ }
+ return n
+}
+
// := declarations
func colasname(n *Node) bool {
switch n.Op {
obj := p.expr(expr.X)
if obj.Op == OPACK {
obj.Name.SetUsed(true)
- return oldname(restrictlookup(expr.Sel.Value, obj.Name.Pkg))
+ return importName(obj.Name.Pkg.Lookup(expr.Sel.Value))
}
n := nodSym(OXDOT, obj, p.name(expr.Sel))
n.Pos = p.pos(expr) // lineno may have been changed by p.expr(expr.X)
p.setlineno(method)
var n *Node
if method.Name == nil {
- n = p.nodSym(method, ODCLFIELD, oldname(p.packname(method.Type)), nil)
+ n = p.nodSym(method, ODCLFIELD, importName(p.packname(method.Type)), nil)
} else {
mname := p.name(method.Name)
sig := p.typeExpr(method.Type)
def.Name.SetUsed(true)
pkg = def.Name.Pkg
}
- return restrictlookup(expr.Sel.Value, pkg)
+ return pkg.Lookup(expr.Sel.Value)
}
panic(fmt.Sprintf("unexpected packname: %#v", expr))
}
}
sym := p.packname(typ)
- n := p.nodSym(typ, ODCLFIELD, oldname(sym), lookup(sym.Name))
+ n := p.nodSym(typ, ODCLFIELD, importName(sym), lookup(sym.Name))
n.SetEmbedded(true)
if isStar {
return lookupN(prefix, int(n))
}
-func restrictlookup(name string, pkg *types.Pkg) *types.Sym {
- if !types.IsExported(name) && pkg != localpkg {
- yyerror("cannot refer to unexported name %s.%s", pkg.Name, name)
- }
- return pkg.Lookup(name)
-}
-
// find all the exported symbols in package opkg
// and make them available in the current package
func importdot(opkg *types.Pkg, pack *Node) {
func main() {
var t testing.T
-
+
// make sure error mentions that
// name is unexported, not just "name not found".
- t.common.name = nil // ERROR "unexported"
-
- println(testing.anyLowercaseName("asdf")) // ERROR "unexported" "undefined: testing.anyLowercaseName"
+ t.common.name = nil // ERROR "unexported"
+
+ println(testing.anyLowercaseName("asdf")) // ERROR "unexported"
}
--- /dev/null
+// errorcheck
+
+// 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 main
+
+import "bytes"
+
+type _ struct{ bytes.nonexist } // ERROR "unexported"
+
+type _ interface{ bytes.nonexist } // ERROR "unexported"
+
+func main() {
+ var _ bytes.Buffer
+ var _ bytes.buffer // ERROR "unexported"
+}
import "runtime"
func main() {
- runtime.printbool(true) // ERROR "unexported" "undefined"
+ runtime.printbool(true) // ERROR "unexported"
}