Skip to content

Nested index_expression is interpreted as type_instantiation_expression in certain cases  #160

@AislingHPE

Description

@AislingHPE

I came across this issue with syntax highlighting where when treesitter interprets index into a map-inside-a-map using variables, it reads it as a type instantiation.

Code demonstrating this issue

package main

func other() {
	b := "b"
	c := "c"
	a := map[string]map[string]int{"b": {}}
	// doesn't work
	a[b][c] = 10
	// works
	a["b"][c] = 10
	// works
	a[b]["c"] = 10
	// works
	a["b"]["c"] = 10
}

Highlighting (using vscode theme in neovim:
image

Treesitter playground output:

package_clause [0, 0] - [0, 12]
  package_identifier [0, 8] - [0, 12]
function_declaration [2, 0] - [14, 1]
  name: identifier [2, 5] - [2, 10]
  parameters: parameter_list [2, 10] - [2, 12]
  body: block [2, 13] - [14, 1]
    short_var_declaration [3, 1] - [3, 9]
      left: expression_list [3, 1] - [3, 2]
        identifier [3, 1] - [3, 2]
      right: expression_list [3, 6] - [3, 9]
        interpreted_string_literal [3, 6] - [3, 9]
    short_var_declaration [4, 1] - [4, 9]
      left: expression_list [4, 1] - [4, 2]
        identifier [4, 1] - [4, 2]
      right: expression_list [4, 6] - [4, 9]
        interpreted_string_literal [4, 6] - [4, 9]
    short_var_declaration [5, 1] - [5, 40]
      left: expression_list [5, 1] - [5, 2]
        identifier [5, 1] - [5, 2]
      right: expression_list [5, 6] - [5, 40]
        composite_literal [5, 6] - [5, 40]
          type: map_type [5, 6] - [5, 31]
            key: type_identifier [5, 10] - [5, 16]
            value: map_type [5, 17] - [5, 31]
              key: type_identifier [5, 21] - [5, 27]
              value: type_identifier [5, 28] - [5, 31]
          body: literal_value [5, 31] - [5, 40]
            keyed_element [5, 32] - [5, 39]
              key: literal_element [5, 32] - [5, 35]
                interpreted_string_literal [5, 32] - [5, 35]
              value: literal_element [5, 37] - [5, 39]
                literal_value [5, 37] - [5, 39]
    comment [6, 1] - [6, 16]
    assignment_statement [7, 1] - [7, 13]
      left: expression_list [7, 1] - [7, 8]
        type_instantiation_expression [7, 1] - [7, 8]
          type: generic_type [7, 1] - [7, 5]
            type: type_identifier [7, 1] - [7, 2]
            type_arguments: type_arguments [7, 2] - [7, 5]
              type_elem [7, 3] - [7, 4]
                type_identifier [7, 3] - [7, 4]
          type_identifier [7, 6] - [7, 7]
      right: expression_list [7, 11] - [7, 13]
        int_literal [7, 11] - [7, 13]
    comment [8, 1] - [8, 9]
    assignment_statement [9, 1] - [9, 15]
      left: expression_list [9, 1] - [9, 10]
        index_expression [9, 1] - [9, 10]
          operand: index_expression [9, 1] - [9, 7]
            operand: identifier [9, 1] - [9, 2]
            index: interpreted_string_literal [9, 3] - [9, 6]
          index: identifier [9, 8] - [9, 9]
      right: expression_list [9, 13] - [9, 15]
        int_literal [9, 13] - [9, 15]
    comment [10, 1] - [10, 9]
    assignment_statement [11, 1] - [11, 15]
      left: expression_list [11, 1] - [11, 10]
        index_expression [11, 1] - [11, 10]
          operand: index_expression [11, 1] - [11, 5]
            operand: identifier [11, 1] - [11, 2]
            index: identifier [11, 3] - [11, 4]
          index: interpreted_string_literal [11, 6] - [11, 9]
      right: expression_list [11, 13] - [11, 15]
        int_literal [11, 13] - [11, 15]
    comment [12, 1] - [12, 9]
    assignment_statement [13, 1] - [13, 17]
      left: expression_list [13, 1] - [13, 12]
        index_expression [13, 1] - [13, 12]
          operand: index_expression [13, 1] - [13, 7]
            operand: identifier [13, 1] - [13, 2]
            index: interpreted_string_literal [13, 3] - [13, 6]
          index: interpreted_string_literal [13, 8] - [13, 11]
      right: expression_list [13, 15] - [13, 17]
        int_literal [13, 15] - [13, 17]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions