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

text/template: integer literals wrapped in parens don't act as untyped constants #45134

Closed
jo3-l opened this issue Mar 19, 2021 · 2 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@jo3-l
Copy link

jo3-l commented Mar 19, 2021

What version of Go are you using (go version)?

$ go version
go version go1.16.2 windows/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Joe\AppData\Local\go-build
set GOENV=C:\Users\Joe\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Joe\Documents\Code\golang\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Joe\Documents\Code\golang
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.2
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Joe\AppData\Local\Temp\go-build716891636=/tmp/go-build -gno-record-gcc-switches

What did you do?

I ran this script: https://play.golang.org/p/0nprITNVbYP

What did you expect to see?

I expected to see

template without parens executed successfully
template with parens executed successfully

printed to standard output.

Per the text/template documentation on arguments:

A boolean, string, character, integer, floating-point, imaginary
or complex constant in Go syntax. These behave like Go's untyped
constants. Note that, as in Go, whether a large integer constant
overflows when assigned or passed to a function can depend on whether
the host machine's ints are 32 or 64 bits.

As such, I thought that passing (1) to the function would have had the same effect as passing 1 - that is, both are interpreted as untyped constants and are accepted as arguments of type time.Duration. Furthermore, a similar program in Go not using text/template works fine - given a function f that accepts one argument of type time.Duration, f(1) and f((1)) both work correctly. See https://play.golang.org/p/f-BvvUqA9Y_o.

What did you see instead?

I saw

template without parens executed successfully
error executing template text with parens: template: with_parens:1:5: executing "with_parens" at <1>: wrong type for value; expected time.Duration; got int

printed to standard output, seemingly showing that (1) was not interpreted as an untyped constant while 1 was.

@cherrymui cherrymui added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 19, 2021
@cherrymui cherrymui added this to the Backlog milestone Mar 19, 2021
@cherrymui
Copy link
Member

cc @robpike @mvdan

@robpike
Copy link
Contributor

robpike commented Mar 19, 2021

I feel this is to be expected. In the template language (1) is not a parenthesized 1, it is a pipeline that evaluates to 1. The syntax of arithmetic in templates is not "standard" mathematical notation. In order to evaluate (1), it needs to have a type, and in isolation (through bottom-up execution) there is no indication to the template executor that it needs to be a time.Duration.

@robpike robpike closed this as completed Mar 19, 2021
@golang golang locked and limited conversation to collaborators Mar 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge 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