Skip to content

go/printer: unexpected handling of indented comment #12822

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

Open
griesemer opened this issue Oct 2, 2015 · 3 comments
Open

go/printer: unexpected handling of indented comment #12822

griesemer opened this issue Oct 2, 2015 · 3 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@griesemer
Copy link
Contributor

The comment was indented one extra tab stop beyond where it should be by gofmt.

  • Deleting one of the tabs and running gofmt again results in the proper alignment.
  • Removing the statement below the comment and re-formatting will remove the extra tab stop.
  • Changing the statement above the comment so the "func" line is on the same line as the assignment causes the extra tab to be removed as well.

Also unexpected, and the reason the bug was noticed: gofmt converts two spaces into two tabs here, which causes an unfortunate interaction with xxx (when not in tab mode) and probably other editors -- if you attempt to remove the extra indent and then re-run gofmt, you may be led to believe it's impossible to get the comment to line up as desired/expected.

package p

func f() {
    a =
        func(T1) T2 {
            return q
        }

        // Comment
    b =
        func(T1) T2 {
            return q
        }
}
@griesemer griesemer self-assigned this Oct 2, 2015
@rsc rsc changed the title cmd/gofmt: unexpected handling of indented comment go/printer: unexpected handling of indented comment Oct 23, 2015
@rsc rsc added this to the Go1.6 milestone Oct 23, 2015
@rsc
Copy link
Contributor

rsc commented Nov 30, 2015

Too late for Go 1.6.

@rsc rsc modified the milestones: Unplanned, Go1.6 Nov 30, 2015
@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 13, 2024
@linsite
Copy link
Contributor

linsite commented Mar 3, 2025

hi, @griesemer

The newly test 2025-03-03 shows it has been fixed, can you confirm this?

/home/ulin/data/go1.23/bin/gofmt: go1.23.4
path cmd/gofmt
build -buildmode=exe
build -compiler=gc
build -gcflags=cmd/...=-dwarf=false
build -trimpath=true
build DefaultGODEBUG=asynctimerchan=1,gotypesalias=0,httplaxcontentlength=1,httpmuxgo121=1,httpservecontentkeepheaders=1,netedns0=0,panicnil=1,tls10server=1,tls3des=1,tlskyber=0,tlsrsakex=1,tlsunsafeekm=1,winreadlinkvolume=0,winsymlink=0,x509keypairleaf=0,x509negativeserial=1
build CGO_ENABLED=0
build GOARCH=amd64
build GOOS=linux
build GOAMD64=v1

$ gofmt p.go
package p

func f() {
        a =
                func(T1) T2 {
                        return q
                }

        // Comment
        b =
                func(T1) T2 {
                        return q
                }
}

@linsite
Copy link
Contributor

linsite commented Mar 3, 2025

hi, @griesemer

The newly test 2025-03-03 shows it has been fixed, can you confirm this?

/home/ulin/data/go1.23/bin/gofmt: go1.23.4 path cmd/gofmt build -buildmode=exe build -compiler=gc build -gcflags=cmd/...=-dwarf=false build -trimpath=true build DefaultGODEBUG=asynctimerchan=1,gotypesalias=0,httplaxcontentlength=1,httpmuxgo121=1,httpservecontentkeepheaders=1,netedns0=0,panicnil=1,tls10server=1,tls3des=1,tlskyber=0,tlsrsakex=1,tlsunsafeekm=1,winreadlinkvolume=0,winsymlink=0,x509keypairleaf=0,x509negativeserial=1 build CGO_ENABLED=0 build GOARCH=amd64 build GOOS=linux build GOAMD64=v1

$ gofmt p.go
package p

func f() {
        a =
                func(T1) T2 {
                        return q
                }

        // Comment
        b =
                func(T1) T2 {
                        return q
                }
}

sorry for noise. it was formated by gopls, not gofmt. I'll dig deeper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants