[go] go/parser: don't parse a nil IndexExpr.Index

85 views
Skip to first unread message

Robert Findley (Gerrit)

unread,
Apr 30, 2021, 5:05:04 PM4/30/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Robert Findley has uploaded this change for review.

View Change

go/parser: don't parse a nil IndexExpr.Index

When parsing type parameters, an empty type instantiation was parsed as
an IndexExpr with nil Index. This should be considered a breaking change
to parsing: ast.Walk previously assumed that Index was non-nil.

Back out the nil check in ast.Walk, and for now pack an empty argument
list as a non-nil ListExpr with nil Elems.

Alternatives considered:
- Parsing the entire index expression as a BadExpr: this led to
inferior errors while type checking.
- Parsing the Index as a BadExpr: this seems reasonable, but encodes
strictly less information into the AST.

We may want to opt for one of these alternatives in the future, but for
now let's just fix the breaking change.

Change-Id: I93f2b89641692ac014b8ee98bfa031ed3477afb8
---
M src/go/ast/walk.go
M src/go/internal/typeparams/notypeparams.go
M src/go/internal/typeparams/typeparams.go
M src/go/parser/parser.go
4 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/src/go/ast/walk.go b/src/go/ast/walk.go
index ac1395f..9224264 100644
--- a/src/go/ast/walk.go
+++ b/src/go/ast/walk.go
@@ -112,11 +112,7 @@

case *IndexExpr:
Walk(v, n.X)
- // n.Index may be nil for invalid type instantiation expressions, e.g.
- // var x T[].
- if n.Index != nil {
- Walk(v, n.Index)
- }
+ Walk(v, n.Index)

case *SliceExpr:
Walk(v, n.X)
diff --git a/src/go/internal/typeparams/notypeparams.go b/src/go/internal/typeparams/notypeparams.go
index a8c25ac..2ceafaa 100644
--- a/src/go/internal/typeparams/notypeparams.go
+++ b/src/go/internal/typeparams/notypeparams.go
@@ -15,8 +15,6 @@

func PackExpr(list []ast.Expr) ast.Expr {
switch len(list) {
- case 0:
- return nil
case 1:
return list[0]
default:
diff --git a/src/go/internal/typeparams/typeparams.go b/src/go/internal/typeparams/typeparams.go
index 66f66af..871e95d 100644
--- a/src/go/internal/typeparams/typeparams.go
+++ b/src/go/internal/typeparams/typeparams.go
@@ -17,7 +17,10 @@
func PackExpr(list []ast.Expr) ast.Expr {
switch len(list) {
case 0:
- return nil
+ // Return an empty ListExpr here, rather than nil, as IndexExpr.Index must
+ // never be nil.
+ // TODO(rFindley) would a BadExpr be more appropriate here?
+ return &ast.ListExpr{}
case 1:
return list[0]
default:
diff --git a/src/go/parser/parser.go b/src/go/parser/parser.go
index 36a044e..3965641 100644
--- a/src/go/parser/parser.go
+++ b/src/go/parser/parser.go
@@ -1095,6 +1095,7 @@
}

func (p *parser) parseTypeInstance(typ ast.Expr) ast.Expr {
+ assert(p.parseTypeParams(), "parseTypeInstance while not parsing type params")
if p.trace {
defer un(trace(p, "TypeInstance"))
}

To view, visit change 315851. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I93f2b89641692ac014b8ee98bfa031ed3477afb8
Gerrit-Change-Number: 315851
Gerrit-PatchSet: 1
Gerrit-Owner: Robert Findley <rfin...@google.com>
Gerrit-Reviewer: Robert Findley <rfin...@google.com>
Gerrit-MessageType: newchange

Robert Findley (Gerrit)

unread,
May 5, 2021, 10:42:48 AM5/5/21
to goph...@pubsubhelper.golang.org, Go Bot, Robert Griesemer, golang-co...@googlegroups.com

Attention is currently required from: Robert Griesemer.

View Change

1 comment:

  • Patchset:

To view, visit change 315851. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I93f2b89641692ac014b8ee98bfa031ed3477afb8
Gerrit-Change-Number: 315851
Gerrit-PatchSet: 1
Gerrit-Owner: Robert Findley <rfin...@google.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Robert Findley <rfin...@google.com>
Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
Gerrit-Attention: Robert Griesemer <g...@golang.org>
Gerrit-Comment-Date: Wed, 05 May 2021 14:42:43 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Robert Griesemer (Gerrit)

unread,
May 5, 2021, 12:06:00 PM5/5/21
to Robert Findley, goph...@pubsubhelper.golang.org, Robert Griesemer, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Robert Findley.

Patch set 1:Code-Review +2Trust +1

View Change

2 comments:

  • Patchset:

  • File src/go/internal/typeparams/typeparams.go:

    • Patch Set #1, Line 23: ListExpr

      I'm concerned that this will cause other problems with code that assumes a ListExpr is never empty.

To view, visit change 315851. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I93f2b89641692ac014b8ee98bfa031ed3477afb8
Gerrit-Change-Number: 315851
Gerrit-PatchSet: 1
Gerrit-Owner: Robert Findley <rfin...@google.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Robert Findley <rfin...@google.com>
Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
Gerrit-Attention: Robert Findley <rfin...@google.com>
Gerrit-Comment-Date: Wed, 05 May 2021 16:05:55 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

Robert Findley (Gerrit)

unread,
May 5, 2021, 4:58:36 PM5/5/21
to goph...@pubsubhelper.golang.org, Robert Griesemer, Go Bot, golang-co...@googlegroups.com

View Change

1 comment:

  • File src/go/internal/typeparams/typeparams.go:

    • Patch Set #1, Line 23: ListExpr

      I'm concerned that this will cause other problems with code that assumes a ListExpr is never empty.

    • Ack. I think for now I'll merge this, but work on that in a follow-up (perhaps we should just parse a BadExpr after all).

      For now, the priority is ensuring that invariants are not broken in 1.17.

To view, visit change 315851. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I93f2b89641692ac014b8ee98bfa031ed3477afb8
Gerrit-Change-Number: 315851
Gerrit-PatchSet: 1
Gerrit-Owner: Robert Findley <rfin...@google.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Robert Findley <rfin...@google.com>
Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
Gerrit-Comment-Date: Wed, 05 May 2021 20:58:32 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Robert Griesemer <g...@golang.org>
Gerrit-MessageType: comment

Robert Findley (Gerrit)

unread,
May 5, 2021, 4:58:45 PM5/5/21
to goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Robert Griesemer, Go Bot, golang-co...@googlegroups.com

Robert Findley submitted this change.

View Change

Approvals: Robert Griesemer: Looks good to me, approved; Trusted Robert Findley: Trusted; Run TryBots Go Bot: TryBots succeeded
go/parser: don't parse a nil IndexExpr.Index

When parsing type parameters, an empty type instantiation was parsed as
an IndexExpr with nil Index. This should be considered a breaking change
to parsing: ast.Walk previously assumed that Index was non-nil.

Back out the nil check in ast.Walk, and for now pack an empty argument
list as a non-nil ListExpr with nil Elems.

Alternatives considered:
- Parsing the entire index expression as a BadExpr: this led to
inferior errors while type checking.
- Parsing the Index as a BadExpr: this seems reasonable, but encodes
strictly less information into the AST.

We may want to opt for one of these alternatives in the future, but for
now let's just fix the breaking change.

Change-Id: I93f2b89641692ac014b8ee98bfa031ed3477afb8
Reviewed-on: https://go-review.googlesource.com/c/go/+/315851
Trust: Robert Findley <rfin...@google.com>
Trust: Robert Griesemer <g...@golang.org>
Run-TryBot: Robert Findley <rfin...@google.com>
TryBot-Result: Go Bot <go...@golang.org>
Reviewed-by: Robert Griesemer <g...@golang.org>

To view, visit change 315851. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I93f2b89641692ac014b8ee98bfa031ed3477afb8
Gerrit-Change-Number: 315851
Gerrit-PatchSet: 2
Gerrit-Owner: Robert Findley <rfin...@google.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Robert Findley <rfin...@google.com>
Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
Gerrit-MessageType: merged
Reply all
Reply to author
Forward
0 new messages