]> Cypherpunks repositories - gostls13.git/commitdiff
- don't call String method of AST nodes when converting them to text
authorRobert Griesemer <gri@golang.org>
Fri, 31 Jul 2009 02:39:47 +0000 (19:39 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 31 Jul 2009 02:39:47 +0000 (19:39 -0700)
- make token.Position.String more robust

TBR=rsc
DELTA=20  (10 added, 6 deleted, 4 changed)
OCL=32564
CL=32564

src/cmd/godoc/godoc.go
src/pkg/go/token/token.go

index 4fe628fb50b9661e2b03f123e5979afbe67cad10..688806c426fcad0c65cc64d79d42c071c6adb5f9 100644 (file)
@@ -213,19 +213,21 @@ func nodeText(node interface{}) []byte {
 
 // Convert x, whatever it is, to text form.
 func toText(x interface{}) []byte {
-       type String interface { String() string }
+       type Stringer interface { String() string }
 
        switch v := x.(type) {
        case []byte:
                return v;
        case string:
                return strings.Bytes(v);
-       case String:
-               return strings.Bytes(v.String());
        case ast.Decl:
                return nodeText(v);
        case ast.Expr:
                return nodeText(v);
+       case Stringer:
+               // last resort (AST nodes get a String method
+               // from token.Position - don't call that one)
+               return strings.Bytes(v.String());
        }
        var buf bytes.Buffer;
        fmt.Fprint(&buf, x);
index 61a0c622c820a9b0785558582dbda511fc78225b..32958b53f015b4b4268fd1690a8056cf0b4c1599 100644 (file)
@@ -353,15 +353,17 @@ func (pos *Position) IsValid() bool {
 
 
 func (pos *Position) String() string {
-       s := pos.Filename;
-       if pos.IsValid() {
+       if pos != nil {
+               s := pos.Filename;
+               if pos.IsValid() {
+                       if s != "" {
+                               s += ":";
+                       }
+                       s += fmt.Sprintf("%d:%d", pos.Line, pos.Column);
+               }
                if s != "" {
-                       s += ":";
+                       return s;
                }
-               s += fmt.Sprintf("%d:%d", pos.Line, pos.Column);
-       }
-       if s != "" {
-               return s;
        }
        return "<unknown position>";
 }