1
0
mirror of https://github.com/golang/go synced 2024-07-05 18:00:52 +00:00

cmd/compile: report error for unexported name only once

Fixes #22921

Change-Id: If29bd962335ac7676ea4f379727db3d55ae1bf8e
Reviewed-on: https://go-review.googlesource.com/c/go/+/250177
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Cuong Manh Le 2020-08-24 15:23:27 +07:00
parent 27136419d4
commit d3f6e2f300
6 changed files with 37 additions and 16 deletions

View File

@ -297,6 +297,16 @@ func oldname(s *types.Sym) *Node {
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 {

View File

@ -653,7 +653,7 @@ func (p *noder) expr(expr syntax.Expr) *Node {
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)
@ -857,7 +857,7 @@ func (p *noder) interfaceType(expr *syntax.InterfaceType) *Node {
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)
@ -896,7 +896,7 @@ func (p *noder) packname(expr syntax.Expr) *types.Sym {
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))
}
@ -911,7 +911,7 @@ func (p *noder) embedded(typ syntax.Expr) *Node {
}
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 {

View File

@ -271,13 +271,6 @@ func autolabel(prefix string) *types.Sym {
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) {

View File

@ -10,11 +10,11 @@ import "testing"
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"
}

View File

@ -0,0 +1,18 @@
// 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"
}

View File

@ -17,5 +17,5 @@ package main
import "runtime"
func main() {
runtime.printbool(true) // ERROR "unexported" "undefined"
runtime.printbool(true) // ERROR "unexported"
}