Skip to content

cmd/compile: principled line numbering #16943

@mdempsky

Description

@mdempsky

Currently, cmd/compile assigns line numbers somewhat ad-hocly based on whatever the global lineno value happens to be set at when it calls Nod. With the new parser, @griesemer and I want to do a better job at this. I think this will be especially useful if/once we start tracking/reporting column information; e.g., in error messages for #10324, or in export data for something like golang.org/cl/22936.

I want to propose two simple rules:

  1. Objects are associated with the identifier used to declare them; e.g., in var x int = 3, we associate the newly declared int variable with the position of the x identifier.
  2. Non-terminal grammar rules are associated with a terminal token uniquely associated with that rule, generally the first. For example, a binary expression x + y is associated with the + token, not x or y. This ensures that compound expressions like x + y + z are unambiguous (at least when column position is available).

Contrived example:

var
a,
b int =
1,
f() +
2

Implications:

  • The variables a and b are associated with the line numbers their respective identifiers.
  • Instructions to zero-initialize a and b would be associated with the var keyword (though in practice I expect they'd be optimized away).
  • The add instruction for adding 2 and 3 would be associated with the + token.
  • The call to f() is associated with the ( token.
  • Assigning the values to a and b would be associated with the = token.

As a near-term plan I'm trying to match the old behavior as much as possible to ease transition, and I'm now to the point where go build -a -toolexec='toolstash -cmp' std cmd passes with enabling the new parser. I can continue matching more obscure line number behaviors, but unless people think line numbering compatibility is really important, I don't think it's worthwhile. I'd rather focus on making sure performance is acceptable so we can switch over and continue cleaning up the compiler.

/cc @griesemer @ianlancetaylor @alandonovan

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions