mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 22:35:24 +00:00
add compile error for assigning number literal to non-comptime var
This commit is contained in:
parent
03b6d9f547
commit
6d0afc2bd2
@ -7858,6 +7858,8 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
|
||||
result_type = ira->codegen->builtin_types.entry_invalid;
|
||||
}
|
||||
|
||||
bool is_comptime_var = ir_get_var_is_comptime(var);
|
||||
|
||||
switch (result_type->id) {
|
||||
case TypeTableEntryIdTypeDecl:
|
||||
zig_unreachable();
|
||||
@ -7865,7 +7867,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
|
||||
break; // handled above
|
||||
case TypeTableEntryIdNumLitFloat:
|
||||
case TypeTableEntryIdNumLitInt:
|
||||
if (is_export || is_extern || casted_init_value->value.special == ConstValSpecialRuntime) {
|
||||
if (is_export || is_extern || (!var->src_is_const && !is_comptime_var)) {
|
||||
ir_add_error_node(ira, source_node, buf_sprintf("unable to infer variable type"));
|
||||
result_type = ira->codegen->builtin_types.entry_invalid;
|
||||
}
|
||||
|
||||
@ -7,7 +7,8 @@ const Allocator = mem.Allocator;
|
||||
const want_modification_safety = !@compileVar("is_release");
|
||||
const debug_u32 = if (want_modification_safety) u32 else void;
|
||||
|
||||
pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn(key: K)->u32,
|
||||
pub fn HashMap(comptime K: type, comptime V: type,
|
||||
comptime hash: fn(key: K)->u32,
|
||||
comptime eql: fn(a: K, b: K)->bool) -> type
|
||||
{
|
||||
struct {
|
||||
|
||||
@ -1697,6 +1697,13 @@ fn foo() {
|
||||
}
|
||||
fn bar() -> i32 { 0 }
|
||||
)SOURCE", 1, ".tmp_source.zig:3:8: error: return value ignored");
|
||||
|
||||
add_compile_fail_case("integer literal on a non-comptime var", R"SOURCE(
|
||||
fn foo() {
|
||||
var i = 0;
|
||||
while (i < 10; i += 1) { }
|
||||
}
|
||||
)SOURCE", 1, ".tmp_source.zig:3:5: error: unable to infer variable type");
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user