aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/basic.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-08-05 16:37:21 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-08-05 16:37:21 -0700
commite9e3a2994696a3131125ebc4b1f0eec7ca5306d9 (patch)
treede6bf27daddd48b9ab15144312f558d3cd0c53c2 /test/behavior/basic.zig
parentf58cbef1659742e57377d3f8c92a0b9b97af91ad (diff)
downloadzig-e9e3a2994696a3131125ebc4b1f0eec7ca5306d9.tar.gz
zig-e9e3a2994696a3131125ebc4b1f0eec7ca5306d9.zip
stage2: implement generic function memoization
Module has a new field `monomorphed_funcs` which stores the set of `*Module.Fn` objects which are generic function instantiations. The hash is based on hashes of comptime values of parameters known to be comptime based on an explicit comptime keyword or must-be-comptime type expressions that can be evaluated without performing monomorphization. This allows function calls to be semantically analyzed cheaply for generic functions which are already instantiated. The table is updated with a single `getOrPutAdapted` in the semantic analysis of `call` instructions, by pre-allocating the `Fn` object and passing it to the child `Sema`.
Diffstat (limited to 'test/behavior/basic.zig')
-rw-r--r--test/behavior/basic.zig70
1 files changed, 70 insertions, 0 deletions
diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig
index ac1dc3889c..1372dfaeeb 100644
--- a/test/behavior/basic.zig
+++ b/test/behavior/basic.zig
@@ -92,3 +92,73 @@ fn first4KeysOfHomeRow() []const u8 {
test "return string from function" {
try expect(mem.eql(u8, first4KeysOfHomeRow(), "aoeu"));
}
+
+test "hex escape" {
+ try expect(mem.eql(u8, "\x68\x65\x6c\x6c\x6f", "hello"));
+}
+
+test "multiline string" {
+ const s1 =
+ \\one
+ \\two)
+ \\three
+ ;
+ const s2 = "one\ntwo)\nthree";
+ try expect(mem.eql(u8, s1, s2));
+}
+
+test "multiline string comments at start" {
+ const s1 =
+ //\\one
+ \\two)
+ \\three
+ ;
+ const s2 = "two)\nthree";
+ try expect(mem.eql(u8, s1, s2));
+}
+
+test "multiline string comments at end" {
+ const s1 =
+ \\one
+ \\two)
+ //\\three
+ ;
+ const s2 = "one\ntwo)";
+ try expect(mem.eql(u8, s1, s2));
+}
+
+test "multiline string comments in middle" {
+ const s1 =
+ \\one
+ //\\two)
+ \\three
+ ;
+ const s2 = "one\nthree";
+ try expect(mem.eql(u8, s1, s2));
+}
+
+test "multiline string comments at multiple places" {
+ const s1 =
+ \\one
+ //\\two
+ \\three
+ //\\four
+ \\five
+ ;
+ const s2 = "one\nthree\nfive";
+ try expect(mem.eql(u8, s1, s2));
+}
+
+test "call result of if else expression" {
+ try expect(mem.eql(u8, f2(true), "a"));
+ try expect(mem.eql(u8, f2(false), "b"));
+}
+fn f2(x: bool) []const u8 {
+ return (if (x) fA else fB)();
+}
+fn fA() []const u8 {
+ return "a";
+}
+fn fB() []const u8 {
+ return "b";
+}