aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/syntax/parser.go
AgeCommit message (Collapse)Author
2021-08-25cmd/compile/internal/syntax: fix position of type parameter fieldRobert Griesemer
Change-Id: I8bca01b935301e7bd4efa55ed21921dbf31a75b9 Reviewed-on: https://go-review.googlesource.com/c/go/+/344575 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2021-08-20cmd/compile/internal/syntax: add PosBase.TrimmedMatthew Dempsky
With types2, some syntax.PosBases need to be constructed from export data, which must only contain "trimmed" filenames (i.e., that they've already been made absolute and undergone -trimpath processing). However, it's not safe to apply trimming to a filename multiple times, and in general we can't distinguish trimmed from untrimmed filenames. This CL resolves this by adding a PosBase.Trimmed boolean so we can distinguish whether the associated filename has been trimmed yet. This is a bit hacky, but is the least bad solution I've come up with so far. This unblocks enabling -G=3 by default. Change-Id: I7383becfb704680a36f7603e3246af38b21f100b Reviewed-on: https://go-review.googlesource.com/c/go/+/343731 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Matthew Dempsky <mdempsky@google.com> Trust: Dan Scales <danscales@google.com> Reviewed-by: Robert Griesemer <gri@golang.org>
2021-08-15cmd/compile/internal/types2: better error message for index syntax error ↵Robert Griesemer
(follow-up) For #47704. Change-Id: I09e6f638df0cd456a20a3b68ab55c47bb5b1f555 Reviewed-on: https://go-review.googlesource.com/c/go/+/342370 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org>
2021-08-15cmd/compile/internal/syntax: better error message for index syntax errorRobert Griesemer
Fixes #47704. Change-Id: I1de9fd00baaa4b534c23f011ade54120f5153a9d Reviewed-on: https://go-review.googlesource.com/c/go/+/342369 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
2021-08-06[dev.typeparams] cmd/compile/internal/syntax: cleanup panic callsRobert Griesemer
End-users are not expected to deal with the details of panics, so providing extra information such as an "internal error" prefix is not helpful. Matches the types2 changes made in https://golang.org/cl/339969 . Change-Id: Icb34a9daab981a84f41f8ae7ae5dc1b85b2d2c81 Reviewed-on: https://go-review.googlesource.com/c/go/+/339904 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
2021-06-05[dev.typeparams] cmd/compile/internal/syntax: not all index expressions can ↵Robert Griesemer
be instantiated types An index expression followed by an opening "{" may indicate a composite literal but only if the index expression can be a type. Exclude cases where the index expression cannot be a type (e.g. s[0], a[i+j], etc.). This leads to a better error message in code that is erroneous. Fixes #46558. Change-Id: Ida9291ca30683c211812dfb95abe4969f44c474f Reviewed-on: https://go-review.googlesource.com/c/go/+/325009 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
2021-05-19[dev.typeparams] cmd/compile/internal/syntax: accept embedded type literalsRobert Griesemer
The parser accepted embedded elements but the first term of an element had to be a ~-term or a type name. This CL fixes that. Change-Id: I013b6cdc5963fb228867ca6597f9139db2be7ec5 Reviewed-on: https://go-review.googlesource.com/c/go/+/321109 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
2021-04-28cmd/compile/internal/types2: catch unexpected expression listsRobert Griesemer
This is a modified port of the https://golang.org/cl/313909 change for go/types. - add catch-all cases for unexpected expression lists - add Checker.singleIndex function to check single indices - better syntax error handling in parser for invalid type instantiations that are missing a type argument Change-Id: I6f0f396d637ad66b79f803d886fdc20ee55a98b3 Reviewed-on: https://go-review.googlesource.com/c/go/+/314409 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
2021-04-15cmd/compile/internal/syntax: fix error message for ... without typeRobert Griesemer
Only complain about missing type; leave it to type-checking to decide whether "..." is permitted in the first place. Fixes #43674. Change-Id: Icbc8f084e364fe3ac16076406a134354219c08d0 Reviewed-on: https://go-review.googlesource.com/c/go/+/310209 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
2021-04-10cmd/compile/internal/syntax: accept "~" and "|" interface elementsRobert Griesemer
Type lists continue to be accepted as before. While at it, print missing filenames in error tests (which uses an ad-hoc position representation). Change-Id: I933b3acbc9cf1985ad8f70f6b206e3a1dbd64d1e Reviewed-on: https://go-review.googlesource.com/c/go/+/307371 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
2021-01-21[dev.typeparams] cmd/compile: use nil instead of syntax.ImplicitOneRobert Griesemer
Represent x++/-- as x +=/-= with the RHS of the assignment being nil rather than syntax.ImplicitOne. Dependent code already had to check for syntax.ImplicitOne, but then shared some existing code for regular assignment operations. Now always handle this case fully explicit, which simplifies the code. Change-Id: I28c7918153c27cbbf97b041d0c85ff027c58687c Reviewed-on: https://go-review.googlesource.com/c/go/+/285172 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2020-12-16[dev.typeparams] cmd/compile/internal/syntax: type parameters must always be ↵Robert Griesemer
named Report an error otherwise. Change-Id: Ia76ea03a3f26b13dd9bca49f7bd42101d1ff1f9e Reviewed-on: https://go-review.googlesource.com/c/go/+/278475 Trust: Robert Griesemer <gri@golang.org> Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
2020-12-15[dev.typeparams] cmd/compile/internal/types2: don't crash if import path is ↵Robert Griesemer
missing In package syntax: - fix parser appendGroup to not add nil entries - non-string paths are syntax errors per the spec; report in parser - document ImportDecl.Path invariants In package types2: - guard against absent paths In package gc: - guard against absent paths Fixes #43190. Change-Id: Ic6a06f6a96b7f519feaa1ceaf4376fc5ab0f0129 Reviewed-on: https://go-review.googlesource.com/c/go/+/278114 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
2020-12-14[dev.typeparams] cmd/compile/internal/syntax: export NewName and use itRobert Griesemer
Most syntax.Nodes are allocated in one place and there didn't seem a need to provide factory methods - so as a matter of API design, all nodes are "naked", without any constructors. However, Name nodes are frequently used/replaced and also are created as helper nodes in clients (types2). Make an exception and export NewName. Change-Id: I4b5c499d65bba74592dea68b0936c118b3edaca7 Reviewed-on: https://go-review.googlesource.com/c/go/+/277572 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
2020-10-14[dev.typeparams] cmd/compile/internal/syntax: always use IndexExpr node for ↵Robert Griesemer
type instantiation Per @mdempsky's suggestion: Instead of representing a type instantiation T[P] by an IndexExpr node, and a type instantiation with multiple type arguments T[P1, P2] by a CallExpr node with special Brackets flag, always use an IndexExpr. Use a ListExpr as index in the (less common) case of multiple type arguments. This removes the need for the CallExpr.Brackets field and cleans up the parser code around type instantiations. Backport of syntax package changes from https://golang.org/cl/262020. Change-Id: I32e8bc4eafac5b3ef2e7eb40fa8c790a5a905b69 Reviewed-on: https://go-review.googlesource.com/c/go/+/262137 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2020-10-13[dev.typeparams] cmd/compile/internal/syntax: implement parsing of type ↵Robert Griesemer
parameters Port from dev.go2go prototype branch. The compiler doesn't yet set the syntax.AllowGenerics mode, so parsing of generic code remains disabled. Known issue: The doc strings documenting the specific syntax accepted by parser methods are not all up-to-date. Change-Id: I13d134289fd9330fd0ed7f97c997cca6f23466fd Reviewed-on: https://go-review.googlesource.com/c/go/+/261658 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2020-10-13[dev.typeparams] cmd/compile/internal/syntax: prepare syntax nodes for type ↵Robert Griesemer
parameters - add TParamList fields to TypeDecl, FuncDecl - also: change File.Lines to File.EOF so we have the actual file end position Change-Id: Ia345f888080a884f7ac5cefd8bff3d80e4a59cdc Reviewed-on: https://go-review.googlesource.com/c/go/+/261657 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2020-10-12cmd/compile/internal/syntax: provide BadExpr where needed, call correct ↵Robert Griesemer
error handler - For "if" statements without a condition, provide a BadExpr rather than nil (clients expect IfStmt.Cond != nil since the parser is taking care of reporting a missing condition). - For 3-index slice expressions, also provide BadExpr where an index is required but missing. - Declare a parser-local error method to hide the embedded error method so we don't use it by mistake. Accidentally found while adjusting prototype parser to work for generics. Change-Id: Iacc211cc60869be05efe9ae630d65dff1dac00a0 Reviewed-on: https://go-review.googlesource.com/c/go/+/261218 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2020-04-21cmd/compile: detect and diagnose invalid //go: directive placementRuss Cox
Thie CL changes cmd/compile/internal/syntax to give the gc half of the compiler more control over pragma handling, so that it can prepare better errors, diagnose misuse, and so on. Before, the API between the two was hard-coded as a uint16. Now it is an interface{}. This should set us up better for future directives. In addition to the split, this CL emits a "misplaced compiler directive" error for any directive that is in a place where it has no effect. I've certainly been confused in the past by adding comments that were doing nothing and not realizing it. This should help avoid that kind of confusion. The rule, now applied consistently, is that a //go: directive must appear on a line by itself immediately before the declaration specifier it means to apply to. See cmd/compile/doc.go for precise text and test/directive.go for examples. This may cause some code to stop compiling, but that code was broken. For example, this code formerly applied the //go:noinline to f (not c) but now will fail to compile: //go:noinline const c = 1 func f() {} Change-Id: Ieba9b8d90a27cfab25de79d2790a895cefe5296f Reviewed-on: https://go-review.googlesource.com/c/go/+/228578 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2020-03-05cmd/compile/internal/syntax: faster and simpler source readerRobert Griesemer
This is one of several changes that were part of a larger rewrite which I made in early 2019 after switching to the new number literal syntax implementation. The purpose of the rewrite was to simplify reading of source code (Unicode character by character) and speed up the scanner but was never submitted for review due to other priorities. Part 3 of 3: This change contains a complete rewrite of source.go, the file that implements reading individual Unicode characters from the source. The new implementation is easier to use and has simpler literal buffer management, resulting in faster scanner and thus parser performance. Thew new source.go (internal) API is centered around nextch() which advances the scanner by one character. The scanner has been adjusted around nextch() and now consistently does one character look-ahead (there's no need for complicated ungetr-ing anymore). Only in one case backtrack is needed (when finding '..' rather than '...') and that case is now more cleanly solved with the new reset() function. Measuring line/s parsing peformance by running go test -run StdLib -fast -skip "syntax/(scanner|source)\.go" (best of 5 runs on "quiet" MacBook Pro, 3.3GHz Dual-Core i7, 16GB RAM, OS X 10.15.3) before and after shows consistently 3-5% improvement of line parsing speed: old: parsed 1788155 lines (3969 files) in 1.255520307s (1424234 lines/s) new: parsed 1788155 lines (3969 files) in 1.213197037s (1473919 lines/s) (scanner.go and parser.go are skipped because this CL changed those files.) Change-Id: Ida947f4b538d42eb2d2349062c69edb6c9e5ca66 Reviewed-on: https://go-review.googlesource.com/c/go/+/221603 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2020-02-21cmd/compile/internal/syntax: better error when an assignment is used in ↵Robert Griesemer
value context The error message is now positioned at the statement position (which is an identifing token, such as the '=' for assignments); and in case of assignments it emphasizes the assignment by putting the Lhs and Rhs in parentheses. Finally, the wording is changed from "use of * as value" to the stronger "cannot use * as value" (for which there is precedent elsewhere in the parser). Fixes #36858. Change-Id: Ic3f101bba50f58e3a1d9b29645066634631f2d61 Reviewed-on: https://go-review.googlesource.com/c/go/+/218337 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2019-08-29cmd/compile/internal/syntax: add BasicLit.Bad field for lexical errorsRobert Griesemer
The new (internal) field scanner.bad indicates whether a syntax error occurred while scanning a literal; the corresponding scanner.lit string may be syntactically incorrect in that case. Store the value of scanner.bad together with the scanner.lit in BasicLit. Clean up error handling so that all syntactic errors use one of the scanner's error reporting methods which also set scanner.bad. Make use of the new field in a few places where we used to track a prior error separately. Preliminary step towards fixing #32133 in a comprehensive manner. Change-Id: I4d79ad6e3b50632dd5fb3fc32ca3df0598ee77b4 Reviewed-on: https://go-review.googlesource.com/c/go/+/192278 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2019-08-27cmd/compile/internal/syntax: better error recovery after missing expressionRobert Griesemer
Don't skip closing parentheses of any kind after a missing expression. They are likely part of the lexical construct enclosing the expression. Fixes #33386. Change-Id: Ic0abc2037ec339a345ec357ccc724b7ad2a64c00 Reviewed-on: https://go-review.googlesource.com/c/go/+/188502 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2019-04-03cmd/compile: better recovery after := (rather than =) in declarationsRobert Griesemer
Before this fix, a mistaken := in a (const/type/var) declaration ended that declaration with an error from which the parser didn't recover well. This low-cost change will provide a better error message and lets the parser recover perfectly. Fixes #31092. Change-Id: Ic4f94dc5e29dd00b7ef6d53a80dded638e3cea80 Reviewed-on: https://go-review.googlesource.com/c/go/+/169958 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2018-04-03cmd/compile/internal/syntax: better error message for incorrect if/switch headerRobert Griesemer
Fixes #23664. Change-Id: Ic0637e9f896b2fc6502dfbab2d1c4de3c62c0bd2 Reviewed-on: https://go-review.googlesource.com/104616 Reviewed-by: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Robert Griesemer <gri@golang.org>
2018-04-03cmd/compile: better handling of incorrect type switchesRobert Griesemer
Don't report errors if we don't have a correct type switch guard; instead ignore it and leave it to the type-checker to report the error. This leads to better error messages concentrating on the type switch guard rather than errors around (confusing) syntactic details. Also clean up some code setting up AssertExpr (they never have a nil Type field) and remove some incorrect TODOs. Fixes #24470. Change-Id: I69512f36e0417e3b5ea9c8856768e04b19d654a8 Reviewed-on: https://go-review.googlesource.com/103615 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2018-02-28cmd/compile, cmd/compile/internal/syntax: print relative column infoRobert Griesemer
This change enables printing of relative column information if a prior line directive specified a valid column. If there was no line directive, or the line directive didn't specify a column (or the -C flag is specified), no column information is shown in file positions. Implementation: Column values (and line values, for that matter) that are zero are interpreted as "unknown". A line directive that doesn't specify a column records that as a zero column in the respective PosBase data structure. When computing relative columns, a relative value is zero of the base's column value is zero. When formatting a position, a zero column value is not printed. To make this work without special cases, the PosBase for a file is given a concrete (non-0:0) position 1:1 with the PosBase's line and column also being 1:1. In other words, at the position 1:1 of a file, it's relative positions are starting with 1:1 as one would expect. In the package syntax, this requires self-recursive PosBases for file bases, matching what cmd/internal/src.PosBase was already doing. In src.PosBase, file and inlining bases also need to be based at 1:1 to indicate "known" positions. This change completes the cmd/compiler part of the issue below. Fixes #22662. Change-Id: I6c3d2dee26709581fba0d0261b1d12e93f1cba1a Reviewed-on: https://go-review.googlesource.com/97375 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2018-02-26cmd/compile/internal/syntax: implement //line :line:col handlingRobert Griesemer
For line directives which have a line and a column number, an omitted filename means that the filename has not changed (per the issue below). For line directives w/o a column number, an omitted filename means the empty filename (to preserve the existing behavior). For #22662. Change-Id: I32cd9037550485da5445a34bb104706eccce1df1 Reviewed-on: https://go-review.googlesource.com/96476 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2018-02-26cmd/compile/internal/syntax: remove dependency on cmd/internal/srcRobert Griesemer
For dependency reasons, the data structure implementing source positions in the compiler is in cmd/internal/src. It contains highly compiler specific details (e.g. inlining index). This change introduces a parallel but simpler position representation, defined in the syntax package, which removes that package's dependency on cmd/internal/src, and also removes the need to deal with certain filename-specific operations (defined by the needs of the compiler) in the syntax package. As a result, the syntax package becomes again a compiler- independent, stand-alone package that at some point might replace (or augment) the existing top-level go/* syntax-related packages. Additionally, line directives that update column numbers are now correctly tracked through the syntax package, with additional tests added. (The respective changes also need to be made in cmd/internal/src; i.e., the compiler accepts but still ignores column numbers in line directives.) This change comes at the cost of a new position translation step, but that step is cheap because it only needs to do real work if the position base changed (i.e., if there is a new file, or new line directive). There is no noticeable impact on overall compiler performance measured with `compilebench -count 5 -alloc`: name old time/op new time/op delta Template 220ms ± 8% 228ms ±18% ~ (p=0.548 n=5+5) Unicode 119ms ±11% 113ms ± 5% ~ (p=0.056 n=5+5) GoTypes 684ms ± 6% 677ms ± 3% ~ (p=0.841 n=5+5) Compiler 3.19s ± 7% 3.01s ± 1% ~ (p=0.095 n=5+5) SSA 7.92s ± 8% 7.79s ± 1% ~ (p=0.690 n=5+5) Flate 141ms ± 7% 139ms ± 4% ~ (p=0.548 n=5+5) GoParser 173ms ±12% 171ms ± 4% ~ (p=1.000 n=5+5) Reflect 417ms ± 5% 411ms ± 3% ~ (p=0.548 n=5+5) Tar 205ms ± 5% 198ms ± 2% ~ (p=0.690 n=5+5) XML 232ms ± 4% 229ms ± 4% ~ (p=0.690 n=5+5) StdCmd 28.7s ± 5% 28.2s ± 2% ~ (p=0.421 n=5+5) name old user-time/op new user-time/op delta Template 269ms ± 4% 265ms ± 5% ~ (p=0.421 n=5+5) Unicode 153ms ± 7% 149ms ± 3% ~ (p=0.841 n=5+5) GoTypes 850ms ± 7% 862ms ± 4% ~ (p=0.841 n=5+5) Compiler 4.01s ± 5% 3.86s ± 0% ~ (p=0.190 n=5+4) SSA 10.9s ± 4% 10.8s ± 2% ~ (p=0.548 n=5+5) Flate 166ms ± 7% 167ms ± 6% ~ (p=1.000 n=5+5) GoParser 204ms ± 8% 206ms ± 7% ~ (p=0.841 n=5+5) Reflect 514ms ± 5% 508ms ± 4% ~ (p=0.548 n=5+5) Tar 245ms ± 6% 244ms ± 3% ~ (p=0.690 n=5+5) XML 280ms ± 4% 278ms ± 4% ~ (p=0.841 n=5+5) name old alloc/op new alloc/op delta Template 37.9MB ± 0% 37.9MB ± 0% ~ (p=0.841 n=5+5) Unicode 28.8MB ± 0% 28.8MB ± 0% ~ (p=0.841 n=5+5) GoTypes 113MB ± 0% 113MB ± 0% ~ (p=0.151 n=5+5) Compiler 468MB ± 0% 468MB ± 0% -0.01% (p=0.032 n=5+5) SSA 1.50GB ± 0% 1.50GB ± 0% ~ (p=0.548 n=5+5) Flate 24.4MB ± 0% 24.4MB ± 0% ~ (p=1.000 n=5+5) GoParser 30.7MB ± 0% 30.7MB ± 0% ~ (p=1.000 n=5+5) Reflect 76.5MB ± 0% 76.5MB ± 0% ~ (p=0.548 n=5+5) Tar 38.9MB ± 0% 38.9MB ± 0% ~ (p=0.222 n=5+5) XML 41.6MB ± 0% 41.6MB ± 0% ~ (p=0.548 n=5+5) name old allocs/op new allocs/op delta Template 382k ± 0% 382k ± 0% +0.01% (p=0.008 n=5+5) Unicode 343k ± 0% 343k ± 0% ~ (p=0.841 n=5+5) GoTypes 1.19M ± 0% 1.19M ± 0% +0.01% (p=0.008 n=5+5) Compiler 4.53M ± 0% 4.53M ± 0% +0.03% (p=0.008 n=5+5) SSA 12.4M ± 0% 12.4M ± 0% +0.00% (p=0.008 n=5+5) Flate 235k ± 0% 235k ± 0% ~ (p=0.079 n=5+5) GoParser 318k ± 0% 318k ± 0% ~ (p=0.730 n=5+5) Reflect 978k ± 0% 978k ± 0% ~ (p=1.000 n=5+5) Tar 393k ± 0% 393k ± 0% ~ (p=0.056 n=5+5) XML 405k ± 0% 405k ± 0% ~ (p=0.548 n=5+5) name old text-bytes new text-bytes delta HelloSize 672kB ± 0% 672kB ± 0% ~ (all equal) CmdGoSize 7.12MB ± 0% 7.12MB ± 0% ~ (all equal) name old data-bytes new data-bytes delta HelloSize 133kB ± 0% 133kB ± 0% ~ (all equal) CmdGoSize 390kB ± 0% 390kB ± 0% ~ (all equal) name old exe-bytes new exe-bytes delta HelloSize 1.07MB ± 0% 1.07MB ± 0% ~ (all equal) CmdGoSize 11.2MB ± 0% 11.2MB ± 0% ~ (all equal) Passes toolstash compare. For #22662. Change-Id: I19edb53dd9675af57f7122cb7dba2a6d8bdcc3da Reviewed-on: https://go-review.googlesource.com/94515 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2018-02-21cmd/compile/internal/syntax: simpler position base update for line ↵Robert Griesemer
directives (cleanup) The existing code was somewhat convoluted and made several assumptions about the encoding of position bases: 1) The position's base for a file contained a position whose base pointed to itself (which is true but an implementation detail of src.Pos). 2) Updating the position base for a line directive required finding the base of the most recent's base position. This change simply stores the file's position base and keeps using it directly for each line directive (instead of getting it from the most recently updated base). Change-Id: I4d80da513bededb636eab0ce53257fda73f0dbc0 Reviewed-on: https://go-review.googlesource.com/95736 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2018-02-15cmd/compile/internal/syntax: more tolerant handling of missing function ↵Robert Griesemer
invocation in go/defer Assume that an expression that is not a function call in a defer/go statement is indeed a function that is just missing its invocation. Report the error but continue with a sane syntax tree. Fixes #23586. Change-Id: Ib45ebac57c83b3e39ae4a1b137ffa291dec5b50d Reviewed-on: https://go-review.googlesource.com/94156 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2018-02-14cmd/compile/internal/syntax: follow Go naming conventions for error methodsRobert Griesemer
Also, remove parser.error method (in favor of parser.errorAt) as it's only used twice. This is a purely cosmetic change. Change-Id: Idb3b8b50f1c2e4d10de2ffb1c1184ceba8f7de8a Reviewed-on: https://go-review.googlesource.com/94030 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2018-02-12cmd/compile/internal/syntax: implement comment reporting in scannerRobert Griesemer
R=go1.11 In order to collect comments in the AST and for error testing purposes, the scanner needs to not only recognize and skip comments, but also be able to report them if so desired. This change adds a mode flag to the scanner's init function which controls the scanner behavior around comments. In the common case where comments are not needed, there must be no significant overhead. Thus, comments are reported via a handler upcall rather than being returned as a _Comment token (which the parser would have to filter out with every scanner.next() call). Because the handlers for error messages, directives, and comments all look the same (they take a position and text), and because directives look like comments, and errors never start with a '/', this change simplifies the scanner's init call to only take one (error) handler instead of 2 or 3 different handlers with identical signature. It is trivial in the handler to determine if we have an error, directive, or general comment. Finally, because directives are comments, when reporting directives the full comment text is returned now rather than just the directive text. This simplifies the implementation and makes the scanner API more regular. Furthermore, it provides important information about the comment style used by a directive, which may matter eventually when we fully implement /*line file:line:col*/ directives. Change-Id: I2adbfcebecd615e4237ed3a832b6ceb9518bf09c Reviewed-on: https://go-review.googlesource.com/88215 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2018-02-12cmd/compile/internal/syntax: better error recovery after missing typeRobert Griesemer
R=go1.11. This is just a copy of the fix for #23434: https://go-review.googlesource.com/c/go/+/87898. Test pending test harness for the syntax package. Change-Id: I52409aebe13ec784ddd7e41190a81c7e126bdd0c Reviewed-on: https://go-review.googlesource.com/87901 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2018-02-12cmd/compile/internal/syntax: better error msg for some 'if' statementsRobert Griesemer
R=go1.11 A common error is to write '=' instead of '==' inside the condition of a simple 'if' statement: if x = 0 { ... } Highlight the fact that we have an assignment in the error message to prevent further confusion. Fixes #23385. Change-Id: I1552050fd6da927bd12a1be0977bd2e98eca5885 Reviewed-on: https://go-review.googlesource.com/87316 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2018-02-12cmd/compile/internal/syntax: permit /*line file:line:col*/ directivesRobert Griesemer
R=go1.11 This implements parsing of /*line file:line*/ and /*line file:line:col*/ directives and also extends the optional column format to regular //line directives, per #22662. For a line directive to be recognized, its comment text must start with the prefix "line " which is followed by one of the following: :line :line:col filename:line filename:line:col with at least one : present. The line and col values must be unsigned decimal integers; everything before is considered part of the filename. Valid line directives are: //line :123 //line :123:8 //line foo.go:123 //line C:foo.go:123 (filename is "C:foo.go") //line C:foo.go:123:8 (filename is "C:foo.go") /*line ::123*/ (filename is ":") No matter the comment format, at the moment all directives act as if they were in //line comments, and column information is ignored. To be addressed in subsequent CLs. For #22662. Change-Id: I1a2dc54bacc94bc6cdedc5229ee13278971f314e Reviewed-on: https://go-review.googlesource.com/86037 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2018-01-11cmd/compile/internal/syntax: don't record semi position if there's noneRobert Griesemer
Fixes #23406. Change-Id: Ief04e20357c9ca03a5e496f1742428394c8ee658 Reviewed-on: https://go-review.googlesource.com/87317 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-11-13cmd/compile: record original and absolute file names for line directivesgriesemer
Also, with this change, error locations don't print absolute positions in [] brackets following positions relative to line directives. To get the absolute positions as well, specify the -L flag. Fixes #22660. Change-Id: I9ecfa254f053defba9c802222874155fa12fee2c Reviewed-on: https://go-review.googlesource.com/77090 Reviewed-by: David Crawshaw <crawshaw@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2017-11-06cmd/compile/internal/syntax: better syntax errors for typos in if/switch/for ↵griesemer
headers Be more pessimistic when parsing if/switch/for headers for better error messages when things go wrong. Fixes #22581. Change-Id: Ibb99925291ff53f35021bc0a59a4c9a7f695a194 Reviewed-on: https://go-review.googlesource.com/76290 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-10-17cmd/compile/internal/parser: removed TODO (cleanup)griesemer
When an opening "{" of a block is missing and after advancing we find a closing "}", it's likely better to assume the end of the block. Fixed and removed TODO. Change-Id: I20c9b4ecca798933a7cd4cbf21185bd4ca04f5f7 Reviewed-on: https://go-review.googlesource.com/71291 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-10-17cmd/compile/internal/parser: use same logic for stmtList as for other lists ↵griesemer
(cleanup) Change-Id: I2c2571b33603f0fd0ba5a79400da7b845d246b8c Reviewed-on: https://go-review.googlesource.com/71290 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-10-17cmd/compile/internal/parser: removed TODO (cleanup)griesemer
- checking for the correct closing token leads to slightly better behavior for some randomly bogus programs - removed `switch` in favor of an `if` statement Follow-up on https://go-review.googlesource.com/c/go/+/71250. Change-Id: I47f6c47b43baf790907f55ed97a947661687a9db Reviewed-on: https://go-review.googlesource.com/71252 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-10-17cmd/compile/internal/syntax: better recovery after missing closing parenthesesgriesemer
Fine-tune skipping of tokens after missing closing parentheses in lists. Fixes #22164. Change-Id: I575d86e21048cd40340a2c08399e8b0deec337cf Reviewed-on: https://go-review.googlesource.com/71250 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-10-16cmd/compile/internal/syntax: factor out list parsinggriesemer
Instead of repeating the same list parsing pattern for parenthesized of braced comma or semicolon-separated lists, introduce a single list parsing function that can be parametrized and which takes a closure to parse list elements. This ensures the same error handling and recovery logic is used across all lists and simplifies the code. No semantic change. Change-Id: Ia738d354d6c2e0c3d84a5f1c7269a6eb95685edc Reviewed-on: https://go-review.googlesource.com/70492 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-10-16cmd/compile/internal/syntax: match argument and parameter parsing (cleanup)griesemer
No semantic change. Move functionality not related to argument out of the argument parsing function, and thus match parameter parsing. Also, use a better function name. Change-Id: Ic550875251d64e6fe1ebf91c11d33a9e4aec9fdd Reviewed-on: https://go-review.googlesource.com/70491 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-10-16cmd/compile/internal/syntax: cleanups around parser tracinggriesemer
These changes affect the parser only when the internal trace constant is set. - factored our printing code used for tracing - streamlined advance function and added trace output The parser's trace output now more clearly prints what tokens are skipped and which is the next token in case of an error. Example trace: 4: . . . . . . . . . . call ( 4: . . . . . . . . . . . expr ( 4: . . . . . . . . . . . . unaryExpr ( 4: . . . . . . . . . . . . . pexpr ( 4: . . . . . . . . . . . . . . operand name ( 4: . . . . . . . . . . . . . . ) 4: . . . . . . . . . . . . . . call ( 4: . . . . . . . . . . . . . . ) 4: . . . . . . . . . . . . . ) 4: . . . . . . . . . . . . ) 4: . . . . . . . . . . . ) 4: . . . . . . . . . . . syntax error: expecting comma or ) 4: . . . . . . . . . . . skip ; 6: . . . . . . . . . . . skip name 6: . . . . . . . . . . . skip := 6: . . . . . . . . . . . skip literal 6: . . . . . . . . . . . skip ; 7: . . . . . . . . . . . skip } 7: . . . . . . . . . . . skip ; 9: . . . . . . . . . . . skip func 9: . . . . . . . . . . . skip name 9: . . . . . . . . . . . skip ( 9: . . . . . . . . . . . next ) 9: . . . . . . . . . . ) For #22164. Change-Id: I4a233696b1f989ee3287472172afaf92cf424565 Reviewed-on: https://go-review.googlesource.com/70490 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-10-11cmd/compile/internal/syntax: consider function nesting for error recoverygriesemer
This re-enables functionality that inadvertently was disabled in the (long) past. Also, don't perform branch checks if we had errors in a function to avoid spurious errors or (worst-case) crashes. Slightly modified test/fixedbugs/issue14006.go to make sure the test still reports invalid label errors (the surrounding function must be syntactically correct). Change-Id: Id5642930877d7cf3400649094ec75c753b5084b7 Reviewed-on: https://go-review.googlesource.com/69770 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-10-11cmd/compile/internal/syntax: factor out parsing of func bodies (cleanup)griesemer
Change-Id: If6481a5401940a923fc9a104981dfb90eed0d1ac Reviewed-on: https://go-review.googlesource.com/69750 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-10-11cmd/compile/internal/syntax: remove some outdated comments (cleanup)griesemer
Change-Id: If242bb99d501420827b764c908580f2363e01ac4 Reviewed-on: https://go-review.googlesource.com/69730 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-10-09cmd/compile/internal/syntax: remove unused codegriesemer
Change-Id: I9c75dee7e4498cc11c08cad1ae34ff2af75f1469 Reviewed-on: https://go-review.googlesource.com/69071 Reviewed-by: Matthew Dempsky <mdempsky@google.com>