Skip to content

Commit 574fc66

Browse files
committed
go/types: add lookup method to context and factor out LookupParent calls
R=go1.11 Also: Moved Checker.pos field into context where it belongs. This is a cleanup/code factoring. For #22992. Change-Id: If9d4f0af537cb181f73735e709ebc8258b2a1378 Reviewed-on: https://go-review.googlesource.com/83017 Reviewed-by: Alan Donovan <[email protected]>
1 parent dd44895 commit 574fc66

File tree

5 files changed

+13
-5
lines changed

5 files changed

+13
-5
lines changed

src/go/types/call.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
314314
// can only appear in qualified identifiers which are mapped to
315315
// selector expressions.
316316
if ident, ok := e.X.(*ast.Ident); ok {
317-
_, obj := check.scope.LookupParent(ident.Name, check.pos)
317+
obj := check.lookup(ident.Name)
318318
if pname, _ := obj.(*PkgName); pname != nil {
319319
assert(pname.pkg == check.pkg)
320320
check.recordUse(ident, pname)

src/go/types/check.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,19 @@ type funcInfo struct {
5151
type context struct {
5252
decl *declInfo // package-level declaration whose init expression/function body is checked
5353
scope *Scope // top-most scope for lookups
54+
pos token.Pos // if valid, identifiers are looked up as if at position pos (used by Eval)
5455
iota constant.Value // value of iota in a constant declaration; nil otherwise
5556
sig *Signature // function signature if inside a function; nil otherwise
5657
hasLabel bool // set if a function makes use of labels (only ~1% of functions); unused outside functions
5758
hasCallOrRecv bool // set if an expression contains a function call or channel receive operation
5859
}
5960

61+
// lookup looks up name in the current context and returns the matching object, or nil.
62+
func (ctxt *context) lookup(name string) Object {
63+
_, obj := ctxt.scope.LookupParent(name, ctxt.pos)
64+
return obj
65+
}
66+
6067
// An importKey identifies an imported package by import path and source directory
6168
// (directory containing the file containing the import). In practice, the directory
6269
// may always be the same, or may not matter. Given an (import path, directory), an
@@ -95,7 +102,6 @@ type Checker struct {
95102
// context within which the current object is type-checked
96103
// (valid only for the duration of type-checking a specific object)
97104
context
98-
pos token.Pos // if valid, identifiers are looked up as if at position pos (used by Eval)
99105

100106
// debugging
101107
indent int // indentation for tracing

src/go/types/interfaces.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (check *Checker) infoFromTypeName(name *ast.Ident, path []*TypeName) *iface
288288

289289
typenameLoop:
290290
// name must be a type name denoting a type whose underlying type is an interface
291-
_, obj := check.scope.LookupParent(name.Name, check.pos /* use Eval position, if any */)
291+
obj := check.lookup(name.Name)
292292
if obj == nil {
293293
return nil
294294
}
@@ -363,7 +363,7 @@ func (check *Checker) infoFromQualifiedTypeName(qname *ast.SelectorExpr) *ifaceI
363363
if name == nil {
364364
return nil
365365
}
366-
_, obj1 := check.scope.LookupParent(name.Name, check.pos /* use Eval position, if any */)
366+
obj1 := check.lookup(name.Name)
367367
if obj1 == nil {
368368
return nil
369369
}

src/go/types/stmt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func (check *Checker) stmt(ctxt stmtContext, s ast.Stmt) {
440440
// list in a "return" statement if a different entity (constant, type, or variable)
441441
// with the same name as a result parameter is in scope at the place of the return."
442442
for _, obj := range res.vars {
443-
if _, alt := check.scope.LookupParent(obj.name, check.pos); alt != nil && alt != obj {
443+
if alt := check.lookup(obj.name); alt != nil && alt != obj {
444444
check.errorf(s.Pos(), "result parameter %s not in scope at return", obj.name)
445445
check.errorf(alt.Pos(), "\tinner declaration of %s", obj)
446446
// ok to continue

src/go/types/typexpr.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ func (check *Checker) ident(x *operand, e *ast.Ident, def *Named, path []*TypeNa
2222
x.mode = invalid
2323
x.expr = e
2424

25+
// Note that we cannot use check.lookup here because the returned scope
26+
// may be different from obj.Parent(). See also Scope.LookupParent doc.
2527
scope, obj := check.scope.LookupParent(e.Name, check.pos)
2628
if obj == nil {
2729
if e.Name == "_" {

0 commit comments

Comments
 (0)