checker: fix indexing a type alias instance (#7889)

pull/7902/head
Nick Treleaven 2021-01-05 18:17:18 +00:00 committed by GitHub
parent a02de42450
commit 7a6b160d63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 3 deletions

View File

@ -4643,7 +4643,7 @@ fn (mut c Checker) check_index_type(typ_sym &table.TypeSymbol, index_type table.
pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) table.Type {
typ := c.expr(node.left)
node.left_type = typ
typ_sym := c.table.get_type_symbol(typ)
typ_sym := c.table.get_final_type_symbol(typ)
if typ_sym.kind !in [.array, .array_fixed, .string, .map] && !typ.is_ptr() && !(!typ_sym.name[0].is_capital() &&
typ_sym.name.ends_with('ptr')) && !typ.has_flag(.variadic) { // byteptr, charptr etc
c.error('type `$typ_sym.name` does not support indexing', node.pos)

View File

@ -3846,7 +3846,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
g.write(')')
}
else {
sym := g.table.get_type_symbol(node.left_type)
sym := g.table.get_final_type_symbol(node.left_type)
left_is_ptr := node.left_type.is_ptr()
if sym.kind == .array {
info := sym.info as table.Array

View File

@ -638,7 +638,7 @@ pub fn (mut t Table) add_placeholder_type(name string, language Language) int {
[inline]
pub fn (t &Table) value_type(typ Type) Type {
typ_sym := t.get_type_symbol(typ)
typ_sym := t.get_final_type_symbol(typ)
if typ.has_flag(.variadic) {
// ...string => string
// return typ.clear_flag(.variadic)

View File

@ -0,0 +1,6 @@
type Test = []int
fn test_index() {
t := Test([2,4])
assert t[1] == 4
}

View File

@ -0,0 +1,6 @@
type Test = map[string]string
fn test_index() {
t := Test({'c': 'abc'})
assert t['c'] == 'abc'
}