From 34cbccd3411308bffecab9982a27735a56b648f3 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 18 Nov 2015 14:16:28 -0800 Subject: [PATCH] cmd/compile/internal/gc: add line numbers for complit elts if needed (addresses TODO) For #13243. Change-Id: I802cef3dad5d1236e70d0cd52047008a6a7a311a Reviewed-on: https://go-review.googlesource.com/17045 Reviewed-by: Chris Manghane --- src/cmd/compile/internal/gc/parser.go | 49 ++++++++++++++------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/cmd/compile/internal/gc/parser.go b/src/cmd/compile/internal/gc/parser.go index 94814cb539..51d04f59fe 100644 --- a/src/cmd/compile/internal/gc/parser.go +++ b/src/cmd/compile/internal/gc/parser.go @@ -659,15 +659,10 @@ func (p *parser) simple_stmt(labelOk, rangeOk bool) *Node { default: // expr - // These nodes do not carry line numbers. // Since a bare name used as an expression is an error, - // introduce a wrapper node to give the correct line. - switch lhs.Op { - case ONAME, ONONAME, OTYPE, OPACK, OLITERAL: - lhs = Nod(OPAREN, lhs, nil) - lhs.Implicit = true - } - return lhs + // introduce a wrapper node where necessary to give the + // correct line. + return wrapname(lhs) } } @@ -1626,9 +1621,29 @@ func (p *parser) keyval() *Node { defer p.trace("keyval")() } + // A composite literal commonly spans several lines, + // so the line number on errors may be misleading. + // Wrap values (but not keys!) that don't carry line + // numbers. + x := p.bare_complitexpr() + if p.got(':') { - x = Nod(OKEY, x, p.bare_complitexpr()) + // key ':' value + return Nod(OKEY, x, wrapname(p.bare_complitexpr())) + } + + // value + return wrapname(x) +} + +func wrapname(x *Node) *Node { + // These nodes do not carry line numbers. + // Introduce a wrapper node to give the correct line. + switch x.Op { + case ONAME, ONONAME, OTYPE, OPACK, OLITERAL: + x = Nod(OPAREN, x, nil) + x.Implicit = true } return x } @@ -1644,21 +1659,7 @@ func (p *parser) bare_complitexpr() *Node { return p.complitexpr() } - x := p.expr() - - // These nodes do not carry line numbers. - // Since a composite literal commonly spans several lines, - // the line number on errors may be misleading. - // Introduce a wrapper node to give the correct line. - - // TODO(gri) This is causing trouble when used for keys. Need to fix complit parsing. - // switch x.Op { - // case ONAME, ONONAME, OTYPE, OPACK, OLITERAL: - // x = Nod(OPAREN, x, nil) - // x.Implicit = true - // } - // (issue 13243) - return x + return p.expr() } // go.y:complitexpr -- 2.48.1