adhocteam / pushup

Pushup is for making modern, page-oriented web apps in Go
https://pushup.adhoc.dev
MIT License
844 stars 30 forks source link

`b` is a forbidden name in for loops #88

Closed lvignoli closed 1 year ago

lvignoli commented 1 year ago

In a fresh project from pushup new (on commit dc4b44b), the following index.up cannot be built:

^for b:=0; b<10; b++ {
    <p>^b</p>
}

Output error:

# example/myproject/build
index.up:2: cannot use b (variable of type int) as type io.Writer in argument to io.WriteString:
    int does not implement io.Writer (missing Write method)
index.up:2: cannot use b (variable of type int) as type io.Writer in argument to printEscaped:
    int does not implement io.Writer (missing Write method)
index.up:3: cannot use b (variable of type int) as type io.Writer in argument to io.WriteString:
    int does not implement io.Writer (missing Write method)
build command: building project: building project main executable: exit status 2

Changing the variable name to a or i builds fine.

Observed on a MacBook Pro 2021 (M1 Pro) with macOS 13.1.

paulsmith commented 1 year ago

Ouch! I knew someone would stumble on this eventually, apologies. This is due to the code generator not being hygienic, so the code you write in a Pushup page can clash with runtime code needed by Pushup itself. In this case there is literally a variable named "b" that Pushup uses for outputting the results of expressions.

lvignoli commented 1 year ago

Thanks for the very quick fix!

Are there many problematic names? Mangling all of the runtime seems unreasonable, but a larger fix seems necessary.