From 90c988cc1f02dc62b75adfd3cc882baa05774cc1 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 20 May 2021 00:12:58 +0200 Subject: [PATCH] Fix issue with unifying void returns. --- resources/testfragments/demo1.c3 | 152 +++++++++++++++++++++++++ resources/testfragments/demo_errors.c3 | 0 src/compiler/sema_expr.c | 2 +- 3 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 resources/testfragments/demo1.c3 create mode 100644 resources/testfragments/demo_errors.c3 diff --git a/resources/testfragments/demo1.c3 b/resources/testfragments/demo1.c3 new file mode 100644 index 000000000..71fb820d1 --- /dev/null +++ b/resources/testfragments/demo1.c3 @@ -0,0 +1,152 @@ +module demo1; + +// Use C functions directly. +extern func int printf(char *, ...); +extern func void puts(char *); + +struct Foo +{ + int a; + int b; +} + +func Foo createFoo(int a, int b = 10) +{ + // Compound initializer + return Foo({a, b}); +} + +struct Bar +{ + int x; + int y; +} + + +func void testStruct() +{ + // Default arguments + Foo foo = createFoo(100); + printf("Foo a:%d b:%d\n", foo.a, foo.b); + + // No need to write foo = (Foo){ 11, 22 } + // unlike C + foo = { 11, 12 }; + + // Named arguments + foo = createFoo(.b = 13, .a = 14); + printf("Foo a:%d b:%d\n", foo.a, foo.b); + + // Structural copy + Bar bar = (Bar)(foo); + printf("Bar x:%d y:%d\n", bar.x, bar.y); + + // Type functions + printf("Mult: %d\n", foo.mult()); + printf("Add: %d\n", bar.add()); + +} + +func int Bar.add(Bar* b) +{ + return b.x + b.y; +} + +func int Foo.mult(Foo* f) +{ + return f.a * f.b; +} + +func void printArray(int[] array) +{ + printf("["); + foreach (i, a : array) + { + if (i != 0) printf(", "); + printf("%d", a); + } + printf("]\n"); +} + + +func void testArrays() +{ + int[5] x = { [0] = 100, [1..2] = 4 }; + puts("Testing arrays---"); + foreach (a : x) + { + printf("%d\n", a); + } + puts("Arrays with index---"); + foreach (i, a : x) + { + printf("index[%d]: %d\n", i, a); + } + puts("Mutating arrays---"); + foreach (i, int* &a : x) { *a += (int)(i); } + foreach (i, a : x) + { + printf("index[%d]: %d\n", i, a); + } + puts("Without & there is no change---"); + foreach (i, a : x) { a += (int)(i); } + foreach (i, a : x) + { + printf("index[%d]: %d\n", i, a); + } + printf("Length is a compile time value: %d\n", x.len); + puts("Getting a slice---"); + int[] y = x[1..3]; + foreach (i, a : y) + { + printf("index[%d]: %d\n", i, a); + } + printf("Length is a runtime value: %d\n", y.len()); + puts("Getting a slice from the beginning to an offset of the end---"); + y = x[..^2]; // Same as x[0..^2] + foreach (i, a : y) + { + printf("index[%d]: %d\n", i, a); + } + puts("Printing arrays---"); + printArray(&x); + printArray(y); + printArray(x[1..]); + puts("Array to pointer to subarray---"); + int* z = &x; + printArray(z[0..2]); + printf("Pointer to array: %p\nPointer to slice: %p\nPointer to first element of slice: %p\n", z, &y, &y[0]); +} + +func void testExpressionBlocks() +{ + int x = {| + int j = 0; + for (int i = 0; i < 10; i++) + { + j += i + j; + if (j > 10) return j; + } + return 0; + |}; + printf("x: %d\n", x); + {| + for (int i = 0; i < 10; i++) + { + printf("%d\n", i); + if (i && i % 4 == 0) return; + } + puts("Reached end"); + |}; + if ({| for (int i = 0; i < 10; i++) printf("--%d\n", i); puts("Done!"); return true; |}) + { + puts("In if"); + } +} + +func void main() +{ + testStruct(); + testArrays(); + testExpressionBlocks(); +} diff --git a/resources/testfragments/demo_errors.c3 b/resources/testfragments/demo_errors.c3 new file mode 100644 index 000000000..e69de29bb diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index fae3f80ee..4001c7ef2 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -1071,7 +1071,7 @@ static inline Type *unify_returns(Context *context, Type *to) SEMA_PREV(context->returns[i - 1], "Previous return was here."); return NULL; } - if (to) + if (to != type_void) { if (current_expr_was_void) {