1
0
mirror of https://github.com/golang/go synced 2024-11-24 09:30:07 -07:00

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 <cmang@golang.org>
This commit is contained in:
Robert Griesemer 2015-11-18 14:16:28 -08:00
parent 431c232842
commit 34cbccd341

View File

@ -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