checker: fix #13626, when unwrapped type is fixed array, and selector expr is 'len' (#13627)

pull/13636/head
ChAoS_UnItY 2022-03-03 02:46:18 +08:00 committed by GitHub
parent 57c6454656
commit 6a3d34ae11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View File

@ -1667,6 +1667,12 @@ pub fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
if sym.kind !in [.struct_, .aggregate, .interface_, .sum_type] {
if sym.kind != .placeholder {
unwrapped_sym := c.table.sym(c.unwrap_generic(typ))
if unwrapped_sym.kind == .array_fixed && node.field_name == 'len' {
node.typ = ast.int_type
return ast.int_type
}
c.error('`$unwrapped_sym.name` has no property `$node.field_name`', node.pos)
}
} else {

View File

@ -0,0 +1,38 @@
fn test_comptime_generic() {
a := [5]int{}
func1(&a)
}
[inline]
pub fn func1<T>(t &T) {
func2<T>(t)
}
[inline]
pub fn func2<T>(t &T) {
$if T is $Array {
unsafe {
for i in 0 .. t.len {
func2(&t[i])
}
}
} $else $if T is $Map {
// fake_map(t, df)
} $else $if T is $Struct {
$for f in T.fields {
$if f.typ is string {
}
// Dummy expression to generate and specify t.$(f.name)'s type
// mut attrs := get_attrs(t.$(f.name), f)
// if !attrs.skip() {
// fake_data_wdf(&(t.$(f.name)))
// }
}
} $else {
unsafe {
// *t = fake_primitive_value<T>(df) or { panic(err) }
}
}
}