mirror of
https://github.com/ziglang/zig.git
synced 2026-02-14 21:38:33 +00:00
array.len generates a constant number literal expression
This commit is contained in:
parent
42fe4e3cc8
commit
6b3ce918db
@ -29,6 +29,8 @@ static TypeTableEntry *analyze_block_expr(CodeGen *g, ImportTableEntry *import,
|
||||
static TypeTableEntry *resolve_expr_const_val_as_void(CodeGen *g, AstNode *node);
|
||||
static TypeTableEntry *resolve_expr_const_val_as_fn(CodeGen *g, AstNode *node, FnTableEntry *fn);
|
||||
static TypeTableEntry *resolve_expr_const_val_as_type(CodeGen *g, AstNode *node, TypeTableEntry *type);
|
||||
static TypeTableEntry *resolve_expr_const_val_as_unsigned_num_lit(CodeGen *g, AstNode *node,
|
||||
TypeTableEntry *expected_type, uint64_t x);
|
||||
static void detect_top_level_decl_deps(CodeGen *g, ImportTableEntry *import, AstNode *node);
|
||||
static void analyze_top_level_decls_root(CodeGen *g, ImportTableEntry *import, AstNode *node);
|
||||
|
||||
@ -2229,7 +2231,7 @@ static TypeTableEntry *analyze_container_init_expr(CodeGen *g, ImportTableEntry
|
||||
}
|
||||
|
||||
static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
|
||||
AstNode *node)
|
||||
TypeTableEntry *expected_type, AstNode *node)
|
||||
{
|
||||
assert(node->type == NodeTypeFieldAccessExpr);
|
||||
|
||||
@ -2266,7 +2268,8 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i
|
||||
}
|
||||
} else if (struct_type->id == TypeTableEntryIdArray) {
|
||||
if (buf_eql_str(field_name, "len")) {
|
||||
return g->builtin_types.entry_isize;
|
||||
return resolve_expr_const_val_as_unsigned_num_lit(g, node, expected_type,
|
||||
struct_type->data.array.len);
|
||||
} else {
|
||||
add_node_error(g, node,
|
||||
buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name),
|
||||
@ -2671,7 +2674,7 @@ static TypeTableEntry *analyze_lvalue(CodeGen *g, ImportTableEntry *import, Bloc
|
||||
} else if (lhs_node->type == NodeTypeArrayAccessExpr) {
|
||||
expected_rhs_type = analyze_array_access_expr(g, import, block_context, lhs_node);
|
||||
} else if (lhs_node->type == NodeTypeFieldAccessExpr) {
|
||||
expected_rhs_type = analyze_field_access_expr(g, import, block_context, lhs_node);
|
||||
expected_rhs_type = analyze_field_access_expr(g, import, block_context, nullptr, lhs_node);
|
||||
} else if (lhs_node->type == NodeTypePrefixOpExpr &&
|
||||
lhs_node->data.prefix_op_expr.prefix_op == PrefixOpDereference)
|
||||
{
|
||||
@ -4849,7 +4852,7 @@ static TypeTableEntry *analyze_expression(CodeGen *g, ImportTableEntry *import,
|
||||
return_type = analyze_slice_expr(g, import, context, node);
|
||||
break;
|
||||
case NodeTypeFieldAccessExpr:
|
||||
return_type = analyze_field_access_expr(g, import, context, node);
|
||||
return_type = analyze_field_access_expr(g, import, context, expected_type, node);
|
||||
break;
|
||||
case NodeTypeContainerInitExpr:
|
||||
return_type = analyze_container_init_expr(g, import, context, node);
|
||||
|
||||
@ -201,7 +201,7 @@ pub fn buf_print_i64(out_buf: []u8, x: i64) -> isize {
|
||||
pub fn buf_print_u64(out_buf: []u8, x: u64) -> isize {
|
||||
var buf: [max_u64_base10_digits]u8 = undefined;
|
||||
var a = x;
|
||||
var index = buf.len;
|
||||
var index: isize = buf.len;
|
||||
|
||||
while (true) {
|
||||
const digit = a % 10;
|
||||
|
||||
@ -1043,11 +1043,9 @@ import "std.zig";
|
||||
|
||||
pub fn main(args: [][]u8) -> %void {
|
||||
const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
|
||||
var i: @typeof(array_of_strings.len) = 0;
|
||||
while (i < array_of_strings.len) {
|
||||
%%stdout.printf(array_of_strings[i]);
|
||||
for (array_of_strings) |str| {
|
||||
%%stdout.printf(str);
|
||||
%%stdout.printf("\n");
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
)SOURCE", "hello\nthis\nis\nmy\nthing\n");
|
||||
|
||||
@ -269,3 +269,11 @@ fn memcpy_and_memset_intrinsics() {
|
||||
|
||||
if (bar[11] != 'A') unreachable{};
|
||||
}
|
||||
|
||||
|
||||
#attribute("test")
|
||||
fn array_dot_len_const_expr() { }
|
||||
struct ArrayDotLenConstExpr {
|
||||
y: [@const_eval(some_array.len)]u8,
|
||||
}
|
||||
const some_array = []u8 {0, 1, 2, 3};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user