From: Robert Griesemer Date: Tue, 7 Jul 2009 19:02:54 +0000 (-0700) Subject: - ast.FilterExports: filter non-exported anonymous fields X-Git-Tag: weekly.2009-11-06~1219 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=61824ff3a4c8dbb5ad5c454c7e97d75d1a950f31;p=gostls13.git - ast.FilterExports: filter non-exported anonymous fields - fixed typo in parser.go - removed test w/ syntax errors from gofmt test script R=rsc DELTA=25 (21 added, 0 deleted, 4 changed) OCL=31296 CL=31298 --- diff --git a/src/cmd/gofmt/test.sh b/src/cmd/gofmt/test.sh index 6dd1fc4bc1..d37070bad1 100755 --- a/src/cmd/gofmt/test.sh +++ b/src/cmd/gofmt/test.sh @@ -34,7 +34,7 @@ apply1() { test_errors.go | calc.go | method1.go | selftest1.go | func3.go | const2.go | \ bug014.go | bug025.go | bug029.go | bug032.go | bug039.go | bug040.go | bug050.go | bug068.go | \ bug088.go | bug083.go | bug106.go | bug121.go | bug125.go | bug126.go | bug132.go | bug133.go | \ - bug134.go | bug160.go | bug166.go ) ;; + bug134.go | bug160.go | bug163.go | bug166.go ) ;; * ) $1 $2; count $F;; esac } diff --git a/src/pkg/go/ast/filter.go b/src/pkg/go/ast/filter.go index a9dd080152..8bb90d9955 100644 --- a/src/pkg/go/ast/filter.go +++ b/src/pkg/go/ast/filter.go @@ -22,6 +22,23 @@ func filterIdentList(list []*Ident) []*Ident { } +// isExportedType assumes that typ is a correct type. +func isExportedType(typ Expr) bool { + switch t := typ.(type) { + case *Ident: + return t.IsExported(); + case *ParenExpr: + return isExportedType(t.X); + case *SelectorExpr: + // assume t.X is a typename + return t.Sel.IsExported(); + case *StarExpr: + return isExportedType(t.X); + } + return false; +} + + func filterType(typ Expr) func filterFieldList(list []*Field) []*Field { @@ -30,8 +47,12 @@ func filterFieldList(list []*Field) []*Field { exported := false; if len(f.Names) == 0 { // anonymous field - // TODO(gri) check if the type is exported for anonymous field - exported = true; + // (Note that a non-exported anonymous field + // may still refer to a type with exported + // fields, so this is not absolutely correct. + // However, this cannot be done w/o complete + // type information.) + exported = isExportedType(f.Type); } else { f.Names = filterIdentList(f.Names); exported = len(f.Names) > 0; diff --git a/src/pkg/go/parser/parser.go b/src/pkg/go/parser/parser.go index 86b578b650..273b36607b 100644 --- a/src/pkg/go/parser/parser.go +++ b/src/pkg/go/parser/parser.go @@ -1094,7 +1094,7 @@ func (p *parser) parseCompositeLit(typ ast.Expr) ast.Expr { // TODO Consider different approach to checking syntax after parsing: // Provide a arguments (set of flags) to parsing functions -// restricting what they are syupposed to accept depending +// restricting what they are supposed to accept depending // on context. // checkExpr checks that x is an expression (and not a type).