summaryrefslogtreecommitdiff
path: root/src/go/parser/parser.go
Commit message (Collapse)AuthorAgeFilesLines
* go/parser: report //go:build-derived Go version in ast.File.GoVersionRuss Cox2023-04-131-6/+24
| | | | | | | | | | | | | | | | | | | | | | | | | | For #57001, compilers and others tools will need to understand that a different Go version can be used in different files in a program, according to the //go:build lines in those files. Update go/parser to populate the new ast.File.GoVersion field. This requires running the go/scanner in ParseComments mode always and then implementing discarding of comments in the parser instead of the scanner. The same work is done either way, since the scanner was already preparing the comment result and then looping. The loop has just moved into go/parser. Also make the same changes to cmd/compile/internal/syntax, both because they're necessary and to keep in sync with go/parser. For #59033. Change-Id: I7b867f5f9aaaccdca94af146b061d16d9a3fd07f Reviewed-on: https://go-review.googlesource.com/c/go/+/476277 Auto-Submit: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
* all: fix misuses of "a" vs "an"cui fliter2023-04-041-1/+1
| | | | | | | | | | | | | | Fixes the misuse of "a" vs "an", according to English grammatical expectations and using https://www.a-or-an.com/ Change-Id: I53ac724070e3ff3d33c304483fe72c023c7cda47 Reviewed-on: https://go-review.googlesource.com/c/go/+/480536 Run-TryBot: shuang cui <imcusg@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
* go/parser: allow trailing commas in embedded instantiated typescia-rana2022-11-171-2/+10
| | | | | | | | | | | | | | | | | | | | | | | go/parser can correctly parse interfaces that instantiate and embed generic interfaces, but not structs. This is because in the case of structs, it does not expect RBRACK as a token trailing COMMA in the type argument, even though it is allowed by the spec. For example, go/parser produces an error for the type declaration below: type A struct { B[byte, []byte,] } Fixes #56748 Change-Id: Ibb2addd6cf9b381d8470a6d20eedb93f13f93cd6 Reviewed-on: https://go-review.googlesource.com/c/go/+/450175 Run-TryBot: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
* go/parser: add missing parenthesis in a commentishwargowda2022-11-021-1/+1
| | | | | | | | | | | | Change-Id: I30783aa6a13ad8348fa24b27672d542a868f96de GitHub-Last-Rev: c4584ad9dab9736275c2f2554affb6af214f82dd GitHub-Pull-Request: golang/go#56526 Reviewed-on: https://go-review.googlesource.com/c/go/+/447217 Auto-Submit: Robert Griesemer <gri@google.com> Run-TryBot: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Bryan Mills <bcmills@google.com>
* go/parser: simplify code (cleanup)Park Zhou2022-10-111-2/+1
| | | | | | | | | | Change-Id: I0c8823b9c3c12f0f581b24db6a7aa5a0cd913224 Reviewed-on: https://go-review.googlesource.com/c/go/+/407537 Auto-Submit: Robert Griesemer <gri@google.com> Run-TryBot: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
* go/ast: record start and end of file in File.File{Start,End}Alan Donovan2022-09-281-6/+8
| | | | | | | | | | | | | | | | | | | | This change causes the parser to record the positions of the first and last character in the file in new ast.File fields FileStart and FileEnd. The behavior of the existing Pos() and End() methods, which record the span of declarations, must remain unchanged for compatibility. Fixes golang/go#53202 Change-Id: I250b19e69f41e3590292c3fe6dea1943ec98f629 Reviewed-on: https://go-review.googlesource.com/c/go/+/427955 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@google.com> Run-TryBot: Alan Donovan <adonovan@google.com> Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Robert Findley <rfindley@google.com>
* go/scanner: emit implicit semicolon tokens in correct orderAlan Donovan2022-09-271-17/+24
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Prior to this change, the scanner, in ScanComments mode, would emit the implicit SEMICOLON token generated by a newline ahead of any immediately preceding comment "tokens". For example: foo /*a*/ /*b*/ /*c*/ \n => [IDENT SEMICOLON COMMENT COMMENT COMMENT] Now, tokens and comments are emitted by the scanner in lexical order of their start position. SEMICOLON tokens corresponding to newlines always have the position of the newline (even in the special case in which the newline is in the middle of a general comment /*\n*/). The scanner no longer needs to look ahead, possibly across multiple comments, for a newline, when it encounters a comment. The scanner semicolon tests have been rewritten to be less magical. The parser must now expect line comments before an implicit semicolon. Line comments for an explicit semicolon still appear after. The new assertions in the parser TestLeadAndLineComments are not changes to behavior. Fixes golang/go#54941 Change-Id: Iffe97fd10e9e0b52882da8659307698ccb31c093 Reviewed-on: https://go-review.googlesource.com/c/go/+/429635 Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Alan Donovan <adonovan@google.com>
* go/ast: add Range token.Pos to RangeStmtcuiweixie2022-09-051-0/+1
| | | | | | | | | | | | For #50429 Change-Id: Idb027244f901d9f482c894b5b979a054d0f07de5 Reviewed-on: https://go-review.googlesource.com/c/go/+/426091 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Robert Findley <rfindley@google.com>
* go/parser: match syntax parser error for unnamed type parametersRobert Griesemer2022-09-021-2/+2
| | | | | | | | | | | | For #54511. Change-Id: I1ae391b5f157bf688f9f31b1577c90e681b6df26 Reviewed-on: https://go-review.googlesource.com/c/go/+/426655 Reviewed-by: Robert Griesemer <gri@google.com> Run-TryBot: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Robert Griesemer <gri@google.com>
* go/parser: more tolerant parsing of import declarationsRobert Griesemer2022-09-021-13/+21
| | | | | | | | | | | | | | | | | | | | This is a port of CL 427156 from the syntax package's parser to go/parser. While at it, remove an unused token.Pos parameter from parseSpecFunction and dependent declarations. Also, consolidate the respective test file. For #54511. Change-Id: Id6a28eb3d23a46fa5fa1d85d2c4e634b7015513c Reviewed-on: https://go-review.googlesource.com/c/go/+/427157 Run-TryBot: Robert Griesemer <gri@google.com> Auto-Submit: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com>
* go/parser: parse import specs the same way as the syntax parserRobert Griesemer2022-09-021-3/+10
| | | | | | | | | | | | | | | This results in better error recovery and allows us to use the same tests for go/types and types2. For #54511. Change-Id: Ic11a9dafb8c62e0cb952b3924d55a28b438241c6 Reviewed-on: https://go-review.googlesource.com/c/go/+/427154 Run-TryBot: Robert Griesemer <gri@google.com> Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@google.com>
* go/parser: leave checking of LHS in short var decls to type checkerRobert Griesemer2022-09-011-18/+2
| | | | | | | | | | | | | | | | | | | Instead of checking at parse-time that the LHS of a short variable declaration contains only identifiers, leave the check to the the type checker which tests this already. This removes a duplicate error and matches the behavior of the syntax package. For #54511. Change-Id: I4c68f2bd8a0e015133685f9308beb98e714a83fc Reviewed-on: https://go-review.googlesource.com/c/go/+/426476 Run-TryBot: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@google.com> Auto-Submit: Robert Griesemer <gri@google.com>
* go/parser: check that go/defer expressions are not parenthesizedRobert Griesemer2022-09-011-0/+4
| | | | | | | | | | | | | | | | Logic matches the code in the syntax package. This error was missing from go/parser and go/types. Added some tests. For #54511. Change-Id: I418de4bd4c7169457b424366caae70227a92a761 Reviewed-on: https://go-review.googlesource.com/c/go/+/425795 Auto-Submit: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Robert Griesemer <gri@google.com> Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
* go/parser: remove validation of expression syntax, leave to type checkerRobert Griesemer2022-09-011-108/+26
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Remove the code that verifies that an expression is a type or non-type expression. For one, it cannot be done perfectly accurate (e.g., consider *p which could be an indirection or a pointer type), it also unnecessarily slows down parsing. It's simpler to leave the verification to the type checker which has all the information needed. Remove short compiler tests that tested the expression/type property. Adjust a couple of go/types tests which now trigger because the parser doesn't complain anymore. Change file for benchmark from "parser.go" to "../printer/nodes.go" to avoid a moving target when benchmarking. The parser may be marginally faster when tested on nodes.go: name old time/op new time/op delta ParseOnly-12 1.35ms ± 0% 1.31ms ± 0% ~ (p=0.100 n=3+3) name old speed new speed delta ParseOnly-12 39.9MB/s ± 0% 41.0MB/s ± 0% ~ (p=0.100 n=3+3) For #54511. Change-Id: I9a32c24c2c6e843c3d1af4587651c352f378b490 Reviewed-on: https://go-review.googlesource.com/c/go/+/425716 Reviewed-by: Robert Griesemer <gri@google.com> Run-TryBot: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com> Auto-Submit: Robert Griesemer <gri@google.com>
* go/parser: match go/defer error message of syntax packageRobert Griesemer2022-08-251-1/+1
| | | | | | | | | | | | | | Adjust corresponding type checker tests accordingly. For #54511. Change-Id: Ieaf29f26c0877973fc0acbde35292cd69a4b709c Reviewed-on: https://go-review.googlesource.com/c/go/+/425007 Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Alan Donovan <adonovan@google.com> Run-TryBot: Robert Griesemer <gri@google.com> Reviewed-by: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
* go/types: match types2 errors for missing index expressionsRobert Griesemer2022-08-191-3/+3
| | | | | | | | | | | | | | Use "middle" and "final" rather than "2nd" and "3rd" in error messages for invalid slice expressions. This is the original compiler error message and many tests check for this specific message. For #54511. Change-Id: I86eb739aa7218b7f393fab1ab402732cb9e9a1f6 Reviewed-on: https://go-review.googlesource.com/c/go/+/424906 Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
* go/parser: match const/var decl parsing of syntax packageRobert Griesemer2022-08-191-14/+18
| | | | | | | | | | | | | | | | | | | | Use same approach to parsing const and var declarations as the syntax package. Specifically, don't complain if the first const specification in a const declaration doesn't have a type and initialization expression. This removes some duplicate errors when combined with the type checker. Adjust corresponding type checker tests accordingly. For #54511. Change-Id: I96702eba51dda6b581dad44577a7f93e4c02c857 Reviewed-on: https://go-review.googlesource.com/c/go/+/424904 Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Alan Donovan <adonovan@google.com> Run-TryBot: Robert Griesemer <gri@google.com>
* go/parser: fix spelling in error messageRobert Griesemer2022-08-191-1/+1
| | | | | | | | | Make spelling consistent with uses in other error messages. Change-Id: I584cd22413842fb8bae1632ed34c8b0e7ef163cc Reviewed-on: https://go-review.googlesource.com/c/go/+/424902 Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
* go/parser: remove (internal) ability to disable generic codeRobert Griesemer2022-08-191-44/+12
| | | | | | | | | | | | | Generics are part of the language now; there's no need anymore to switch back to a syntax without generics. Remove the associated machinery and adjust short tests accordingly. Change-Id: I6b16c5c75fd9354ee87e3b9bee110f49f514565a Reviewed-on: https://go-review.googlesource.com/c/go/+/424857 Reviewed-by: Robert Griesemer <gri@google.com> Run-TryBot: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
* go/parser: remove import path string syntax checkingRobert Griesemer2022-08-191-17/+0
| | | | | | | | | | | | | | | | | The validity of an import path string is checked by the type checker (and possibly other tools); it doesn't need to be done by the parser. Remove the respective code and tests. Also, adjust a corresponding go/types test which resolves a TODO. For #54511. Change-Id: Id1fc80df4e3e83be3ef123da3946ccb8f759779f Reviewed-on: https://go-review.googlesource.com/c/go/+/424855 Run-TryBot: Robert Griesemer <gri@google.com> Reviewed-by: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
* go/parser: disallow parenthesizing embedded types in structsRobert Griesemer2022-08-191-6/+42
| | | | | | | | | | | | | | | | This was never permitted in Go but the flexibility to do so was introduced through the generics prototype code where we experimented with parentheses to enclose type parameters. Restore original (pre-generics) behavior. Fixes #51655. Change-Id: Ia7a4b2e393e0214a70e840c8663cf4474c5c754b Reviewed-on: https://go-review.googlesource.com/c/go/+/424694 Run-TryBot: Robert Griesemer <gri@google.com> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
* go/parser: limit recursion depthRoland Shoemaker2022-07-121-4/+50
| | | | | | | | | | | | | | | | | | | | Limit nested parsing to 100,000, which prevents stack exhaustion when parsing deeply nested statements, types, and expressions. Also limit the scope depth to 1,000 during object resolution. Thanks to Juho Nurminen of Mattermost for reporting this issue. Fixes #53616 Fixes CVE-2022-1962 Change-Id: I4d7b86c1d75d0bf3c7af1fdea91582aa74272c64 Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1491025 Reviewed-by: Russ Cox <rsc@google.com> Reviewed-by: Damien Neil <dneil@google.com> Reviewed-on: https://go-review.googlesource.com/c/go/+/417063 Run-TryBot: Michael Knyszek <mknyszek@google.com> Reviewed-by: Heschi Kreinick <heschi@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
* go/parser: remove unused method checkBinaryExprRobert Griesemer2022-06-161-17/+0
| | | | | | | | | | Change-Id: Ica981657e50e30cbfa1757e8457819a479f11c7d Reviewed-on: https://go-review.googlesource.com/c/go/+/412775 Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Robert Griesemer <gri@google.com> Run-TryBot: Robert Griesemer <gri@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
* go/parser: accept all valid type parameter listsRobert Griesemer2022-05-041-71/+83
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is a port of CL 402256 from the syntax package to go/parser with adjustments because of the different AST structure, and excluding any necessary go/printer changes (separate CL). Type parameter lists starting with the form [name *T|...] or [name (X)|...] may look like an array length expression [x]. Only after parsing the entire initial expression and checking whether the expression contains type elements or is followed by a comma can we make the final decision. This change simplifies the existing parsing strategy: instead of trying to make an upfront decision with limited information (which is insufficient), the parser now parses the start of a type parameter list or array length specification as expression. In a second step, if the expression can be split into a name followed by a type element, or a name followed by an ordinary expression which is succeeded by a comma, we assume a type parameter list (because it can't be an array length). In all other cases we assume an array length specification. Fixes #52559. Change-Id: I11ab6e62b073b78b2331bb6063cf74d2a9eaa236 Reviewed-on: https://go-review.googlesource.com/c/go/+/403937 Run-TryBot: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
* go/parser: parser to accept ~x as unary expressionRobert Griesemer2022-05-041-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is a port of CL 402255 from the syntax package to go/parser with adjustments because of the different AST structure. Accept ~x as ordinary unary expression in the parser but recognize such expressions as invalid in the type checker. This change opens the door to recognizing complex type constraint literals such as `*E|~int` in `[P *E|~int]` and parse them correctly instead of reporting a parse error because `P*E|~int` syntactically looks like an incorrect array length expression (binary expression where the RHS of | is an invalid unary expression ~int). As a result, the parser is more forgiving with expressions but the type checker will reject invalid uses as before. We could pass extra information into the binary/unary expression parse functions to prevent the use of ~ in invalid situations but it doesn't seem worth the trouble. In fact it may be advantageous to allow a more liberal expression syntax especially in the presence of errors (better parser synchronization after an error). Preparation for fixing #52559. Change-Id: I48562cf40ccf5f14c20fcd92c40a0303b2d8b2b8 Reviewed-on: https://go-review.googlesource.com/c/go/+/403696 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: David Chase <drchase@google.com>
* all: gofmt main repoRuss Cox2022-04-111-1/+0
| | | | | | | | | | | | | | | [This CL is part of a sequence implementing the proposal #51082. The design doc is at https://go.dev/s/godocfmt-design.] Run the updated gofmt, which reformats doc comments, on the main repository. Vendored files are excluded. For #51082. Change-Id: I7332f099b60f716295fb34719c98c04eb1a85407 Reviewed-on: https://go-review.googlesource.com/c/go/+/384268 Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* all: remove trailing blank doc comment linesRuss Cox2022-04-011-6/+0
| | | | | | | | | | | | | | | | | | | | | | | | A future change to gofmt will rewrite // Doc comment. // func f() to // Doc comment. func f() Apply that change preemptively to all doc comments. For #51082. Change-Id: I4023e16cfb0729b64a8590f071cd92f17343081d Reviewed-on: https://go-review.googlesource.com/c/go/+/384259 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
* go/parser, go/printer: fix parsing of ambiguous type parameter listsRobert Findley2022-02-151-25/+136
| | | | | | | | | | | | | | | This is a port of CL 370774 to go/parser and go/printer. It is adjusted for the slightly different factoring of parameter list parsing and printing in go/parser and go/printer. For #49482 Change-Id: I1c5b1facddbfcb7f7b2be356c817fc7e608223f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/385575 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
* go/parser, go/types: don't parse type parameters on methodsRobert Findley2022-02-041-8/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The go/parser package is updated to report an error on method type parameters, and to not store them in the AST. Tests are updated accordingly, and error messages are normalized accross go/parser and the compiler syntax package. Before this CL, go/parser would parse type parameters on method declarations and interface methods, leaving it to go/types to complain. There are several problems with this: - Interface Methods and Method declarations do not have type parameters in the spec. We try to align the parser with the productions in the spec. - Parsing method type parameters means that downstream libraries (go/doc, go/format, etc.) theoretically need to handle them, even though they are not part of the language. - Relatedly, go/types has inconsistent handling of method type parameters due to support being removed, leading to the crasher in #50427. It is more consistent and safer to disallow type parameters on methods in the parser. Fixes #50427 Change-Id: I555766da0c76c4cf1cfe0baa9416863088088b4e Reviewed-on: https://go-review.googlesource.com/c/go/+/382538 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
* all: gofmt -w -r 'interface{} -> any' srcRuss Cox2021-12-131-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | And then revert the bootstrap cmd directories and certain testdata. And adjust tests as needed. Not reverting the changes in std that are bootstrapped, because some of those changes would appear in API docs, and we want to use any consistently. Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories when preparing the bootstrap copy. A few files changed as a result of running gofmt -w not because of interface{} -> any but because they hadn't been updated for the new //go:build lines. Fixes #49884. Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09 Reviewed-on: https://go-review.googlesource.com/c/go/+/368254 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
* go/parser: allow parsing aliases with type parametersRobert Findley2021-11-101-2/+4
| | | | | | | | | | | | | | We already guard against this in the type checker, and it will eventually be allowed per the accepted proposal. Add a placeholder error code for the corresponding type checker error. Change-Id: I5cc2f1413ecc89ec2094f7178fdb156fb8cc2e43 Reviewed-on: https://go-review.googlesource.com/c/go/+/360235 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* go/parser: simplify parsing of array or slice constraint typesRobert Findley2021-10-311-52/+46
| | | | | | | | | | | | Simplify the parsing of array or slice constraint types added in CL 359134, following the port in CL 360135. Change-Id: Ia86d4b0149a222423d3b19623dd39d4aeb23857d Reviewed-on: https://go-review.googlesource.com/c/go/+/360115 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* go/types, types2, go/ast, go/parser: remove support for type listsRobert Findley2021-10-281-12/+0
| | | | | | | | | | | | | | | This is a rough port of CL 354131 to go/* libraries, though in practice I just tried to reconcile any places where the phrase "type list" occurred in the source. This resulted in adjusting quite a bit more code than initially expected, including a few lingering cases in the compiler. Change-Id: Ie62a9e1aeb831b73931bc4c78bbb6ccb24f53fb0 Reviewed-on: https://go-review.googlesource.com/c/go/+/359135 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* go/parser: fix parsing of array or slice constraint typesRobert Findley2021-10-281-29/+52
| | | | | | | | | | | | | | | | | | | | | | | | | Now that we allow eliding 'interface' from constraint types, we need to be a bit more careful about not consuming a '[' when parsing the next expression after "type T [". We want to check if the next expression is an identifier not followed by ']', in which case we're in a generic type, but need to avoid parsing index or slice expressions. Such expressions aren't valid array lengths because these expressions are never constant, so when encountering a following '[' we can instead assume that this is a type parameter field with array or slice type constraint. Test cases are added for the related issues #49174 and #49175, along with a flag to enable tracing error tests. For #49174 For #49175 Change-Id: I0476ef20c4c134ac537118272f20caaf123ee70e Reviewed-on: https://go-review.googlesource.com/c/go/+/359134 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* go/parser: allow eliding interface in constraint literalsRobert Findley2021-10-111-47/+79
| | | | | | | | | | | | | | | | | | | | | | | | | | | This is a port of CL 353133 from cmd/compile/internal/syntax, with significant adjustments for the mechanics of go/parser. Some additional cleanup is made along the way: parseParameterList can call parseParamDecl without indirection, and the tparams argument is redundant with the closing token. Also, the error that "all type parameters must be named" is positioned on the first unnamed type parameter. Error recovery in go/parser is notably worse here than the compiler parser, so the test data had to be adjusted to synchronize positions. Fixing this error recovery will have to wait for a later CL. As with the compiler changes, these changes are guarded behind a flag so that they may be easily removed if #48424 is not accepted. For #48424 Change-Id: If87925d246f36aaab11a95442f75f659462d4286 Reviewed-on: https://go-review.googlesource.com/c/go/+/354870 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* go/parser: clean up unnecessary arguments and replace an if statementRobert Findley2021-10-111-7/+8
| | | | | | | | | | | | Eliminate an unnecessary argument from parseGenericType, and replace an if statement with a switch. Change-Id: Iaa8afeface929332579f183c8e523961cca9aca4 Reviewed-on: https://go-review.googlesource.com/c/go/+/354869 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* go/ast: rename MultiIndexExpr to IndexListExprRobert Findley2021-09-081-2/+2
| | | | | | | | | | | | | | As discussed in #47781, IndexListExpr is one character shorter and has the advantage of being next to IndexExpr in documentation. Updates #47781 Change-Id: I709d5c1a79b4f9aebcd6445e4ab0cd6dae45bab7 Reviewed-on: https://go-review.googlesource.com/c/go/+/348609 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org>
* go/ast: rename TParams fields to TypeParamsRobert Findley2021-09-081-9/+9
| | | | | | | | | | | | As discussed in the ast proposal (#47781), there's not really a strong reason to avoid spelling out 'Type'. Change-Id: I0ba1bf03b112ea60509a78a89a050a302779d9d0 Reviewed-on: https://go-review.googlesource.com/c/go/+/348375 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org>
* go/internal/typeparams: remove typeparams.{Get,Set} (cleanup)Robert Findley2021-08-311-4/+8
| | | | | | | | | | | | | | | | | | | These helper functions are no longer necessary, now that type parameters are enabled; we can access type parameters directly. When considering the existence or non-existence of type parameters, we can either check whether node.TParams != nil, or whether node.TParams.NumFields() > 0. The heuristic I'm using for deciding between these checks is as follows: - For data access, just check node.TParams != nil. - For producing errors if type parameters exist, check NumFields() > 0. Change-Id: I6597536898e975564e9e8bf6a3a91bc798e0f110 Reviewed-on: https://go-review.googlesource.com/c/go/+/346549 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org>
* [dev.typeparams] go/internal/typeparams: remove the Enabled guardRob Findley2021-07-161-1/+1
| | | | | | | | | | | | | | Type parameters are now always enabled. Users should guard against type checking generic code by using the types.Config.GoVersion field. This cleans up some differences with types2. Change-Id: Ie3e35a549e456a90a10d6a7e158ff58653cc1394 Reviewed-on: https://go-review.googlesource.com/c/go/+/335033 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* [dev.typeparams] go/*: switch from ListExpr to MultiIndexExprRob Findley2021-07-161-6/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | When instantiating a generic type or function with multiple type arguments, we need to represent an index expression with multiple indexes in the AST. Previous to this CL this was done with a new ast.ListExpr node, which allowed packing multiple expressions into a single ast.Expr. This compositional pattern can be both inefficient and cumbersome to work with, and introduces a new node type that only exists to augment the meaning of an existing node type. By comparison, other specializations of syntax are given distinct nodes in go/ast, for example variations of switch or for statements, so the use of ListExpr was also (arguably) inconsistent. This CL removes ListExpr, and instead adds a MultiIndexExpr node, which is exactly like IndexExpr but allows for multiple index arguments. This requires special handling for this new node type, but a new wrapper in the typeparams helper package largely mitigates this special handling. Change-Id: I65eb29c025c599bae37501716284dc7eb953b2ad Reviewed-on: https://go-review.googlesource.com/c/go/+/327149 Trust: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org>
* [dev.typeparams] all: merge master (37f9a8f) into dev.typeparamsCuong Manh Le2021-06-251-1/+6
|\ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Conflicts: - src/go/types/check_test.go CL 330629 fixed a bug in package qualification logic - src/internal/buildcfg/exp.go CL 329930 make parseExperiments get go arch string as input param Merge List: + 2021-06-25 37f9a8f69d go/types: fix a bug in package qualification logic + 2021-06-24 c309c89db5 reflect: document that InterfaceData is a low-entropy RNG + 2021-06-24 cce621431a cmd/compile: fix wrong type in SSA generation for OSLICE2ARRPTR + 2021-06-24 600a2a4ffb cmd/go: don't try to add replaced versions that won't be selected + 2021-06-24 a9bb38222a net: remove hard-coded timeout in dialClosedPort test helper + 2021-06-24 86d72fa2cb time: handle invalid UTF-8 byte sequences in quote to prevent panic + 2021-06-24 44a12e5f33 cmd/go: search breadth-first instead of depth-first for test dependency cycles + 2021-06-24 73496e0df0 net: use absDomainName in the Windows lookupPTR test helper + 2021-06-24 222ed1b38a os: enable TestFifoEOF on openbsd + 2021-06-22 0ebd5a8de0 cmd/go: update ToolTags based on GOARCH value + 2021-06-22 5bd09e5efc spec: unsafe.Add/Slice are not permitted in statement context + 2021-06-22 666315b4d3 runtime/internal/atomic: remove incorrect pointer indirection in comment + 2021-06-22 63daa774b5 go/types: guard against checking instantiation when generics is disabled + 2021-06-22 197a5ee2ab cmd/gofmt: remove stale documentation for the -G flag + 2021-06-22 9afd158eb2 go/parser: parse an ast.IndexExpr for a[] + 2021-06-21 1bd5a20e3c cmd/go: add a -go flag to 'go mod graph' + 2021-06-21 761edf71f6 cmd/internal/moddeps: use a temporary directory for GOMODCACHE if needed + 2021-06-21 a0400420ad cmd/internal/moddeps: use -mod=readonly instead of -mod=mod + 2021-06-21 3f9ec83b10 cmd/go: document GOPPC64 environment variable + 2021-06-21 20bdfba325 go/scanner: fall back to next() when encountering 0 bytes in parseIdentifier + 2021-06-21 44f9a3566c database/sql: fix deadlock test in prepare statement Change-Id: I16490e8ea70ee65081f467223857033842da513a
| * go/parser: parse an ast.IndexExpr for a[]Rob Findley2021-06-221-1/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | To be consistent with Go 1.16, and to preserve as much information in the AST as possible, parse an ast.IndexExpr with BadExpr Index for the invalid expression a[]. A go/types test had to be adjusted to account for an additional error resulting from this change. We don't have a lot of test coverage for parser error recovery, so rather than write an ad-hoc test for this issue, add a new go/types test that checks that the indexed operand is used. Updates #46403 Change-Id: I21e6ff4179746aaa50e530d4091fded450e69824 Reviewed-on: https://go-review.googlesource.com/c/go/+/329791 Trust: Robert Findley <rfindley@google.com> Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* | [dev.typeparams] go/parser: accept embedded type literalsRob Findley2021-06-171-5/+22
| | | | | | | | | | | | | | | | | | | | | | | | | | This is an approximate port of CL 321109 to go/parser, though go/parser does not have the same internal APIs as cmd/compile/internal/syntax, so this CL required some refactoring. Change-Id: I146ef530c969d61bab99f98f4de94b862e103ddc Reviewed-on: https://go-review.googlesource.com/c/go/+/325703 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* | [dev.typeparams] go/parser: accept "~" and "|" interface elementsRob Findley2021-06-171-7/+67
|/ | | | | | | | | | | | | | | | | | | | | This is a port of CL 307371 to go/parser, adding support for the new embedded type expressions. As in that CL, type lists continue to be accepted. This CL also revealed a pre-existing bug related to embedded instances: the parser was failing to parse embedded instances with multiple type arguments, due to not consuming the initial ','. This is fixed, and along the way TestErrors is modified to use subtests. Several missing tests cases were added to exprstring_test.go. These must have been missed in an earlier CL. Change-Id: I452769536998cddb1618bebdba675fc09d48a12f Reviewed-on: https://go-review.googlesource.com/c/go/+/325690 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* go/parser: don't parse a nil IndexExpr.IndexRob Findley2021-05-051-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | 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 <rfindley@google.com> Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* go: various minor cleanups with the help of Golandkumakichi2021-04-271-1/+1
| | | | | | | | | | | | | | | • fix some typos • remove superfluous conversions/parentheses • remove superfluous nil checks Change-Id: I428bf6a7be551b79270567047878c3076dd6f2ff GitHub-Last-Rev: 3b1c7573cfdf89ac184fd6ae44bca4be78b0cd64 GitHub-Pull-Request: golang/go#45799 Reviewed-on: https://go-review.googlesource.com/c/go/+/314069 Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Findley <rfindley@google.com>
* all: remove redundant spaces before . and ,Yury Smolsky2021-04-201-1/+1
| | | | | | | | | Change-Id: I6a4bd2544276d0638bddf07ebcf2ee636db30fea Reviewed-on: https://go-review.googlesource.com/c/go/+/311009 Run-TryBot: Yury Smolsky <yury@smolsky.by> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Emmanuel Odeke <emmanuel@orijtech.com>
* go/parser: add a SkipObjectResolution mode to bypass object resolutionRob Findley2021-04-161-1/+3
| | | | | | | | | | | | | | | | | | | | | | Parser object resolution is an auxiliary feature in which the parser attempts to resolve identifiers to their declarations. In functionality, it significantly overlaps with go/types and in fact cannot be correctly computed at parse-time without type information (for example, it is generally not possible to resolve k in the composite lit c{k: v}). Due to these limitations, it is of limited utility and rarely used. Now that object resolution is isolated as a post-processing pass, it is trivial to offer a parser mode that skips it entirely. This CL adds that mode. Fixes #45104 Change-Id: I5a2c05437e298964ad2039e1ff98e63d6efbd1af Reviewed-on: https://go-review.googlesource.com/c/go/+/306149 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* go/*,cmd/gofmt: guard AST changes with the typeparams build tagRob Findley2021-04-131-18/+24
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This CL changes our approach to guarding type parameter functionality and API. Previously, we guarded type parameter functionality with the parser.parseTypeParams parser mode, and were in the process of hiding the type parameter API behind the go1.18 build constraint. These mechanisms had several limitations: + Requiring the parser.parseTypeParams mode to be set meant that existing tooling would have to opt-in to type parameters in all places where it parses Go files. + The parseTypeParams mode value had to be copied in several places. + go1.18 is not specific to typeparams, making it difficult to set up the builders to run typeparams tests. This CL addresses the above limitations, and completes the task of hiding the AST API, by switching to a new 'typeparams' build constraint and adding a new go/internal/typeparams helper package. The typeparams build constraint is used to conditionally compile the new AST changes. The typeparams package provides utilities for accessing and writing the new AST data, so that we don't have to fragment our parser or type checker logic across build constraints. The typeparams.Enabled const is used to guard tests that require type parameter support. The parseTypeParams parser mode is gone, replaced by a new typeparams.DisableParsing mode with the opposite sense. Now, type parameters are only parsed if go/parser is compiled with the typeparams build constraint set AND typeparams.DisableParsing not set. This new parser mode allows opting out of type parameter parsing for tests. How exactly to run tests on builders is left to a follow-up CL. Updates #44933 Change-Id: I3091e42a2e5e2f23e8b2ae584f415a784b9fbd65 Reviewed-on: https://go-review.googlesource.com/c/go/+/300649 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>