From 44f38b04b0cc374fcd377df0fe68f29c824185ff Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Wed, 31 Jan 2018 11:13:39 -0500
Subject: [PATCH 01/15] fix assertion fail when using global var number literal
closes #697
---
src/analyze.cpp | 4 ++++
test/compile_errors.zig | 7 +++++++
2 files changed, 11 insertions(+)
diff --git a/src/analyze.cpp b/src/analyze.cpp
index 4fa8dad7ce..2812c9d79e 100644
--- a/src/analyze.cpp
+++ b/src/analyze.cpp
@@ -3295,6 +3295,10 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
is_const, init_val, &tld_var->base);
tld_var->var->linkage = linkage;
+ if (implicit_type != nullptr && type_is_invalid(implicit_type)) {
+ tld_var->var->value->type = g->builtin_types.entry_invalid;
+ }
+
if (var_decl->align_expr != nullptr) {
if (!analyze_const_align(g, tld_var->base.parent_scope, var_decl->align_expr, &tld_var->var->align_bytes)) {
tld_var->var->value->type = g->builtin_types.entry_invalid;
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index 223646e767..3267ea7435 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -1,6 +1,13 @@
const tests = @import("tests.zig");
pub fn addCases(cases: &tests.CompileErrorContext) void {
+ cases.add("use invalid number literal as array index",
+ \\var v = 25;
+ \\export fn entry() void {
+ \\ var arr: [v]u8 = undefined;
+ \\}
+ , ".tmp_source.zig:1:1: error: unable to infer variable type");
+
cases.add("duplicate struct field",
\\const Foo = struct {
\\ Bar: i32,
From a795e4ce32d0f0806a0cef7ca6bbc45533b77c61 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Wed, 31 Jan 2018 11:47:56 -0500
Subject: [PATCH 02/15] add some docs for reflection
---
doc/langref.html.in | 56 ++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 53 insertions(+), 3 deletions(-)
diff --git a/doc/langref.html.in b/doc/langref.html.in
index bd3fb41340..986da22dd2 100644
--- a/doc/langref.html.in
+++ b/doc/langref.html.in
@@ -2785,6 +2785,16 @@ test "implicitly cast to const pointer" {
use the C calling convention may pass structs and unions by value.
{#header_close#}
+ {#header_open|Function Reflection#}
+ {#code_begin|test#}
+const assert = @import("std").debug.assert;
+
+test "fn reflection" {
+ assert(@typeOf(assert).ReturnType == void);
+ assert(@typeOf(assert).is_var_args == false);
+}
+ {#code_end#}
+ {#header_close#}
{#header_close#}
{#header_open|Errors#}
@@ -2998,6 +3008,31 @@ fn createFoo(param: i32) %Foo {
{#see_also|defer|if|switch#}
+ {#header_open|Error Union Type#}
+
An error union is created by putting a % in front of a type.
+ You can use compile-time reflection to access the child type of an error union:
+ {#code_begin|test#}
+const assert = @import("std").debug.assert;
+
+error SomeError;
+
+test "error union" {
+ var foo: %i32 = undefined;
+
+ // Implicitly cast from child type of an error union:
+ foo = 1234;
+
+ // Implicitly cast from an error set:
+ foo = error.SomeError;
+
+ // Use compile-time reflection to access the child type of an error union:
+ comptime assert(@typeOf(foo).Child == i32);
+}
+ {#code_end#}
+ {#header_close#}
+ {#header_open|Error Set Type#}
+ TODO
+ {#header_close#}
{#header_close#}
{#header_open|Nullables#}
@@ -3099,6 +3134,24 @@ fn doAThing(nullable_foo: ?&Foo) void {
The optimizer can sometimes make better decisions knowing that pointer arguments
cannot be null.
+ {#header_open|Nullable Type#}
+ A nullable is created by putting ? in front of a type. You can use compile-time
+ reflection to access the child type of a nullable:
+ {#code_begin|test#}
+const assert = @import("std").debug.assert;
+
+test "nullable type" {
+ // Declare a nullable and implicitly cast from null:
+ var foo: ?i32 = null;
+
+ // Implicitly cast from child type of a nullable
+ foo = 1234;
+
+ // Use compile-time reflection to access the child type of the nullable:
+ comptime assert(@typeOf(foo).Child == i32);
+}
+ {#code_end#}
+ {#header_close#}
{#header_close#}
{#header_open|Casting#}
TODO: explain implicit vs explicit casting
@@ -5731,9 +5784,6 @@ ContainerDecl = option("extern" | "packed")
Together we serve end users.
{#header_close#}
- {#header_open|TODO#}
- TODO: document changes from a31b23c46ba2a8c28df01adc1aa0b4d878b9a5cf (compile time reflection additions)
- {#header_close#}