tree-sitter-go icon indicating copy to clipboard operation
tree-sitter-go copied to clipboard

Incorrect parsing of function parameters and return values

Open FotiadisM opened this issue 1 year ago • 1 comments

Description

The parser fails to identify discrete parameter declarations and combines them under a single one. This behavior only occurs in unnamed parameters and when using the language built-in type map.

To reproduce

go mod init treesitter-error

create a main.go file with contents:

// main.go
package main

func namedParameters(s []string, i int, m map[int]int, e error) {
}

func unamedParameters([]string, int, map[int]int, error) {
}

func inReturns() ([]string, int, map[int]int, error) {
	return nil, 0, nil, nil
}

func main() {
	namedParameters(nil, 0, nil, nil)
	unamedParameters(nil, 0, nil, nil)
	_, _, _, _ = inReturns()
}

I am using the latest stable version of neovim, with the latest version of the go parser. The tree looks like this:

package_clause [0, 0] - [0, 12]
  package_identifier [0, 8] - [0, 12]
function_declaration [2, 0] - [3, 1]
  name: identifier [2, 5] - [2, 20]
  parameters: parameter_list [2, 20] - [2, 63]
    parameter_declaration [2, 21] - [2, 31]
      name: identifier [2, 21] - [2, 22]
      type: slice_type [2, 23] - [2, 31]
        element: type_identifier [2, 25] - [2, 31]
    parameter_declaration [2, 33] - [2, 38]
      name: identifier [2, 33] - [2, 34]
      type: type_identifier [2, 35] - [2, 38]
    parameter_declaration [2, 40] - [2, 53]
      name: identifier [2, 40] - [2, 41]
      type: map_type [2, 42] - [2, 53]
        key: type_identifier [2, 46] - [2, 49]
        value: type_identifier [2, 50] - [2, 53]
    parameter_declaration [2, 55] - [2, 62]
      name: identifier [2, 55] - [2, 56]
      type: type_identifier [2, 57] - [2, 62]
  body: block [2, 64] - [3, 1]
function_declaration [5, 0] - [6, 1]
  name: identifier [5, 5] - [5, 21]
  parameters: parameter_list [5, 21] - [5, 56]
    parameter_declaration [5, 22] - [5, 30]
      type: slice_type [5, 22] - [5, 30]
        element: type_identifier [5, 24] - [5, 30]
    parameter_declaration [5, 32] - [5, 48]                             // NOTE THE ERROR HERE
      name: identifier [5, 32] - [5, 35]
      name: identifier [5, 37] - [5, 40]
      type: array_type [5, 40] - [5, 48]
        length: identifier [5, 41] - [5, 44]
        element: type_identifier [5, 45] - [5, 48]
    parameter_declaration [5, 50] - [5, 55]
      type: type_identifier [5, 50] - [5, 55]
  body: block [5, 57] - [6, 1]
function_declaration [8, 0] - [10, 1]
  name: identifier [8, 5] - [8, 14]
  parameters: parameter_list [8, 14] - [8, 16]
  result: parameter_list [8, 17] - [8, 52]
    parameter_declaration [8, 18] - [8, 26]
      type: slice_type [8, 18] - [8, 26]
        element: type_identifier [8, 20] - [8, 26]
    parameter_declaration [8, 28] - [8, 44]                            // NOTE THE ERROR HERE
      name: identifier [8, 28] - [8, 31]
      name: identifier [8, 33] - [8, 36]
      type: array_type [8, 36] - [8, 44]
        length: identifier [8, 37] - [8, 40]
        element: type_identifier [8, 41] - [8, 44]
    parameter_declaration [8, 46] - [8, 51]
      type: type_identifier [8, 46] - [8, 51]
  body: block [8, 53] - [10, 1]
    return_statement [9, 1] - [9, 24]
      expression_list [9, 8] - [9, 24]
        nil [9, 8] - [9, 11]
        int_literal [9, 13] - [9, 14]
        nil [9, 16] - [9, 19]
        nil [9, 21] - [9, 24]
function_declaration [12, 0] - [16, 1]
  name: identifier [12, 5] - [12, 9]
  parameters: parameter_list [12, 9] - [12, 11]
  body: block [12, 12] - [16, 1]
    expression_statement [13, 1] - [13, 34]
      call_expression [13, 1] - [13, 34]
        function: identifier [13, 1] - [13, 16]
        arguments: argument_list [13, 16] - [13, 34]
          nil [13, 17] - [13, 20]
          int_literal [13, 22] - [13, 23]
          nil [13, 25] - [13, 28]
          nil [13, 30] - [13, 33]
    expression_statement [14, 1] - [14, 35]
      call_expression [14, 1] - [14, 35]
        function: identifier [14, 1] - [14, 17]
        arguments: argument_list [14, 17] - [14, 35]
          nil [14, 18] - [14, 21]
          int_literal [14, 23] - [14, 24]
          nil [14, 26] - [14, 29]
          nil [14, 31] - [14, 34]
    assignment_statement [15, 1] - [15, 25]
      left: expression_list [15, 1] - [15, 11]
        identifier [15, 1] - [15, 2]
        identifier [15, 4] - [15, 5]
        identifier [15, 7] - [15, 8]
        identifier [15, 10] - [15, 11]
      right: expression_list [15, 14] - [15, 25]
        call_expression [15, 14] - [15, 25]
          function: identifier [15, 14] - [15, 23]
          arguments: argument_list [15, 23] - [15, 25]

Screenshot from 2023-08-10 12-25-50

FotiadisM avatar Aug 15 '23 15:08 FotiadisM