Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/printer: does not always insert required parens around &x and *x #1847

Closed
gopherbot opened this issue May 18, 2011 · 9 comments
Closed

go/printer: does not always insert required parens around &x and *x #1847

gopherbot opened this issue May 18, 2011 · 9 comments
Milestone

Comments

@gopherbot
Copy link

by qyzhai:

What steps will reproduce the problem?
#cat x.go
package main

type T struct {
    id int
}
func (t *T) Id() int {
    return t.id
}

func main() {
    var t = (&T{1000}).Id()
    println(t)
}
#gofmt -r'(a) -> a' -w x.go

What is the expected output?
same as the origin file

What do you see instead?
package main

type T struct {
    id int
}
func (t *T) Id() int {
    return t.id
}

func main() {
    var t = &T{1000}.Id()
    println(t)
}


Which revision are you using?  (hg identify)
7383ed409256+ tip

Please provide any additional information below.
roger peppe:
this bug applies to other unary operators too. the following is valid code
that will be broken by gofmt '(a)->a' and is not abusive:

       x := struct{a int}{}
       xp := &x
       xpp := &xp
       fmt.Println((*xpp).a)
@griesemer
Copy link
Contributor

Comment 1:

ASTs before and after rewrite:
{
   168  .  .  .  .  .  .  .  .  .  .  .  .  X: *ast.ParenExpr {
   169  .  .  .  .  .  .  .  .  .  .  .  .  .  Lparen: test.go:12:10
   170  .  .  .  .  .  .  .  .  .  .  .  .  .  X: *ast.UnaryExpr {
   171  .  .  .  .  .  .  .  .  .  .  .  .  .  .  OpPos: test.go:12:11
   172  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Op: &
   173  .  .  .  .  .  .  .  .  .  .  .  .  .  .  X: *ast.CompositeLit {
   174  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Type: *ast.Ident {
   175  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  NamePos: test.go:12:12
   176  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Name: "T"
   177  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Obj: *(obj @ 16)
   178  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  }
   179  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Lbrace: test.go:12:13
   180  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Elts: []ast.Expr (len = 1) {
   181  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  0: *ast.BasicLit {
   182  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  ValuePos: test.go:12:14
   183  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Kind: INT
   184  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Value: "1000"
   185  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  }
   186  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  }
   187  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Rbrace: test.go:12:18
   188  .  .  .  .  .  .  .  .  .  .  .  .  .  .  }
   189  .  .  .  .  .  .  .  .  .  .  .  .  .  }
   190  .  .  .  .  .  .  .  .  .  .  .  .  .  Rparen: test.go:12:19
   191  .  .  .  .  .  .  .  .  .  .  .  .  }
   192  .  .  .  .  .  .  .  .  .  .  .  .  Sel: *ast.Ident {
   193  .  .  .  .  .  .  .  .  .  .  .  .  .  NamePos: test.go:12:21
   194  .  .  .  .  .  .  .  .  .  .  .  .  .  Name: "Id"
   195  .  .  .  .  .  .  .  .  .  .  .  .  }
   196  .  .  .  .  .  .  .  .  .  .  .  }

Owner changed to @griesemer.

Status changed to Accepted.

@griesemer
Copy link
Contributor

Comment 2:

ASTs before and after rewrite:
   165  .  .  .  .  .  .  .  .  .  Values: []ast.Expr (len = 1) {
   166  .  .  .  .  .  .  .  .  .  .  0: *ast.CallExpr {
   167  .  .  .  .  .  .  .  .  .  .  .  Fun: *ast.SelectorExpr {
   168  .  .  .  .  .  .  .  .  .  .  .  .  X: *ast.ParenExpr {
   169  .  .  .  .  .  .  .  .  .  .  .  .  .  Lparen: test.go:12:10
   170  .  .  .  .  .  .  .  .  .  .  .  .  .  X: *ast.UnaryExpr {
   171  .  .  .  .  .  .  .  .  .  .  .  .  .  .  OpPos: test.go:12:11
   172  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Op: &
   173  .  .  .  .  .  .  .  .  .  .  .  .  .  .  X: *ast.CompositeLit {
   174  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Type: *ast.Ident {
   175  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  NamePos: test.go:12:12
   176  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Name: "T"
   177  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Obj: *(obj @ 16)
   178  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  }
   179  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Lbrace: test.go:12:13
   180  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Elts: []ast.Expr (len = 1) {
   181  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  0: *ast.BasicLit {
   182  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  ValuePos: test.go:12:14
   183  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Kind: INT
   184  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Value: "1000"
   185  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  }
   186  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  }
   187  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Rbrace: test.go:12:18
   188  .  .  .  .  .  .  .  .  .  .  .  .  .  .  }
   189  .  .  .  .  .  .  .  .  .  .  .  .  .  }
   190  .  .  .  .  .  .  .  .  .  .  .  .  .  Rparen: test.go:12:19
   191  .  .  .  .  .  .  .  .  .  .  .  .  }
   192  .  .  .  .  .  .  .  .  .  .  .  .  Sel: *ast.Ident {
   193  .  .  .  .  .  .  .  .  .  .  .  .  .  NamePos: test.go:12:21
   194  .  .  .  .  .  .  .  .  .  .  .  .  .  Name: "Id"
   195  .  .  .  .  .  .  .  .  .  .  .  .  }
   196  .  .  .  .  .  .  .  .  .  .  .  }
   197  .  .  .  .  .  .  .  .  .  .  .  Lparen: test.go:12:23
   198  .  .  .  .  .  .  .  .  .  .  .  Ellipsis: -
   199  .  .  .  .  .  .  .  .  .  .  .  Rparen: test.go:12:24
   200  .  .  .  .  .  .  .  .  .  .  }
   201  .  .  .  .  .  .  .  .  .  }
   165  .  .  .  .  .  .  .  .  .  Values: []ast.Expr (len = 1) {
   166  .  .  .  .  .  .  .  .  .  .  0: *ast.UnaryExpr {
   167  .  .  .  .  .  .  .  .  .  .  .  OpPos: <standard input>:12:10
   168  .  .  .  .  .  .  .  .  .  .  .  Op: &
   169  .  .  .  .  .  .  .  .  .  .  .  X: *ast.CallExpr {
   170  .  .  .  .  .  .  .  .  .  .  .  .  Fun: *ast.SelectorExpr {
   171  .  .  .  .  .  .  .  .  .  .  .  .  .  X: *ast.CompositeLit {
   172  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Type: *ast.Ident {
   173  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  NamePos: <standard input>:12:11
   174  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Name: "T"
   175  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Obj: *(obj @ 16)
   176  .  .  .  .  .  .  .  .  .  .  .  .  .  .  }
   177  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Lbrace: <standard input>:12:12
   178  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Elts: []ast.Expr (len = 1) {
   179  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  0: *ast.BasicLit {
   180  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  ValuePos: <standard input>:12:13
   181  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Kind: INT
   182  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Value: "1000"
   183  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  }
   184  .  .  .  .  .  .  .  .  .  .  .  .  .  .  }
   185  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Rbrace: <standard input>:12:17
   186  .  .  .  .  .  .  .  .  .  .  .  .  .  }
   187  .  .  .  .  .  .  .  .  .  .  .  .  .  Sel: *ast.Ident {
   188  .  .  .  .  .  .  .  .  .  .  .  .  .  .  NamePos: <standard input>:12:19
   189  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Name: "Id"
   190  .  .  .  .  .  .  .  .  .  .  .  .  .  }
   191  .  .  .  .  .  .  .  .  .  .  .  .  }
   192  .  .  .  .  .  .  .  .  .  .  .  .  Lparen: <standard input>:12:21
   193  .  .  .  .  .  .  .  .  .  .  .  .  Ellipsis: -
   194  .  .  .  .  .  .  .  .  .  .  .  .  Rparen: <standard input>:12:22
   195  .  .  .  .  .  .  .  .  .  .  .  }
   196  .  .  .  .  .  .  .  .  .  .  }
   197  .  .  .  .  .  .  .  .  .  }

@rsc
Copy link
Contributor

rsc commented Dec 9, 2011

Comment 3:

Labels changed: added priority-later.

@rsc
Copy link
Contributor

rsc commented Dec 12, 2011

Comment 4:

Labels changed: added priority-go1.

@robpike
Copy link
Contributor

robpike commented Jan 13, 2012

Comment 5:

Owner changed to builder@golang.org.

@griesemer
Copy link
Contributor

Comment 6:

Owner changed to @griesemer.

@rsc
Copy link
Contributor

rsc commented Jan 30, 2012

Comment 8:

Labels changed: added go1-must.

@griesemer
Copy link
Contributor

Comment 9:

Status changed to Started.

@griesemer
Copy link
Contributor

Comment 10:

This issue was closed by revision 47afa4d.

Status changed to Fixed.

@rsc rsc added this to the Go1 milestone Apr 10, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants