Skip to content

text/template: fix truth handling of typed interface nils in if and with #30534

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

Closed
wants to merge 1 commit into from

Conversation

bep
Copy link
Contributor

@bep bep commented Mar 2, 2019

Before this commit, the two logically equivalent conditionals below would
produce different output:

{{ if not .NonEmptyInterfaceTypedNil }}OK{{ else }}{{ end }}
{{ if .NonEmptyInterfaceTypedNil }}{{ else }}OK{{ end }}

The functions not, or, and and all use the same truth function, which
unwraps any concrete interface value before passing it to isTrue.

if and with also use isTrue to establish truth, but was missing the
interface indirect call.

Fixes #30501

@googlebot googlebot added the cla: yes Used by googlebot to label PRs as having a valid CLA. The text of this label should not change. label Mar 2, 2019
@bep bep force-pushed the with-if-interface branch from 7a2f08b to d8d8c03 Compare March 2, 2019 15:40
@gopherbot
Copy link
Contributor

This PR (HEAD: d8d8c03) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/164958 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

@gopherbot
Copy link
Contributor

Message from Gobot Gobot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
Within the next week or so, a maintainer will review your change and provide
feedback. See https://golang.org/doc/contribute.html#review for more info and
tips to get your patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11, it means that this CL will be reviewed as part of the next development
cycle. See https://golang.org/s/release for more details.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Daniel Martí:

Patch Set 2:

We don't use markdown in commit messages; see https://github.com/golang/go/wiki/CommitMessage


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Bjørn Erik Pedersen:

Patch Set 3: Commit message was updated.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Bjørn Erik Pedersen:

Patch Set 3:

Patch Set 2:

We don't use markdown in commit messages; see https://github.com/golang/go/wiki/CommitMessage

I assume that was the code fence wrappers (now removed); I assume backticks around identifiers (e.g. isTru) isn't consiedered to be Markdown.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Bjørn Erik Pedersen:

Patch Set 5: Commit message was updated.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

Before this commit, the two logically equivalent conditionals below would produce different output:

{{ if not .NonEmptyInterfaceTypedNil }}OK{{ else }}{{ end }}
{{ if .NonEmptyInterfaceTypedNil }}{{ else }}OK{{ end }}

The functions `not`, `or`, and `and` all use the same `truth` function, which unwraps any concrete interface value before passing it to `isTrue`.

`if` and `with` also use `isTrue` to establish truth, but was missing the interface indirect call.

Fixes golang#30501
@bep bep force-pushed the with-if-interface branch from d8d8c03 to 95fc2c8 Compare March 2, 2019 17:11
@gopherbot
Copy link
Contributor

This PR (HEAD: 95fc2c8) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/164958 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

@gopherbot
Copy link
Contributor

Message from Bjørn Erik Pedersen:

Patch Set 7:

Patch Set 3:

Patch Set 2:

We don't use markdown in commit messages; see https://github.com/golang/go/wiki/CommitMessage

I assume that was the code fence wrappers (now removed, I hope ...); I assume backticks around identifiers (e.g. isTru) isn't consiedered to be Markdown.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Daniel Martí:

Patch Set 7:

The commit message is still like it was initially, unless I've missed something?

Remember that if you're using the PR bot, you must make all changes through the PR. It ignores commit messages; you must modify the PR title and description.

I know that's cumbersome, which is why I'd just send a CL directly.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Bjørn Erik Pedersen:

Patch Set 8: Commit message was updated.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Bjørn Erik Pedersen:

Patch Set 10: Commit message was updated.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Bjørn Erik Pedersen:

Patch Set 11:

Patch Set 7:

The commit message is still like it was initially, unless I've missed something?

Remember that if you're using the PR bot, you must make all changes through the PR. It ignores commit messages; you must modify the PR title and description.

I know that's cumbersome, which is why I'd just send a CL directly.

I tried both, but it seems to be reverted ... I will have to read up on the Gerrit documentation tomorrow, the section about how to edit the commit message ...


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Daniel Martí:

Patch Set 11:

Please note what I mentioned earlier; you must modify the PR body, i.e. the top comment's content.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Bjørn Erik Pedersen:

Patch Set 11:

Patch Set 11:

Please note what I mentioned earlier; you must modify the PR body, i.e. the top comment's content.

Yes, I understand that. I have done that several times, but the change gets reverted.

I suppose it is this issue:

#24887


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Daniel Martí:

Patch Set 12:

Seems to work fine for me :) See my edit on the PR content.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Dmitri Shuralyov:

Patch Set 11:

Please note what I mentioned earlier; you must modify the PR body, i.e. the top comment's content.

Yes, I understand that. I have done that several times, but the change gets reverted.

I see a total of two edits on the PR body:

https://user-images.githubusercontent.com/1924134/53703193-0d32a380-3ddd-11e9-8aea-6b0c86cc3dc2.png

Neither of those were reverted as far as I can see.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Bjørn Erik Pedersen:

Patch Set 13:

Patch Set 11:

Please note what I mentioned earlier; you must modify the PR body, i.e. the top comment's content.

Yes, I understand that. I have done that several times, but the change gets reverted.

I see a total of two edits on the PR body:

https://user-images.githubusercontent.com/1924134/53703193-0d32a380-3ddd-11e9-8aea-6b0c86cc3dc2.png

Neither of those were reverted as far as I can see.

OK, I got it now. The "PR body" referred to the original GitHub PR. I edited the commit message here in the Gerrit UI, which got reverted.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Dmitri Shuralyov:

Patch Set 14:

OK, I got it now. The "PR body" referred to the original GitHub PR. I edited the commit message here in the Gerrit UI, which got reverted.

Do you think we can improve the wording in the documentation (i.e.,

This PR will be imported into Gerrit with the title and first
comment (this text) used to generate the subject and body of
the Gerrit change.
and https://golang.org/wiki/GerritBot#how-does-gerritbot-determine-the-final-commit-message) to make it more clear or visible?


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Daniel Martí:

Patch Set 14:

(3 comments)

This makes sense to me, in theory. There are no comments about explicitly avoiding indirectInterface in if/with, and there are no tests locking the old behavior, so perhaps this was just due to human error.

I also tend to agree that this change makes text/template more consistent. Inside a template, all values are evaluated behind interface{}, so it's easy for nil pointers to suddenly gain a type and become non-nil when automatically translated to an interface.

On the other hand, it seems like text/template has behaved this way for a long time; at least since 2011, from what I can tell. There's a good chance that some template out there will suddenly change behavior, so this would need to be submitted early and contain a clear changelog entry for 1.13.

Adding Rob and Roger for a second opinion, but I think we can give this a go for 1.13. Worst case, if it turns out that this behavior was on purpose, or that it broke too many programs, we can revert and consider other options.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Bjørn Erik Pedersen:

Patch Set 15:

(3 comments)

Friendly bump. To me this is an obvious fix, but what do I know.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Russ Cox:

Patch Set 15: Code-Review+2

Looked at with Rob. +2


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Russ Cox:

Patch Set 15: Run-TryBot+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Gobot Gobot:

Patch Set 15:

TryBots beginning. Status page: https://farmer.golang.org/try?commit=abc6b648


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Gobot Gobot:

Patch Set 15:

Build is still in progress...
This change failed on misc-vet-vetall:
See https://storage.googleapis.com/go-build-log/abc6b648/misc-vet-vetall_50ae1850.log

Other builds still in progress; subsequent failure notices suppressed until final report. Consult https://build.golang.org/ to see whether they are new failures. Keep in mind that TryBots currently test exactly your git commit, without rebasing. If your commit's git parent is old, the failure might've already been fixed.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Gobot Gobot:

Patch Set 15: TryBot-Result-1

1 of 19 TryBots failed:
Failed on misc-vet-vetall: https://storage.googleapis.com/go-build-log/abc6b648/misc-vet-vetall_50ae1850.log

Consult https://build.golang.org/ to see whether they are new failures. Keep in mind that TryBots currently test exactly your git commit, without rebasing. If your commit's git parent is old, the failure might've already been fixed.


Please don’t reply on this GitHub thread. Visit golang.org/cl/164958.
After addressing review feedback, remember to publish your drafts!

gopherbot pushed a commit that referenced this pull request May 14, 2019
Before this commit, the two logically equivalent conditionals below would
produce different output:

    {{ if not .NonEmptyInterfaceTypedNil }}OK{{ else }}{{ end }}
    {{ if .NonEmptyInterfaceTypedNil }}{{ else }}OK{{ end }}

The functions `not`, `or`, and `and` all use the same `truth` function, which
unwraps any concrete interface value before passing it to `isTrue`.

`if` and `with` also use `isTrue` to establish truth, but was missing the
interface indirect call.

Fixes #30501

Change-Id: I9c49eed41e737d8f162e39bef1c3b82fd5518fed
GitHub-Last-Rev: 95fc2c8
GitHub-Pull-Request: #30534
Reviewed-on: https://go-review.googlesource.com/c/go/+/164958
Reviewed-by: Russ Cox <[email protected]>
Run-TryBot: Russ Cox <[email protected]>
@gopherbot
Copy link
Contributor

This PR is being closed because golang.org/cl/164958 has been merged.

@gopherbot gopherbot closed this May 14, 2019
veigaribo pushed a commit to veigaribo/template that referenced this pull request Jul 29, 2024
Before this commit, the two logically equivalent conditionals below would
produce different output:

    {{ if not .NonEmptyInterfaceTypedNil }}OK{{ else }}{{ end }}
    {{ if .NonEmptyInterfaceTypedNil }}{{ else }}OK{{ end }}

The functions `not`, `or`, and `and` all use the same `truth` function, which
unwraps any concrete interface value before passing it to `isTrue`.

`if` and `with` also use `isTrue` to establish truth, but was missing the
interface indirect call.

Fixes #30501

Change-Id: I9c49eed41e737d8f162e39bef1c3b82fd5518fed
GitHub-Last-Rev: 95fc2c82f26d24a457de4deaa7e5756718fbf07c
GitHub-Pull-Request: golang/go#30534
Reviewed-on: https://go-review.googlesource.com/c/go/+/164958
Reviewed-by: Russ Cox <[email protected]>
Run-TryBot: Russ Cox <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Used by googlebot to label PRs as having a valid CLA. The text of this label should not change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

text/template: Invalid handling off typed nils in if and with
3 participants