Skip to content

The C3 Blog

C3 0.7.3 - Small Improvements

This month's release is 0.7.3, and is about gradual improvements to the language. Some parts of a language will always have less use than others, and that is true for C3 as well. For C3 it's mostly the compile time parts that reveal their rough edges fairly late, but they need polish as well.

Type / typeid

Types can be introspected an manipulated in two ways in C3. One is to just use the type directly, like in Type.sizeof. The other is to use the typeid value, to get the same thing: Type.typeid.sizeof. However, they have differences: a typeid value can never be in the position of a type, and only compile time constant typeid has the full range of values:

typeid t = int.typeid;
const typeid T = int.typeid;
...
// Getting the size, this works for all
int.sizeof;     // Compile time value
T.sizeof;       // Compile time value
t.sizeof;       // Runtime value
// But others doesn't
int.max;        // Direct type access ok!
T.max;          // Const typeid access ok!
t.max;          // ERROR! Runtime access is not ok!
// Using it as a type in an assignment:
int a;          // Ok of course
T a;            // ERROR!
$typefrom(T) a; // Ok! Converts a typeid to a type.
$typefrom(t) a; // ERROR! Must be a compile time typeid

For macros that had type arguments they usually looked like this:

macro new_thing($Type) { ... }
...

Foo* f = new_thing(Foo);

The disadvantage was that if you had a constant typeid you'd first have to convert it to a type:

const typeid T = Foo.typeid;

void* thing = new_thing($typefrom(T));

Starting from 0.7.3 though, we can use the constant typeid directly and get an implicit conversion to a type:

const typeid T = Foo.typeid;

void* thing = new_thing(T);  // New in 0.7.3

The reason this is useful, is because constant macros can't return a type directly BUT can return a typeid. So maybe you have macro code like this:

typeid $type = @select_the_type($foo);

void* thing = new_thing($typefrom($type));

It can now be this instead:

typeid $type = @select_the_type($foo);

void* thing = new_thing($type);

And contracting the code it's even more of an improvement:

void* thing = new_thing($typefrom(@select_the_type($foo))); // 0.7.2
void* thing = new_thing(@select_the_type($foo));            // 0.7.3

It's just overall clearer to drop the superfluous $typefrom.

This also extends to the $assignable builtin, which also now takes either a type or a typeid.

Removing $evaltype

Also related to typeid and types is the deprecation of $evaltype. It takes a constant string and turns it into a type, so you could write:

String $name = "foo";
$evaltype($name) x = 1;

In order to simplify the language $evaltype is getting removed but the functionality is still there, because $typefrom can now also take a string.

String $name = "foo";
$typefrom($name) x = 1; // New in 0.7.3

This hopefully reduces the amount of different constructs it's necessary to know in order to understand advanced compile time code generation.

Improved type inference

C3 has type inference for initializers:

fn void test(int[3] x)
{ ... }

test({1, 2, 3});

However, this did not extend to &&:

// 0.7.2
fn void test(int[3]* x)
{ ... }

test(&&{1, 2, 3});      // ERROR!

This has now been improved:

// 0.7.3
fn void test(int[3]* x)
{ ... }

test(&&{1, 2, 3});      // Works in 0.7.3

Compile time @sprintf

Creating strings at compile time is sometimes desirable, but the help for it has been complicated an inefficient. Usually the use of +++ has been needed:

String $s = $foo +++ ":" +++ $bar;

Now, however, there is a @sprintf. It's not a full-fledged printf-like function. It only supports "%s" arguments, but will do a best effort to convert any compile time value:

int $v = 123;
Enum $x = FOO;
String $s = @sprintf("%s:%s", $v, $x); // $s = "123:FOO"

Often this is good enough, and the operation is much cheaper than the other compile time concatenation in terms of compiler resources.

$assert and $error "printf"

Hand-in-hand with the @sprintf, $assert and $error now accepts the same format as @sprintf for better compile time errors:

var $a = -123;
$assert $a > 0 : "$a was %s", $a;
// Above is a compile time error printing "$a was -123"

Better $eval

Where $evaltype is going away, $eval, which can turn a string into an identifier has been improved to properly work with @foo, #foo and $foo identifiers.

More liberal "main"

The main function used to only support String[] or int argc, char **argv as parameters. Now it accepts distinct types that lowers to the same arguments, so int argc, ZString* argv works properly.

--sources to add additional files to compile

Sometimes it's useful to add extra sources to compile with a regular build. --sources allows you to do exactly that:

c3c build --sources inject.c3

Improved overloading with wildcard types

One omission of the arithmetic overloads was supporting macros with wildcard types, so rather than writing a macro Foo Foo.add(self, int x) @operator(+) for every type you wanted to support, what if you could write macro Foo Foo.add(self, x) @operator(+) and handle the rest with contracts and compile time checks?

This actually crashed in 0.7.2, but in 0.7.3 this works correctly. It's even allowed with @operator_r and @operator_s variants.

Distinct improvements

Bitstructs can now be based on a distinct type, created by typedef:

typedef Test = int;
bitstruct Foo : Test // Works in 0.7.3
{
    int z : 1..3;
}

And it's now possible for generics to be generic over distinct const values:

module foo { VAL };
const GLOBAL = VAL;

module test;
import foo;
typedef Bar = int;

fn void main()
{
    const Bar B = 123;
    Bar b = foo::GLOBAL{B}; // Works in 0.7.3
}

Constant vector comparison

Vectors has been comparable at runtime, but not at compile time. However, from 0.7.3 and onwards this works:

int[<2>] $v = { 1, 2 };
int[<2>] $w = { 1, 2 };
$assert $v == $w;

Silence deprecation

@deprecated has been mostly used as a migration mechanism for the c3 standard library, but using it with user code requires more fine-grained control, which is why there is now an @allow_deprecated attribute to silence deprecation warnings inside a particular function:

fn void old() @deprecated {}

fn void test1()
{
    old(); // Emits warning
}

fn void test2() @allow_deprecated
{
    old(); // No warning
}

Removal of '\f' as whitespace

\f (the form-feed character) is not really used in normal text, so it has been removed from the characters recognized as whitespace by the lexer.

$memberof get/set improvements

You can get the member of a struct or union by indexing Type.membersof and then use the get method on the member to retrieve the value. However, there was no corresponding set method, instead you had to do *&$member.get(2) = 123. People have been asking for a .set for a long time, and that was also the only way to properly support setting bitstruct values:

bitstruct Foo : int
{
    int x : 1..5;
    int y : 10..14;
}
fn void test()
{
    var $member = Foo.membersof[1];
    Foo f = { 1, 3 };
    int val = $member.get(f);        // .get on bitstructs now works
    $member.set(f, val + 1);         // .set method updates
    io::printn(f.y);                 // Prints 4
}

Custom file extensions in build targets

It's now possible to override the extension used for libraries and executables of a target with the "extension" project property.

Bug fixes

0.7.3 shipped with around 40 bug fixes, which is a bit below average for a 0.0.1 release.

Stdlib additions

ZString types can now be compared using == by having equality overloaded. We have a SHA512 library and String.escape, String.unescape for escaping and unescaping strings. io::struct_to_format also now supports bitstructs, which means io::printn and related functions properly prints bitstructs.

Stdlib deprecations

String.is_zstr and String.quick_zstr are unsafe and are deprecated is_array_or_slice_of_char and is_arrayptr_or_slice_of_char are replaced by constant @ variants.

Change Log

Click for full change log

Changes / improvements

  • $typefrom now also accepts a constant string, and so works like $evaltype.
  • $evaltype is deprecated in favour of $typefrom.
  • Literal rules have changed, this makes -0xFF now a signed integer.
  • Implicitly convert from constant typeid to Type in $Type assignment, and $assignable.
  • Make $Type parameters accept constant typeid values.
  • Deprecate foo.#bar.
  • Allow inference across && #2172.
  • Added support for custom file extensions in project.json targets.
  • $eval now also works with @foo, #foo, $Foo and $foo parameters #2114.
  • @sprintf macro (based on the $$sprintf builtin) allows compile time format strings #1874.
  • Improve error reports when encountering a broken "if-catch".
  • Add printf format to $assert and $error #2183.
  • Make accepting arguments for main a bit more liberal, accepting main(int argc, ZString* argv)
  • Make $echo and @sprintf correctly stringify compile time initializers and slices.
  • Add --sources build option to add additional files to compile. #2097
  • Support untyped second argument for operator overloading.
  • The form-feed character '\f' is no longer valid white space.
  • Show code that caused unreachable code #2207
  • Allow generics over distinct types #2216.
  • Support distrinct types as the base type of bitstructs. #2218
  • Add hash::sha512 module to stdlib. #2227
  • Compile time type assignment (eg $Foo = int) is no longer an expression.
  • Add @allow_deprecated attribute to functions to selectively allow deprecated declarations #2223.
  • Improve error message on pointer diff #2239.
  • Compile-time comparison of constant vectors. #1575.
  • $member.get supports bitstructs.
  • $member.set for setting members without the *& trick.
  • Initial support for #1925, does not affect C compilation yet, and doesn't try to link etc. Using "--emit-only"

Fixes

  • -2147483648, MIN literals work correctly.
  • Splatting const slices would not be const. #2185
  • Fixes to $define handling of binary ops.
  • Fixes methodsof to pick up all sorts of extension methods. #2192
  • --lsp sometimes does not emit end tag #2194.
  • Improve Android termux detection.
  • Update Android ABI.
  • Fixes to @format checking #2199.
  • Distinct versions of builtin types ignore @operator overloads #2204.
  • @operator macro using untyped parameter causes compiler segfault #2200.
  • Make unreachable() only panic in safe mode.
  • cflags additions for targets was not handed properly. #2209
  • $echo would suppress warning about unreachable code. #2205
  • Correctly format '%c' when given a width. #2199
  • Fix to is_array_or_slice_of_char #2214.
  • Method on array slice caused segfault #2211.
  • In some cases, the compiler would dereference a compile time null. #2215
  • Incorrect codegen if a macro ends with unreachable and is assigned to something. #2210
  • Fix error for named arguments-order with compile-time arguments #2212
  • Bug in AST copying would make operator overloading like += compile incorrectly #2217.
  • $defined(#expr) broken with binary. #2219
  • Method ambiguity when importing parent module publicly in private submodule. #2208
  • Linker errors when shadowing @local with public function #2198
  • Bug when offsetting pointers of large structs using ++ and --.
  • x++ and x-- works on pointer vectors #2222.
  • x += 1 and x -= 1 works properly on pointer vectors #2222.
  • Fixes to x += { 1, 1 } for enum and pointer vectors #2222.
  • Linking fails on operator method imported as @public #2224.
  • Lambda C-style vaargs were not properly rejected, leading to crash #2229.
  • Incorrect handling of constant null fault causing compiler crash #2232.
  • Overload resolution fixes to inline typedef #2226.
  • math::overflow_* wrappers incorrectly don't allow distinct integers #2221.
  • Compiler segfault when using distinct type in attribute imported from other module #2234.
  • Assert casting bitstruct to short/char #2237.
  • @tag didn't work with members #2236.
  • Assert comparing untyped lists #2240.
  • Fix bugs relating to optional interface addr-of #2244.
  • Compiler null pointer when building a static-lib with -o somedir/... #2246
  • Segfault in the compiler when using a bitstruct constant defined using a cast with an operator #2248.
  • Default assert() message drops parens #2249.

Stdlib changes

  • Deprecate String.is_zstr and String.quick_zstr #2188.
  • Add comparison with == for ZString types.
  • is_array_or_slice_of_char and is_arrayptr_or_slice_of_char are replaced by constant @ variants.
  • @pool now has an optional reserve parameter, some minor changes to the temp_allocator API
  • io::struct_to_format now supports bitstructs.
  • Add String.escape, String.unescape for escaping and unescaping a string.

Want To Dive Into C3?

Check out the documentation or download it and try it out.

Have questions? Come and chat with us on Discord.

Discuss this article on Reddit or Hacker News.

C3 0.7.2 - Quality Of Life

Unlike 0.7.1, 0.7.2 is not about big new features, instead it is laser focused on adding quality of life improvements, which are backwards compatible with other 0.7 releases.

Compile time additions

:::note[Note on compile time] C3 has visual differentiation between compile time and runtime code, to make it explicit when the code will be run.

Compile time code uses $if condition: ... $endif for conditional logic. Learn more about macros and compile time evaluation. :::

Simplifying compile time evaluated code

Setting a variable at compile time can now be simplified using compile time logical or ||| and compile time logical and &&& with a variable right hand expression.

The original code to set a variable at compile time:

$if FOO:
    bool x = true;
$else
    bool x = foo();
$endif

Now this compile time variable can be set with compile time logical or ||| compactly as:

bool x = FOO ||| foo();

You can also use compile time logical and &&& in a similar way.

Compile time random: @rnd

Sometimes it can be useful to create unique ids at compile time. This is now possible with the @rnd macro.

Compile time ceil: math::@ceil

It is somewhat hard to write a good compile time ceil function, so 0.7.2 adds a builtin which is accessible using the math::@ceil macro.

General additions

Set the run directory

To make c3c run and c3c compile-run more convenient, it's now possible to use --run-dir (or the project setting run-dir) to set the director from which the compiler runs the resulting binary.

Limits to SIMD sizes

Limitations of vectors are sometimes misunderstood, they will cause code bloat if used for large vectors. For this reason a max vector size has been introduced. By default this is 4096 bits, so basically the size of double[<64>]. It is possible to increase this using --max-vector-size as needed. (As a comparison, the biggest SIMD vectors on x64 is 512 bits, so such a 4096 bit vector would be represented by 8 registers).

has_tagof works on builtin types

While has_tagof will always return false on builtin types, this change nonetheless simplifies writing compile time code involving tags.

Allow recursive generic modules

To simplify generic module resolution it was not possible to recursively generate generic module. This restriction has been lifted in 0.7.1.

Deprecations

Bitsize suffix deprecations

The bitsize suffixes are deprecated, so rather than writing 23u64 use the C style 23UL instead. u128 and i128 suffixes are replaced by ULL and LL suffixes.

Finally, the d suffix for doubles have been added as a complement to f.

Compile time reflection deprecations

MyEnum.elements have been deprecated. Use MyEnum.len instead. SomeFn.params have been deprecated. Use SomeFn.paramsof instead.

Deprecation for old @param docs

By 0.7.1 the declaration style @param foo "abc" would be allowed rather than @param foo : "abc". This was by accident. It's now properly deprecated.

Generic faults are not allowed

Creating faults that are parameterized is usually a mistake and should not have been allowed. It's been completely removed in 0.7.2 as it was classified as a bug.

This is no longer allowed:

module Foo {Type};
typedef BAZ, HELLO;

Faults should be defined in a non-generic module, such as a parent module or sub-module instead.

Fixes

This release contains about 30 different fixes, most of them newly discovered and not regressions.

They range from fixes to advanced generic types to stdlib bugs.

Stdlib updates

Currently a new version of the matrix library is incubating, and while it didn't make it for the 0.7.2 release, I hope it can be included by 0.7.4 and contributions are welcome!

Optimized String -> ZString conversions

:::note[Note On Strings] Default C3 string: typedef String = inline char[];
C compatible, null terminated: typedef ZString = inline char*;

Learn more about strings. :::

Sometimes a String is already pointing to a valid ZString, so no copy is needed. To check this, String gets two new methods .is_zstr to check if the String is also zero terminated, and .quick_zstr which will create a temp ZString if needed, but otherwise use the String.

std::ascii moves into std::core

std::ascii moved into std::core::ascii. Old _m variants are deprecated, as is uint methods.

Better String tokenization

It's now possible to further customize tokenization, to ignore empty elements in the middle (or not), ignore any last empty elements at the end (or not). This introduces .tokenize_all which replaces the now deprecated .splitter method.

Count and replace

String further gets some conveniences: .count to count the number of instances of a string within the string and replace / treplace to return a new String with a substring replaced. This functionality was already in DString, but was now added to String as well.

Operator overloads for std::time and Maybe

Duration * Int, Clock - Clock and DateTime + Duration overloads were added for manipulating time.

For Maybe, the == operator is available when the inner type is equatable.

Subprocess improvements

Subprocess added an inherit_stdio option to inherit the parent's stdin, stdout, and stderr instead of creating pipes.

Change Log

Click for full change log
Changes / improvements
  • Better default assert messages when no message is specified #2122
  • Add --run-dir, to specify directory for running executable using compile-run and run #2121.
  • Add run-dir to project.json.
  • Add quiet to project.json.
  • Deprecate uXX and iXX bit suffixes.
  • Add experimental LL / ULL suffixes for int128 and uint128 literals.
  • Allow the right hand side of ||| and &&& be runtime values.
  • Added @rnd() compile time random function (using the $$rnd() builtin). #2078
  • Add math::@ceil() compile time ceil function. #2134
  • Improve error message when using keywords as functions/macros/variables #2133.
  • Deprecate MyEnum.elements.
  • Deprecate SomeFn.params.
  • Improve error message when encountering recursively defined structs. #2146
  • Limit vector max size, default is 4096 bits, but may be increased using --max-vector-size.
  • Allow the use of has_tagof on builtin types.
  • @jump now included in --list-attributes #2155.
  • Add $$matrix_mul and $$matrix_transpose builtins.
  • Add d as floating point suffix for double types.
  • Deprecate f32, f64 and f128 suffixes.
  • Allow recursive generic modules.
  • Add deprecation for @param foo "abc".
  • Add --header-output and header-output options for controlling header output folder.
  • Generic faults is disallowed.
Fixes
  • Assert triggered when casting from int[2] to uint[2] #2115
  • Assert when a macro with compile time value is discarded, e.g. foo(); where foo() returns an untyped list. #2117
  • Fix stringify for compound initializers #2120.
  • Fix No index OOB check for [:^n] #2123.
  • Fix regression in Time diff due to operator overloading #2124.
  • attrdef with any invalid name causes compiler assert #2128.
  • Correctly error on @attrdef Foo = ;.
  • Contract on trying to use Object without initializing it.
  • Variable aliases of aliases would not resolve correctly. #2131
  • Variable aliases could not be assigned to.
  • Some folding was missing in binary op compile time resolution #2135.
  • Defining an enum like ABC = { 1 2 } was accidentally allowed.
  • Using a non-const as the end range for a bitstruct would trigger an assert.
  • Incorrect parsing of ad hoc generic types, like Foo{int}**** #2140.
  • $define did not correctly handle generic types #2140.
  • Incorrect parsing of call attributes #2144.
  • Error when using named argument on trailing macro body expansion #2139.
  • Designated const initializers with {} would overwrite the parent field.
  • Empty default case in @jump switch does not fallthrough #2147.
  • &&& was accidentally available as a valid prefix operator.
  • Missing error on default values for body with default arguments #2148.
  • --path does not interact correctly with relative path arguments #2149.
  • Add missing @noreturn to os::exit.
  • Implicit casting from struct to interface failure for inheriting interfaces #2151.
  • Distinct types could not be used with tagof #2152.
  • $$sat_mul was missing.
  • for with incorrect var declaration caused crash #2154.
  • Check pointer/slice/etc on [out] and & params. #2156.
  • Compiler didn't check foreach over flexible array member, and folding a flexible array member was allowed #2164.
  • Too strict project view #2163.
  • Bug using #foo arguments with $defined #2173
  • Incorrect ensure on String.split.
  • Removed the naive check for compile time modification, which fixes #1997 but regresses in detection.
Stdlib changes
  • Added String.quick_ztr and String.is_zstr
  • std::ascii moved into std::core::ascii. Old _m variants are deprecated, as is uint methods.
  • Add String.tokenize_all to replace the now deprecated String.splitter
  • Add String.count to count the number of instances of a string.
  • Add String.replace and String.treplace to replace substrings within a string.
  • Add Duration * Int and Clock - Clock overload.
  • Add DateTime + Duration overloads.
  • Add Maybe.equals and respective == operator when the inner type is equatable.
  • Add inherit_stdio option to SubProcessOptions to inherit parent's stdin, stdout, and stderr instead of creating pipes. #2012
  • Remove superfluous cleanup parameter in os::exit and os::fastexit.
  • Add extern fn ioctl(CInt fd, ulong request, ...) binding to libc;

Want To Dive Into C3?

Check out the documentation or download it and try it out.

Have questions? Come and chat with us on Discord.

Gradual improvements: C3 0.7.2

Originally from: https://c3.handmade.network/blog/p/9028-gradual_improvements__c3_0.7.2

It's another months and consequently it's time for a new incremental 0.7 release.

Additions

Unlike 0.7.1, 0.7.2 doesn't really add anything major, but just add a few quality of life enhancements:

||| and &&& with variable right hand side

Rather than writing:

$if FOO:
    bool x = true;
$else
    bool x = foo();
$endif

It's now possible to use ||| to write this compactly as:

bool x = FOO ||| foo();

&&& also supports this.

Compile time random: @rnd

Sometimes it can be useful to create unique ids at compile time. This is now possible with the @rnd macro.

Compile time ceil: math::@ceil

It is somewhat hard to write a good compile time ceil function, so 0.7.2 adds a builtin which is accessible using the math::@ceil macro.

Set the run directory

To make c3c run and c3c compile-run more convenient, it's now possible to use --run-dir (or the project setting run-dir) to set the director from which the compiler runs the resulting binary.

Suffix deprecations

The bitsize suffixes are deprecated, so rather than writing 23u64 use the C style 23UL instead. u128 and i128 suffixes are replaced by ULL and LL suffixes.

Finally, the d suffix for doubles have been added as a complement to f.

Compile time reflection deprecations

MyEnum.elements have been deprecated. Use MyEnum.len instead.

SomeFn.params have been deprecated. Use SomeFn.paramsof instead.

Limits to SIMD sizes

Limitations of vectors are sometimes misunderstood, they will cause code bloat if used for large vectors. For this reason a max vector size has been introduced. By default this is 4096 bits, so basically the size of double[<64>]. It is possible to increase this using --max-vector-size as needed. (As a comparison, the biggest SIMD vectors on x64 is 512 bits, so such a 4096 bit vector would be represented by 8 registers).

has_tagof works on builtin types

While has_tagof will always return false on builtin types, this change nonetheless simplifies writing compile time code involving tags.

Allow recursive generic modules

To simplify generic module resolution it was not possible to recursively generate generic module. This restriction has been lifted in 0.7.1.

Deprecation for old @param docs

By 0.7.1 the declaration style @param foo "abc" would be allowed rather than @param foo : "abc". This was by accident. It's now properly deprecated.

Generic faults are not allowed

Creating faults that are parameterized is usually a mistake and should not have been allowed. It's been completely removed in 0.7.2 as it was classified as a bug.

So no more

module Foo {Type};
typedef BAZ, HELLO;

Faults should be places in a parent or sub-module instead.

Fixes

This release contains about 30 different fixes, most of them newly discovered and not regressions.

They range from fixes to advanced generic types to stdlib bugs.

Stdlib updates

Currently a new version of the matrix library is incubating, and while it didn't make it for the 0.7.2 release, I hope it can be included by 0.7.4.

Optimized String -> ZString conversions

Sometimes a String is already pointing to a valid ZString, so no copy is needed. To check this, String gets two new methods .is_zstr to check if the String is also zero terminated, and .quick_zstr which will create a temp ZString if needed, but otherwise use the String.

std::ascii moves into core

std::ascii moved into std::core::ascii. Old _m variants are deprecated, as is uint methods.

Better String tokenization

It's now possible to further customize tokenization, to ignore empty elements in the middle (or not), ignore any last empty elements at the end (or not). This introduces .tokenize_all which replaces the now deprecated .splitter method.

Count and replace

String further gets some conveniences: .count to count the number of instances of a string within the string and replace / treplace to return a new String with a substring replaced. This functionality was already in DString, but was now added to String as well.

Operator overloads for std::time and Maybe

Duration * Int, Clock - Clock and DateTime + Duration overloads were added for manipulating time.

For Maybe, the == operator is available when the inner type is equatable.

Subprocess improvements

Subprocess added an inherit_stdio option to inherit the parent's stdin, stdout, and stderr instead of creating pipes.

Changelist

The full changelist is here:

### Changes / improvements
- Better default assert messages when no message is specified #2122
- Add `--run-dir`, to specify directory for running executable using `compile-run` and `run` #2121.
- Add `run-dir` to project.json.
- Add `quiet` to project.json.
- Deprecate uXX and iXX bit suffixes.
- Add experimental LL / ULL suffixes for int128 and uint128 literals.
- Allow the right hand side of `|||` and `&&&` be runtime values.
- Added `@rnd()` compile time random function (using the `$$rnd()` builtin). #2078
- Add `math::@ceil()` compile time ceil function. #2134
- Improve error message when using keywords as functions/macros/variables #2133.
- Deprecate `MyEnum.elements`.
- Deprecate `SomeFn.params`.
- Improve error message when encountering recursively defined structs. #2146
- Limit vector max size, default is 4096 bits, but may be increased using --max-vector-size.
- Allow the use of `has_tagof` on builtin types.
- `@jump` now included in `--list-attributes` #2155.
- Add `$$matrix_mul` and `$$matrix_transpose` builtins.
- Add `d` as floating point suffix for `double` types.
- Deprecate `f32`, `f64` and `f128` suffixes.
- Allow recursive generic modules.
- Add deprecation for `@param foo "abc"`.
- Add `--header-output` and `header-output` options for controlling header output folder.
- Generic faults is disallowed.

### Fixes
- Assert triggered when casting from `int[2]` to `uint[2]` #2115
- Assert when a macro with compile time value is discarded, e.g. `foo();` where `foo()` returns an untyped list. #2117
- Fix stringify for compound initializers #2120.
- Fix No index OOB check for `[:^n]` #2123.
- Fix regression in Time diff due to operator overloading #2124.
- attrdef with any invalid name causes compiler assert #2128.
- Correctly error on `@attrdef Foo = ;`.
- Contract on trying to use Object without initializing it.
- Variable aliases of aliases would not resolve correctly. #2131
- Variable aliases could not be assigned to.
- Some folding was missing in binary op compile time resolution #2135.
- Defining an enum like `ABC = { 1 2 }` was accidentally allowed.
- Using a non-const as the end range for a bitstruct would trigger an assert.
- Incorrect parsing of ad hoc generic types, like `Foo{int}****` #2140.
- $define did not correctly handle generic types #2140.
- Incorrect parsing of call attributes #2144.
- Error when using named argument on trailing macro body expansion #2139.
- Designated const initializers with `{}` would overwrite the parent field.
- Empty default case in @jump switch does not fallthrough #2147.
- `&&&` was accidentally available as a valid prefix operator.
- Missing error on default values for body with default arguments #2148.
- `--path` does not interact correctly with relative path arguments #2149.
- Add missing `@noreturn` to `os::exit`.
- Implicit casting from struct to interface failure for inheriting interfaces #2151.
- Distinct types could not be used with tagof #2152.
- `$$sat_mul` was missing.
- `for` with incorrect `var` declaration caused crash #2154.
- Check pointer/slice/etc on `[out]` and `&` params. #2156.
- Compiler didn't check foreach over flexible array member, and folding a flexible array member was allowed #2164.
- Too strict project view #2163.
- Bug using `#foo` arguments with `$defined` #2173
- Incorrect ensure on String.split.
- Removed the naive check for compile time modification, which fixes #1997 but regresses in detection.

### Stdlib changes
- Added `String.quick_ztr` and `String.is_zstr`
- std::ascii moved into std::core::ascii. Old _m variants are deprecated, as is uint methods.
- Add `String.tokenize_all` to replace the now deprecated `String.splitter`
- Add `String.count` to count the number of instances of a string.
- Add `String.replace` and `String.treplace` to replace substrings within a string.
- Add `Duration * Int` and `Clock - Clock` overload.
- Add `DateTime + Duration` overloads.
- Add `Maybe.equals` and respective `==` operator when the inner type is equatable.
- Add `inherit_stdio` option to `SubProcessOptions` to inherit parent's stdin, stdout, and stderr instead of creating pipes. #2012
- Remove superfluous `cleanup` parameter in `os::exit` and `os::fastexit`.
- Add `extern fn ioctl(CInt fd, ulong request, ...)` binding to libc;

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

Comments


Comment by Christoffer Lernö

It's another months and consequently it's time for a new incremental 0.7 release.

Additions

Unlike 0.7.1, 0.7.2 doesn't really add anything major, but just add a few quality of life enhancements:

||| and &&& with variable right hand side

Rather than writing:

$if FOO:
    bool x = true;
$else
    bool x = foo();
$endif

It's now possible to use ||| to write this compactly as:

bool x = FOO ||| foo();

&&& also supports this.

Compile time random: @rnd

Sometimes it can be useful to create unique ids at compile time. This is now possible with the @rnd macro.

Compile time ceil: math::@ceil

It is somewhat hard to write a good compile time ceil function, so 0.7.2 adds a builtin which is accessible using the math::@ceil macro.

Set the run directory

To make c3c run and c3c compile-run more convenient, it's now possible to use --run-dir (or the project setting run-dir) to set the director from which the compiler runs the resulting binary.

Suffix deprecations

The bitsize suffixes are deprecated, so rather than writing 23u64 use the C style 23UL instead. u128 and i128 suffixes are replaced by ULL and LL suffixes.

Finally, the d suffix for doubles have been added as a complement to f.

Compile time reflection deprecations

MyEnum.elements have been deprecated. Use MyEnum.len instead.

SomeFn.params have been deprecated. Use SomeFn.paramsof instead.

Limits to SIMD sizes

Limitations of vectors are sometimes misunderstood, they will cause code bloat if used for large vectors. For this reason a max vector size has been introduced. By default this is 4096 bits, so basically the size of double[<64>]. It is possible to increase this using --max-vector-size as needed. (As a comparison, the biggest SIMD vectors on x64 is 512 bits, so such a 4096 bit vector would be represented by 8 registers).

has_tagof works on builtin types

While has_tagof will always return false on builtin types, this change nonetheless simplifies writing compile time code involving tags.

Allow recursive generic modules

To simplify generic module resolution it was not possible to recursively generate generic module. This restriction has been lifted in 0.7.1.

Deprecation for old @param docs

By 0.7.1 the declaration style @param foo "abc" would be allowed rather than @param foo : "abc". This was by accident. It's now properly deprecated.

Generic faults are not allowed

Creating faults that are parameterized is usually a mistake and should not have been allowed. It's been completely removed in 0.7.2 as it was classified as a bug.

So no more

module Foo {Type};
typedef BAZ, HELLO;

Faults should be places in a parent or sub-module instead.

Fixes

This release contains about 30 different fixes, most of them newly discovered and not regressions.

They range from fixes to advanced generic types to stdlib bugs.

Stdlib updates

Currently a new version of the matrix library is incubating, and while it didn't make it for the 0.7.2 release, I hope it can be included by 0.7.4.

Optimized String -> ZString conversions

Sometimes a String is already pointing to a valid ZString, so no copy is needed. To check this, String gets two new methods .is_zstr to check if the String is also zero terminated, and .quick_zstr which will create a temp ZString if needed, but otherwise use the String.

std::ascii moves into core

std::ascii moved into std::core::ascii. Old _m variants are deprecated, as is uint methods.

Better String tokenization

It's now possible to further customize tokenization, to ignore empty elements in the middle (or not), ignore any last empty elements at the end (or not). This introduces .tokenize_all which replaces the now deprecated .splitter method.

Count and replace

String further gets some conveniences: .count to count the number of instances of a string within the string and replace / treplace to return a new String with a substring replaced. This functionality was already in DString, but was now added to String as well.

Operator overloads for std::time and Maybe

Duration * Int, Clock - Clock and DateTime + Duration overloads were added for manipulating time.

For Maybe, the == operator is available when the inner type is equatable.

Subprocess improvements

Subprocess added an inherit_stdio option to inherit the parent's stdin, stdout, and stderr instead of creating pipes.

Changelist

The full changelist is here:

### Changes / improvements
- Better default assert messages when no message is specified #2122
- Add `--run-dir`, to specify directory for running executable using `compile-run` and `run` #2121.
- Add `run-dir` to project.json.
- Add `quiet` to project.json.
- Deprecate uXX and iXX bit suffixes.
- Add experimental LL / ULL suffixes for int128 and uint128 literals.
- Allow the right hand side of `|||` and `&&&` be runtime values.
- Added `@rnd()` compile time random function (using the `$$rnd()` builtin). #2078
- Add `math::@ceil()` compile time ceil function. #2134
- Improve error message when using keywords as functions/macros/variables #2133.
- Deprecate `MyEnum.elements`.
- Deprecate `SomeFn.params`.
- Improve error message when encountering recursively defined structs. #2146
- Limit vector max size, default is 4096 bits, but may be increased using --max-vector-size.
- Allow the use of `has_tagof` on builtin types.
- `@jump` now included in `--list-attributes` #2155.
- Add `$$matrix_mul` and `$$matrix_transpose` builtins.
- Add `d` as floating point suffix for `double` types.
- Deprecate `f32`, `f64` and `f128` suffixes.
- Allow recursive generic modules.
- Add deprecation for `@param foo "abc"`.
- Add `--header-output` and `header-output` options for controlling header output folder.
- Generic faults is disallowed.

### Fixes
- Assert triggered when casting from `int[2]` to `uint[2]` #2115
- Assert when a macro with compile time value is discarded, e.g. `foo();` where `foo()` returns an untyped list. #2117
- Fix stringify for compound initializers #2120.
- Fix No index OOB check for `[:^n]` #2123.
- Fix regression in Time diff due to operator overloading #2124.
- attrdef with any invalid name causes compiler assert #2128.
- Correctly error on `@attrdef Foo = ;`.
- Contract on trying to use Object without initializing it.
- Variable aliases of aliases would not resolve correctly. #2131
- Variable aliases could not be assigned to.
- Some folding was missing in binary op compile time resolution #2135.
- Defining an enum like `ABC = { 1 2 }` was accidentally allowed.
- Using a non-const as the end range for a bitstruct would trigger an assert.
- Incorrect parsing of ad hoc generic types, like `Foo{int}****` #2140.
- $define did not correctly handle generic types #2140.
- Incorrect parsing of call attributes #2144.
- Error when using named argument on trailing macro body expansion #2139.
- Designated const initializers with `{}` would overwrite the parent field.
- Empty default case in @jump switch does not fallthrough #2147.
- `&&&` was accidentally available as a valid prefix operator.
- Missing error on default values for body with default arguments #2148.
- `--path` does not interact correctly with relative path arguments #2149.
- Add missing `@noreturn` to `os::exit`.
- Implicit casting from struct to interface failure for inheriting interfaces #2151.
- Distinct types could not be used with tagof #2152.
- `$$sat_mul` was missing.
- `for` with incorrect `var` declaration caused crash #2154.
- Check pointer/slice/etc on `[out]` and `&` params. #2156.
- Compiler didn't check foreach over flexible array member, and folding a flexible array member was allowed #2164.
- Too strict project view #2163.
- Bug using `#foo` arguments with `$defined` #2173
- Incorrect ensure on String.split.
- Removed the naive check for compile time modification, which fixes #1997 but regresses in detection.

### Stdlib changes
- Added `String.quick_ztr` and `String.is_zstr`
- std::ascii moved into std::core::ascii. Old _m variants are deprecated, as is uint methods.
- Add `String.tokenize_all` to replace the now deprecated `String.splitter`
- Add `String.count` to count the number of instances of a string.
- Add `String.replace` and `String.treplace` to replace substrings within a string.
- Add `Duration * Int` and `Clock - Clock` overload.
- Add `DateTime + Duration` overloads.
- Add `Maybe.equals` and respective `==` operator when the inner type is equatable.
- Add `inherit_stdio` option to `SubProcessOptions` to inherit parent's stdin, stdout, and stderr instead of creating pipes. #2012
- Remove superfluous `cleanup` parameter in `os::exit` and `os::fastexit`.
- Add `extern fn ioctl(CInt fd, ulong request, ...)` binding to libc;

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

C3 0.7.1 - Operator Overloading

0.7.0 was supposed to be THE big change this year, but 0.7.1 is actually quitely introducing some very important features.

Operator overloading

Operator overloading has been added to help games and maths programming, a previous blog goes into more depth, below is a short overview.

Operator overloading overview

The @operator attribute defines the overload for Vec + int

To define something like int + Vec there are two additional attributes added:

  • @operator_s ("symmetric operator") allows either type to go first.
  • @operator_r ("reverse operator") requires the second term to be the first one in the arithmetic expression.
Examples

Overloading with the @operator overload attribute:

// Defining a simple add: "Complex + Complex"
macro Complex Complex.add(self, Complex b) @operator(+)
{
    return { .v = self.v + b.v };
} 
// Define the self-modifying +=: "Complex += Complex"
macro Complex Complex.add_this(&self, Complex b) @operator(+=) 
{
    return { .v = self.v += b.v };
}

Overloading with the @operator_s symmetric overload attribute:

// This defines "Quaternion * Real" /and/ "Real * Quaternion"
macro Quaternion Quaternion.scale(self, Real s) @operator_s(*)
{
    return { .v = self.v * s };
}

Overloading with the @operator_r reverse overload attribute:

// This defines "Real / Complex"
macro Complex Complex.div_real_inverse(Complex c, Real r) @operator_r(/) 
{
    return ((Complex) { .r = self }).div(c);
}

More static checking

An important step towards correctness is that @require contracts for functions are now inlined at the caller site in safe mode. This has always been a thing for macros, but now this works for functions as well.

This means simple errors can be caught by the compiler, such as using a null pointer where it wasn't allowed or passing a zero where the argument:

<*
 @require a > 0 : "The parameter cannot be zero"
*>
fn void test(int a)
{ ... }

fn void main()
{
    // Compile time error: contract violation 
    // "The parameter cannot be zero"
    test(0); 
}

This is just the start. C3 will increasingly use more static analysis to check correctness at compile time.

Improvements to attributes

@if attributes now work on locals and aliases defined using alias can now be @deprecated.

Enum lookup

0.6.9 and 0.7.0 introduced ways to let an enum convert to its associated value:

enum Foo : (inline int val)
{
    ABC = 3,
    LIFF = 42
}

fn void main()
{
    int val = Foo.LIFF; // Same as Foo.LIFF.val
}

Still, converting from such a associated value to an enum used the rather unknown @enum_from_value macro.

To make this more convenient, enums gain two new type functions .lookup and .lookup_field: - lookup(value) lookup an enum by inlined value. - lookup_field(field_name, value) lookup an enum by associated value.

fn void main()
{
    Foo? foo = Foo.lookup(42);            // Returns Foo.LIFF
    Foo? foo2 = Foo.lookup_field(val, 3); // Returns Foo.ABC
    Foo? foo3 = Foo.lookup_field(val, 1); // Returns NO_FOUND?
}

Other changes

  • String str = "" was not guaranteed to return a pointer to a null terminated empty string, unlike ZString str = "". This was a little inconsistent, so both return the same thing now.
  • Slice assignment foo[..] = bar would sometimes mean copying the right hand side to each element of foo and sometimes it meant a slice copy. Now semantics have been tightened: foo[..] = bar assigns to each element and foo[..] = bar[..] is a copy.
  • c3c build now picks the first target rather than the first executable.
  • Finally the Win32 thread implementation has been improved.

What's next?

Following this release, the focus will be on improving the standard library organization. New guidelines for submitting modules have been adopted to ensure that the implementation can be worked on without having to be tied to a pull request. Right now an updated matrix library is actively worked on.

There is also a new repo for uncurated C3 resources, where anyone can be showcased: So if you have a project you want to share – do file a pull request.

Change Log

Click for full change log

Changes / improvements

  • Better errors on some common casting mistakes (pointer->slice, String->ZString, deref pointer->array) #2064.
  • Better errors trying to convert an enum to an int and vice versa.
  • Function @require checks are added to the caller in safe mode. #186
  • Improved error message when narrowing isn't allowed.
  • Operator overloading for + - * / % & | ^ << >> ~ == != += -= *= /= %= &= |= ^= <<= >>=
  • Add @operator_r and @operator_s attributes.
  • More stdlib tests: sincos, ArenaAllocator, Slice2d.
  • Make aliases able to use @deprecated.
  • Refactored stdlib file organization.
  • Allow @if on locals.
  • String str = "" is now guaranteed to be null terminated. #2083
  • Improved error messages on Foo { 3, abc } #2099.
  • Foo[1..2] = { .baz = 123 } inference now works. #2095
  • Deprecated old inference with slice copy. Copying must now ensure a slicing operator at the end of the right hand side: foo[1..2] = bar[..] rather than the old foo[1..2] = bar. The old behaviour can be mostly retained with --use-old-slice-copy).
  • Added Enum.lookup and Enum.lookup_field.
  • c3c build picks first target rather than the first executable #2105.
  • New Win32 Mutex, ConditionVariable and OnceFlag implementation

Fixes

  • Trying to cast an enum to int and back caused the compiler to crash.
  • Incorrect rounding at compile time going from double to int.
  • Regression with invalid setup of the WASM temp allocator.
  • Correctly detect multiple overloads of the same type.
  • ABI bug on x64 Linux / MacOS when passing a union containing a struct of 3 floats. #2087
  • Bug with slice acces as inline struct member #2088.
  • @if now does implicit conversion to bool like $if. #2086
  • Fix broken enum inline -> bool conversions #2094.
  • @if was ignored on attrdef, regression 0.7 #2093.
  • @ensure was not included when the function doesn't return a value #2098.
  • Added missing @clone_aligned and add checks to @tclone
  • Comparing a distinct type with an enum with an inline distinct type failed unexpectedly.
  • The %s would not properly print function pointers.
  • Compiler crash when passing an untyped list as an argument to assert #2108.
  • @ensure should be allowed to read "out" variables. #2107
  • Error message for casting generic to incompatible type does not work properly with nested generics #1953
  • Fixed enum regression after 0.7.0 enum change.
  • ConditionVariable now properly works on Win32

Stdlib changes

  • Hash functions for integer vectors and arrays.
  • Prefer math::I and math::I_F for math::IMAGINARY and math::IMAGINARYF the latter is deprecated.
  • Add array::contains to check for a value in an array or slice.

Want To Dive Into C3?

Check out the documentation or download it and try it out.

Have questions? Come and chat with us on Discord.

C3 0.7.1 - Operator overloading, here we come!

Originally from: https://c3.handmade.network/blog/p/9021-c3_0.7.1_-_operator_overloading%252C_here_we_come%2521

0.7.0 was supposed to be THE big change this year, but 0.7.1 is actually quitely introducing some very important features.

Operator overloading

Operator overloading is the biggest change of course, which is already covered in great detail here. The @operator, @operator_s and @operator_r attributes are used to enable overloading for user defined types.

More static checking

An important step towards correctness is that @require contracts for functions are now inlined at the caller site for safe mode. This has always been a thing for macros, but now this works for functions as well. This means simple errors, such as using a null pointer where it wasn't allowed or passing a zero where the argument:

<*
 @require a > 0 : "The parameter cannot be zero"
*>
fn void test(int a)
{ ... }

fn void main()
{
    test(0); // Compile time error: contract violation "The parameter cannot be zero"
}

This is just the start. C3 will increasingly use more static analysis to check correctness at compile time.

Improvements to attributes

@if attributes now work on locals and aliases defined using alias can now be @deprecated.

Enum lookup

0.6.9 and 0.7.0 introduced ways to let an enum convert to its associated value:

enum Foo : (inline int val)
{
    ABC = 3,
    LIFF = 42
}

fn void main()
{
    int val = Foo.LIFF; // Same as Foo.LIFF.val
}

Still, converting from such a associated value would use the rather unknown @enum_from_value macro.

To make this more convenient, enums gain two new type functions .lookup and .lookup_field:

fn void main()
{
    Foo? foo = Foo.lookup(42);            // Returns Foo.LIFF
    Foo? foo2 = Foo.lookup_field(val, 3); // Returns Foo.ABC
    Foo? foo3 = Foo.lookup_field(val, 1); // Returns NO_FOUND?
}

Other changes

  • String str = "" was not guaranteed to return a pointer to a null terminated empty string, unlike ZString str = "". This was a little inconsistent, so both return the same thing now.
  • Slice assignment foo[..] = bar would sometimes mean copying the right hand side to each element of foo and sometimes it meant a slice copy. Now semantics have been tightened: foo[..] = bar assigns to each element and foo[..] = bar[..] is a copy.
  • c3c build now picks the first target rather than the first executable.
  • Finally the Win32 thread implementation has been improved.

What's next?

Following this release, the focus will be on improving the standard library organization. New guidelines for submitting modules have been adopted to ensure that the implementation can be worked on without having to be tied to a pull request. Right now an updated matrix library is actively worked on.

There is also a new repo for uncurated C3 resources, where anyone can be showcased: https://github.com/c3lang/c3-showcase, so if you have a project you want to share – do file a pull request.

The full change list for 0.7.1 can be found below:

Changes / improvements

  • Better errors on some common casting mistakes (pointer->slice, String->ZString, deref pointer->array) #2064.
  • Better errors trying to convert an enum to an int and vice versa.
  • Function @require checks are added to the caller in safe mode. #186
  • Improved error message when narrowing isn't allowed.
  • Operator overloading for + - * / % & | ^ << >> ~ == != += -= *= /= %= &= |= ^= <<= >>=
  • Add @operator_r and @operator_s attributes.
  • More stdlib tests: sincos, ArenaAllocator, Slice2d.
  • Make aliases able to use @deprecated.
  • Refactored stdlib file organization.
  • Allow @if on locals.
  • String str = "" is now guaranteed to be null terminated. #2083
  • Improved error messages on Foo { 3, abc } #2099.
  • Foo[1..2] = { .baz = 123 } inference now works. #2095
  • Deprecated old inference with slice copy. Copying must now ensure a slicing operator at the end of the right hand side: foo[1..2] = bar[..] rather than the old foo[1..2] = bar. The old behaviour can be mostly retained with --use-old-slice-copy).
  • Added Enum.lookup and Enum.lookup_field.
  • c3c build picks first target rather than the first executable #2105.
  • New Win32 Mutex, ConditionVariable and OnceFlag implementation

Fixes

  • Trying to cast an enum to int and back caused the compiler to crash.
  • Incorrect rounding at compile time going from double to int.
  • Regression with invalid setup of the WASM temp allocator.
  • Correctly detect multiple overloads of the same type.
  • ABI bug on x64 Linux / MacOS when passing a union containing a struct of 3 floats. #2087
  • Bug with slice acces as inline struct member #2088.
  • @if now does implicit conversion to bool like $if. #2086
  • Fix broken enum inline -> bool conversions #2094.
  • @if was ignored on attrdef, regression 0.7 #2093.
  • @ensure was not included when the function doesn't return a value #2098.
  • Added missing @clone_aligned and add checks to @tclone
  • Comparing a distinct type with an enum with an inline distinct type failed unexpectedly.
  • The %s would not properly print function pointers.
  • Compiler crash when passing an untyped list as an argument to assert #2108.
  • @ensure should be allowed to read "out" variables. #2107
  • Error message for casting generic to incompatible type does not work properly with nested generics #1953
  • Fixed enum regression after 0.7.0 enum change.
  • ConditionVariable now properly works on Win32

Stdlib changes

  • Hash functions for integer vectors and arrays.
  • Prefer math::I and math::I_F for math::IMAGINARY and math::IMAGINARYF the latter is deprecated.
  • Add array::contains to check for a value in an array or slice.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

Comments


Comment by Christoffer Lernö

0.7.0 was supposed to be THE big change this year, but 0.7.1 is actually quitely introducing some very important features.

Operator overloading

Operator overloading is the biggest change of course, which is already covered in great detail here. The @operator, @operator_s and @operator_r attributes are used to enable overloading for user defined types.

More static checking

An important step towards correctness is that @require contracts for functions are now inlined at the caller site for safe mode. This has always been a thing for macros, but now this works for functions as well. This means simple errors, such as using a null pointer where it wasn't allowed or passing a zero where the argument:

<*
 @require a > 0 : "The parameter cannot be zero"
*>
fn void test(int a)
{ ... }

fn void main()
{
    test(0); // Compile time error: contract violation "The parameter cannot be zero"
}

This is just the start. C3 will increasingly use more static analysis to check correctness at compile time.

Improvements to attributes

@if attributes now work on locals and aliases defined using alias can now be @deprecated.

Enum lookup

0.6.9 and 0.7.0 introduced ways to let an enum convert to its associated value:

enum Foo : (inline int val)
{
    ABC = 3,
    LIFF = 42
}

fn void main()
{
    int val = Foo.LIFF; // Same as Foo.LIFF.val
}

Still, converting from such a associated value would use the rather unknown @enum_from_value macro.

To make this more convenient, enums gain two new type functions .lookup and .lookup_field:

fn void main()
{
    Foo? foo = Foo.lookup(42);            // Returns Foo.LIFF
    Foo? foo2 = Foo.lookup_field(val, 3); // Returns Foo.ABC
    Foo? foo3 = Foo.lookup_field(val, 1); // Returns NO_FOUND?
}

Other changes

  • String str = "" was not guaranteed to return a pointer to a null terminated empty string, unlike ZString str = "". This was a little inconsistent, so both return the same thing now.
  • Slice assignment foo[..] = bar would sometimes mean copying the right hand side to each element of foo and sometimes it meant a slice copy. Now semantics have been tightened: foo[..] = bar assigns to each element and foo[..] = bar[..] is a copy.
  • c3c build now picks the first target rather than the first executable.
  • Finally the Win32 thread implementation has been improved.

What's next?

Following this release, the focus will be on improving the standard library organization. New guidelines for submitting modules have been adopted to ensure that the implementation can be worked on without having to be tied to a pull request. Right now an updated matrix library is actively worked on.

There is also a new repo for uncurated C3 resources, where anyone can be showcased: https://github.com/c3lang/c3-showcase, so if you have a project you want to share – do file a pull request.

The full change list for 0.7.1 can be found below:

Changes / improvements

  • Better errors on some common casting mistakes (pointer->slice, String->ZString, deref pointer->array) #2064.
  • Better errors trying to convert an enum to an int and vice versa.
  • Function @require checks are added to the caller in safe mode. #186
  • Improved error message when narrowing isn't allowed.
  • Operator overloading for + - * / % & | ^ << >> ~ == != += -= *= /= %= &= |= ^= <<= >>=
  • Add @operator_r and @operator_s attributes.
  • More stdlib tests: sincos, ArenaAllocator, Slice2d.
  • Make aliases able to use @deprecated.
  • Refactored stdlib file organization.
  • Allow @if on locals.
  • String str = "" is now guaranteed to be null terminated. #2083
  • Improved error messages on Foo { 3, abc } #2099.
  • Foo[1..2] = { .baz = 123 } inference now works. #2095
  • Deprecated old inference with slice copy. Copying must now ensure a slicing operator at the end of the right hand side: foo[1..2] = bar[..] rather than the old foo[1..2] = bar. The old behaviour can be mostly retained with --use-old-slice-copy).
  • Added Enum.lookup and Enum.lookup_field.
  • c3c build picks first target rather than the first executable #2105.
  • New Win32 Mutex, ConditionVariable and OnceFlag implementation

Fixes

  • Trying to cast an enum to int and back caused the compiler to crash.
  • Incorrect rounding at compile time going from double to int.
  • Regression with invalid setup of the WASM temp allocator.
  • Correctly detect multiple overloads of the same type.
  • ABI bug on x64 Linux / MacOS when passing a union containing a struct of 3 floats. #2087
  • Bug with slice acces as inline struct member #2088.
  • @if now does implicit conversion to bool like $if. #2086
  • Fix broken enum inline -> bool conversions #2094.
  • @if was ignored on attrdef, regression 0.7 #2093.
  • @ensure was not included when the function doesn't return a value #2098.
  • Added missing @clone_aligned and add checks to @tclone
  • Comparing a distinct type with an enum with an inline distinct type failed unexpectedly.
  • The %s would not properly print function pointers.
  • Compiler crash when passing an untyped list as an argument to assert #2108.
  • @ensure should be allowed to read "out" variables. #2107
  • Error message for casting generic to incompatible type does not work properly with nested generics #1953
  • Fixed enum regression after 0.7.0 enum change.
  • ConditionVariable now properly works on Win32

Stdlib changes

  • Hash functions for integer vectors and arrays.
  • Prefer math::I and math::I_F for math::IMAGINARY and math::IMAGINARYF the latter is deprecated.
  • Add array::contains to check for a value in an array or slice.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

C3 goes game and maths friendly with operator overloading

Originally from: https://c3.handmade.network/blog/p/9019-c3_goes_game_and_maths_friendly_with_operator_overloading

Operator overloading is a divisive feature. It's been used for atrocious hacks, and to make code totally unreadable, but it's also the reason people stick to a C-like subset of C++ rather than using plain C for projects.

C3 has had operator overloading for a long time, but only for array indexing. In fact there are four different overloads: len [] []= and &[]. Together they do not only enable indexing, but more importantly they enable foreach.

If a type implements len and at least one of [] and &[], then you get foreach on that type. &[] is required for foreach with ref arguments though. And while you can use [] without implementing len, you need len for reverse indexing like a[^3].

However, I've resisted adding operator overloading for arithmetics and equals. Such a change brings us further away from C semantics than I've felt comfortable with. It can certainly be extremely misused and cause confusion.

But, as of 0.7.1 it's in the language. So what made me change my mind?

Most important to me is the fact that in practice people need this. C already has features that can be misused, such as untyped pointers, and we still use it. The same can be said of operator overloading.

Overview

C3 allows the arithmetic operators + - * / % and unary - to be overloaded. In addition to this, bit operators ^ | & and << >> may be overloaded. == and != and "operator + assign" operators such as += and <<= are also possible to overload.

Some technical details:

  1. You can create multiple overloads for a single type, for example Vec * Vec and Vec * int.
  2. Only user defined types may be overloaded. However, bitstructs already have ^ | & defined, so those may not be overloaded.
  3. Overloading == will implicitly make != work and vice versa.
  4. "operator + assign" operators are not required, they will be generated from the operator if not present.

Working with primitive types

While it's not possible to add @operator (the attribute usually used to define the overload) to define something like int + Vec, there are two additional attributes added: @operator_s ("symmetric operator") and @operator_r ("reverse operator") to help it. The former allows either type to go first, and @operator_r requires the second term to be the first one in the arithmetic expression.

Examples

// Defining a simple add: "Complex + Complex"
macro Complex Complex.add(self, Complex b) @operator(+) => { .v = self.v + b.v };
// Define the self-modifying +=: "Complex += Complex"
macro Complex Complex.add_this(&self, Complex b) @operator(+=) => { .v = self.v += b.v };

The use of the symmetric operator overloading:

// This defines "Quaternion * Real" /and/ "Real * Quaternion"
macro Quaternion Quaternion.scale(self, Real s) @operator_s(*) => { .v = self.v * s };

And finally, reverse operator:

// This defines "Real / Complex"
macro Complex Complex.div_real_inverse(Complex c, Real r) @operator_r(/) => ((Complex) { .r = self }).div(c);

The goals and non-goals of C3 operator overloading

The goal of overloading in C3 is allow arithmetics to be used on more types than the built in ones. In C, types such as Complex needs to be implemented in the language itself to get access to + - * /. Expanding the number of builtin types is possible, and the solution the programming language Odin uses. In Odin there is a rich number of builtins, including quaternion and matrix types.

There is a limit to this, as each builtin would increase the complexity of the language itself. I recently considered fixed point types which in the absence of operator overloading would have to use method syntax or be builtins. The former makes such types second class and builtins would increase the language complexity.

That this is such a popular feature with maths and game programming also speaks strongly for its inclusion.

But we also have non goals for C3: I don't want operator overloading that is used to create "magic" solutions in C++. So overloading for things like casting operations, dereference, method dot and logical comparisons are out of scope.

The goal is to enable numerical types to be used in a convenient manner, not to enable advanced abstractions with smart pointers and the like.

That said, there are some omissions:

  1. Comparison operators: useful for some numerical types, such as fixed point numbers, but less so for others – like a complex number or a matrix. Does it need to be in?
  2. Bool conversion: again more useful for fixed point numbers than other numerical types even though zero may be well defined. That's said, it's easily abused.

So at least for 0.7.1, these are omitted.

C3: An evolved C + operator overloading

C3 brings a lot of conveniences and improvements to C: slices, modules, generics, error handling, SIMD vectors, methods, contracts and more.

Mostly these are incremental improvements. If we look at something like slices, C can have slice types too, using macros, but it's inconvenient. Similar with generics (macro-based generics are possible in C), SIMD vectors, contracts etc.

On the other hand .method() syntax is certainly not in C, and C3 has much more compile time reflection available, but this is rarely a major pain point for C programmers. Operator overloading on the other hand: this is something you can't properly emulate in C. So for C programmers that really need operator overloading, there isn't really any way to get this. C++ or some similarly more complex language - such as Rust – is the only way out.

Of the recent indie C alternatives: Odin, Zig, C3 and Hare, only C3 offers operator overloading(*). Odin, as previously mentioned, addresses the issue with a rich set of builtin numerical types instead.

So moving forward it's possible that this feature should be pushed a bit more as it differentiates C3 from the alternatives. People looking for a simpler alternative to C++ for "C + operator overloading", now has C3 as another option to consider.

(* One should note that Jai, Jonathan Blow's C++ alternative geared towards game programming, offers operator overloading)

Wrapping it up

It's taken a long time for C3 to get operator overloading, but now that it's finally here, the language will lean into it, taking advantage of what it offers. Some features, such as @compact implicitly allowing comparisons will likely be removed. The standard library will add fixed point numbers and potentially leverage it for other things as well that otherwise was so marginal as to never be considered (saturating integers comes to mind).

I have been extremely reluctant to add operator overloading for arithmetics and resisted it for years, so the fact that we're adding it to C3 anyway is showing how much value it brings.

0.7.1 will officially be released the 1st of May (possibly earlier)

Comments


Comment by Christoffer Lernö

Operator overloading is a divisive feature. It's been used for atrocious hacks, and to make code totally unreadable, but it's also the reason people stick to a C-like subset of C++ rather than using plain C for projects.

C3 has had operator overloading for a long time, but only for array indexing. In fact there are four different overloads: len [] []= and &[]. Together they do not only enable indexing, but more importantly they enable foreach.

If a type implements len and at least one of [] and &[], then you get foreach on that type. &[] is required for foreach with ref arguments though. And while you can use [] without implementing len, you need len for reverse indexing like a[^3].

However, I've resisted adding operator overloading for arithmetics and equals. Such a change brings us further away from C semantics than I've felt comfortable with. It can certainly be extremely misused and cause confusion.

But, as of 0.7.1 it's in the language. So what made me change my mind?

Most important to me is the fact that in practice people need this. C already has features that can be misused, such as untyped pointers, and we still use it. The same can be said of operator overloading.

Overview

C3 allows the arithmetic operators + - * / % and unary - to be overloaded. In addition to this, bit operators ^ | & and << >> may be overloaded. == and != and "operator + assign" operators such as += and <<= are also possible to overload.

Some technical details:

  1. You can create multiple overloads for a single type, for example Vec * Vec and Vec * int.
  2. Only user defined types may be overloaded. However, bitstructs already have ^ | & defined, so those may not be overloaded.
  3. Overloading == will implicitly make != work and vice versa.
  4. "operator + assign" operators are not required, they will be generated from the operator if not present.

Working with primitive types

While it's not possible to add @operator (the attribute usually used to define the overload) to define something like int + Vec, there are two additional attributes added: @operator_s ("symmetric operator") and @operator_r ("reverse operator") to help it. The former allows either type to go first, and @operator_r requires the second term to be the first one in the arithmetic expression.

Examples

// Defining a simple add: "Complex + Complex"
macro Complex Complex.add(self, Complex b) @operator(+) => { .v = self.v + b.v };
// Define the self-modifying +=: "Complex += Complex"
macro Complex Complex.add_this(&self, Complex b) @operator(+=) => { .v = self.v += b.v };

The use of the symmetric operator overloading:

// This defines "Quaternion * Real" /and/ "Real * Quaternion"
macro Quaternion Quaternion.scale(self, Real s) @operator_s(*) => { .v = self.v * s };

And finally, reverse operator:

// This defines "Real / Complex"
macro Complex Complex.div_real_inverse(Complex c, Real r) @operator_r(/) => ((Complex) { .r = self }).div(c);

The goals and non-goals of C3 operator overloading

The goal of overloading in C3 is allow arithmetics to be used on more types than the built in ones. In C, types such as Complex needs to be implemented in the language itself to get access to + - * /. Expanding the number of builtin types is possible, and the solution the programming language Odin uses. In Odin there is a rich number of builtins, including quaternion and matrix types.

There is a limit to this, as each builtin would increase the complexity of the language itself. I recently considered fixed point types which in the absence of operator overloading would have to use method syntax or be builtins. The former makes such types second class and builtins would increase the language complexity.

That this is such a popular feature with maths and game programming also speaks strongly for its inclusion.

But we also have non goals for C3: I don't want operator overloading that is used to create "magic" solutions in C++. So overloading for things like casting operations, dereference, method dot and logical comparisons are out of scope.

The goal is to enable numerical types to be used in a convenient manner, not to enable advanced abstractions with smart pointers and the like.

That said, there are some omissions:

  1. Comparison operators: useful for some numerical types, such as fixed point numbers, but less so for others – like a complex number or a matrix. Does it need to be in?
  2. Bool conversion: again more useful for fixed point numbers than other numerical types even though zero may be well defined. That's said, it's easily abused.

So at least for 0.7.1, these are omitted.

C3: An evolved C + operator overloading

C3 brings a lot of conveniences and improvements to C: slices, modules, generics, error handling, SIMD vectors, methods, contracts and more.

Mostly these are incremental improvements. If we look at something like slices, C can have slice types too, using macros, but it's inconvenient. Similar with generics (macro-based generics are possible in C), SIMD vectors, contracts etc.

On the other hand .method() syntax is certainly not in C, and C3 has much more compile time reflection available, but this is rarely a major pain point for C programmers. Operator overloading on the other hand: this is something you can't properly emulate in C. So for C programmers that really need operator overloading, there isn't really any way to get this. C++ or some similarly more complex language - such as Rust – is the only way out.

Of the recent indie C alternatives: Odin, Zig, C3 and Hare, only C3 offers operator overloading(*). Odin, as previously mentioned, addresses the issue with a rich set of builtin numerical types instead.

So moving forward it's possible that this feature should be pushed a bit more as it differentiates C3 from the alternatives. People looking for a simpler alternative to C++ for "C + operator overloading", now has C3 as another option to consider.

(* One should note that Jai, Jonathan Blow's C++ alternative geared towards game programming, offers operator overloading)

Wrapping it up

It's taken a long time for C3 to get operator overloading, but now that it's finally here, the language will lean into it, taking advantage of what it offers. Some features, such as @compact implicitly allowing comparisons will likely be removed. The standard library will add fixed point numbers and potentially leverage it for other things as well that otherwise was so marginal as to never be considered (saturating integers comes to mind).

I have been extremely reluctant to add operator overloading for arithmetics and resisted it for years, so the fact that we're adding it to C3 anyway is showing how much value it brings.

0.7.1 will officially be released the 1st of May (possibly earlier)

C3: Operator overloading is coming in 0.7.1

Originally from: https://c3.handmade.network/blog/p/9016-c3__operator_overloading_is_coming_in_0.7.1

Initially I had planned 0.7.1 to be mostly patching 0.7.0, but plans change.

The big news is that operator overloading is coming. Not just the existing overload of [] &[] and []=, but arithmetics, bit operations and equals is all coming.

Comparisons is conspicuously missing, but might arrive.

Overloading is here to simplify creating numerical types that can be manipulated with the normal operators.

Consequently, there won't be anything like C++, where you can overload deref, dot invocation, casts etc. Also, overloads may not manipulate global state.

All in all, using these overloads for anything but numerical types is not intended (yes, so don't string concat with + nor should you use << to append to lists etc)

Operator overloading will only allowed on user defined types.

Comments


Comment by Christoffer Lernö

Initially I had planned 0.7.1 to be mostly patching 0.7.0, but plans change.

The big news is that operator overloading is coming. Not just the existing overload of [] &[] and []=, but arithmetics, bit operations and equals is all coming.

Comparisons is conspicuously missing, but might arrive.

Overloading is here to simplify creating numerical types that can be manipulated with the normal operators.

Consequently, there won't be anything like C++, where you can overload deref, dot invocation, casts etc. Also, overloads may not manipulate global state.

All in all, using these overloads for anything but numerical types is not intended (yes, so don't string concat with + nor should you use << to append to lists etc)

Operator overloading will only allowed on user defined types.

C3 0.7.0 - One Step Closer To 1.0

The 0.7.0 milestone reached

Originally I was going to write this as normal release notes, but it's a bit too much to go through the whys and wherefores of the changes. So the format of this blog post is going to be a bit different.

0.6.0 was tagged in June 2024, and since then there's been monthly releases up until the last one, 0.6.8. While the language has evolved during this year, each 0.6.x version has been backwards compatible with code written for previous 0.6 versions. Despite this constraint, there have been a lot of improvements, especially in enhancing compile time to have much fewer corners, and of course the standard library has been improved as well.

Well worth mentioning is the impact Tsoding had on the visibility of the language. His "recreational programming" streams on C3 brought a lot of new users to the community, and with it a lot of extremely valuable feedback on both the langage and the standard library.

The release format of C3 allows for breaking changes in every new 0.x release. This means that 0.7.0 and 0.6.8 code will not be compatible. Some of this is already deprecated code in 0.6.8, but for some things there were no clear migration path.

For that reason, I'd like to start by giving an primer on how to convert 0.6.x code to 0.7.0

Migrating to 0.7.0

While these changes may seem like a lot, most of the changes were actually gradually introduced in 0.6.x with the now removed variants are being deprecated.

Syntax changes

  1. Generics now use {} rather than (<>). E.g. List(<int>) x; -> List {int} x;
  2. The keyword def has been replaced by alias, syntax is otherwise unchanged. E.g. def Foo = int; -> alias Foo = int;
  3. The keyword distinct has been replaced by typedef, syntax is otherwise unchanged. E.g. distinct Bar = int; -> typedef Bar = int;
  4. Optional types are now declared with ? instad of !. E.g. int! a; -> int? a;
  5. Declare faults with faultdef: fault MyFault { ABC, FOO_FAULT } -> faultdef ABC, FOO_FAULT;
  6. The anyfault type is now called fault. E.g. anyfault the_err = @catch(foo); -> fault the_err = @catch(foo);
  7. Compound literal syntax Foo { 1, 2 } is no longer valid. Use C-style (Foo) { 1, 2 } instead.
  8. {| |} expression blocks have been removed. Use do { ... }; with break instead.
  9. &ref and $varef have been removed. Use #expr and $vaexpr instead.
  10. $vaarg(1) syntax is now invalid. Use $vaarg[1] instead.
  11. Casting an enum to its ordinal is no longer valid. .ordinal instead.
  12. $or, $and are removed. Use ||| and &&&.
  13. $concat is removed. Use +++ instead.
  14. .allocator = allocator syntax for named arguments in calls is removed. Use the new syntax allocator: allocator instead.
  15. The "any-switch" has been removed. Switch on foo.type then explicitly do the cast instead.
  16. if (catch foo) { case ... } syntax is removed. Use if (catch err = foo) { switch (err) { ... } } instead.
  17. $foreach, $for and $switch no longer uses (). Remove the parentheses and end the statement with :, e.g. $foreach ($x, $y : $foo) -> $foreach $x, $y : $foo:
  18. Remove the int[?] syntax which was tested as an alternative to int[*].
  19. @return! in contracts are changed to @return?
  20. Contracts that have a trailing description, now MUST have a : before the description. E.g. @require a > b "a must be greater than b" -> @require a > b : "a must be greater than b".
  21. Order of attribute declaration is changed for alias: def Foo = int @public; -> alias Foo @public = int;
  22. Definition of new attributes use attrdef as the keyword rather than def.

Stdlib changes

  1. Use tmem rather than allocator::temp().
  2. Use mem rather than allocator::heap().
  3. The new_init and other new_* functions have been removed. Use init(mem), s.concat(mem, s2) etc.
  4. The temp allocator is not implicitly set up on other than the main thread. Use @pool_init() to set up a temporary memory pool on a thread.
  5. Lists and maps no longer allocate on the heap when not initialized. Instead, they use the temp allocator if not initialized.
  6. @pool() no longer requires (or permits) an allocator. The temp allocator supports any number on in-flight temp allocators.

Overall, the new functions, which implicitly used the heap allocator, has gone away and been replaced by a function that explicitly takes the allocator. There are often also temp allocator variants, which are then prefixed with t, e.g. tconcat.

Try to use the temp allocator when possible.

Additions and improvements

There are not only removals and changes, there are also some language additions and improvements in 0.7.0 compared to 0.6.8.

  1. Swizzling assignment is now possible, e.g. foo.xy += { 1, 2 }
  2. A @format attribute has been added to compile time check formatting strings.
  3. Enum associated values can reference the calling enum.
  4. !!foo used to not work, now it properly is interpreted as !(!foo).
  5. Enum inline values were already in 0.6.8, but from 0.7.0 the .ordinal has the same behaviour as associated values. This makes enums more flexible and closer to C enum behaviour.
  6. @wstring, @wstring32, @char32 and @char16 macros were added for interfacing with wide character libraries.

Thoughts on 0.7.0

The breaking changes of 0.7.0 are of two categories: (1) removals (2) syntax changes.

Why remove things?

One might ask why one would remove things from a language? Surely more is better?

The reason is that learning a language this "one more feature", is additional effort to learn the language. It's also one more thing for people knowing the language to remember. In this way, each feature has a cost outside of compiler complexity.

When people encounter a rare feature such as the any-switch, they will need to look up how it works. This both takes time and undermines people's confidence that they know the language.

So during the development of a language it's important to weed out things that aren't carrying its weight, and this is why 0.7 removes features.

Why change syntax?

As an example, int! a; has been used in C3 since the introduction of optionals. Why change it now?

In this case it's because the semantics of int! a; was quite different from an optional when it was introduced. The use of ! was exactly not to confuse it with an optional. Now it's so much closer to the "common" optional that a change to int? a; makes perfect sense.

def -> alias follows a similar trajectory. Initially it would be used both for aliases and to define new attributes and distinct types. Then distinct type definition got its own separate definition, so it was more like an alias aside from the attributes. With attributes getting its own attrdef, what remained were only aliases, and changing the name to alias suddenly made perfect sense.

Both these changes make the language follow more common syntax conventions as well, which is a win.

The goal is for the syntax to feel straightforward and have as little "oh this is odd" as possible. This makes it both easier to learn and remember.

More like C

Even though neither alias nor int? exist in C, they're familiar from adjacent languages. I believe this will make C3 feel "more like C", even though the changes are not in C.

Similarly, removing novel constructs like the expression block reduces the divergence from C syntax.

C3 0.7.1 and onwards

Where C3 0.7.0 has been a big cleanup release, it doesn't have that many improvements to general quality of life aside from the @format attribute.

I am really looking forward to inlining and resolving more function contracts on the caller side. In the first pass it will catch some low hanging fruit such as passing invalid constants, but this can be gradually improved to catch even more bugs.

Another thing is improving the standard library. There are additions I'd like to do both to the collections and to threads.

There are also a number of minor improvements to the compiler that would add to the quality of life for some uses. On top of this the inline asm is in need of bug fixes and cleanup.

Outside of the language and the compiler, a good docgen is needed.

But as for language changes, there is nothing planned, just incremental improvements to what's already there.

Change Log

Here is the full changelist for 0.6.8 to 0.7.0. Obviously if we'd bundle all changes from 0.6.1 to 0.7.0 the list would be MUCH bigger :D

Click for full change log
  • Removed Foo { 1, 2 } initializer.
  • Changed Foo(<int>) to Foo {int}.
  • Removed {| |} expression blocks.
  • Removed macro &ref and $varef parameters.
  • Removed $vaexpr(0) syntax in favour of $vaexpr[0]
  • Enum does not cast to/from an integer (its ordinal).
  • Removed use of void! for main, test and benchmark functions.
  • Removed $or, $and, $concat compile time functions.
  • Removed @adhoc attribute.
  • Disallow inline use of nested generics (e.g. List{List{int}}.
  • Remove .allocator = allocator syntax for functions.
  • Remove @operator(construct).
  • Removal of "any-switch".
  • Allow swizzling assign, eg. abc.xz += { 5, 10 };
  • Added $$wstr16 and $$wstr32 builtins.
  • $foreach "()" replaced by trailing ":" $foreach ($x, $y : $foo) -> $foreach $x, $y : $foo:
  • $for "()" replaced by trailing ":" $for (var $x = 0; $x < FOO; $x++) -> $for var $x = 0; $x < FOO; $x++:
  • $switch "()" replaced by trailing ":" $switch ($Type) -> $switch $Type:
  • Empty $switch requires trailing ":" $switch -> $switch:
  • Rename @return! to @return? and change syntax to require ":" after faults.
  • Remove if (catch foo) { case ... } syntax.
  • Remove [?] syntax.
  • Change int! to int? syntax.
  • New fault declaration using faultdef.
  • Enum associated values can reference the calling enum.
  • Improve error message on foo ?? io::EOF with missing '?' #2036
  • Make @public import recursive. #2018
  • Fault nameof prefixes the first last module path, for instance std::io::EOF is rendered as io::EOF.
  • Rename def to alias.
  • Change distinct -> typedef.
  • Order of attribute declaration is changed for alias.
  • Added LANGUAGE_DEV_VERSION env constant.
  • Rename anyfault -> fault.
  • !!foo now works same as as ! ! foo.
  • Temp allocator now supports more than 2 in-flight stacks.
  • Printing stacktrace uses its own temp allocator.
  • Allow inferred type on body parameters. E.g. @stack_mem(1024; alloc) { ... };
  • Use @pool_init() to set up a temp pool on a thread. Only the main thread has implicit temp pool setup.
  • tmem is now a variable.
  • Compile test and benchmark functions when invoking --lsp #2058.
  • Added @format attribute for compile time printf validation #2057.
  • Formatter no longer implicitly converts enums to ordinals.

Fixes

  • Fix address sanitizer to work on MachO targets (e.g. MacOS).
  • Post and pre-decrement operators switched places for vector elements #2010.
  • Aliases were incorrectly considered compile time constants.
  • FreeBSD libc stat definitions were incorrect.
  • Atomic max was incorrect.
  • "+".to_float() would panic.
  • import can now both be @public and @norecurse.
  • Crash when trying to convert a struct slice to a vector #2039.
  • Crash resolving a method on Foo[2] when Foo is distinct #2042.
  • Bug due to missing cast when doing $i[$x] = $z.
  • Incorrectly allowed getting pointer to a macro #2049.
  • &self not runtime null-checked in macro #1827.
  • Bug when printing a boolean value as an integer using printf.
  • Show error when a generic module contains a self-generic type.
  • "Single module" was not enforced when creating a static library using as a project target.

Stdlib changes

  • new_* functions in general moved to version without new_ prefix.
  • string::new_from_* changed to string::from_*.
  • String.to_utf16_copy and related changed to String.to_utf16.
  • String.to_utf16_tcopy and related changed to String.to_temp_utf16
  • mem::temp_new changed to mem::tnew.
  • mem::temp_alloc and related changed to mem::talloc.
  • mem::temp_new_array changed to mem::temp_array.
  • Add ONHEAP variants for List/HashMap for initializing global maps on the heap.
  • Remove Vec2 and other aliases from std::math. Replace .length_sq() with sq_magnitude()
  • Change all hash functions to have a common hash function.
  • @wstring, @wstring32, @char32 and @char16 compile time macros added.
  • Updates to Atomic to handle distinct types and booleans.
  • Added math::iota.
  • @pool no longer takes an argument.
  • Allocator interface removes mark and reset.
  • DynamicArenaAllocator has changed init function.
  • Added BackedArenaAllocator which is allocated to a fixed size, then allocates on the backing allocator and supports mark/reset.
  • AnyList now also defaults to the temp allocator.
  • os::getcwd and os::get_home_dir requires an explicit allocator.
  • file::load_new and file::load_path_new removed.
  • os::exit and os::fastexit added.

Want To Dive Into C3?

Check out the documentation or download it and try it out.

Have questions? Come and chat with us on Discord.

C3 0.7 released - one step closer to 1.0

Originally from: https://c3.handmade.network/blog/p/9010-c3_0.7_released_-_one_step_closer_to_1.0

The 0.7.0 milestone reached

Originally I was going to write this as normal release notes, but it's a bit too much to go through the whys and wherefores of the changes. So the format of this blog post is going to be a bit different.

0.6.0 was tagged in June 2024, and since then there's been monthly releases up until the last one, 0.6.8. While the language has evolved during this year, each 0.6.x version has been backwards compatible with code written for previous 0.6 versions. Despite this constraint, there have been a lot of improvements, especially in enhancing compile time to have much fewer corners, and of course the standard library has been improved as well.

Well worth mentioning is the impact Tsoding (https://www.twitch.tv/tsoding) had on the visibility of the language. His "recreational programming" streams on C3 brought a lot of new users to the community, and with it a lot of extremely valuable feedback on both the langage and the standard library.

The release format of C3 allows for breaking changes in every 0.x release. This means that 0.7.0 and 0.6.8 code will not be compatible. Some of this is already deprecated code in 0.6.8, but for some things there were no clear migration path.

For that reason, I'd like to start by giving an primer on how to convert 0.6.x code to 0.7.0

Migrating to 0.7.0

(While these changes may seem like a lot, most of the changes were actually gradually introduced in 0.6.x with the now removed variants being deprecated)

Syntax changes

  1. Generics now use {} rather than (<>). E.g. List(<int>) x; -> List {int} x;
  2. The keyword def has been replaced by alias, syntax is otherwise unchanged. E.g. def Foo = int; -> alias Foo = int;
  3. The keyword distinct has been replaced by typedef, syntax is otherwise unchanged. E.g. distinct Bar = int; -> typedef Bar = int;
  4. Optional types are now declared with ? instad of !. E.g. int! a; -> int? a;
  5. Declare faults with faultdef: fault MyFault { ABC, FOO_FAULT } -> faultdef ABC, FOO_FAULT;
  6. The anyfault type is now called fault. E.g. anyfault the_err = @catch(foo); -> fault the_err = @catch(foo);
  7. Compound literal syntax Foo { 1, 2 } is no longer valid. Use C-style (Foo) { 1, 2 } instead.
  8. {| |} expression blocks have been removed. Use do { ... }; with break instead.
  9. &ref and $varef have been removed. Use #expr and $vaexpr instead.
  10. $vaarg(1) syntax is now invalid. Use $vaarg[1] instead.
  11. Casting an enum to its ordinal is no longer valid. .ordinal instead.
  12. $or, $and are removed. Use ||| and &&&.
  13. $concat is removed. Use +++ instead.
  14. .allocator = allocator syntax for named arguments in calls is removed. Use the new syntax allocator: allocator instead.
  15. The "any-switch" has been removed. Switch on foo.type then explicitly do the cast instead.
  16. if (catch foo) { case ... } syntax is removed. Use if (catch err = foo) { switch (err) { ... } } instead.
  17. $foreach, $for and $switch no longer uses (). Remove the parentheses and end the statement with :, e.g. $foreach ($x, $y : $foo) -> $foreach $x, $y : $foo:
  18. Remove the int[?] syntax which was tested as an alternative to int[*].
  19. @return! in contracts are changed to @return?
  20. Contracts that have a trailing description, now MUST have a : before the description. E.g. @require a > b "a must be greater than b" -> @require a > b : "a must be greater than b".
  21. Order of attribute declaration is changed for alias: def Foo = int @public; -> alias Foo @public = int;
  22. Definition of new attributes use attrdef as the keyword rather than def.

Stdlib changes

  1. Use tmem rather than allocator::temp().
  2. Use mem rather than allocator::heap().
  3. The new_init and other new_* functions have been removed. Use init(mem), s.concat(mem, s2) etc.
  4. The temp allocator is not implicitly set up on other than the main thread. Use @pool_init() to set up a temporary memory pool on a thread.
  5. Lists and maps no longer allocate on the heap when not initialized. Instead, they use the temp allocator if not initialized.
  6. @pool() no longer requires (or permits) an allocator. The temp allocator supports any number on in-flight temp allocators.

Overall, the new functions, which implicitly used the heap allocator, has gone away and been replaced by a function that explicitly takes the allocator. There are often also temp allocator variants, which are then prefixed with t, e.g. tconcat.

Try to use the temp allocator when possible.

Additions and improvements

There are not only removals and changes, there are also some language additions and improvements in 0.7.0 compared to 0.6.8.

  1. Swizzling assignment is now possible, e.g. foo.xy += { 1, 2 }
  2. A @format attribute has been added to compile time check formatting strings.
  3. Enum associated values can reference the calling enum.
  4. !!foo used to not work, now it properly is interpreted as !(!foo).
  5. Enum inline values were already in 0.6.8, but from 0.7.0 the .ordinal has the same behaviour as associated values. This makes enums more flexible and closer to C enum behaviour.
  6. @wstring, @wstring32, @char32 and @char16 macros were added for interfacing with wide character libraries.

Thoughts on 0.7.0

The breaking changes of 0.7.0 are of two categories: (1) removals (2) syntax changes.

Why remove things?

One might ask why one would remove things from a language? Surely more is better?

The reason is that learning a language this "one more feature", is additional effort to learn the language. It's also one more thing for people knowing the language to remember. In this way, each feature has a cost outside of compiler complexity.

When people encounter a rare feature such as the any-switch, they will need to look up how it works. This both takes time and undermines people's confidence that they know the language.

So during the development of a language it's important to weed out things that aren't carrying its weight, and this is why 0.7 removes features.

Why change syntax?

As an example, int! a; has been used in C3 since the introduction of optionals. Why change it now?

In this case it's because the semantics of int! a; was quite different from an optional when it was introduced. The use of ! was exactly not to confuse it with an optional. Now it's so much closer to the "common" optional that a change to int? a; makes perfect sense.

def -> alias follows a similar trajectory. Initially it would be used both for aliases and to define new attributes and distinct types. Then distinct type definition got its own separate definition, so it was more like an alias aside from the attributes. With attributes getting its own attrdef, what remained were only aliases, and changing the name to alias suddenly made perfect sense.

Both these changes make the language follow more common syntax conventions as well, which is a win.

The goal is for the syntax to feel straightforward and have as little "oh this is odd" as possible. This makes it both easier to learn and remember.

More like C

Even though neither alias nor int? exist in C, they're familiar from adjacent languages. I believe this will make C3 feel "more like C", even though the changes are not in C.

Similarly, removing novel constructs like the expression block reduces the divergence from C syntax.

C3 0.7.1 and onwards

Where C3 0.7.0 has been a big cleanup release, it doesn't have that many improvements to general quality of life aside from the @format attribute.

I am really looking forward to inlining and resolving more function contracts on the caller side. In the first pass it will catch some low hanging fruit such as passing invalid constants, but this can be gradually improved to catch even more bugs.

Another thing is improving the standard library. There are additions I'd like to do both to the collections and to threads.

There are also a number of minor improvements to the compiler that would add to the quality of life for some uses. On top of this the inline asm is in need of bug fixes and cleanup.

Outside of the language and the compiler, a good docgen is needed.

But as for language changes, there is nothing planned, just incremental improvements to what's already there.

Full changelist for 0.7.0

Here is the full changelist for 0.6.8 to 0.7.0. Obviously if we'd bundle all changes from 0.6.1 to 0.7.0 the list would be MUCH bigger :D

Changes / improvements

  • Removed Foo { 1, 2 } initializer.
  • Changed Foo(<int>) to Foo {int}.
  • Removed {| |} expression blocks.
  • Removed macro &ref and $varef parameters.
  • Removed $vaexpr(0) syntax in favour of $vaexpr[0]
  • Enum does not cast to/from an integer (its ordinal).
  • Removed use of void! for main, test and benchmark functions.
  • Removed $or, $and, $concat compile time functions.
  • Removed @adhoc attribute.
  • Disallow inline use of nested generics (e.g. List{List{int}}.
  • Remove .allocator = allocator syntax for functions.
  • Remove @operator(construct).
  • Removal of "any-switch".
  • Allow swizzling assign, eg. abc.xz += { 5, 10 };
  • Added $$wstr16 and $$wstr32 builtins.
  • $foreach "()" replaced by trailing ":" $foreach ($x, $y : $foo) -> $foreach $x, $y : $foo:
  • $for "()" replaced by trailing ":" $for (var $x = 0; $x < FOO; $x++) -> $for var $x = 0; $x < FOO; $x++:
  • $switch "()" replaced by trailing ":" $switch ($Type) -> $switch $Type:
  • Empty $switch requires trailing ":" $switch -> $switch:
  • Rename @return! to @return? and change syntax to require ":" after faults.
  • Remove if (catch foo) { case ... } syntax.
  • Remove [?] syntax.
  • Change int! to int? syntax.
  • New fault declaration using faultdef.
  • Enum associated values can reference the calling enum.
  • Improve error message on foo ?? io::EOF with missing '?' #2036
  • Make @public import recursive. #2018
  • Fault nameof prefixes the first last module path, for instance std::io::EOF is rendered as io::EOF.
  • Rename def to alias.
  • Change distinct -> typedef.
  • Order of attribute declaration is changed for alias.
  • Added LANGUAGE_DEV_VERSION env constant.
  • Rename anyfault -> fault.
  • !!foo now works same as as ! ! foo.
  • Temp allocator now supports more than 2 in-flight stacks.
  • Printing stacktrace uses its own temp allocator.
  • Allow inferred type on body parameters. E.g. @stack_mem(1024; alloc) { ... };
  • Use @pool_init() to set up a temp pool on a thread. Only the main thread has implicit temp pool setup.
  • tmem is now a variable.
  • Compile test and benchmark functions when invoking --lsp #2058.
  • Added @format attribute for compile time printf validation #2057.
  • Formatter no longer implicitly converts enums to ordinals.

Fixes

  • Fix address sanitizer to work on MachO targets (e.g. MacOS).
  • Post and pre-decrement operators switched places for vector elements #2010.
  • Aliases were incorrectly considered compile time constants.
  • FreeBSD libc stat definitions were incorrect.
  • Atomic max was incorrect.
  • "+".to_float() would panic.
  • import can now both be @public and @norecurse.
  • Crash when trying to convert a struct slice to a vector #2039.
  • Crash resolving a method on Foo[2] when Foo is distinct #2042.
  • Bug due to missing cast when doing $i[$x] = $z.
  • Incorrectly allowed getting pointer to a macro #2049.
  • &self not runtime null-checked in macro #1827.
  • Bug when printing a boolean value as an integer using printf.
  • Show error when a generic module contains a self-generic type.
  • "Single module" was not enforced when creating a static library using as a project target.

Stdlib changes

  • new_* functions in general moved to version without new_ prefix.
  • string::new_from_* changed to string::from_*.
  • String.to_utf16_copy and related changed to String.to_utf16.
  • String.to_utf16_tcopy and related changed to String.to_temp_utf16
  • mem::temp_new changed to mem::tnew.
  • mem::temp_alloc and related changed to mem::talloc.
  • mem::temp_new_array changed to mem::temp_array.
  • Add ONHEAP variants for List/HashMap for initializing global maps on the heap.
  • Remove Vec2 and other aliases from std::math. Replace .length_sq() with sq_magnitude()
  • Change all hash functions to have a common hash function.
  • @wstring, @wstring32, @char32 and @char16 compile time macros added.
  • Updates to Atomic to handle distinct types and booleans.
  • Added math::iota.
  • @pool no longer takes an argument.
  • Allocator interface removes mark and reset.
  • DynamicArenaAllocator has changed init function.
  • Added BackedArenaAllocator which is allocated to a fixed size, then allocates on the backing allocator and supports mark/reset.
  • AnyList now also defaults to the temp allocator.
  • os::getcwd and os::get_home_dir requires an explicit allocator.
  • file::load_new and file::load_path_new removed.
  • os::exit and os::fastexit added.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

Comments


Comment by Christoffer Lernö

The 0.7.0 milestone reached

Originally I was going to write this as normal release notes, but it's a bit too much to go through the whys and wherefores of the changes. So the format of this blog post is going to be a bit different.

0.6.0 was tagged in June 2024, and since then there's been monthly releases up until the last one, 0.6.8. While the language has evolved during this year, each 0.6.x version has been backwards compatible with code written for previous 0.6 versions. Despite this constraint, there have been a lot of improvements, especially in enhancing compile time to have much fewer corners, and of course the standard library has been improved as well.

Well worth mentioning is the impact Tsoding (https://www.twitch.tv/tsoding) had on the visibility of the language. His "recreational programming" streams on C3 brought a lot of new users to the community, and with it a lot of extremely valuable feedback on both the langage and the standard library.

The release format of C3 allows for breaking changes in every 0.x release. This means that 0.7.0 and 0.6.8 code will not be compatible. Some of this is already deprecated code in 0.6.8, but for some things there were no clear migration path.

For that reason, I'd like to start by giving an primer on how to convert 0.6.x code to 0.7.0

Migrating to 0.7.0

(While these changes may seem like a lot, most of the changes were actually gradually introduced in 0.6.x with the now removed variants being deprecated)

Syntax changes

  1. Generics now use {} rather than (<>). E.g. List(<int>) x; -> List {int} x;
  2. The keyword def has been replaced by alias, syntax is otherwise unchanged. E.g. def Foo = int; -> alias Foo = int;
  3. The keyword distinct has been replaced by typedef, syntax is otherwise unchanged. E.g. distinct Bar = int; -> typedef Bar = int;
  4. Optional types are now declared with ? instad of !. E.g. int! a; -> int? a;
  5. Declare faults with faultdef: fault MyFault { ABC, FOO_FAULT } -> faultdef ABC, FOO_FAULT;
  6. The anyfault type is now called fault. E.g. anyfault the_err = @catch(foo); -> fault the_err = @catch(foo);
  7. Compound literal syntax Foo { 1, 2 } is no longer valid. Use C-style (Foo) { 1, 2 } instead.
  8. {| |} expression blocks have been removed. Use do { ... }; with break instead.
  9. &ref and $varef have been removed. Use #expr and $vaexpr instead.
  10. $vaarg(1) syntax is now invalid. Use $vaarg[1] instead.
  11. Casting an enum to its ordinal is no longer valid. .ordinal instead.
  12. $or, $and are removed. Use ||| and &&&.
  13. $concat is removed. Use +++ instead.
  14. .allocator = allocator syntax for named arguments in calls is removed. Use the new syntax allocator: allocator instead.
  15. The "any-switch" has been removed. Switch on foo.type then explicitly do the cast instead.
  16. if (catch foo) { case ... } syntax is removed. Use if (catch err = foo) { switch (err) { ... } } instead.
  17. $foreach, $for and $switch no longer uses (). Remove the parentheses and end the statement with :, e.g. $foreach ($x, $y : $foo) -> $foreach $x, $y : $foo:
  18. Remove the int[?] syntax which was tested as an alternative to int[*].
  19. @return! in contracts are changed to @return?
  20. Contracts that have a trailing description, now MUST have a : before the description. E.g. @require a > b "a must be greater than b" -> @require a > b : "a must be greater than b".
  21. Order of attribute declaration is changed for alias: def Foo = int @public; -> alias Foo @public = int;
  22. Definition of new attributes use attrdef as the keyword rather than def.

Stdlib changes

  1. Use tmem rather than allocator::temp().
  2. Use mem rather than allocator::heap().
  3. The new_init and other new_* functions have been removed. Use init(mem), s.concat(mem, s2) etc.
  4. The temp allocator is not implicitly set up on other than the main thread. Use @pool_init() to set up a temporary memory pool on a thread.
  5. Lists and maps no longer allocate on the heap when not initialized. Instead, they use the temp allocator if not initialized.
  6. @pool() no longer requires (or permits) an allocator. The temp allocator supports any number on in-flight temp allocators.

Overall, the new functions, which implicitly used the heap allocator, has gone away and been replaced by a function that explicitly takes the allocator. There are often also temp allocator variants, which are then prefixed with t, e.g. tconcat.

Try to use the temp allocator when possible.

Additions and improvements

There are not only removals and changes, there are also some language additions and improvements in 0.7.0 compared to 0.6.8.

  1. Swizzling assignment is now possible, e.g. foo.xy += { 1, 2 }
  2. A @format attribute has been added to compile time check formatting strings.
  3. Enum associated values can reference the calling enum.
  4. !!foo used to not work, now it properly is interpreted as !(!foo).
  5. Enum inline values were already in 0.6.8, but from 0.7.0 the .ordinal has the same behaviour as associated values. This makes enums more flexible and closer to C enum behaviour.
  6. @wstring, @wstring32, @char32 and @char16 macros were added for interfacing with wide character libraries.

Thoughts on 0.7.0

The breaking changes of 0.7.0 are of two categories: (1) removals (2) syntax changes.

Why remove things?

One might ask why one would remove things from a language? Surely more is better?

The reason is that learning a language this "one more feature", is additional effort to learn the language. It's also one more thing for people knowing the language to remember. In this way, each feature has a cost outside of compiler complexity.

When people encounter a rare feature such as the any-switch, they will need to look up how it works. This both takes time and undermines people's confidence that they know the language.

So during the development of a language it's important to weed out things that aren't carrying its weight, and this is why 0.7 removes features.

Why change syntax?

As an example, int! a; has been used in C3 since the introduction of optionals. Why change it now?

In this case it's because the semantics of int! a; was quite different from an optional when it was introduced. The use of ! was exactly not to confuse it with an optional. Now it's so much closer to the "common" optional that a change to int? a; makes perfect sense.

def -> alias follows a similar trajectory. Initially it would be used both for aliases and to define new attributes and distinct types. Then distinct type definition got its own separate definition, so it was more like an alias aside from the attributes. With attributes getting its own attrdef, what remained were only aliases, and changing the name to alias suddenly made perfect sense.

Both these changes make the language follow more common syntax conventions as well, which is a win.

The goal is for the syntax to feel straightforward and have as little "oh this is odd" as possible. This makes it both easier to learn and remember.

More like C

Even though neither alias nor int? exist in C, they're familiar from adjacent languages. I believe this will make C3 feel "more like C", even though the changes are not in C.

Similarly, removing novel constructs like the expression block reduces the divergence from C syntax.

C3 0.7.1 and onwards

Where C3 0.7.0 has been a big cleanup release, it doesn't have that many improvements to general quality of life aside from the @format attribute.

I am really looking forward to inlining and resolving more function contracts on the caller side. In the first pass it will catch some low hanging fruit such as passing invalid constants, but this can be gradually improved to catch even more bugs.

Another thing is improving the standard library. There are additions I'd like to do both to the collections and to threads.

There are also a number of minor improvements to the compiler that would add to the quality of life for some uses. On top of this the inline asm is in need of bug fixes and cleanup.

Outside of the language and the compiler, a good docgen is needed.

But as for language changes, there is nothing planned, just incremental improvements to what's already there.

Full changelist for 0.7.0

Here is the full changelist for 0.6.8 to 0.7.0. Obviously if we'd bundle all changes from 0.6.1 to 0.7.0 the list would be MUCH bigger :D

Changes / improvements

  • Removed Foo { 1, 2 } initializer.
  • Changed Foo(<int>) to Foo {int}.
  • Removed {| |} expression blocks.
  • Removed macro &ref and $varef parameters.
  • Removed $vaexpr(0) syntax in favour of $vaexpr[0]
  • Enum does not cast to/from an integer (its ordinal).
  • Removed use of void! for main, test and benchmark functions.
  • Removed $or, $and, $concat compile time functions.
  • Removed @adhoc attribute.
  • Disallow inline use of nested generics (e.g. List{List{int}}.
  • Remove .allocator = allocator syntax for functions.
  • Remove @operator(construct).
  • Removal of "any-switch".
  • Allow swizzling assign, eg. abc.xz += { 5, 10 };
  • Added $$wstr16 and $$wstr32 builtins.
  • $foreach "()" replaced by trailing ":" $foreach ($x, $y : $foo) -> $foreach $x, $y : $foo:
  • $for "()" replaced by trailing ":" $for (var $x = 0; $x < FOO; $x++) -> $for var $x = 0; $x < FOO; $x++:
  • $switch "()" replaced by trailing ":" $switch ($Type) -> $switch $Type:
  • Empty $switch requires trailing ":" $switch -> $switch:
  • Rename @return! to @return? and change syntax to require ":" after faults.
  • Remove if (catch foo) { case ... } syntax.
  • Remove [?] syntax.
  • Change int! to int? syntax.
  • New fault declaration using faultdef.
  • Enum associated values can reference the calling enum.
  • Improve error message on foo ?? io::EOF with missing '?' #2036
  • Make @public import recursive. #2018
  • Fault nameof prefixes the first last module path, for instance std::io::EOF is rendered as io::EOF.
  • Rename def to alias.
  • Change distinct -> typedef.
  • Order of attribute declaration is changed for alias.
  • Added LANGUAGE_DEV_VERSION env constant.
  • Rename anyfault -> fault.
  • !!foo now works same as as ! ! foo.
  • Temp allocator now supports more than 2 in-flight stacks.
  • Printing stacktrace uses its own temp allocator.
  • Allow inferred type on body parameters. E.g. @stack_mem(1024; alloc) { ... };
  • Use @pool_init() to set up a temp pool on a thread. Only the main thread has implicit temp pool setup.
  • tmem is now a variable.
  • Compile test and benchmark functions when invoking --lsp #2058.
  • Added @format attribute for compile time printf validation #2057.
  • Formatter no longer implicitly converts enums to ordinals.

Fixes

  • Fix address sanitizer to work on MachO targets (e.g. MacOS).
  • Post and pre-decrement operators switched places for vector elements #2010.
  • Aliases were incorrectly considered compile time constants.
  • FreeBSD libc stat definitions were incorrect.
  • Atomic max was incorrect.
  • "+".to_float() would panic.
  • import can now both be @public and @norecurse.
  • Crash when trying to convert a struct slice to a vector #2039.
  • Crash resolving a method on Foo[2] when Foo is distinct #2042.
  • Bug due to missing cast when doing $i[$x] = $z.
  • Incorrectly allowed getting pointer to a macro #2049.
  • &self not runtime null-checked in macro #1827.
  • Bug when printing a boolean value as an integer using printf.
  • Show error when a generic module contains a self-generic type.
  • "Single module" was not enforced when creating a static library using as a project target.

Stdlib changes

  • new_* functions in general moved to version without new_ prefix.
  • string::new_from_* changed to string::from_*.
  • String.to_utf16_copy and related changed to String.to_utf16.
  • String.to_utf16_tcopy and related changed to String.to_temp_utf16
  • mem::temp_new changed to mem::tnew.
  • mem::temp_alloc and related changed to mem::talloc.
  • mem::temp_new_array changed to mem::temp_array.
  • Add ONHEAP variants for List/HashMap for initializing global maps on the heap.
  • Remove Vec2 and other aliases from std::math. Replace .length_sq() with sq_magnitude()
  • Change all hash functions to have a common hash function.
  • @wstring, @wstring32, @char32 and @char16 compile time macros added.
  • Updates to Atomic to handle distinct types and booleans.
  • Added math::iota.
  • @pool no longer takes an argument.
  • Allocator interface removes mark and reset.
  • DynamicArenaAllocator has changed init function.
  • Added BackedArenaAllocator which is allocated to a fixed size, then allocates on the backing allocator and supports mark/reset.
  • AnyList now also defaults to the temp allocator.
  • os::getcwd and os::get_home_dir requires an explicit allocator.
  • file::load_new and file::load_path_new removed.
  • os::exit and os::fastexit added.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

Some tips organizing code in C3

Originally from: https://c3.handmade.network/blog/p/9008-some_tips_organizing_code_in_c3

I occasionally get requests for aliasing modules in C3, so something like import foo::mylongname = ml;. Often the use case is to get a shorter prefix for their types, as they're doing foo::mylongname::MyType everywhere.

This is a fundamental misunderstanding of how you are supposed to organize code in C3.

The reason for mandatory module prefixing of functions and globals

In C, we typically have some name spacing scheme in place. For example one might have settings_parse_options(...) settings_print_help(...), settings_apply_options(...) and so on.

What C3 tries to do is to formalize this informal style using the last component of the module path:

import app::settings;

fn void something(String[] args)
{
    SettingOptions? options = settings::parse_options(args);
    if (catch options)
    {
        settings::print_help();
        os::exit(1);
    }
    ...
}

How to pick a good module name

This scheme puts a lot of responsibility on the author of the module to pick a submodule name that actually is succinct and nice enough to use repeatedly. The submodule is not just for organizing code, but to be part of the identifier.

In a language where imports are typically aliased, the submodule name is fairly insignificant, but in C3 it should give you all the necessary context without being overly verbose. This is also why the C3 standard library generally goes for shorter names and the occasional abbreviation.

So to repeat: it is bad practice in C3 to use long submodule names. However, the top module should ensure that the name is unique.

module com.foo.mylib.io;
       ------------- --
       ^ unique part  ^ prefix to keep succinct

Good type names

Type names does not require a prefix, because they are intended to keep the initial prefix as part of the name, e.g. SettingOptions, HttpContext, IntList etc. It is bad practice to add the submodule. So settings::Options and http::Context are both bad in the context of C3.

So a good C3 type name should be clearly distinguishable all others. So no http::Context vs parsing::Context and so on, it's just bad practice. And again this violates how you'd typically write it in C: http::Context is http_Context, which few will use.

Organizing your own code

In C3, prefer making modules as big as possible inside of your app. There is no intrinsic gain in creating module hierarchies. You can use submodules to delineate subsystems in your app, but that should be done as need arises, not up front.

This is also why types should be unique: if you rely on submodule names to disambiguate them, then you're forced to create submodules you do not need.

Summary

Coming from other languages with other conventions, it's natural to not find best practices in a new language, but try to stick with these rules and you'll probably feel a lot less need for module name aliasing.

Let's summarize the above in a few do/don'ts

Don't

Create module names that people will want to alias:

module com::mysite::thecanvasrenderinglib;

fn Canvas? create_a_new_rendering_canvas()
{ ... }

module foo;
import com::mysite::thecanvasrenderinglib;

fn void test()
{
    Canvas? c = thecanvasrenderinglib::create_a_new_rendering_canvas();
    ...
}
Do

Create submodule names that are succinct and work with the function name:

module com::mysite::thecanvasrenderinglib::canvas;

fn Canvas? create_new_for_rendering()
{ ... }

module foo;
import com::mysite::thecanvasrenderinglib;

fn void test()
{
    Canvas? c = canvas::create_new_for_rendering();
    ...
}
Don't

Use type names that are ambiguous:

module abc::http;

struct Context { ... }

module abc::file;

struct Context { ... }

module foo;
import abc;

fn void main()
{
  http::Context hc = http::get_context();
  file::Context fc = hc.get_file_context();  
  ...
}
Do

Use type names that are distinct and clear what they're used for:

module abc::http;

struct HttpContext { ... }

module abc::file;

struct FileContext { ... }

module foo;
import abc;

fn void main()
{
  HttpContext hc = http::get_context();
  FileContext fc = hc.get_file_context();  
  ...
}
Don't

Add unnecessary module paths.

import std::io;

fn void main()
{
    std::io::File f = std::io::file::open("foo.txt", "rb");
    char[]? data = std::io::read_fully(mem, &f);
    ...
}
Do

Use as short module paths as permitted:

import std::io;

fn void main()
{
    File f = file::open("foo.txt", "rb");
    char[]? data = io::read_fully(mem, &f);
    ...
}

And that's it. Happy coding!

Comments


Comment by Christoffer Lernö

I occasionally get requests for aliasing modules in C3, so something like import foo::mylongname = ml;. Often the use case is to get a shorter prefix for their types, as they're doing foo::mylongname::MyType everywhere.

This is a fundamental misunderstanding of how you are supposed to organize code in C3.

The reason for mandatory module prefixing of functions and globals

In C, we typically have some name spacing scheme in place. For example one might have settings_parse_options(...) settings_print_help(...), settings_apply_options(...) and so on.

What C3 tries to do is to formalize this informal style using the last component of the module path:

import app::settings;

fn void something(String[] args)
{
    SettingOptions? options = settings::parse_options(args);
    if (catch options)
    {
        settings::print_help();
        os::exit(1);
    }
    ...
}

How to pick a good module name

This scheme puts a lot of responsibility on the author of the module to pick a submodule name that actually is succinct and nice enough to use repeatedly. The submodule is not just for organizing code, but to be part of the identifier.

In a language where imports are typically aliased, the submodule name is fairly insignificant, but in C3 it should give you all the necessary context without being overly verbose. This is also why the C3 standard library generally goes for shorter names and the occasional abbreviation.

So to repeat: it is bad practice in C3 to use long submodule names. However, the top module should ensure that the name is unique.

module com.foo.mylib.io;
       ------------- --
       ^ unique part  ^ prefix to keep succinct

Good type names

Type names does not require a prefix, because they are intended to keep the initial prefix as part of the name, e.g. SettingOptions, HttpContext, IntList etc. It is bad practice to add the submodule. So settings::Options and http::Context are both bad in the context of C3.

So a good C3 type name should be clearly distinguishable all others. So no http::Context vs parsing::Context and so on, it's just bad practice. And again this violates how you'd typically write it in C: http::Context is http_Context, which few will use.

Organizing your own code

In C3, prefer making modules as big as possible inside of your app. There is no intrinsic gain in creating module hierarchies. You can use submodules to delineate subsystems in your app, but that should be done as need arises, not up front.

This is also why types should be unique: if you rely on submodule names to disambiguate them, then you're forced to create submodules you do not need.

Summary

Coming from other languages with other conventions, it's natural to not find best practices in a new language, but try to stick with these rules and you'll probably feel a lot less need for module name aliasing.

Let's summarize the above in a few do/don'ts

Don't

Create module names that people will want to alias:

module com::mysite::thecanvasrenderinglib;

fn Canvas? create_a_new_rendering_canvas()
{ ... }

module foo;
import com::mysite::thecanvasrenderinglib;

fn void test()
{
    Canvas? c = thecanvasrenderinglib::create_a_new_rendering_canvas();
    ...
}
Do

Create submodule names that are succinct and work with the function name:

module com::mysite::thecanvasrenderinglib::canvas;

fn Canvas? create_new_for_rendering()
{ ... }

module foo;
import com::mysite::thecanvasrenderinglib;

fn void test()
{
    Canvas? c = canvas::create_new_for_rendering();
    ...
}
Don't

Use type names that are ambiguous:

module abc::http;

struct Context { ... }

module abc::file;

struct Context { ... }

module foo;
import abc;

fn void main()
{
  http::Context hc = http::get_context();
  file::Context fc = hc.get_file_context();  
  ...
}
Do

Use type names that are distinct and clear what they're used for:

module abc::http;

struct HttpContext { ... }

module abc::file;

struct FileContext { ... }

module foo;
import abc;

fn void main()
{
  HttpContext hc = http::get_context();
  FileContext fc = hc.get_file_context();  
  ...
}
Don't

Add unnecessary module paths.

import std::io;

fn void main()
{
    std::io::File f = std::io::file::open("foo.txt", "rb");
    char[]? data = std::io::read_fully(mem, &f);
    ...
}
Do

Use as short module paths as permitted:

import std::io;

fn void main()
{
    File f = file::open("foo.txt", "rb");
    char[]? data = io::read_fully(mem, &f);
    ...
}

And that's it. Happy coding!

An early 0.6.8 release

Originally from: https://c3.handmade.network/blog/p/8996-an_early_0.6.8_release

Release 0.6.8

0.6.8 is to be the last release before 0.7.0 and had a short cycle of development - only two weeks. As usual it has fixes, but more importantly, it prepares for 0.7.0.

So let's dive into what's new in this release!

"New generics" - List{int}

0.6.8 has the flag --enable-new-generics, which implements the new syntax for generics, from, for example, List(<int>) a; to List{int} a;. With this flag the compound literal syntax of Foo { 1, 2 } is disallowed, instead it uses C-style (Foo) { 1, 2 }.

Expression blocks and @operator(construct) deprecated

The expression blocks {| |} helps you turn any sequence of statements into an expression. This would be immensely useful in C-like macros, but with semantic macros in C3 it's much less so, to the point that it's being removed in 0.7.0. The feature isn't bad, it just hasn't been useful enough to keep it.

The @operator(construct) which allowed static method-like invocations on types is deprecated. It was introduced in 0.6.6 as an experiment, but will ultimately be removed in the next version.

Changes to the stdlib

After a lot of testing during the last year, we're retiring the new_* and temp_ functions. The old pattern of foo.new_init becomes foo.init(mem) with "mem" being the default heap allocator, and initializers with temp allocators usin foo.tinit().

Tooling changes

The compiler now places temporaries in .build by default when using compile. There is a --suppress-run for benchmark and test targets to not run them immediately. There is also a --build-env option to get some information about the build environment.

What's next:

The release of 0.7.0 is planned for early April, with the main feature being the new generics and and standard library "standardization" aroud "init(mem)". The other big thing is of course the removal of deprecated functionality.

There are some long standing issues, like completing the asm functionality and resolving some of the semantics around #hash arguments that hopefully will be fixed in 0.7.0 as well.

Even though focus is on 0.7.0, a 0.6.9 version might appear if there is an urgent need for bug fixes in 0.6.x.

Here is the full change list:

0.6.8 Change list

Changes / improvements

  • Increase precedence of (Foo) { 1, 2 }
  • Add --enable-new-generics to enable Foo{int} generic syntax.
  • {| |} expression blocks deprecated.
  • c3c --test-leak-report flag for displaying full memory lead report if any
  • Output into /.build/obj/ by default.
  • Output llvm/asm into llvm/ and asm/ by default.
  • Add flag --suppress-run. For commands which may run executable after building, skip the run step. #1931
  • Add --build-env for build environment information.
  • Deprecation of @operator(construct).

Fixes

  • Bug appearing when ?? was combined with boolean in some cases.
  • Test runner --test-disable-sort didn't work, c3c was expecting --test-nosort
  • Test runner with tracking allocator assertion at failed test #1963
  • Test runner with tracking allocator didn't properly handle teardown_fn
  • Correctly give an error if a character literal contains a line break.
  • Implicitly unwrapped optional value in defer incorrectly copied #1982.
  • Crash when trying to define a method macro that isn't @construct but has no arguments.
  • Regression, .gitkeep files were generated incorrectly.
  • Aliases are now correctly handled as if they were variables/functions in regards to namespacing and accept @builtin.
  • Correctly handle in/out when interacting with inout.
  • Don't delete .o files not produced by the compiler.
  • Fix optional jumps in expression lists, #1942.
  • Several fixes for .o files and -o output, improving handling and naming.
  • Fix bug casting bool to int to other int #1995.
  • @if declarations were missing from -P output #1973.
  • Check exe and lib output so -o works with directories.
  • Swizzling an inline vector in a struct would cause a crash.
  • Fixed error and poor error message when using an invalid target name.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

Comments


Comment by Christoffer Lernö

Release 0.6.8

0.6.8 is to be the last release before 0.7.0 and had a short cycle of development - only two weeks. As usual it has fixes, but more importantly, it prepares for 0.7.0.

So let's dive into what's new in this release!

"New generics" - List{int}

0.6.8 has the flag --enable-new-generics, which implements the new syntax for generics, from, for example, List(<int>) a; to List{int} a;. With this flag the compound literal syntax of Foo { 1, 2 } is disallowed, instead it uses C-style (Foo) { 1, 2 }.

Expression blocks and @operator(construct) deprecated

The expression blocks {| |} helps you turn any sequence of statements into an expression. This would be immensely useful in C-like macros, but with semantic macros in C3 it's much less so, to the point that it's being removed in 0.7.0. The feature isn't bad, it just hasn't been useful enough to keep it.

The @operator(construct) which allowed static method-like invocations on types is deprecated. It was introduced in 0.6.6 as an experiment, but will ultimately be removed in the next version.

Changes to the stdlib

After a lot of testing during the last year, we're retiring the new_* and temp_ functions. The old pattern of foo.new_init becomes foo.init(mem) with "mem" being the default heap allocator, and initializers with temp allocators usin foo.tinit().

Tooling changes

The compiler now places temporaries in .build by default when using compile. There is a --suppress-run for benchmark and test targets to not run them immediately. There is also a --build-env option to get some information about the build environment.

What's next:

The release of 0.7.0 is planned for early April, with the main feature being the new generics and and standard library "standardization" aroud "init(mem)". The other big thing is of course the removal of deprecated functionality.

There are some long standing issues, like completing the asm functionality and resolving some of the semantics around #hash arguments that hopefully will be fixed in 0.7.0 as well.

Even though focus is on 0.7.0, a 0.6.9 version might appear if there is an urgent need for bug fixes in 0.6.x.

Here is the full change list:

0.6.8 Change list

Changes / improvements

  • Increase precedence of (Foo) { 1, 2 }
  • Add --enable-new-generics to enable Foo{int} generic syntax.
  • {| |} expression blocks deprecated.
  • c3c --test-leak-report flag for displaying full memory lead report if any
  • Output into /.build/obj/ by default.
  • Output llvm/asm into llvm/ and asm/ by default.
  • Add flag --suppress-run. For commands which may run executable after building, skip the run step. #1931
  • Add --build-env for build environment information.
  • Deprecation of @operator(construct).

Fixes

  • Bug appearing when ?? was combined with boolean in some cases.
  • Test runner --test-disable-sort didn't work, c3c was expecting --test-nosort
  • Test runner with tracking allocator assertion at failed test #1963
  • Test runner with tracking allocator didn't properly handle teardown_fn
  • Correctly give an error if a character literal contains a line break.
  • Implicitly unwrapped optional value in defer incorrectly copied #1982.
  • Crash when trying to define a method macro that isn't @construct but has no arguments.
  • Regression, .gitkeep files were generated incorrectly.
  • Aliases are now correctly handled as if they were variables/functions in regards to namespacing and accept @builtin.
  • Correctly handle in/out when interacting with inout.
  • Don't delete .o files not produced by the compiler.
  • Fix optional jumps in expression lists, #1942.
  • Several fixes for .o files and -o output, improving handling and naming.
  • Fix bug casting bool to int to other int #1995.
  • @if declarations were missing from -P output #1973.
  • Check exe and lib output so -o works with directories.
  • Swizzling an inline vector in a struct would cause a crash.
  • Fixed error and poor error message when using an invalid target name.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

It's February and time for a new C3 release: 0.6.7

Originally from: https://c3.handmade.network/blog/p/8994-it%2527s_february_and_time_for_a_new_c3_release__0.6.7

0.6.7 has been interesting. There has been some significant additions to the language that opens for new solutions. Also because it's now finally possible to manipulate compile time arrays in a simple way, there are some simple but useful compile time tricks that now is available. For example, string manipulation at compile time.

So let's have look at the biggest changes.

Compile time improvements

As previously mentioned, compile time arrays can now be mutated. This allows things like $arr[$i] = 123. And at this point, the only thing still not possible to mutate at compile time are struct fields.

In addition to this the concatenation operator, +++, now works on all types of arrays, even ones defined with gaps.

"Inline" enums

It's now possible to set enums to have its ordinal or an associated value marked "inline".

The feature allows an enum value to implicitly convert to that value:

enum Foo : int (inline String name, int y, int z)
{
  ABC = { "Hello", 1, 2 },
  DEF = { "World", 2, 3 },
}

fn void main()
{
  String hello = Foo.ABC;
  io::printn(hello); // Prints "Hello"
}

Short function syntax combined with macros

The short function syntax handles macros with trailing bodies in a special way, allowing the macro's trailing body to work as the body of the function, which simplifies the code when a function starts with a macro with a body:

// 0.6.6
fn Path! new_cwd(Allocator allocator = allocator::heap())
{
  @pool(allocator)
  {
    return new(os::getcwd(allocator::temp()), allocator);
  };
}

// 0.6.7
fn Path! new_cwd(Allocator allocator = allocator::heap()) => @pool()
{
  return new(os::getcwd(allocator::temp()), allocator);
}

C style compound literals

C style compound literals is making a comeback, it's now possible to do (int[2]) { 1, 2 } as an alternative to int[2] { 1, 2 }.

This is in preparation for possibly changing the syntax for generic modules. Regardless, this style is probably here to stay.

Improvements to runtime and unit test error checking

Unaligned loads will now be detected in safe mode, and the test runner has been improved to check for memory leaks, and the test runner has been greatly updated.

A new module test in the standard library adds various test utilities for unit testing.

Type inference on ??

Previously the expression MyEnum f = foo() ?? MyEnum.ABC; would require the full enum name as in this example but this has been improved so that in 0.6.7 MyEnum f = foo() ? ABC; is sufficient. This also affects all other types of inference on the right hand side of ??

Better bytes printing in the stdlib

0.6.7 adds %h and %H for printing binary data in hexadecimal.

Other stdlib improvements

  1. Channels for use with threaded code were added.
  2. @select to perform constant a ? x : y at compile time yielding a constant result.
  3. HashMap is now implementing Printable
  4. allocator::wrap allows quickly creating an arena allocator from a collection of bytes.
  5. Nolibc math code was expanded with some additional functions.

Bug fixes

0.6.7, like 0.6.6, contains about 50 bug fixes of various severity.

What's next?

There is an ongoing discussion in regards to generic syntax. (< >) works, but it not particularly lightweight. Some other alternatives, such as < > ( ) and [ ] suffer from ambiguities, so other options are investigated, such as $() and {}

Also in a quest to simplify the language, it's an open question whether {| |} should be removed or not. The expression blocks have their uses, but significantly less in C3 with semantic macros than it would have in C.

Other than that, expansion of the standard library is a priority.

With 0.7.0 scheduled for April, there is pretty much only room for one more 0.6.x release: 0.6.8.

I'm looking forward to cleaning out deprecated code when 0.7.0 finally comes around.

Here is the full change list:

Changes / improvements

  • Contracts @require/@ensure are no longer treated as conditionals, but must be explicitly bool.
  • Add win-debug setting to be able to pick dwarf for output #1855.
  • Error on switch case fallthough if there is more than one newline #1849.
  • Added flags to c3c project view to filter displayed properties
  • Compile time array assignment #1806.
  • Allow +++ to work on all types of arrays.
  • Allow (int[*]) { 1, 2 } cast style initialization.
  • Experimental change from [*] to [?]
  • Warn on if-catch with just a default case.
  • Compile time array inc/dec.
  • Improve error message when using ',' in struct declarations. #1920
  • Compile time array assign ops, e.g. $c[1] += 3 #1890.
  • Add inline to enums #1819.
  • Cleaner error message when missing comma in struct initializer #1941.
  • Distinct inline void causes unexpected error if used in slice #1946.
  • Allow fn int test() => @pool() { return 1; } short function syntax usage #1906.
  • Test runner will also check for leaks.
  • Improve inference on ?? #1943.
  • Detect unaligned loads #1951.

Fixes

  • Fix issue requiring prefix on a generic interface declaration.
  • Fix bug in SHA1 for longer blocks #1854.
  • Fix lack of location for reporting lambdas with missing return statement #1857.
  • Compiler allows a generic module to be declared with different parameters #1856.
  • Fix issue with @const where the statement $foo = 1; was not considered constant.
  • Const strings and bytes were not properly converted to compile time bools.
  • Concatenating a const empty slice with another array caused a null pointer access.
  • Fix linux-crt and linux-crtbegin not getting recognized as a project paramater
  • Fix dues to crash when converting a const vector to another vector #1864.
  • Filter $exec output from \r, which otherwise would cause a compiler assert #1867.
  • Fixes to `"exec" use, including issue when compiling with MinGW.
  • Correctly check jump table size and be generous when compiling it #1877.
  • Fix bug where .min/.max would fail on a distinct int #1888.
  • Fix issue where compile time declarations in expression list would not be handled properly.
  • Issue where trailing body argument was allowed without type even though the definition specified it #1879.
  • Fix issues with @jump on empty default or only default #1893 #1894
  • Fixes miscompilation of nested @jump #1896.
  • Fixed STB_WEAK errors when using consts in macros in the stdlib #1871.
  • Missing error when placing a single statement for-body on a new row #1892.
  • Fix bug where in dead code, only the first statement would be turned into a nop.
  • Remove unused $inline argument to mem::copy.
  • Defer is broken when placed before a $foreach #1912.
  • Usage of @noreturn macro is type-checked as if it returns #1913.
  • Bug when indexing into a constant array at compile time.
  • Fixing various issues around shifts, like z <<= { 1, 2 }.
  • return (any)&foo would not be reported as an escaping variable if foo was a pointer or slice.
  • Incorrect error message when providing too many associated values for enum #1934.
  • Allow function types to have a calling convention. #1938
  • Issue with defer copying when triggered by break or continue #1936.
  • Assert when using optional as init or inc part in a for loop #1942.
  • Fix bigint hex parsing #1945.
  • bigint::from_int(0) throws assertion #1944.
  • write of qoi would leak memory.
  • Issue when having an empty Path or just "."
  • set_env would leak memory.
  • Fix issue where aligned bitstructs did not store/load with the given alignment.
  • Fix issue in GrowableBitSet with sanitizers.
  • Fix issue in List with sanitizers.
  • Circumvent Aarch64 miscompilations of atomics.
  • Fixes to ByteBuffer allocation/free.
  • Fix issue where compiling both for asm and object file would corrupt the obj file output.
  • Fix poll and POLL_FOREVER.
  • Missing end padding when including a packed struct #1966.
  • Issue when scalar expanding a boolean from a conditional to a bool vector #1954.
  • Fix issue when parsing bitstructs, preventing them from implementing interfaces.
  • Regression String! a; char* b = a.ptr; would incorrectly be allowed.
  • Fix issue where target was ignored for projects.
  • Fix issue when dereferencing a constant string.
  • Fix problem where a line break in a literal was allowed.

Stdlib changes

  • Added '%h' and '%H' for printing out binary data in hexadecimal using the formatter.
  • Added weakly linked __powidf2
  • Added channels for threads.
  • New std::core::test module for unit testing machinery.
  • New unit test default runner.
  • Added weakly linked fmodf.
  • Add @select to perform the equivalent of a ? x : y at compile time.
  • HashMap is now Printable.
  • Add allocator::wrap to create an arena allocator on the stack from bytes.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

Comments


Comment by Christoffer Lernö

0.6.7 has been interesting. There has been some significant additions to the language that opens for new solutions. Also because it's now finally possible to manipulate compile time arrays in a simple way, there are some simple but useful compile time tricks that now is available. For example, string manipulation at compile time.

So let's have look at the biggest changes.

Compile time improvements

As previously mentioned, compile time arrays can now be mutated. This allows things like $arr[$i] = 123. And at this point, the only thing still not possible to mutate at compile time are struct fields.

In addition to this the concatenation operator, +++, now works on all types of arrays, even ones defined with gaps.

"Inline" enums

It's now possible to set enums to have its ordinal or an associated value marked "inline".

The feature allows an enum value to implicitly convert to that value:

enum Foo : int (inline String name, int y, int z)
{
  ABC = { "Hello", 1, 2 },
  DEF = { "World", 2, 3 },
}

fn void main()
{
  String hello = Foo.ABC;
  io::printn(hello); // Prints "Hello"
}

Short function syntax combined with macros

The short function syntax handles macros with trailing bodies in a special way, allowing the macro's trailing body to work as the body of the function, which simplifies the code when a function starts with a macro with a body:

// 0.6.6
fn Path! new_cwd(Allocator allocator = allocator::heap())
{
  @pool(allocator)
  {
    return new(os::getcwd(allocator::temp()), allocator);
  };
}

// 0.6.7
fn Path! new_cwd(Allocator allocator = allocator::heap()) => @pool()
{
  return new(os::getcwd(allocator::temp()), allocator);
}

C style compound literals

C style compound literals is making a comeback, it's now possible to do (int[2]) { 1, 2 } as an alternative to int[2] { 1, 2 }.

This is in preparation for possibly changing the syntax for generic modules. Regardless, this style is probably here to stay.

Improvements to runtime and unit test error checking

Unaligned loads will now be detected in safe mode, and the test runner has been improved to check for memory leaks, and the test runner has been greatly updated.

A new module test in the standard library adds various test utilities for unit testing.

Type inference on ??

Previously the expression MyEnum f = foo() ?? MyEnum.ABC; would require the full enum name as in this example but this has been improved so that in 0.6.7 MyEnum f = foo() ? ABC; is sufficient. This also affects all other types of inference on the right hand side of ??

Better bytes printing in the stdlib

0.6.7 adds %h and %H for printing binary data in hexadecimal.

Other stdlib improvements

  1. Channels for use with threaded code were added.
  2. @select to perform constant a ? x : y at compile time yielding a constant result.
  3. HashMap is now implementing Printable
  4. allocator::wrap allows quickly creating an arena allocator from a collection of bytes.
  5. Nolibc math code was expanded with some additional functions.

Bug fixes

0.6.7, like 0.6.6, contains about 50 bug fixes of various severity.

What's next?

There is an ongoing discussion in regards to generic syntax. (< >) works, but it not particularly lightweight. Some other alternatives, such as < > ( ) and [ ] suffer from ambiguities, so other options are investigated, such as $() and {}

Also in a quest to simplify the language, it's an open question whether {| |} should be removed or not. The expression blocks have their uses, but significantly less in C3 with semantic macros than it would have in C.

Other than that, expansion of the standard library is a priority.

With 0.7.0 scheduled for April, there is pretty much only room for one more 0.6.x release: 0.6.8.

I'm looking forward to cleaning out deprecated code when 0.7.0 finally comes around.

Here is the full change list:

Changes / improvements

  • Contracts @require/@ensure are no longer treated as conditionals, but must be explicitly bool.
  • Add win-debug setting to be able to pick dwarf for output #1855.
  • Error on switch case fallthough if there is more than one newline #1849.
  • Added flags to c3c project view to filter displayed properties
  • Compile time array assignment #1806.
  • Allow +++ to work on all types of arrays.
  • Allow (int[*]) { 1, 2 } cast style initialization.
  • Experimental change from [*] to [?]
  • Warn on if-catch with just a default case.
  • Compile time array inc/dec.
  • Improve error message when using ',' in struct declarations. #1920
  • Compile time array assign ops, e.g. $c[1] += 3 #1890.
  • Add inline to enums #1819.
  • Cleaner error message when missing comma in struct initializer #1941.
  • Distinct inline void causes unexpected error if used in slice #1946.
  • Allow fn int test() => @pool() { return 1; } short function syntax usage #1906.
  • Test runner will also check for leaks.
  • Improve inference on ?? #1943.
  • Detect unaligned loads #1951.

Fixes

  • Fix issue requiring prefix on a generic interface declaration.
  • Fix bug in SHA1 for longer blocks #1854.
  • Fix lack of location for reporting lambdas with missing return statement #1857.
  • Compiler allows a generic module to be declared with different parameters #1856.
  • Fix issue with @const where the statement $foo = 1; was not considered constant.
  • Const strings and bytes were not properly converted to compile time bools.
  • Concatenating a const empty slice with another array caused a null pointer access.
  • Fix linux-crt and linux-crtbegin not getting recognized as a project paramater
  • Fix dues to crash when converting a const vector to another vector #1864.
  • Filter $exec output from \r, which otherwise would cause a compiler assert #1867.
  • Fixes to `"exec" use, including issue when compiling with MinGW.
  • Correctly check jump table size and be generous when compiling it #1877.
  • Fix bug where .min/.max would fail on a distinct int #1888.
  • Fix issue where compile time declarations in expression list would not be handled properly.
  • Issue where trailing body argument was allowed without type even though the definition specified it #1879.
  • Fix issues with @jump on empty default or only default #1893 #1894
  • Fixes miscompilation of nested @jump #1896.
  • Fixed STB_WEAK errors when using consts in macros in the stdlib #1871.
  • Missing error when placing a single statement for-body on a new row #1892.
  • Fix bug where in dead code, only the first statement would be turned into a nop.
  • Remove unused $inline argument to mem::copy.
  • Defer is broken when placed before a $foreach #1912.
  • Usage of @noreturn macro is type-checked as if it returns #1913.
  • Bug when indexing into a constant array at compile time.
  • Fixing various issues around shifts, like z <<= { 1, 2 }.
  • return (any)&foo would not be reported as an escaping variable if foo was a pointer or slice.
  • Incorrect error message when providing too many associated values for enum #1934.
  • Allow function types to have a calling convention. #1938
  • Issue with defer copying when triggered by break or continue #1936.
  • Assert when using optional as init or inc part in a for loop #1942.
  • Fix bigint hex parsing #1945.
  • bigint::from_int(0) throws assertion #1944.
  • write of qoi would leak memory.
  • Issue when having an empty Path or just "."
  • set_env would leak memory.
  • Fix issue where aligned bitstructs did not store/load with the given alignment.
  • Fix issue in GrowableBitSet with sanitizers.
  • Fix issue in List with sanitizers.
  • Circumvent Aarch64 miscompilations of atomics.
  • Fixes to ByteBuffer allocation/free.
  • Fix issue where compiling both for asm and object file would corrupt the obj file output.
  • Fix poll and POLL_FOREVER.
  • Missing end padding when including a packed struct #1966.
  • Issue when scalar expanding a boolean from a conditional to a bool vector #1954.
  • Fix issue when parsing bitstructs, preventing them from implementing interfaces.
  • Regression String! a; char* b = a.ptr; would incorrectly be allowed.
  • Fix issue where target was ignored for projects.
  • Fix issue when dereferencing a constant string.
  • Fix problem where a line break in a literal was allowed.

Stdlib changes

  • Added '%h' and '%H' for printing out binary data in hexadecimal using the formatter.
  • Added weakly linked __powidf2
  • Added channels for threads.
  • New std::core::test module for unit testing machinery.
  • New unit test default runner.
  • Added weakly linked fmodf.
  • Add @select to perform the equivalent of a ? x : y at compile time.
  • HashMap is now Printable.
  • Add allocator::wrap to create an arena allocator on the stack from bytes.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

Another monthly release: C3 0.6.6 is here

Originally from: https://c3.handmade.network/blog/p/8983-another_monthly_release__c3_0.6.6_is_here

The 0.6.6 release is surprisingly on time despite (or perhaps thanks to?) the Christmas holidays. It's a new year, and this summer C3 will turn 6. By April, version 0.7.0 will be released, removing deprecated code. The plan is to have one "dot one" release each year until 1.0 is reached (and if everything goes according to plan, the version after 0.9 will be 1.0).

But let's dive into what's new in this release!

Enum from_ordinal / ordinal changes

A longstanding pain point with enums has been modeling C enums with "gaps," as in this example:

typedef enum
{
    ABC = 42,
    GHJ = 101
} MyEnum;

A workaround in C3 using associated values like this doesn't feel ideal:

enum MyEnum : (int val)
{
    ABC = 42,
    GHJ = 101
}

This is because you can get the ordinal of the enum with a cast, e.g., (int)MyEnum.ABC, whereas retrieving the an associated value requires access, like MyEnum.ABC.val.

There are some ideas to bridge this gap, but the fact that the enums would cast to their ordinal by default made it hard to create a reasonable feature for it.

To address this, casts are now deprecated and replaced by MyEnum.from_ordinal(value). This type method is essentially a no-op, as is converting an enum to a number with .ordinal (e.g. MyEnum.ABC.ordinal).

This puts something like MyEnum.ABC.val and MyEnum.ABC.ordinal on, more equal footing which will make it easier to implement possible solutions to the gap enum problem.

No more & macro arguments

Macros differ from functions by allowing 5 (!) additional types of arguments:

  1. Compile-time constant arguments ($foo)
  2. Compile-time type arguments ($Type)
  3. Unevaluated expression arguments (#expr)
  4. And, finally, reference arguments (&ref)

With 0.6.6 &ref arguments become deprecated. With #expr arguments actually already being able to do what &ref does, this is not a loss in functionality. It does put somewhat more effort in using an #expr argument in terms of checking, but with the fairly marginal use of &ref in the standard library it didn't seem like a loss.

The main advantage for dropping &ref is that it no longer needs to be explained or motivated. It also simplifies the compiler in places where it ended up adding surprising complexities – but those simplifications can't be done until 0.7 when &ref is removed (rather than deprecated).

More important than the change is that the removal of & (which has been around for as long as the macros), marks the beginning of consolidating the language: removing things that have proven over time to be superfluous.

void rather than void! as default for main, tests and benchmarks

Another consolidation is the removal of void! as a return type for main.

Previously, it was possible to return an optional Empty value from main and have the compiler turn it into a 1 exit code.

This was problematic because it effectively discarded the Optional’s excuse. It also encouraged bad code which would just rethrow Optionals from functions called by main

void! was removed from @test functions for a similar reason. Actually panicking (with !! rather than !) would yield much better error messages for free. So again the choice between void and void! for test functions were not really helpful.

Starting from version 0.6.6, use void or int as the return type for main functions, and void for @test and @benchmark functions.

$foreach iterating over Strings

An oversight previously made it impossible to iterate over Strings and bytes. With 0.6.6, this now works correctly.

var declaring lambdas

Using var has been limited to macros in C3, but now they're also allowed in functions when declaring lambdas.

@operator(construct)

An experimental feature called construct is available from 0.6.6.

This allows a limited form of static methods on types. For example:

fn Foo Foo.new_with_bar(Bar b) @operator(construct)
{
    return { .b = b }; // Same as Foo { .b = b }
}

The limitation is that such a method must either return the type or a pointer to the type with the method. (So in the above example, either returning Foo or Foo* would be possible).

Consider this an experimental preview for now!

Standard Library Improvements The standard library has seen useful additions, including foreach-compatible iterators for HashMap and an URL parser.

The default hash functions have been improved, enhancing HashMap performance. There have also been other minor additions and changes.

Bug fixes

This version contains over 50 bug fixes, which is about twice from 0.6.5, but less than the record setting 0.6.2.

Refactoring

0.6.6 includes initial steps toward refactoring parts of the frontend representation to facilitate the addition of new backends. A C backend is planned for this year, making these improvements essential.

What's next?

The enums might finally get the "gap enum" usecase filled (pun intended), but it's not clear whether this will happen in 0.6.7 or will be pushed to later.

There is also a glaring hole in compile time evaluation where $i[1] = 2; isn't allowed, but $a = $i[1]; is. This will need to change, but to do so there needs to be some refactoring, which ties into the overall changes in the frontend representation to help the backend lowering, but also static analysis.

Users, despite work under the hood, should see very little of these changes, except maybe that some corner cases (like compile time subscript above) starts working.

There are some syntax tweaks coming up, but they should be very minor and probably not even be visible to most users.

More importantly though, is that C3 will see the beginning of work to prune unused features from the language, which will then eventually be removed with 0.7.0.

0.6.6 Change list

Here is the full change list

Changes / improvements

  • Split help into normal and "full" help, #1703
  • Removed 'headers' command line option.
  • Add enum.from_ordinal and fault.from_ordinal
  • Deprecate cast-style conversion from integer <-> enum.
  • Make deprecation an error in test mode.
  • Add --win-vs-dirs to override VS detection dirs.
  • Add "name" project property to override the name of the resulting binary. #1719
  • Improved add-project to take arguments.
  • Improve error reporting when using type names as the function argument #1750.
  • Improve ordering of method registration to support adding methods to generic modules with method constraints #1746
  • Support experimental @operator(construct) operator overload.
  • Allow using 'var' to declare lambdas in functions.
  • Add 'validation' setting and make dead code a warning.
  • Allow compile time $foreach iteration over constant Strings and bytes.
  • Improved error message when accessing @private from other modules #1769.
  • Include @name when searching for possible matches to name in the error message. #1779
  • Improve @param parse errors #1777
  • Improved #foo resolution inside of the compiler.
  • Deprecated '&' macro arguments.
  • Deprecate `fn void! main() type main functions.
  • Deprecate old void! @benchmark and @test functions.
  • Allow test runners to take String[] arguments.
  • Added --lsp output.
  • Improve the error message when running out of memory.
  • Allowed passing arguments to @test / @benchmark runners via c3c test[benchmark] -- -o --opt1 <arg1>
  • Handle bytes and string literals the same way in terms of zero termination.
  • Function comments are stored and displayed with -P.
  • Prevent #hash arguments from taking code that modifies ct variables. #1794
  • Make stringify to recursively enter #hash expressions #1834.

Fixes

  • Fix case trying to initialize a char[*]* from a String.
  • Fix Map & HashMap put_all_for_create not copying all elements, causing init_from_map to create incomplete copy.
  • Fix bug when a macro calling an extern function was called in another module also declaring and calling the same function. #1690
  • static-lib and dynamic-lib options from the command line now produces headers.
  • Fix bug outputting exported functions without predefined extname.
  • Fix problem where crt1 was linked for dynamic libraries on Linux and BSD. #1710
  • Fix CRT detection on Arch Linux.
  • Fix lexer allowing a trailing underscore (_) with hex and binary literals.
  • Fix --list-operators CLI command printing underscore (_) and hash (#).
  • Fix bug in temp allocator when temp memory is exhausted and allocation needs overaligned mem. #1715
  • Incorrectly handles distinct enums and pointers with '+=' and '-=' #1717.
  • Prevent DString from being initialized with "".
  • Fix bug in OnStackAllocator when freeing overallocated data. #1720
  • Use weak_odr rather than weak on Windows which seems to prevent issues such as #1704.
  • Use weak on dyn-symbols on Linux.
  • Fix crash on project.json not having an empty set of targets.
  • Miscompile when indexing an array with small unsigned types for enums.
  • Change CBool to be 1 byte.
  • any_to_int checks value to be int and no longer works with enum.
  • Add check in formatter printing "%c".
  • Fix bug where !! and ! was not recognized to jump out of the current scope.
  • Fix bug when including compile time parameters in trailing body more than once.
  • Fix issue with compiling a constant struct containing a string array in a local context.
  • Fix error where panic would not properly stop the program when stacktrace couldn't be printed #1751.
  • Macros with default arguments to &, # and type parameters didn't work as expected. #1754.
  • net::poll() with negative timeout behaved incorrectly.
  • Return type inference bugs with macros #1757
  • $defined in a global scope should accept testing normal macros.
  • Assert on add to uninitialized ct variable #1765.
  • Dynamic function lookup fails after changing type without dummy anycast #1761
  • $vasplat was allowed inside of a function when passed as an argument to a function.
  • Prohibit raw vaargs in regular functions with a function body.
  • Assert on certain slice to slice casts. #1768.
  • Fix vector float -> bool conversion.
  • Fix +a = 1 erronously being accepted.
  • Fix not freeing a zero length String
  • Macros with trailing bodys aren't allowed as the single statement after a while loop with no body #1772.
  • Deref subscripts as needed for macro ref method arguments. #1789
  • Change ordering to simplify adding methods to type in conditional modules.
  • #foo style arguments were not type checked when given a type. #1790
  • Bug when using +++ on value build a slice or array: the rhs cast was not done.
  • Fix bug preventing compile time slices from being iterated over with $foreach.
  • Fix bug with defer assignment in macro #1807.
  • Fix regression with swizzle references for vectors #1810.
  • Assert when partially initializing a constant struct containing a slice #1812.
  • Assert concatenating constant slices #1805.
  • Do not link "ld" on Linux with no libc.
  • Fix bug when multiple $else clauses followed an $if #1824.
  • Report the correct type as not having a method when access fails #1828.
  • Prevent temp arena scribbling from causing an asan warning. #1825
  • Fix bug where &i[0] = null was not detected to be an error #1833.

Stdlib changes

  • Increase BitWriter.write_bits limit up to 32 bits.
  • Updates to Slice2d, like get_xy and others.
  • Added iter() value_iter() and key_iter() to HashMap.
  • Add "tokenizer" to String.
  • Add "skip_empty" to split methods. Add split_to_buffer method.
  • Add @enum_from_value.
  • Updated hash function.
  • Added URL parser.
  • Added convenience functions to Maybe.
  • Added String.trim_left() and .trim_right().
  • Deprecation of several & macros.
  • Format functions for timedates.
  • Add @assert_leak() to assert on memory leaks in the scope.
  • Added double.set_high_word(), double.set_low_word(), and float.set_word().

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

Comments


Comment by Christoffer Lernö

The 0.6.6 release is surprisingly on time despite (or perhaps thanks to?) the Christmas holidays. It's a new year, and this summer C3 will turn 6. By April, version 0.7.0 will be released, removing deprecated code. The plan is to have one "dot one" release each year until 1.0 is reached (and if everything goes according to plan, the version after 0.9 will be 1.0).

But let's dive into what's new in this release!

Enum from_ordinal / ordinal changes

A longstanding pain point with enums has been modeling C enums with "gaps," as in this example:

typedef enum
{
    ABC = 42,
    GHJ = 101
} MyEnum;

A workaround in C3 using associated values like this doesn't feel ideal:

enum MyEnum : (int val)
{
    ABC = 42,
    GHJ = 101
}

This is because you can get the ordinal of the enum with a cast, e.g., (int)MyEnum.ABC, whereas retrieving the an associated value requires access, like MyEnum.ABC.val.

There are some ideas to bridge this gap, but the fact that the enums would cast to their ordinal by default made it hard to create a reasonable feature for it.

To address this, casts are now deprecated and replaced by MyEnum.from_ordinal(value). This type method is essentially a no-op, as is converting an enum to a number with .ordinal (e.g. MyEnum.ABC.ordinal).

This puts something like MyEnum.ABC.val and MyEnum.ABC.ordinal on, more equal footing which will make it easier to implement possible solutions to the gap enum problem.

No more & macro arguments

Macros differ from functions by allowing 5 (!) additional types of arguments:

  1. Compile-time constant arguments ($foo)
  2. Compile-time type arguments ($Type)
  3. Unevaluated expression arguments (#expr)
  4. And, finally, reference arguments (&ref)

With 0.6.6 &ref arguments become deprecated. With #expr arguments actually already being able to do what &ref does, this is not a loss in functionality. It does put somewhat more effort in using an #expr argument in terms of checking, but with the fairly marginal use of &ref in the standard library it didn't seem like a loss.

The main advantage for dropping &ref is that it no longer needs to be explained or motivated. It also simplifies the compiler in places where it ended up adding surprising complexities – but those simplifications can't be done until 0.7 when &ref is removed (rather than deprecated).

More important than the change is that the removal of & (which has been around for as long as the macros), marks the beginning of consolidating the language: removing things that have proven over time to be superfluous.

void rather than void! as default for main, tests and benchmarks

Another consolidation is the removal of void! as a return type for main.

Previously, it was possible to return an optional Empty value from main and have the compiler turn it into a 1 exit code.

This was problematic because it effectively discarded the Optional’s excuse. It also encouraged bad code which would just rethrow Optionals from functions called by main

void! was removed from @test functions for a similar reason. Actually panicking (with !! rather than !) would yield much better error messages for free. So again the choice between void and void! for test functions were not really helpful.

Starting from version 0.6.6, use void or int as the return type for main functions, and void for @test and @benchmark functions.

$foreach iterating over Strings

An oversight previously made it impossible to iterate over Strings and bytes. With 0.6.6, this now works correctly.

var declaring lambdas

Using var has been limited to macros in C3, but now they're also allowed in functions when declaring lambdas.

@operator(construct)

An experimental feature called construct is available from 0.6.6.

This allows a limited form of static methods on types. For example:

fn Foo Foo.new_with_bar(Bar b) @operator(construct)
{
    return { .b = b }; // Same as Foo { .b = b }
}

The limitation is that such a method must either return the type or a pointer to the type with the method. (So in the above example, either returning Foo or Foo* would be possible).

Consider this an experimental preview for now!

Standard Library Improvements The standard library has seen useful additions, including foreach-compatible iterators for HashMap and an URL parser.

The default hash functions have been improved, enhancing HashMap performance. There have also been other minor additions and changes.

Bug fixes

This version contains over 50 bug fixes, which is about twice from 0.6.5, but less than the record setting 0.6.2.

Refactoring

0.6.6 includes initial steps toward refactoring parts of the frontend representation to facilitate the addition of new backends. A C backend is planned for this year, making these improvements essential.

What's next?

The enums might finally get the "gap enum" usecase filled (pun intended), but it's not clear whether this will happen in 0.6.7 or will be pushed to later.

There is also a glaring hole in compile time evaluation where $i[1] = 2; isn't allowed, but $a = $i[1]; is. This will need to change, but to do so there needs to be some refactoring, which ties into the overall changes in the frontend representation to help the backend lowering, but also static analysis.

Users, despite work under the hood, should see very little of these changes, except maybe that some corner cases (like compile time subscript above) starts working.

There are some syntax tweaks coming up, but they should be very minor and probably not even be visible to most users.

More importantly though, is that C3 will see the beginning of work to prune unused features from the language, which will then eventually be removed with 0.7.0.

0.6.6 Change list

Here is the full change list

Changes / improvements

  • Split help into normal and "full" help, #1703
  • Removed 'headers' command line option.
  • Add enum.from_ordinal and fault.from_ordinal
  • Deprecate cast-style conversion from integer <-> enum.
  • Make deprecation an error in test mode.
  • Add --win-vs-dirs to override VS detection dirs.
  • Add "name" project property to override the name of the resulting binary. #1719
  • Improved add-project to take arguments.
  • Improve error reporting when using type names as the function argument #1750.
  • Improve ordering of method registration to support adding methods to generic modules with method constraints #1746
  • Support experimental @operator(construct) operator overload.
  • Allow using 'var' to declare lambdas in functions.
  • Add 'validation' setting and make dead code a warning.
  • Allow compile time $foreach iteration over constant Strings and bytes.
  • Improved error message when accessing @private from other modules #1769.
  • Include @name when searching for possible matches to name in the error message. #1779
  • Improve @param parse errors #1777
  • Improved #foo resolution inside of the compiler.
  • Deprecated '&' macro arguments.
  • Deprecate `fn void! main() type main functions.
  • Deprecate old void! @benchmark and @test functions.
  • Allow test runners to take String[] arguments.
  • Added --lsp output.
  • Improve the error message when running out of memory.
  • Allowed passing arguments to @test / @benchmark runners via c3c test[benchmark] -- -o --opt1 <arg1>
  • Handle bytes and string literals the same way in terms of zero termination.
  • Function comments are stored and displayed with -P.
  • Prevent #hash arguments from taking code that modifies ct variables. #1794
  • Make stringify to recursively enter #hash expressions #1834.

Fixes

  • Fix case trying to initialize a char[*]* from a String.
  • Fix Map & HashMap put_all_for_create not copying all elements, causing init_from_map to create incomplete copy.
  • Fix bug when a macro calling an extern function was called in another module also declaring and calling the same function. #1690
  • static-lib and dynamic-lib options from the command line now produces headers.
  • Fix bug outputting exported functions without predefined extname.
  • Fix problem where crt1 was linked for dynamic libraries on Linux and BSD. #1710
  • Fix CRT detection on Arch Linux.
  • Fix lexer allowing a trailing underscore (_) with hex and binary literals.
  • Fix --list-operators CLI command printing underscore (_) and hash (#).
  • Fix bug in temp allocator when temp memory is exhausted and allocation needs overaligned mem. #1715
  • Incorrectly handles distinct enums and pointers with '+=' and '-=' #1717.
  • Prevent DString from being initialized with "".
  • Fix bug in OnStackAllocator when freeing overallocated data. #1720
  • Use weak_odr rather than weak on Windows which seems to prevent issues such as #1704.
  • Use weak on dyn-symbols on Linux.
  • Fix crash on project.json not having an empty set of targets.
  • Miscompile when indexing an array with small unsigned types for enums.
  • Change CBool to be 1 byte.
  • any_to_int checks value to be int and no longer works with enum.
  • Add check in formatter printing "%c".
  • Fix bug where !! and ! was not recognized to jump out of the current scope.
  • Fix bug when including compile time parameters in trailing body more than once.
  • Fix issue with compiling a constant struct containing a string array in a local context.
  • Fix error where panic would not properly stop the program when stacktrace couldn't be printed #1751.
  • Macros with default arguments to &, # and type parameters didn't work as expected. #1754.
  • net::poll() with negative timeout behaved incorrectly.
  • Return type inference bugs with macros #1757
  • $defined in a global scope should accept testing normal macros.
  • Assert on add to uninitialized ct variable #1765.
  • Dynamic function lookup fails after changing type without dummy anycast #1761
  • $vasplat was allowed inside of a function when passed as an argument to a function.
  • Prohibit raw vaargs in regular functions with a function body.
  • Assert on certain slice to slice casts. #1768.
  • Fix vector float -> bool conversion.
  • Fix +a = 1 erronously being accepted.
  • Fix not freeing a zero length String
  • Macros with trailing bodys aren't allowed as the single statement after a while loop with no body #1772.
  • Deref subscripts as needed for macro ref method arguments. #1789
  • Change ordering to simplify adding methods to type in conditional modules.
  • #foo style arguments were not type checked when given a type. #1790
  • Bug when using +++ on value build a slice or array: the rhs cast was not done.
  • Fix bug preventing compile time slices from being iterated over with $foreach.
  • Fix bug with defer assignment in macro #1807.
  • Fix regression with swizzle references for vectors #1810.
  • Assert when partially initializing a constant struct containing a slice #1812.
  • Assert concatenating constant slices #1805.
  • Do not link "ld" on Linux with no libc.
  • Fix bug when multiple $else clauses followed an $if #1824.
  • Report the correct type as not having a method when access fails #1828.
  • Prevent temp arena scribbling from causing an asan warning. #1825
  • Fix bug where &i[0] = null was not detected to be an error #1833.

Stdlib changes

  • Increase BitWriter.write_bits limit up to 32 bits.
  • Updates to Slice2d, like get_xy and others.
  • Added iter() value_iter() and key_iter() to HashMap.
  • Add "tokenizer" to String.
  • Add "skip_empty" to split methods. Add split_to_buffer method.
  • Add @enum_from_value.
  • Updated hash function.
  • Added URL parser.
  • Added convenience functions to Maybe.
  • Added String.trim_left() and .trim_right().
  • Deprecation of several & macros.
  • Format functions for timedates.
  • Add @assert_leak() to assert on memory leaks in the scope.
  • Added double.set_high_word(), double.set_low_word(), and float.set_word().

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

Why doesn't C3 do import aliasing?

Originally from: https://c3.handmade.network/blog/p/8982-why_doesn%2527t_c3_do_import_aliasing

Why doesn't C3 allow aliasing of namespaces? Wouldn't import raylib as rl; be a good idea? Why am I so hell-bent on not allowing it?

So, first off, C2 has aliasing. It offers three levels:

  1. Importing with the full name.
  2. Importing with an alias.
  3. Folding the imports (so everything can be used without prefix)

Why didn't I keep this?

C2's flat module namespace

First off, it's important to realize that C2 has a flat module namespace. There are no std::core::mem submodules, everything is bundled up into fairly large groups of functions and types. For example the stdio module which would have all the io functions and so on.

My concern was that it would be very likely for libraries to collide if they were flat.

EVERYONE would want their particular name to be the name for something. So now you can't use library net from Foo, because you also want to use the library net from Bar.

Aliasing won't help you because import net as foonet only works if you can disambiguate net to begin with.

It's possible people would start gravitating towards "prefix+name", but then everything would be superheavy name-wise: import somecompany_networking; and you'd have to use aliasing because the prefixes are so long.

Ok, hierarchical - now what?

Obviously no one would want to repeatedly prefix things like somecompany::networking::open(...) (except Zig programmers), so it would seem that import somecompany::networking as net would need to be the default?

And then we could all use net.Socket socket; and net.open("localhost") (skip to the end if you want to know why C3 uses :: instead of .).

Except what happens now that we started to have hierarchical module names is that we go from having to rename maybe 2-3 BIG packages to coming up with short names for 20 or 30 modules.

The naming problem is going from fairly trivial to painful.

The best C naming convention

Let's make a short detour: the best naming convention for library functions in C, is in my opinion: <category or type>_name(...) So for example if you look for something relating to the Expr type in the C3 compiler it would be expr_<some name> for the semantic checking, all such functions are sema_<name> and so on.

Wouldn't it be nice to have that kind of formalized? Like expr.copy_single(expr) and sema.resolve_desc(d)?

Is there a way to avoid renames?

I was racking my brains for a way to make it work without the need for renaming or namespace folding. The latter was never really recommended in C2, but was left in – probably when writing your own code and you just wanted everything to feel like a homogeneous code base.

My solution was to match on the minimum necessary prefix to disambiguate two things. So let's say we had std::io::File and foo::io::File then we need the full path to know which File was intended – so it would need the full path, but if it was std::io::File and foo::net::File then net::File and io::File would be enough to do the disambiguation.

This would allow people to use terse submodule names while still avoiding the need for explicit renaming.

Formalizing the C coding convention

With submodule names now rarely being used aside from when one was disambiguating functions, it was now possible to encode the C coding convention and say that functions need at least one level of module path.

Something like io::printn("Hello, world") in C3 should be thought of as io_printn("Hello, world").

And this is why under this new scheme using import renaming went from "needed" to "destructive". Because if you're used to seeing io::printn() and suddenly see io2::printn() you're going to think this is something completely new, unless it's code you wrote yourself 15 minutes ago.

The effect on generic modules

Originally generic modules were instantiated with import, so something like:

import std::collections::list(<int>) as intlist;
import std::collections::list(<doublelist>) as doublelist;

This actually mirrors better what is happening in the compiler, but then using things like doublelist::List l isn't really a user-friendly syntax to use.

With the new style you just imported the generic module and then made defines:

import std::collections::list;

def IntList = List(<int>);
def DoubleList = List(<double>);

This would have been possible before too, but it wasn't until the aliasing was removed this style became the "obvious" solution.

Recursive imports

Another effect of the change was allowing recursive imports. Already I've mentioned how the number of submodules made it more of a problem with naming imports, a key reason it has little place in C3 today is because C3 went for recursive imports.

So in C3, if you import std::collections you will get the module but also any submodule (and their submodules) directly available. (In fact, C3 experimented with no import statements at all!)

Let's say you want to import and add an alias. Fine, but this only affects part of what you import. Say you'd do import std::collections as cl.

Okay, but std::collections itself has zero functions and types. This comes back to the fact that when you start separating things into submodules you have so many more names if you want to rename them.

Big modules vs granular modules

In short, what we see is that rename is quite fine when we have big modules containing a lot of functions. This typically happens in two cases:

  1. You have a flat module namespace – like C2
  2. You are using a C library which is modelled as a single flat namespace.

A good experience in C3 without renaming?

There are good and bad ways to use modules in C3. Unfortunately, unlike in many other languages, if the library author is sloppy, you will suffer.

  1. You have to think about the usage. The raylib 4 bindings (by yours truly) infamously use module raylib;. This meant that everyone needed to type raylib:: in front of all the calls. The raylib5 bindings instead use module raylib5::rl. Not only did that make all functions now prefix with
    the more reasonable rl prefix, but there is room to add raymath as module raylib5::rm and so on.
  2. Coming from other languages, people often use more namespacing than they need to. It's just more to type, worse readability and makes the language look like C++. It's File f;, not std::io::File f;. The latter is the antithesis of good style. – And same with functions, one level, no more.
  3. If you're making up names of types, you need to ensure it looks unique because if it has to be namespaced then it has failed. So for example SdlMutex, not Mutex. There is an idea that using the module name is good enough to disambiguate types. It isn't, because C3 isn't built to accommodate C++ style.

Final Word

C3 doesn't have aliasing anymore because of a rather complex dynamic, and all the organization of the standard library and such does depend on each other.

Allowing some external mechanism to "hack" a broken module name and allowing it to be swapped for another at the command line is one thing (e.g. you have to use that library which has the raylib prefix then you might use something like --namespace-patch raylib rl at the command line to make it alias to rl instead) – This is a fairly straightforward enhancement.

To allow C2 aliasing though, that would mean rethinking everything about C3 modules, so that would require quite strong reasons for it to happen. But I prefer to never say never, I just want the language to be as good as possible.

Bonus: why doesn't C3 use .?

The short answer is that :: makes it possible to do the path shortening. Otherwise figuring out where the path starts would need to have a very complicated heuristic. Another thing is that makes sense if your names are things that won't collide with local variables. Now, in C3 we have std::io::file. If C3 used . then something like File file = file.open("foo.txt", "rb") would be ambiguous, whereas with :: we're always fine: File file = file::open("foo.txt", "rb").

(It also saves the grammar from being complicated in some other places, but these are the main reasons).

Comments


Comment by Christoffer Lernö

Why doesn't C3 allow aliasing of namespaces? Wouldn't import raylib as rl; be a good idea? Why am I so hell-bent on not allowing it?

So, first off, C2 has aliasing. It offers three levels:

  1. Importing with the full name.
  2. Importing with an alias.
  3. Folding the imports (so everything can be used without prefix)

Why didn't I keep this?

C2's flat module namespace

First off, it's important to realize that C2 has a flat module namespace. There are no std::core::mem submodules, everything is bundled up into fairly large groups of functions and types. For example the stdio module which would have all the io functions and so on.

My concern was that it would be very likely for libraries to collide if they were flat.

EVERYONE would want their particular name to be the name for something. So now you can't use library net from Foo, because you also want to use the library net from Bar.

Aliasing won't help you because import net as foonet only works if you can disambiguate net to begin with.

It's possible people would start gravitating towards "prefix+name", but then everything would be superheavy name-wise: import somecompany_networking; and you'd have to use aliasing because the prefixes are so long.

Ok, hierarchical - now what?

Obviously no one would want to repeatedly prefix things like somecompany::networking::open(...) (except Zig programmers), so it would seem that import somecompany::networking as net would need to be the default?

And then we could all use net.Socket socket; and net.open("localhost") (skip to the end if you want to know why C3 uses :: instead of .).

Except what happens now that we started to have hierarchical module names is that we go from having to rename maybe 2-3 BIG packages to coming up with short names for 20 or 30 modules.

The naming problem is going from fairly trivial to painful.

The best C naming convention

Let's make a short detour: the best naming convention for library functions in C, is in my opinion: <category or type>_name(...) So for example if you look for something relating to the Expr type in the C3 compiler it would be expr_<some name> for the semantic checking, all such functions are sema_<name> and so on.

Wouldn't it be nice to have that kind of formalized? Like expr.copy_single(expr) and sema.resolve_desc(d)?

Is there a way to avoid renames?

I was racking my brains for a way to make it work without the need for renaming or namespace folding. The latter was never really recommended in C2, but was left in – probably when writing your own code and you just wanted everything to feel like a homogeneous code base.

My solution was to match on the minimum necessary prefix to disambiguate two things. So let's say we had std::io::File and foo::io::File then we need the full path to know which File was intended – so it would need the full path, but if it was std::io::File and foo::net::File then net::File and io::File would be enough to do the disambiguation.

This would allow people to use terse submodule names while still avoiding the need for explicit renaming.

Formalizing the C coding convention

With submodule names now rarely being used aside from when one was disambiguating functions, it was now possible to encode the C coding convention and say that functions need at least one level of module path.

Something like io::printn("Hello, world") in C3 should be thought of as io_printn("Hello, world").

And this is why under this new scheme using import renaming went from "needed" to "destructive". Because if you're used to seeing io::printn() and suddenly see io2::printn() you're going to think this is something completely new, unless it's code you wrote yourself 15 minutes ago.

The effect on generic modules

Originally generic modules were instantiated with import, so something like:

import std::collections::list(<int>) as intlist;
import std::collections::list(<doublelist>) as doublelist;

This actually mirrors better what is happening in the compiler, but then using things like doublelist::List l isn't really a user-friendly syntax to use.

With the new style you just imported the generic module and then made defines:

import std::collections::list;

def IntList = List(<int>);
def DoubleList = List(<double>);

This would have been possible before too, but it wasn't until the aliasing was removed this style became the "obvious" solution.

Recursive imports

Another effect of the change was allowing recursive imports. Already I've mentioned how the number of submodules made it more of a problem with naming imports, a key reason it has little place in C3 today is because C3 went for recursive imports.

So in C3, if you import std::collections you will get the module but also any submodule (and their submodules) directly available. (In fact, C3 experimented with no import statements at all!)

Let's say you want to import and add an alias. Fine, but this only affects part of what you import. Say you'd do import std::collections as cl.

Okay, but std::collections itself has zero functions and types. This comes back to the fact that when you start separating things into submodules you have so many more names if you want to rename them.

Big modules vs granular modules

In short, what we see is that rename is quite fine when we have big modules containing a lot of functions. This typically happens in two cases:

  1. You have a flat module namespace – like C2
  2. You are using a C library which is modelled as a single flat namespace.

A good experience in C3 without renaming?

There are good and bad ways to use modules in C3. Unfortunately, unlike in many other languages, if the library author is sloppy, you will suffer.

  1. You have to think about the usage. The raylib 4 bindings (by yours truly) infamously use module raylib;. This meant that everyone needed to type raylib:: in front of all the calls. The raylib5 bindings instead use module raylib5::rl. Not only did that make all functions now prefix with
    the more reasonable rl prefix, but there is room to add raymath as module raylib5::rm and so on.
  2. Coming from other languages, people often use more namespacing than they need to. It's just more to type, worse readability and makes the language look like C++. It's File f;, not std::io::File f;. The latter is the antithesis of good style. – And same with functions, one level, no more.
  3. If you're making up names of types, you need to ensure it looks unique because if it has to be namespaced then it has failed. So for example SdlMutex, not Mutex. There is an idea that using the module name is good enough to disambiguate types. It isn't, because C3 isn't built to accommodate C++ style.

Final Word

C3 doesn't have aliasing anymore because of a rather complex dynamic, and all the organization of the standard library and such does depend on each other.

Allowing some external mechanism to "hack" a broken module name and allowing it to be swapped for another at the command line is one thing (e.g. you have to use that library which has the raylib prefix then you might use something like --namespace-patch raylib rl at the command line to make it alias to rl instead) – This is a fairly straightforward enhancement.

To allow C2 aliasing though, that would mean rethinking everything about C3 modules, so that would require quite strong reasons for it to happen. But I prefer to never say never, I just want the language to be as good as possible.

Bonus: why doesn't C3 use .?

The short answer is that :: makes it possible to do the path shortening. Otherwise figuring out where the path starts would need to have a very complicated heuristic. Another thing is that makes sense if your names are things that won't collide with local variables. Now, in C3 we have std::io::file. If C3 used . then something like File file = file.open("foo.txt", "rb") would be ambiguous, whereas with :: we're always fine: File file = file::open("foo.txt", "rb").

(It also saves the grammar from being complicated in some other places, but these are the main reasons).


Comment by Christoffer Lernö

The need to rename a namespace comes from the fact that there are irresolvable namespace collisions in the first place. This solution prevents that from happening.

What would you prefer? That namespace aliases are consistent over all code bases, or that they are only limited by the imagination of the user?

C3 prefers the former while also making sure that there are no irresolvable collisions despite that.

In C3, you cannot have two modules that have the full name net without them being the same module.

Therefore, disambiguation is not a problem the aliasing will resolve.

The way C3 looks for files is different from Odin. C3 takes a list of files, or directories. ALL C3 files in those directories and in those lists of files will be read by the compiler.

If you have libraries, which are basically directories with a small manifest + c3 files + possibly dynamic/static libraries, these are loaded (again completely) if a dependency is declared on the library when you compile.

So for example, you wish to use raylib with a single file you want to compile and run from the command line: c3c compile-run my_program.c3 --lib raylib

Now in addition to that in your program you need to write something like import raylib; to make the functions available to the code in that module.

So the import statements do not control what is compiled, they simply control what modules are visible.


Comment by Christoffer Lernö

The full name being the full module name, such as mylib::net. It is not possible to have two of those from different sources and still consider them distinct. C3 allows extending modules, so they would be seen as different parts of the same module, even if they are placed in different folders.

For C3, it takes a bunch of files of indata. Each of those files may have one or more module sections in them. A module section being a part of a module. To make it more understandable, thing about how C++ namespaces work. You can open the same namespace multiple times.

Similarly in C3 you can open the same module and append functions and types to it multiple times and from multiple files.


Comment by Christoffer Lernö

Well yes. If you had two libraries called io, how would you even be able to tell them apart? What would import io; mean? Importing both?

The only case when it's possible to have two libraries with the same name, is when imports are file based, so you do something like import "foo/io.lib" and import "bar/io.lib".

This is not how the module model works in C3, so the case doesn't even arise. While you could certainly have a foo/io.c3l and bar/io.c3l that you depend on, the way modules work in C3 would merge these modules if you use those libraries. And the reason why they are merged is because C3 has a very open model. It's possible to extend any module and any type.

In the file version we usually HAVE a longer module name in the sense that the path acts as this module name to differentiate it. It's just that it's informal whereas in C3 or something like Java it's formalized.

More splat and bug fixes: C3 0.6.5 is released

Originally from: https://c3.handmade.network/blog/p/8979-more_splat_and_bug_fixes__c3_0.6.5_is_released

0.6.5 is out, and it brings some improvements and a bunch of bug fixes.

Splat in initializers

Splat has gained more and more features the last releases, and this is likely the last one, allowing you to splat into initializers. This means something like this now works:

struct Foo { int a; int b; }

fn void test(int[2] x)
{
  Foo f = { ...x };
}

Improved [] overloading

Using list[i] *= 2; or list[i]++ where "list" was a type with subscript overloading (such as List) was not possible. This now works fine.

Improved Optionals

Previously using a++ where "a" was an optional, had required using the optional result. This is now no longer the case, and

int! a = foo();
a++;

Works fine.

Updated command line options

A "quiet" option using -q is now available to suppress all non-error output. Furthermore the --debug-log and --debug-stats have been replaced by -v, -vv and -vvv for increasing verbosity.

Stdlib changes

Various fixes are in the 0.6.5, as well as a more convenient API when doing base32/64 encoding. Other small quality-of-life additions such as file::save have found its way into the standard library as well.

What's next

Looking towards the 0.7.0 release, there might be a breaking change in how pointer subscripting works.

Currently, given int[5]* a; doing a[0] will subscript the pointer, returning an int[5] value. This is often not what one desires and creates a fair amount of confusion. For this reason, subscripting pointers might be disallowed, making pointers to arrays implicitly dereference the pointer (so a[0] in our example would implicitly be (*a)[0]) with *(pointer + i) still working as usual. The future will tell how this should be tackled in C3.

Here is the full change list:

0.6.5 Change list

Changes / improvements

  • Allow splat in initializers.
  • Init command will now add test-sources to project.json #1520
  • a++ may be discarded if a is optional and ++/-- works for overloaded operators.
  • Improve support for Windows cross compilation on targets with case sensitive file systems.
  • Add "sources" support to library manifest.json, defaults to root folder if unspecified.
  • Add char_at method in DString and operators [], len, []= and &[].
  • Add -q option, make --run-once implicitly -q.
  • Add -v, -vv and -vvv options for increasing verbosity, replacing debug-log and debug-stats options.

Fixes

  • Fix bug where a > 0 ? f() : g() could cause a compiler crash if both returned void!.
  • @builtin was not respected for generic modules #1617.
  • Fix issue writing a single byte in the WriteBuffer
  • A distinct inline pointer type can now participate in pointer arithmetics.
  • Support &a[0] returning the distinct type when applying it to a distinct of a pointer.
  • Fix error when calling HashMap.remove on uninitialized HashMap.
  • Fix issue with resolved try-unwrap in defer.
  • Fix issue with overloaded subscript and ++/-- and assign ops (e.g. *=)
  • Fix issue with properties in different targets not being respected #1633.
  • Indexing an Optional slice would crash in codegen #1636.
  • SimpleHeapAllocator bug when splitting blocks allowed memory overrun.
  • Not possible to alias or take reference for extension methods on non-user defined types. #1637
  • Prevent methods from using names of properties or fields. #1638
  • b64 / hex data strings can now be used with ` as well.
  • Contracts on generic modules would evaluate too late, sometimes not catching the error until it already occurred elsewhere.
  • Fix bug preventing optionals from being used in ranges or as indices.
  • Crash compiling for arm64 when returning 16 byte and smaller structs by value not a power of 2 #1649.
  • Enforce single module compilation for static libraries to make constructors run properly.
  • Crash when using --no-obj without compile-only. #1653
  • Do not produce expression locations for windows.
  • Issue where multiple methods were accepted for the same type.
  • Issue where a method was linked to a type alias instead of the underlying type.
  • Fix Fnv1a encoding.
  • Fix issue with accessing arrays in access-overloaded types, e.g. list[1][2] #1665.
  • Cast removing arbitrary array indices and converting them to pointers should always be fine #1664
  • Incorrect "no-libc" definition of cos, making it unavailable for wasm.
  • Fix issue with the adjoint and inverse calculations for Matrix2x2.
  • It was possible to create 0 length arrays using byte literals. #1678
  • Crash when a constant null typeid is checked for properties. #1679

Stdlib changes

  • Add io::MultiReader, io::MultiWriter, and io::TeeReader structs.
  • Updated Base32 API.
  • Add file::save.
  • Add memcpy / memset / memcmp to nolibc.
  • Add sort::quickselect to find the k-th smallest element in an unordered list.
  • Add sort::is_sorted to determine if a list is sorted.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

Comments


Comment by Christoffer Lernö

0.6.5 is out, and it brings some improvements and a bunch of bug fixes.

Splat in initializers

Splat has gained more and more features the last releases, and this is likely the last one, allowing you to splat into initializers. This means something like this now works:

struct Foo { int a; int b; }

fn void test(int[2] x)
{
  Foo f = { ...x };
}

Improved [] overloading

Using list[i] *= 2; or list[i]++ where "list" was a type with subscript overloading (such as List) was not possible. This now works fine.

Improved Optionals

Previously using a++ where "a" was an optional, had required using the optional result. This is now no longer the case, and

int! a = foo();
a++;

Works fine.

Updated command line options

A "quiet" option using -q is now available to suppress all non-error output. Furthermore the --debug-log and --debug-stats have been replaced by -v, -vv and -vvv for increasing verbosity.

Stdlib changes

Various fixes are in the 0.6.5, as well as a more convenient API when doing base32/64 encoding. Other small quality-of-life additions such as file::save have found its way into the standard library as well.

What's next

Looking towards the 0.7.0 release, there might be a breaking change in how pointer subscripting works.

Currently, given int[5]* a; doing a[0] will subscript the pointer, returning an int[5] value. This is often not what one desires and creates a fair amount of confusion. For this reason, subscripting pointers might be disallowed, making pointers to arrays implicitly dereference the pointer (so a[0] in our example would implicitly be (*a)[0]) with *(pointer + i) still working as usual. The future will tell how this should be tackled in C3.

Here is the full change list:

0.6.5 Change list

Changes / improvements

  • Allow splat in initializers.
  • Init command will now add test-sources to project.json #1520
  • a++ may be discarded if a is optional and ++/-- works for overloaded operators.
  • Improve support for Windows cross compilation on targets with case sensitive file systems.
  • Add "sources" support to library manifest.json, defaults to root folder if unspecified.
  • Add char_at method in DString and operators [], len, []= and &[].
  • Add -q option, make --run-once implicitly -q.
  • Add -v, -vv and -vvv options for increasing verbosity, replacing debug-log and debug-stats options.

Fixes

  • Fix bug where a > 0 ? f() : g() could cause a compiler crash if both returned void!.
  • @builtin was not respected for generic modules #1617.
  • Fix issue writing a single byte in the WriteBuffer
  • A distinct inline pointer type can now participate in pointer arithmetics.
  • Support &a[0] returning the distinct type when applying it to a distinct of a pointer.
  • Fix error when calling HashMap.remove on uninitialized HashMap.
  • Fix issue with resolved try-unwrap in defer.
  • Fix issue with overloaded subscript and ++/-- and assign ops (e.g. *=)
  • Fix issue with properties in different targets not being respected #1633.
  • Indexing an Optional slice would crash in codegen #1636.
  • SimpleHeapAllocator bug when splitting blocks allowed memory overrun.
  • Not possible to alias or take reference for extension methods on non-user defined types. #1637
  • Prevent methods from using names of properties or fields. #1638
  • b64 / hex data strings can now be used with ` as well.
  • Contracts on generic modules would evaluate too late, sometimes not catching the error until it already occurred elsewhere.
  • Fix bug preventing optionals from being used in ranges or as indices.
  • Crash compiling for arm64 when returning 16 byte and smaller structs by value not a power of 2 #1649.
  • Enforce single module compilation for static libraries to make constructors run properly.
  • Crash when using --no-obj without compile-only. #1653
  • Do not produce expression locations for windows.
  • Issue where multiple methods were accepted for the same type.
  • Issue where a method was linked to a type alias instead of the underlying type.
  • Fix Fnv1a encoding.
  • Fix issue with accessing arrays in access-overloaded types, e.g. list[1][2] #1665.
  • Cast removing arbitrary array indices and converting them to pointers should always be fine #1664
  • Incorrect "no-libc" definition of cos, making it unavailable for wasm.
  • Fix issue with the adjoint and inverse calculations for Matrix2x2.
  • It was possible to create 0 length arrays using byte literals. #1678
  • Crash when a constant null typeid is checked for properties. #1679

Stdlib changes

  • Add io::MultiReader, io::MultiWriter, and io::TeeReader structs.
  • Updated Base32 API.
  • Add file::save.
  • Add memcpy / memset / memcmp to nolibc.
  • Add sort::quickselect to find the k-th smallest element in an unordered list.
  • Add sort::is_sorted to determine if a list is sorted.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

New contract syntax with 0.6.4

Originally from: https://c3.handmade.network/blog/p/8965-new_contract_syntax_with_0.6.4

Finally 0.6.4 is out. This version has some very important improvements working with constant slices. It is also more permissive when it comes to working with generic types.

But perhaps the change which is the most obvious is the introduction of a new contract syntax using <* *>, away from the Javadoc-like /** */ which was hard to differentiate from comments.

Constant slices

Constant slices has gotten a lot of low in 0.6.4. Constant vectors can now convert into constant slices, and slicing arrays, bytes and constant slices are now compile time operations.

This also adds constant bytes to/from char[] conversions.

New contract syntax

The use of /** */ for declaring a doc/contract block has been around for a long time in C3.

Unfortunately it has a glaring problem: if you accidentally forget a * and write /* */ your docs are considered comments and are silently ignored. This is obviously very bad.

Another problem would be code where the user accidentally typed /** */ for comments inside a function or similar. This was very hard to give some proper errors for without complicating parsing.

By picking a different syntax - <* *>, these problems go away. They also address some of the criticisms against using doc-comments for contracts.

Interface/any changes

Previously it was possible to assign a void* to an interface or any. This led to the weird situation of the type inside being void, which is a nonsense value and required special handling for it to work properly in almost all code. This is no longer allowed.

As an improvement interfaces now support .ptr and .type directly without casting to an any.

Ternary

Inference now works across ternary, so for example: ZString a = b > 0 ? "x" : "y"; would do the right thing.

0.6.4 also fixes a crash when returning a struct or vector from a ternary expression.

Weakly linking the standard library

The standard library is now weakly linked, which allows multiple copies of the same standard library to work when linked together. Otherwise things like using a static library written in C3 would have problems linking to a C3 program.

What's next?

0.6.5 will clean up the CLI, tests and overall improvements. While tagged unions are discussed, it's unclear whether they will be considered for the next release.

A problem which keeps popping up is the packaged LLVM libraries. These are frequently broken causing unnecessary work for everyone. Currently the Windows libraries are precompiled by a separate CI. This will probably be the way forward for all platforms, and that work will probably go into the 0.6.5 release cycle as well.

0.6.4 Change list

There are many other fixes and changes, see the full changelist:

Changes / improvements

  • Const vector -> const slice implicit conversion.
  • Slicing arrays, slices and bytes at compile time #1466.
  • Better error for int a[4] = .... #1518
  • Better error for int Foo(int a) declarations #1516
  • Improve error message in the case of MyInterface x = foo; #1522
  • Deprecate @adhoc, allow non-nested ad hoc generic types.
  • Constant bytes <=> char[] conversion should work #1514.
  • Infer now works across ternary.
  • Interfaces now support .ptr and .type directly without casting to any.
  • Switch to <* *> docs.
  • Improve error messages on expressions like var $type = int; #1553.
  • Disallow casting a void* to any or an interface, unless it is null.
  • Defer resolution of declarations when looked up in def aliased #1559.
  • Adding constants to the Json AST #1540
  • Adding info to the globals inside Json AST #1541
  • Null-check function pointer invocation #1573.
  • string::new_struct_to_str and io::struct_to_format to dump struct data.
  • io::print will now print structs.
  • Improve error message when using void aliases as variable storage type.
  • Add a target type: "prepare" which doesn't compile anything (but may run exec)

Fixes

  • Unsupported int[*] $x = { 1, 2, 3, 4 } #1489.
  • Unexpected compile error using a typed constant with copysign #1517
  • Incorrect subscript resolution #1519.
  • Segfault with passing a program with - using stdin.
  • Using no module with - would reject the program.
  • Unintended deref of pointers with methods caused regression with hash function.
  • Fix broken sincos function.
  • Bug when a continue is copied in a defer.
  • Compiler error when any/interface initialized using {} #1533.
  • Bug when defers and $if were combined in a macro, which would cause miscompilation.
  • Fixes to the CSV reader.
  • Crash returning struct or vector from function using ternary expression #1537.
  • Improved error message on invalid subscript index type #1535.
  • Improved error message when declaring a variable void!.
  • Cannot use void as a generic parameter #1546
  • Interfaces not correctly copied with generics #1545
  • Memory leak in keys.new_list fixed.
  • Standard library is now correctly weakly linked, fixing the use of C3 .so together with executable. #1549, #1107.
  • Wrong error message for interface methods with body #1536.
  • Empty expression block would crash compiler with debug on #1554.
  • Improve infer conversions on constants, e.g. ZString a = foo ? "a" : "b"; #1561
  • Show error when declarations do not start with fn in interfaces. #1565
  • if (try foo) was handled incorrectly inside a defer.
  • &self argument not implicitly null checked. #1556.
  • (uptr)&((Foo*)null).a incorrectly inserts a null check. #1544
  • Incorrect error message when $eval is provided an invalid string. #1570
  • HashMap.copy_keys did not properly copy keys which needed to be allocated #1569
  • Named vector component access would not fold at compile time. #1574
  • $define would occasionally not properly evaluate declarations it encountered.
  • Fixes with error handling recursive @tag #1583.
  • Sometimes generating introspection info would not be in the global scope causing a crash #1586.
  • @tag on macros cannot be retrieved with tagof #1582
  • Taking the $typeof of a wildcard optional returns void!.

Stdlib changes

  • Remove unintended print of char[] as String
  • Add read/write to stream with big endian ints.
  • Move accidently hidden "wrap_bytes".
  • Added CBool #1530.
  • Added encoding/base32 module.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

Comments


Comment by Christoffer Lernö

Finally 0.6.4 is out. This version has some very important improvements working with constant slices. It is also more permissive when it comes to working with generic types.

But perhaps the change which is the most obvious is the introduction of a new contract syntax using <* *>, away from the Javadoc-like /** */ which was hard to differentiate from comments.

Constant slices

Constant slices has gotten a lot of low in 0.6.4. Constant vectors can now convert into constant slices, and slicing arrays, bytes and constant slices are now compile time operations.

This also adds constant bytes to/from char[] conversions.

New contract syntax

The use of /** */ for declaring a doc/contract block has been around for a long time in C3.

Unfortunately it has a glaring problem: if you accidentally forget a * and write /* */ your docs are considered comments and are silently ignored. This is obviously very bad.

Another problem would be code where the user accidentally typed /** */ for comments inside a function or similar. This was very hard to give some proper errors for without complicating parsing.

By picking a different syntax - <* *>, these problems go away. They also address some of the criticisms against using doc-comments for contracts.

Interface/any changes

Previously it was possible to assign a void* to an interface or any. This led to the weird situation of the type inside being void, which is a nonsense value and required special handling for it to work properly in almost all code. This is no longer allowed.

As an improvement interfaces now support .ptr and .type directly without casting to an any.

Ternary

Inference now works across ternary, so for example: ZString a = b > 0 ? "x" : "y"; would do the right thing.

0.6.4 also fixes a crash when returning a struct or vector from a ternary expression.

Weakly linking the standard library

The standard library is now weakly linked, which allows multiple copies of the same standard library to work when linked together. Otherwise things like using a static library written in C3 would have problems linking to a C3 program.

What's next?

0.6.5 will clean up the CLI, tests and overall improvements. While tagged unions are discussed, it's unclear whether they will be considered for the next release.

A problem which keeps popping up is the packaged LLVM libraries. These are frequently broken causing unnecessary work for everyone. Currently the Windows libraries are precompiled by a separate CI. This will probably be the way forward for all platforms, and that work will probably go into the 0.6.5 release cycle as well.

0.6.4 Change list

There are many other fixes and changes, see the full changelist:

Changes / improvements

  • Const vector -> const slice implicit conversion.
  • Slicing arrays, slices and bytes at compile time #1466.
  • Better error for int a[4] = .... #1518
  • Better error for int Foo(int a) declarations #1516
  • Improve error message in the case of MyInterface x = foo; #1522
  • Deprecate @adhoc, allow non-nested ad hoc generic types.
  • Constant bytes <=> char[] conversion should work #1514.
  • Infer now works across ternary.
  • Interfaces now support .ptr and .type directly without casting to any.
  • Switch to <* *> docs.
  • Improve error messages on expressions like var $type = int; #1553.
  • Disallow casting a void* to any or an interface, unless it is null.
  • Defer resolution of declarations when looked up in def aliased #1559.
  • Adding constants to the Json AST #1540
  • Adding info to the globals inside Json AST #1541
  • Null-check function pointer invocation #1573.
  • string::new_struct_to_str and io::struct_to_format to dump struct data.
  • io::print will now print structs.
  • Improve error message when using void aliases as variable storage type.
  • Add a target type: "prepare" which doesn't compile anything (but may run exec)

Fixes

  • Unsupported int[*] $x = { 1, 2, 3, 4 } #1489.
  • Unexpected compile error using a typed constant with copysign #1517
  • Incorrect subscript resolution #1519.
  • Segfault with passing a program with - using stdin.
  • Using no module with - would reject the program.
  • Unintended deref of pointers with methods caused regression with hash function.
  • Fix broken sincos function.
  • Bug when a continue is copied in a defer.
  • Compiler error when any/interface initialized using {} #1533.
  • Bug when defers and $if were combined in a macro, which would cause miscompilation.
  • Fixes to the CSV reader.
  • Crash returning struct or vector from function using ternary expression #1537.
  • Improved error message on invalid subscript index type #1535.
  • Improved error message when declaring a variable void!.
  • Cannot use void as a generic parameter #1546
  • Interfaces not correctly copied with generics #1545
  • Memory leak in keys.new_list fixed.
  • Standard library is now correctly weakly linked, fixing the use of C3 .so together with executable. #1549, #1107.
  • Wrong error message for interface methods with body #1536.
  • Empty expression block would crash compiler with debug on #1554.
  • Improve infer conversions on constants, e.g. ZString a = foo ? "a" : "b"; #1561
  • Show error when declarations do not start with fn in interfaces. #1565
  • if (try foo) was handled incorrectly inside a defer.
  • &self argument not implicitly null checked. #1556.
  • (uptr)&((Foo*)null).a incorrectly inserts a null check. #1544
  • Incorrect error message when $eval is provided an invalid string. #1570
  • HashMap.copy_keys did not properly copy keys which needed to be allocated #1569
  • Named vector component access would not fold at compile time. #1574
  • $define would occasionally not properly evaluate declarations it encountered.
  • Fixes with error handling recursive @tag #1583.
  • Sometimes generating introspection info would not be in the global scope causing a crash #1586.
  • @tag on macros cannot be retrieved with tagof #1582
  • Taking the $typeof of a wildcard optional returns void!.

Stdlib changes

  • Remove unintended print of char[] as String
  • Add read/write to stream with big endian ints.
  • Move accidently hidden "wrap_bytes".
  • Added CBool #1530.
  • Added encoding/base32 module.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

Welcome to C3 version 0.6.3

Originally from: https://c3.handmade.network/blog/p/8957-welcome_to_c3_version_0.6.3

0.6.3 is out and brings some nice improvements and changes together with bug fixes and standard library additions.

Some highlights:

New named argument syntax

The old initializer list style named arguments (.arg = value) is deprecated and replaced by a arg: value syntax which feels more natural for function invocation. So foo(.some_arg = 23, .other_arg = b) should now be foo(some_arg: 23, other_arg: b). The old syntax will be removed in 0.7.

Slicing of compile time values

C3 now allows slicing of untyped lists and strings. The upcoming 0.6.4 will further support compile time slicing on all compile time arrays, but in 0.6.3 this is still somewhat limited.

Splat everywhere

Ever had an int array and wanted to unpack it into a function call? Now you can do that with ...:

int[2] x = some_call();
foo(...x); // Same as `foo(x[0], x[1]);`

Some limitations apply: when splatting a slice into a normal argument slots the slice length needs to be known at compile time (although it's straightforward to circumvent if needed).

Environment variables for configurability

The environment variable C3C_CC is introduced to allow overriding the default C compiler chosen when compiling C sources as part of the compilation proces, and C3C_LIB allows pointing to a custom default standard library location.

Ad hoc generic type definition changes

Ad hoc generic type definitions, e.g. List(<int>) x; have been deprecated for use outside of type declarations and macros. Only generic types marked @adhoc are now valid for ad hoc declarations, examples of such "adhoc enabled" types is the Maybe type.

This only applies to types. Functions and constants can be all be used ad-hoc style, e.g. foo::some_function(<int>)(123).

$ and # "self" arguments for macro methods

It's now possible to use constant or unevaluated expressions for the "self" parameter in macro methods. This opens up for some useful functionality when using reflection data.

Extended "vendor-fetch"

A project-fetch project function has been added to download missing dependencies for projects into the correct directory, and c3c vendor-fetch can now be used to add and download a new project dependency with a single command.

Wasm improvements

For Wasm, wasm32 / wasm64 targets no longer need to specify use-libc=no as they're assumed to be freestanding by default.

In addition to this, there is now support for Wasm modules: The @wasm attribute now accepts one or two arguments - with two arguments, the first is then assumed to be the module name.

Standard library additions

As usual the standard library got a few additions. In 0.6.3 HashMap has more convenient init functions, the standard random has more variants of random out of the box, cryptographic MD5 and SHA256 hashes were added, as well as HMAC and PBKDF2 support. Datetime has more timezone-related functions and DString received some very useful insert methods.

What's next?

0.6.4 will get complete support for compile time slices, and beyond that hopefully further refinement of the standard library.

For more information about the release, see the full change list:

0.6.3 Change list

Changes / improvements

  • Introduce arg: x named arguments instead of .arg = x, deprecate old style.
  • Support splat for varargs #1352.
  • Allow var in lambdas in macros.
  • Support int[*] { 1, 2, 3 } expressions.
  • Support inline struct designated init as if inline was anonymous.
  • Introduce the .paramsof property.
  • Support environment variable 'C3C_LIB' to find the standard library.
  • Support environment variable 'C3C_CC' to find the default C compiler.
  • Support casting bitstructs to bool.
  • Allow user-defined attributes to have typed parameters.
  • Add .gitkeep files to project subfolders.
  • Add env::COMPILER_BUILD_HASH and env::COMPILER_BUILD_DATE
  • Support linking .o files in compilation command. #1417
  • Slicing constant strings at compile time works.
  • Add project fetch subcommand to fetch missing project dependencies (general and target specific)
  • Ability of vendor-fetch to download the dependencies in the first specified path dependencies-search-path
  • Ability of vendor-fetch to register the fetched dependencies in the project file.
  • Allow the "self" parameter to be $/# for macro methods.
  • Support compile time slicing of untyped lists.
  • Allow specifying an import module using @wasm #1305.
  • Deprecated inline generic types outside of struct definitions and macros unless marked @adhoc.
  • Improved method detection in earlier stages of checking.
  • Allow @norecurse attribute for non-recursive imports #1480.
  • wasm32 / wasm64 targets are use-libc=no by default.

Fixes

  • Issue where a lambda wasn't correctly registered as external. #1408
  • Generic methods were incorrectly registered as functions, leading to naming collisions. #1402
  • Deprecated tuple / triple types.
  • Converting a slice to a vector/array would copy too little data.
  • Crash when reading an empty 'manifest.json'.
  • "optsize" did not work correctly in project.json.
  • l[0].a = 1 now supported for overloads due to better lvalue handling #1357.
  • Asserts are retained regardless of optimization when running tests.
  • Limit object filename lengths. #1415
  • Fix regression for $include.
  • Correct '.so' suffix on dynamic libraries on Linux.
  • Fix bug where inline index access to array in a struct would crash the compiler.
  • Asserts are now correctly included and traced in when running tests.
  • Use atexit to fix finalizers on Windows #1361.
  • Fix bugs in "trap-on-wrap" #1434.
  • Bug with casting anyfault to error.
  • Lambda / function type would accidentally be processed as a method.
  • Fix error message when not finding a particular function.
  • Crash invoking a @body argument with the wrong number of parameters.
  • Fix reordering semantics in struct assignment.
  • Regression when passing types as #expr arguments. #1461
  • Temp allocator overwrites data when doing reset on extra allocated pages. #1462
  • User defined attributes could not have more than 1 parameter due to bug.
  • Folding a constant array of structs at compile time would cause an assert.
  • Enum attributes would be overwritten by enum value attributes.
  • LLVM issue with try when bool is combined #1467.
  • Segfault using ternary with no assignment #1468.
  • Inner types make some errors misleading #1471.
  • Fix bug when passing a type as a compile time value.
  • Fix bug due to enum associated values not being checked for liveness.
  • Regression when compile time accessing a union field not last assigned to.
  • Safer seed of rand() for WASM without libc.
  • Bad error message aliasing an ident with a path. #1481.
  • Error when slicing a struct with an inline array #1488.
  • Improved error messages on Foo a = foo { 1 }; #1496
  • Bug in json decoder escape handling.
  • Fix bug when reading zip manifest, that would not return a zero terminated string. #1490
  • Fix thread tests.
  • Detect recursion errors on non-recursive mutexes in safe mode.
  • Foreach over distinct pointer failed to be caught as error #1506.
  • Foreach over distinct iterable would ignore operator(len).
  • Compiler crash when compiling c code in a library without --obj-out #1503.

Stdlib changes

  • Additional init functions for hashmap.
  • format functions are now functions and work better with splat.
  • Add support for the QOI format.
  • Add io::read_new_fully for reading to the end of a stream.
  • Add io::wrap_bytes for reading bytes with io functions.
  • Add rnd and rand_in_range default random functions.
  • Additional timezone related functions for datetime.
  • Added MD5 and crypto::safe_compare.
  • Added generic HMAC.
  • Added generic PBKDF2 implementation.
  • DString reverse.
  • DString.insert_at now has variants for other types.
  • Added sha256.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

Comments


Comment by Christoffer Lernö

0.6.3 is out and brings some nice improvements and changes together with bug fixes and standard library additions.

Some highlights:

New named argument syntax

The old initializer list style named arguments (.arg = value) is deprecated and replaced by a arg: value syntax which feels more natural for function invocation. So foo(.some_arg = 23, .other_arg = b) should now be foo(some_arg: 23, other_arg: b). The old syntax will be removed in 0.7.

Slicing of compile time values

C3 now allows slicing of untyped lists and strings. The upcoming 0.6.4 will further support compile time slicing on all compile time arrays, but in 0.6.3 this is still somewhat limited.

Splat everywhere

Ever had an int array and wanted to unpack it into a function call? Now you can do that with ...:

int[2] x = some_call();
foo(...x); // Same as `foo(x[0], x[1]);`

Some limitations apply: when splatting a slice into a normal argument slots the slice length needs to be known at compile time (although it's straightforward to circumvent if needed).

Environment variables for configurability

The environment variable C3C_CC is introduced to allow overriding the default C compiler chosen when compiling C sources as part of the compilation proces, and C3C_LIB allows pointing to a custom default standard library location.

Ad hoc generic type definition changes

Ad hoc generic type definitions, e.g. List(<int>) x; have been deprecated for use outside of type declarations and macros. Only generic types marked @adhoc are now valid for ad hoc declarations, examples of such "adhoc enabled" types is the Maybe type.

This only applies to types. Functions and constants can be all be used ad-hoc style, e.g. foo::some_function(<int>)(123).

$ and # "self" arguments for macro methods

It's now possible to use constant or unevaluated expressions for the "self" parameter in macro methods. This opens up for some useful functionality when using reflection data.

Extended "vendor-fetch"

A project-fetch project function has been added to download missing dependencies for projects into the correct directory, and c3c vendor-fetch can now be used to add and download a new project dependency with a single command.

Wasm improvements

For Wasm, wasm32 / wasm64 targets no longer need to specify use-libc=no as they're assumed to be freestanding by default.

In addition to this, there is now support for Wasm modules: The @wasm attribute now accepts one or two arguments - with two arguments, the first is then assumed to be the module name.

Standard library additions

As usual the standard library got a few additions. In 0.6.3 HashMap has more convenient init functions, the standard random has more variants of random out of the box, cryptographic MD5 and SHA256 hashes were added, as well as HMAC and PBKDF2 support. Datetime has more timezone-related functions and DString received some very useful insert methods.

What's next?

0.6.4 will get complete support for compile time slices, and beyond that hopefully further refinement of the standard library.

For more information about the release, see the full change list:

0.6.3 Change list

Changes / improvements

  • Introduce arg: x named arguments instead of .arg = x, deprecate old style.
  • Support splat for varargs #1352.
  • Allow var in lambdas in macros.
  • Support int[*] { 1, 2, 3 } expressions.
  • Support inline struct designated init as if inline was anonymous.
  • Introduce the .paramsof property.
  • Support environment variable 'C3C_LIB' to find the standard library.
  • Support environment variable 'C3C_CC' to find the default C compiler.
  • Support casting bitstructs to bool.
  • Allow user-defined attributes to have typed parameters.
  • Add .gitkeep files to project subfolders.
  • Add env::COMPILER_BUILD_HASH and env::COMPILER_BUILD_DATE
  • Support linking .o files in compilation command. #1417
  • Slicing constant strings at compile time works.
  • Add project fetch subcommand to fetch missing project dependencies (general and target specific)
  • Ability of vendor-fetch to download the dependencies in the first specified path dependencies-search-path
  • Ability of vendor-fetch to register the fetched dependencies in the project file.
  • Allow the "self" parameter to be $/# for macro methods.
  • Support compile time slicing of untyped lists.
  • Allow specifying an import module using @wasm #1305.
  • Deprecated inline generic types outside of struct definitions and macros unless marked @adhoc.
  • Improved method detection in earlier stages of checking.
  • Allow @norecurse attribute for non-recursive imports #1480.
  • wasm32 / wasm64 targets are use-libc=no by default.

Fixes

  • Issue where a lambda wasn't correctly registered as external. #1408
  • Generic methods were incorrectly registered as functions, leading to naming collisions. #1402
  • Deprecated tuple / triple types.
  • Converting a slice to a vector/array would copy too little data.
  • Crash when reading an empty 'manifest.json'.
  • "optsize" did not work correctly in project.json.
  • l[0].a = 1 now supported for overloads due to better lvalue handling #1357.
  • Asserts are retained regardless of optimization when running tests.
  • Limit object filename lengths. #1415
  • Fix regression for $include.
  • Correct '.so' suffix on dynamic libraries on Linux.
  • Fix bug where inline index access to array in a struct would crash the compiler.
  • Asserts are now correctly included and traced in when running tests.
  • Use atexit to fix finalizers on Windows #1361.
  • Fix bugs in "trap-on-wrap" #1434.
  • Bug with casting anyfault to error.
  • Lambda / function type would accidentally be processed as a method.
  • Fix error message when not finding a particular function.
  • Crash invoking a @body argument with the wrong number of parameters.
  • Fix reordering semantics in struct assignment.
  • Regression when passing types as #expr arguments. #1461
  • Temp allocator overwrites data when doing reset on extra allocated pages. #1462
  • User defined attributes could not have more than 1 parameter due to bug.
  • Folding a constant array of structs at compile time would cause an assert.
  • Enum attributes would be overwritten by enum value attributes.
  • LLVM issue with try when bool is combined #1467.
  • Segfault using ternary with no assignment #1468.
  • Inner types make some errors misleading #1471.
  • Fix bug when passing a type as a compile time value.
  • Fix bug due to enum associated values not being checked for liveness.
  • Regression when compile time accessing a union field not last assigned to.
  • Safer seed of rand() for WASM without libc.
  • Bad error message aliasing an ident with a path. #1481.
  • Error when slicing a struct with an inline array #1488.
  • Improved error messages on Foo a = foo { 1 }; #1496
  • Bug in json decoder escape handling.
  • Fix bug when reading zip manifest, that would not return a zero terminated string. #1490
  • Fix thread tests.
  • Detect recursion errors on non-recursive mutexes in safe mode.
  • Foreach over distinct pointer failed to be caught as error #1506.
  • Foreach over distinct iterable would ignore operator(len).
  • Compiler crash when compiling c code in a library without --obj-out #1503.

Stdlib changes

  • Additional init functions for hashmap.
  • format functions are now functions and work better with splat.
  • Add support for the QOI format.
  • Add io::read_new_fully for reading to the end of a stream.
  • Add io::wrap_bytes for reading bytes with io functions.
  • Add rnd and rand_in_range default random functions.
  • Additional timezone related functions for datetime.
  • Added MD5 and crypto::safe_compare.
  • Added generic HMAC.
  • Added generic PBKDF2 implementation.
  • DString reverse.
  • DString.insert_at now has variants for other types.
  • Added sha256.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

A big fat release: C3 0.6.2

Originally from: https://c3.handmade.network/blog/p/8953-a_big_fat_release__c3_0.6.2

The 0.6.2 release is here, and a lot has happened since 0.6.1.

Mainly it's that C3 has seen a lot of new users which have explored the corners of the compiler and also filed request for improvements.

Let's start with the language additions:

&&& and ||| operators

0.5.0 introduced the compile time $and and $or functions. These worked at compile time and would not even type check the right hand side if the left hand side was invalid. So for example $and(false, a < 1.0) would be accepted even if a wasn't in the current scope.

It felt like this feature should be more part of the language proper, so two new operators were added: &&& and ||| to replace $and and $or respectively.

They were chosen for being visually distinct. Other alternatives, such as $& were discarded for not being sufficiently easy to spot.

The +++ operator

Somewhat on trial is the +++ operator. Similar to &&& and ||| it replaces a compile time function – or actually two: $concat and $append. We can for example write "name:" +++ Foo.nameof to get "name:Foo" at compile time.

The operator is slightly controversial and there will be trials with other syntax alternatives, but for now - feel welcome to try it out.

New attributes: @const, @noalias and @tag

A macro with only compile time constructs will be folded at compile time, but until now there's not been a simple way to check it. The @const attribute in 0.6.2 can be added to a macro to give an error if it's not completely compile time folded.

There is also the experimental @noalias attribute to put on parameters. As might be guessed, this indicates to the compiler that the parameter will not alias with any other pointer, similar to C's restrict.

Finally there is the @tag attribute. It allows you to add a key-value pair to any declaration, which later can be checked with .tagof and .has_tagof compile time methods.

This can be used to add arbitrary metadata to types.

Compile time additions

Aside from .tagof and .has_tagof mentioned above, there is .methodsof to get methods associated with a type, and member reflection types has a .get method to generate runtime getters: $member.get(value). And finally is_substruct to check if a struct has an inline member.

RISCV ASM support

Thanks to contributor Chuck Benedict, there is now inline asm for RISCV processors as well!

Language changes

With the addition of new operators, $and, $or, $concat and $append are getting phased out.

$vasplat() also gets a new look: it can now be used directly as $vasplat or ranged using $vasplat[2..3]. This is more consistent with the rest of the language. Similarly $vaarg and others are getting brackets rather than parenthesis: $vaarg[2].

Implicit scalar to vector conversions is also getting a bit less generous. They now only happen with initializers and in binary expressions.

Standard library changes

There has been some added functionality, but mostly tweaks and small additions to existing features.

Fixes

On top of this the 0.6.2 release contains over 50 bug fixes, from standard library bugs to unhandled corner cases in the compiler. This is all thanks to having so many more users.

Where to next?

Language feature wise, 0.6.3 will look at named parameters and allowing more pervasive use of splat (...) in calls.

Other than that, 0.6.3 will try to add more toolchain improvements and better support for other targets than x64 and Aarch64.

Here is the full change list:

Changes / improvements

  • Updated LLVM passes
  • Added is_substruct type property.
  • Scalar -> vector not implicit in call or assign.
  • Added --vector-conv to enable the old scalar->vector conversion behaviour.
  • Added "weak" type aliases def Foo = my_foo::Foo @weak;
  • *-add keys in targets in manifest.json and project.json are deprecated.
  • Made "add" the default for things like sources, dependencies and other keys in project and library files.
  • Give some symbol name suggestions when the path is matched.
  • Don't generate .o files on compile and compile-run if there is no main.
  • c3c init-lib does not create the directory with the .c3l suffix #1253
  • Permit foreach values to be optional.
  • Add --show-backtrace option to disable backtrace for even smaller binary.
  • Untested Xtensa support.
  • && doesn't work correctly with lambdas #1279.
  • Fix incorrect override of optimization levels when using projects.
  • Add experimental @noalias attribute.
  • Add a --run-once option to delete the output file after running it.
  • Add @const attribute for macros, for better error messages with constant macros.
  • Add wincrt setting to libraries.
  • Add +++ &&& ||| as replacement for $concat, $and and $or.
  • Add methodsof to type info for struct, union and bitstruct.
  • Added @tag tagof and has_tagof to user defined types and members.
  • Added c-include-dirs project/manifest setting.
  • The compiler now skips UTF8 BOM.
  • Printable values passed to the Formatter as pointers, will print as if passed by value.
  • Pointers are rendered with "0x" prefix when passed to '%s'.
  • Add temp allocator scribble.
  • Use PIC by default on Linux.
  • $exec may now provide a stdin parameter.
  • Introduce $vaarg[...] syntax and deprecate the old $vaarg(...).
  • Similar change to $vasplat: $vasplat and $vasplat[1..].
  • Add $member.get(value) to replace value.$eval($member.nameof)
  • Improve the error message when the compilation does not produce any files #1390.
  • Add fmod implementation for nolibc.

Fixes

  • Broken WASM library code.
  • Regression: Invalid is_random implementation due to changes in 0.6.
  • dbghelp.lib was linked even on nolibc on Windows.
  • Fix incorrect linker selection on some platforms.
  • Struct members declared in a single line declaration were not sharing attributes. #1266
  • opt project setting now properly documented.
  • Incorrect justify formatting of integers.
  • Assertion with duplicate function pointer signatures #1286
  • Distinct func type would not accept direct function address assign. #1287
  • Distinct inline would not implement protocol if the inlined implemented it. #1292
  • Distinct inline can now be called if it is aliasing a function pointer.
  • Bug in List add_array when reserving memory.
  • Fix issue where a compile time parameter is followed by "...".
  • Fix issue with some conversions to untyped list.
  • Issue where a if (catch e = ...) in a defer would be incorrectly copied. Causing codegen error.
  • Variable in if-try / if-catch cannot be a reused variable name.
  • Vararg interfaces were broken.
  • LLVM codegen for constants in enums could fail.
  • Fixes to the socket functions.
  • Improved output when pointer is out of range.
  • Better error when casting to a distinct fails.
  • With single module, name the .o file after what -o provides. #1306
  • Bitstruct members can now have attributes.
  • % analysis was incorrect for int vectors.
  • When resolving inherited interfaces, the interface type wasn't always resolved.
  • Fix issues when checking methods and interfaces hasn't been resolved yet.
  • Fix Vec2.angle
  • Update to libc::setjmp on Win32, to do no stack unwinding.
  • Recursively follow interfaces when looking up method.
  • Int128 alignment change in LLVM fixed on x64.
  • Fix interface lazy resolution errors.
  • Interface resolution when part of generics #1348.
  • Assert not properly traced #1354.
  • Ordering issues with $include / $exec fixed #1302.
  • Issues with wincrt linking.
  • Debug info with recursive canonical type usage could cause segfault.
  • Missing check on optional left hand side for s.x.
  • Incorrect zero analysis on foo["test"] = {} #1360.
  • Bug converting untyped list #1360.
  • Benchmark / test no longer suppresses debug info. #1364.
  • Bug when compile time subtracting a distinct type.
  • insert_at incorrectly prevented inserts at the end of a list.
  • Fix aligned alloc for Win32 targets.
  • Compiler didn't detect when a module name was used both as a generic and regular module.
  • Assigning a const zero to an aliased distinct caused an error.
  • --path is now properly respected.
  • --test will now provide the full filename and the column.
  • Fix of bug in defer (catch err) with a direct return error.
  • Too restrictive compile time checks for @const.
  • Fixes to wasm nolibc in the standard library.
  • Fixed int128 div/mod.
  • Fix WASM memory init priority.
  • Fix bug with defer (catch err) when used together with regular defer.
  • Methods can now properly be aliased using def #1393.
  • Memory leak in Object when not using temp allocators.
  • Tracking allocator would double the allocations in the report.
  • printf will now show errors in the output when there are errors.
  • Bug where if try would work incorrectly in a macro.
  • Prevent loading / storing large structs with LLVM.

Stdlib changes

  • send and recv added to libc for Posix / Win32.
  • Add support to destroy temp allocators.
  • Deprecated path.append, path.tappend, getcwd, tgetcwd, path.absolute, ls.
  • Deprecated env::get_config_dir, replaced by env::new_get_config_dir.
  • Added path.has_extension, path.new_append, path.temp_append, new_cwd, temp_cwd, path.new_absolute, new_ls, temp_ls.
  • Added dstring.replace
  • New hashmap type, Map
  • Added ElasticArray.
  • Added types::is_signed, types::is_unsigned and types::inner_type.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

Comments


Comment by Christoffer Lernö

The 0.6.2 release is here, and a lot has happened since 0.6.1.

Mainly it's that C3 has seen a lot of new users which have explored the corners of the compiler and also filed request for improvements.

Let's start with the language additions:

&&& and ||| operators

0.5.0 introduced the compile time $and and $or functions. These worked at compile time and would not even type check the right hand side if the left hand side was invalid. So for example $and(false, a < 1.0) would be accepted even if a wasn't in the current scope.

It felt like this feature should be more part of the language proper, so two new operators were added: &&& and ||| to replace $and and $or respectively.

They were chosen for being visually distinct. Other alternatives, such as $& were discarded for not being sufficiently easy to spot.

The +++ operator

Somewhat on trial is the +++ operator. Similar to &&& and ||| it replaces a compile time function – or actually two: $concat and $append. We can for example write "name:" +++ Foo.nameof to get "name:Foo" at compile time.

The operator is slightly controversial and there will be trials with other syntax alternatives, but for now - feel welcome to try it out.

New attributes: @const, @noalias and @tag

A macro with only compile time constructs will be folded at compile time, but until now there's not been a simple way to check it. The @const attribute in 0.6.2 can be added to a macro to give an error if it's not completely compile time folded.

There is also the experimental @noalias attribute to put on parameters. As might be guessed, this indicates to the compiler that the parameter will not alias with any other pointer, similar to C's restrict.

Finally there is the @tag attribute. It allows you to add a key-value pair to any declaration, which later can be checked with .tagof and .has_tagof compile time methods.

This can be used to add arbitrary metadata to types.

Compile time additions

Aside from .tagof and .has_tagof mentioned above, there is .methodsof to get methods associated with a type, and member reflection types has a .get method to generate runtime getters: $member.get(value). And finally is_substruct to check if a struct has an inline member.

RISCV ASM support

Thanks to contributor Chuck Benedict, there is now inline asm for RISCV processors as well!

Language changes

With the addition of new operators, $and, $or, $concat and $append are getting phased out.

$vasplat() also gets a new look: it can now be used directly as $vasplat or ranged using $vasplat[2..3]. This is more consistent with the rest of the language. Similarly $vaarg and others are getting brackets rather than parenthesis: $vaarg[2].

Implicit scalar to vector conversions is also getting a bit less generous. They now only happen with initializers and in binary expressions.

Standard library changes

There has been some added functionality, but mostly tweaks and small additions to existing features.

Fixes

On top of this the 0.6.2 release contains over 50 bug fixes, from standard library bugs to unhandled corner cases in the compiler. This is all thanks to having so many more users.

Where to next?

Language feature wise, 0.6.3 will look at named parameters and allowing more pervasive use of splat (...) in calls.

Other than that, 0.6.3 will try to add more toolchain improvements and better support for other targets than x64 and Aarch64.

Here is the full change list:

Changes / improvements

  • Updated LLVM passes
  • Added is_substruct type property.
  • Scalar -> vector not implicit in call or assign.
  • Added --vector-conv to enable the old scalar->vector conversion behaviour.
  • Added "weak" type aliases def Foo = my_foo::Foo @weak;
  • *-add keys in targets in manifest.json and project.json are deprecated.
  • Made "add" the default for things like sources, dependencies and other keys in project and library files.
  • Give some symbol name suggestions when the path is matched.
  • Don't generate .o files on compile and compile-run if there is no main.
  • c3c init-lib does not create the directory with the .c3l suffix #1253
  • Permit foreach values to be optional.
  • Add --show-backtrace option to disable backtrace for even smaller binary.
  • Untested Xtensa support.
  • && doesn't work correctly with lambdas #1279.
  • Fix incorrect override of optimization levels when using projects.
  • Add experimental @noalias attribute.
  • Add a --run-once option to delete the output file after running it.
  • Add @const attribute for macros, for better error messages with constant macros.
  • Add wincrt setting to libraries.
  • Add +++ &&& ||| as replacement for $concat, $and and $or.
  • Add methodsof to type info for struct, union and bitstruct.
  • Added @tag tagof and has_tagof to user defined types and members.
  • Added c-include-dirs project/manifest setting.
  • The compiler now skips UTF8 BOM.
  • Printable values passed to the Formatter as pointers, will print as if passed by value.
  • Pointers are rendered with "0x" prefix when passed to '%s'.
  • Add temp allocator scribble.
  • Use PIC by default on Linux.
  • $exec may now provide a stdin parameter.
  • Introduce $vaarg[...] syntax and deprecate the old $vaarg(...).
  • Similar change to $vasplat: $vasplat and $vasplat[1..].
  • Add $member.get(value) to replace value.$eval($member.nameof)
  • Improve the error message when the compilation does not produce any files #1390.
  • Add fmod implementation for nolibc.

Fixes

  • Broken WASM library code.
  • Regression: Invalid is_random implementation due to changes in 0.6.
  • dbghelp.lib was linked even on nolibc on Windows.
  • Fix incorrect linker selection on some platforms.
  • Struct members declared in a single line declaration were not sharing attributes. #1266
  • opt project setting now properly documented.
  • Incorrect justify formatting of integers.
  • Assertion with duplicate function pointer signatures #1286
  • Distinct func type would not accept direct function address assign. #1287
  • Distinct inline would not implement protocol if the inlined implemented it. #1292
  • Distinct inline can now be called if it is aliasing a function pointer.
  • Bug in List add_array when reserving memory.
  • Fix issue where a compile time parameter is followed by "...".
  • Fix issue with some conversions to untyped list.
  • Issue where a if (catch e = ...) in a defer would be incorrectly copied. Causing codegen error.
  • Variable in if-try / if-catch cannot be a reused variable name.
  • Vararg interfaces were broken.
  • LLVM codegen for constants in enums could fail.
  • Fixes to the socket functions.
  • Improved output when pointer is out of range.
  • Better error when casting to a distinct fails.
  • With single module, name the .o file after what -o provides. #1306
  • Bitstruct members can now have attributes.
  • % analysis was incorrect for int vectors.
  • When resolving inherited interfaces, the interface type wasn't always resolved.
  • Fix issues when checking methods and interfaces hasn't been resolved yet.
  • Fix Vec2.angle
  • Update to libc::setjmp on Win32, to do no stack unwinding.
  • Recursively follow interfaces when looking up method.
  • Int128 alignment change in LLVM fixed on x64.
  • Fix interface lazy resolution errors.
  • Interface resolution when part of generics #1348.
  • Assert not properly traced #1354.
  • Ordering issues with $include / $exec fixed #1302.
  • Issues with wincrt linking.
  • Debug info with recursive canonical type usage could cause segfault.
  • Missing check on optional left hand side for s.x.
  • Incorrect zero analysis on foo["test"] = {} #1360.
  • Bug converting untyped list #1360.
  • Benchmark / test no longer suppresses debug info. #1364.
  • Bug when compile time subtracting a distinct type.
  • insert_at incorrectly prevented inserts at the end of a list.
  • Fix aligned alloc for Win32 targets.
  • Compiler didn't detect when a module name was used both as a generic and regular module.
  • Assigning a const zero to an aliased distinct caused an error.
  • --path is now properly respected.
  • --test will now provide the full filename and the column.
  • Fix of bug in defer (catch err) with a direct return error.
  • Too restrictive compile time checks for @const.
  • Fixes to wasm nolibc in the standard library.
  • Fixed int128 div/mod.
  • Fix WASM memory init priority.
  • Fix bug with defer (catch err) when used together with regular defer.
  • Methods can now properly be aliased using def #1393.
  • Memory leak in Object when not using temp allocators.
  • Tracking allocator would double the allocations in the report.
  • printf will now show errors in the output when there are errors.
  • Bug where if try would work incorrectly in a macro.
  • Prevent loading / storing large structs with LLVM.

Stdlib changes

  • send and recv added to libc for Posix / Win32.
  • Add support to destroy temp allocators.
  • Deprecated path.append, path.tappend, getcwd, tgetcwd, path.absolute, ls.
  • Deprecated env::get_config_dir, replaced by env::new_get_config_dir.
  • Added path.has_extension, path.new_append, path.temp_append, new_cwd, temp_cwd, path.new_absolute, new_ls, temp_ls.
  • Added dstring.replace
  • New hashmap type, Map
  • Added ElasticArray.
  • Added types::is_signed, types::is_unsigned and types::inner_type.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c


Comment by Christoffer Lernö

No, C3 just supports asm and LLVM IR output on top of the normal binary output.

Fixes and improvements: C3 0.6.1 is here

Originally from: https://c3.handmade.network/blog/p/8934-fixes_and_improvements__c3_0.6.1_is_here

It's time to release 0.6.1. There's been a bit longer to the x.x.1 than for 0.5.0 -> 0.5.1, and as a result there are quite a bit of additions.

Additions first: $append (appending to a compile time array) and $concat (combining two compile time arrays) were added as well as some compile time string macros: @str_hash(...) (hash a string at compile time), @str_upper / @str_lower (convert a compile time string to upper / lower case) and @str_find (find a substring in a string at compile time).

An important addition is @unaligned_load and @unaliged_store for loading/storing data to unaligned pointers.

In general there has been some relaxation constraints for some features. Trailing bodies, for example, may now use all type of macro argument kinds, like type arguments, and compile time folding of constant structs and arrays now work in all cases.

The standard library hasn't seen many changes since 0.6.0, but there have been some small enhancements.

The full changelist:

Changes / improvements

  • Addition of $append and $concat functions.
  • Added $$str_hash, $$str_upper, $$str_lower, $$str_find builtins.
  • Improved error notes when call expressions have errors.
  • Trailing body arguments may now be &ref, #hash, $const and $Type arguments.
  • "panic-msg" setting to suppress panic message output.
  • Require @export functions to have @export types.
  • Disallow leading/trailing/duplicate '_' in module names.
  • Updated mangling.
  • Added $$unaligned_load and $$unaligned_store.
  • --no-headers option to suppress creating headers when generating a library.
  • Support c-file compilation in libraries.
  • Allow using $defined(&a[1]) to check if the operation is supported.
  • Max number of members in a struct is limited to 65535.
  • The maximum number of parameters in a call is now 255, up from 127.
  • Array comparison now uses built-in memcmp on LLVM to enable optimizations.
  • Prevent implicit array casts to pointers with higher alignment #1237.
  • Macro $case statements now pick the first match and does not evaluate the rest.
  • manifest.json is now checked for incorrect keys.
  • Added --list-manifest-properties to list the available properties in manifest.json.
  • Indexing into a constant array / struct now works at compile time.
  • Improved error message when trying user foreach with an untyped list.

Fixes

  • Error with unsigned compare in @ensure when early returning 0 #1207.
  • Prevent Mach-O from removing @init and @dynamic in a more reliable way #1200.
  • Fix of missing copy of parameterized custom attributes.
  • Fixed crash on certain recursive function definitions #1209.
  • Return the typekind "FUNC" for a function pointer.
  • No longer possible to dereference a function pointer.
  • Fix bug with @jump miscompile.
  • Bit negate does implicit integer promotion.
  • Bitstructs, unions and flexible arrays now correctly emitted in headers.
  • Fix distinct inline conversions.
  • Bit negating const zero flags would give an incorrect result.
  • Fix to scalar -> vector conversions.
  • Bug fix for rethrow + defer catch.
  • Wrong size for structs containing overaligned structs #1219
  • $typeof(*x) should be valid when x is an [out] parameter #1226
  • Fix ABI lowering for 128 bit vectors on Linux.
  • Bad error message when using a generic method without generic parameters #1228
  • Private function called from nested macro not visible to linker #1232
  • Bitstructs in structs would not be correctly be handled in some cases.
  • Fix problem where a $$FUNC would return "" when evaluated for a static in a function #1236.
  • ordinal is no longer a valid associated value name for enums.
  • Constants defined by indexing into another constant could fail codegen.
  • Stdlib nolibc code bugs fixed.
  • Regression: duplicate symbols with static variable declared in macro #1248.
  • Unsplat with named parameters was accidentally disallowed.
  • Reference parameter doesn't work with vector subscript #1250.
  • The msvc_sdk script failed to work properly on windows when run in folders with spaces.

Stdlib changes

  • Added remove_first_item remove_last_item and remove_item as aliases for the match functions.
  • Added @str_hash, @str_upper, @str_lower, @str_find compile time macros.
  • Remove "panic" text from unreachable() when safe mode is turned off.
  • Added @unaligned_store and @unaligned_load.
  • Null ZString, DString or pointer prints "(null)" for printf.
  • Updated sorting API.
  • Insertion sort and counting sort added.
  • Added missing mem and mem::allocator functions for aligned allocations.
  • Added new_init_with_array and temp_init_with_array for List.
  • Fixed posix NativeMutex.lock_timeout.
  • Fixed env::ARCH_32_BIT and env::ARCH_64_BIT.
  • Added time::us.

Comments


Comment by Christoffer Lernö

It's time to release 0.6.1. There's been a bit longer to the x.x.1 than for 0.5.0 -> 0.5.1, and as a result there are quite a bit of additions.

Additions first: $append (appending to a compile time array) and $concat (combining two compile time arrays) were added as well as some compile time string macros: @str_hash(...) (hash a string at compile time), @str_upper / @str_lower (convert a compile time string to upper / lower case) and @str_find (find a substring in a string at compile time).

An important addition is @unaligned_load and @unaliged_store for loading/storing data to unaligned pointers.

In general there has been some relaxation constraints for some features. Trailing bodies, for example, may now use all type of macro argument kinds, like type arguments, and compile time folding of constant structs and arrays now work in all cases.

The standard library hasn't seen many changes since 0.6.0, but there have been some small enhancements.

The full changelist:

Changes / improvements

  • Addition of $append and $concat functions.
  • Added $$str_hash, $$str_upper, $$str_lower, $$str_find builtins.
  • Improved error notes when call expressions have errors.
  • Trailing body arguments may now be &ref, #hash, $const and $Type arguments.
  • "panic-msg" setting to suppress panic message output.
  • Require @export functions to have @export types.
  • Disallow leading/trailing/duplicate '_' in module names.
  • Updated mangling.
  • Added $$unaligned_load and $$unaligned_store.
  • --no-headers option to suppress creating headers when generating a library.
  • Support c-file compilation in libraries.
  • Allow using $defined(&a[1]) to check if the operation is supported.
  • Max number of members in a struct is limited to 65535.
  • The maximum number of parameters in a call is now 255, up from 127.
  • Array comparison now uses built-in memcmp on LLVM to enable optimizations.
  • Prevent implicit array casts to pointers with higher alignment #1237.
  • Macro $case statements now pick the first match and does not evaluate the rest.
  • manifest.json is now checked for incorrect keys.
  • Added --list-manifest-properties to list the available properties in manifest.json.
  • Indexing into a constant array / struct now works at compile time.
  • Improved error message when trying user foreach with an untyped list.

Fixes

  • Error with unsigned compare in @ensure when early returning 0 #1207.
  • Prevent Mach-O from removing @init and @dynamic in a more reliable way #1200.
  • Fix of missing copy of parameterized custom attributes.
  • Fixed crash on certain recursive function definitions #1209.
  • Return the typekind "FUNC" for a function pointer.
  • No longer possible to dereference a function pointer.
  • Fix bug with @jump miscompile.
  • Bit negate does implicit integer promotion.
  • Bitstructs, unions and flexible arrays now correctly emitted in headers.
  • Fix distinct inline conversions.
  • Bit negating const zero flags would give an incorrect result.
  • Fix to scalar -> vector conversions.
  • Bug fix for rethrow + defer catch.
  • Wrong size for structs containing overaligned structs #1219
  • $typeof(*x) should be valid when x is an [out] parameter #1226
  • Fix ABI lowering for 128 bit vectors on Linux.
  • Bad error message when using a generic method without generic parameters #1228
  • Private function called from nested macro not visible to linker #1232
  • Bitstructs in structs would not be correctly be handled in some cases.
  • Fix problem where a $$FUNC would return "" when evaluated for a static in a function #1236.
  • ordinal is no longer a valid associated value name for enums.
  • Constants defined by indexing into another constant could fail codegen.
  • Stdlib nolibc code bugs fixed.
  • Regression: duplicate symbols with static variable declared in macro #1248.
  • Unsplat with named parameters was accidentally disallowed.
  • Reference parameter doesn't work with vector subscript #1250.
  • The msvc_sdk script failed to work properly on windows when run in folders with spaces.

Stdlib changes

  • Added remove_first_item remove_last_item and remove_item as aliases for the match functions.
  • Added @str_hash, @str_upper, @str_lower, @str_find compile time macros.
  • Remove "panic" text from unreachable() when safe mode is turned off.
  • Added @unaligned_store and @unaligned_load.
  • Null ZString, DString or pointer prints "(null)" for printf.
  • Updated sorting API.
  • Insertion sort and counting sort added.
  • Added missing mem and mem::allocator functions for aligned allocations.
  • Added new_init_with_array and temp_init_with_array for List.
  • Fixed posix NativeMutex.lock_timeout.
  • Fixed env::ARCH_32_BIT and env::ARCH_64_BIT.
  • Added time::us.

C3 reaches the 0.6 milestone!

Originally from: https://c3.handmade.network/blog/p/8918-c3_reaches_the_0.6_milestone%2521

It's been three months and finally version 0.6.0 is here!

C3 was on a monthly update cycle, so what happened to version 0.5.6? Actually the main branch contained a releasable 0.5.6 for the last two months, but I didn't release it. A release takes a bit of time updating everything and making announcements and so on, and I felt that maybe I should just release version 0.6.0 instead – rather than to push the relatively small 0.5.6.

But on the other hand I kept delaying 0.6.0, because I was looking for more breaking changes to add (recall that 0.5.x cannot introduce any breaking changes for 0.5 code, so 0.6.0 is where the breaking changes happen)

However, now 0.6.0 is stable enough, and there has lately been very little to backport into the 0.5.x branch. So it's time to do the 0.6.0 release.

Let's go through what happened since 0.5.5 first:

Updated enum syntax

Using enums with associated values now look more like defining C enums than Java enums:

// Old
enum Foo : int (int value, double value2)
{
    A(1, 2.0),
    B(2, 4.0)
}
enum Bar : int (int value)
{
    C(7),
    D(9)
}

// New
enum Foo : int (int value, double value2)
{
    A = { 1, 2.0 },
    B = { 2, 4.0 }
}
enum Bar : int (int value)
{
    C = 7, // Also possible to write as C = { 7 }
    D = 9
}

The new syntax also allows the enum size to be elided when there are associated values:

enum Foo : (int value, double value2)
{
    A = { 1, 2.0 },
    B = { 2, 4.0 }
}

There were other ideas around the enums, allowing them to be used more like unordered C enums. However, the experimental attempts showed little promise. Maybe this will be revisited for version 0.7.

Changes to any / interfaces

Two things changed, the most important thing is that the 0.5 change where any was handled written as a pointer (any*) was rolled back. This was a trade-off: in some cases using any* felt like it better expressed what was going on, but in other cases it ended up being confusing.

Secondly @default implementations for interfaces were removed. The need for these were greatly reduced when the stdlib moved from "call the method on the type" to "call a function with the interface" style (see std::io for examples).

Guaranteed jump tables

For switches it's now possible to get guaranteed calculated goto jump tables by using the @jump attribute:

switch (a) @jump
{
    case 1: res(1);
    case 3: res(3);
}

The above code will lower to an array of jump destinations which is indexed into and then jumped to.

RGBA swizzling

In addition to swizzling using xyzw it's now possible to use rgba as well:

int[<4>] abc = { 1, 2, 3, 4 };
int[<3>] z = abc.xwz; // Grabs index 1, 4 and 3
int[<3>] c = abc.rab; // Same as above

More distinct types

It's now possible to make distinct types out of void, typeid, anyfault and fault types.

Catch an error in defer

It's now possible to get the error thrown when using defer catch statements:

defer catch io::printn("Exit due to some error"); 
defer (catch err) io::printfn("Exit due to: %s", err); // New

Stricter semantics for if try

It's no longer possible to do:

if (try foo() + 1) { ... }

The semantics of this code was a bit confusing, so in 0.6.0 you may no longer do "if try" binary or unary expressions. You may however still test expressions like if (try (foo() + 1)).

Changes in command-line arguments

  • Added --output-dir to set the output directory.
  • Added --print-input to print all files used for compilation.
  • Removed --system-linker and replaced it by --linker which also allows setting custom linkers.

Stricter @if evaluation

Evaluating @if on the top level is always subject to ordering issues. For this reason 0.6 does not permit conditional definition guarded by @if to depend on something that in itself depended on @if.

int foo;
// This would succeed on 0.5.x, but changing the
// ordering, placing `B` before `a` would be a compile error.
const B @if($defined(foo)) = 1;
const B @if(!$defined(foo)) = 2;
int a @if(B == 1);

In 0.6.0 the above code is an error, as "a" depends on the conditional "B".

This change helps preventing the user from accidentally building code that depends on top level ordering.

assert(false) disallowed

Aside from (compile time known) unused branches and tests, asserts that are compile time evaluated to be false are now compile time errors. This allows asserts to detect more broken code at compile time.

const A = 1;
if (A == 0)
{
  assert(false); // Ok, dead branch.
}
else
{
  assert(false); // This is a compile time error
}
assert(A == 0); // Also a compile time error.

More permissive function definitions

Functions definitions may now be recursive (e.g. a function type taking as argument a pointer to itself).

Better errors for inlined macros

The code now provides a backtrace to where the macro was first inlined when detecting an error.

Improved debug information

The debug information has gotten an overhaul, in particular debug information for macros are much improved.

Changes to the stdlib

Various fixes, but perhaps most importantly list types now consistently use push rather than add, and pop now always return an Optional result.

The full change list 0.5.5 -> 0.6.0

Changes / improvements

  • @default implementations for interfaces removed.
  • any* => any, same for interfaces.
  • Private / local globals now have internal visibility in LLVM.
  • Updated enum syntax.
  • 'rgba' also available for swizzling.
  • The name "subarray" has been replaced by the more well known name "slice' across the codebase.
  • Improved alignment handling.
  • Add --output-dir to command line. #1155
  • Allow making distinct types out of "void", "typeid", "anyfault" and faults.
  • Removed --system-linker setting.
  • "Try" expressions may not be any binary or unary expressions. So for example try foo() + 1 is disallowed.
  • Added $$REGISTER_SIZE for int register size.
  • assert(false) only allowed in unused branches or in tests. Compile time failed asserts is a compile time error.
  • Require expression blocks returning values to have the value used.
  • Detect "unsigned >= 0" as errors.
  • Improve callstack debug information #1184.
  • Request jump table using @jump for switches.
  • Improved inline debug information.
  • Improved error messages on inlined macros.
  • Introduce MSVC compatible SIMD ABI.
  • $foreach doesn't create an implicit syntactic scope.
  • Error of @if depends on @if
  • Support defer (catch err)
  • Added print-input command argument to print all files used for compilation
  • Allow recursive function definitions as long as they are pointers. #1182
  • Default CPU to native if less than AVX, otherwise use AVX.
  • Bounds checking on length for foo[1:2] slicing #1191.
  • Foreach uses non-wrapping add/dec.

Fixes

  • Fixed issue in safe mode when converting enums.
  • Better checking of operator methods.
  • Bug when assigning an optional from an optional.
  • Lambdas were not type checked thoroughly #1185.
  • Fix problems using reflection on interface types #1203.
  • @param with unnamed macro varargs could crash the compiler.
  • Compiler crash using enum nameof from different module #1205.
  • Incorrect length passed to scratch buffer printf.
  • Casting to a bitstruct would be allowed even if the type was the wrong size.
  • Generic modules parameterized with constants would sometimes get the wrong parameterized module name causing conversion errors #1192.
  • Duplicate emit of expressions on negation would incorrectly compile negated macros.
  • Casting a slice address to its pointer type should not compile #1193.
  • Union is not properly zero-initialized with designated initializer #1194.
  • Compile time fmod evaluates to 0 #1195.
  • Assertion failed when casting (unsigned) argument to enum #1196
  • Correct debug info on parameters without storage.
  • Fix location on foreach debug output.
  • Compiler crash on designated initializer for structs with bitstruct.

Stdlib changes

  • "init_new/init_temp" removed.
  • LinkedList API rewritten.
  • List "pop" and "remove" function now return Optionals.
  • RingBuffer API rewritten. Allocator interface changed.
  • Deprecated Allocator, DString and mem functions removed.
  • "identity" functions are now constants for Matrix and Complex numbers.
  • Removed 'append' from Object and List, replaced by 'push'.
  • GenericList renamed AnyList.
  • Proper handling of '.' and Win32 '//server' paths.
  • Path normalization - fix possible null terminator out of bounds.
  • Add 'zstr' variants for string::new_format / string::tformat.
  • Fix mutex and wait signatures for Win32.

0.6 has feature stability guarantees, code written for 0.6.0 will work with all of 0.6.x.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c

Comments


Comment by Christoffer Lernö

It's been three months and finally version 0.6.0 is here!

C3 was on a monthly update cycle, so what happened to version 0.5.6? Actually the main branch contained a releasable 0.5.6 for the last two months, but I didn't release it. A release takes a bit of time updating everything and making announcements and so on, and I felt that maybe I should just release version 0.6.0 instead – rather than to push the relatively small 0.5.6.

But on the other hand I kept delaying 0.6.0, because I was looking for more breaking changes to add (recall that 0.5.x cannot introduce any breaking changes for 0.5 code, so 0.6.0 is where the breaking changes happen)

However, now 0.6.0 is stable enough, and there has lately been very little to backport into the 0.5.x branch. So it's time to do the 0.6.0 release.

Let's go through what happened since 0.5.5 first:

Updated enum syntax

Using enums with associated values now look more like defining C enums than Java enums:

// Old
enum Foo : int (int value, double value2)
{
    A(1, 2.0),
    B(2, 4.0)
}
enum Bar : int (int value)
{
    C(7),
    D(9)
}

// New
enum Foo : int (int value, double value2)
{
    A = { 1, 2.0 },
    B = { 2, 4.0 }
}
enum Bar : int (int value)
{
    C = 7, // Also possible to write as C = { 7 }
    D = 9
}

The new syntax also allows the enum size to be elided when there are associated values:

enum Foo : (int value, double value2)
{
    A = { 1, 2.0 },
    B = { 2, 4.0 }
}

There were other ideas around the enums, allowing them to be used more like unordered C enums. However, the experimental attempts showed little promise. Maybe this will be revisited for version 0.7.

Changes to any / interfaces

Two things changed, the most important thing is that the 0.5 change where any was handled written as a pointer (any*) was rolled back. This was a trade-off: in some cases using any* felt like it better expressed what was going on, but in other cases it ended up being confusing.

Secondly @default implementations for interfaces were removed. The need for these were greatly reduced when the stdlib moved from "call the method on the type" to "call a function with the interface" style (see std::io for examples).

Guaranteed jump tables

For switches it's now possible to get guaranteed calculated goto jump tables by using the @jump attribute:

switch (a) @jump
{
    case 1: res(1);
    case 3: res(3);
}

The above code will lower to an array of jump destinations which is indexed into and then jumped to.

RGBA swizzling

In addition to swizzling using xyzw it's now possible to use rgba as well:

int[<4>] abc = { 1, 2, 3, 4 };
int[<3>] z = abc.xwz; // Grabs index 1, 4 and 3
int[<3>] c = abc.rab; // Same as above

More distinct types

It's now possible to make distinct types out of void, typeid, anyfault and fault types.

Catch an error in defer

It's now possible to get the error thrown when using defer catch statements:

defer catch io::printn("Exit due to some error"); 
defer (catch err) io::printfn("Exit due to: %s", err); // New

Stricter semantics for if try

It's no longer possible to do:

if (try foo() + 1) { ... }

The semantics of this code was a bit confusing, so in 0.6.0 you may no longer do "if try" binary or unary expressions. You may however still test expressions like if (try (foo() + 1)).

Changes in command-line arguments

  • Added --output-dir to set the output directory.
  • Added --print-input to print all files used for compilation.
  • Removed --system-linker and replaced it by --linker which also allows setting custom linkers.

Stricter @if evaluation

Evaluating @if on the top level is always subject to ordering issues. For this reason 0.6 does not permit conditional definition guarded by @if to depend on something that in itself depended on @if.

int foo;
// This would succeed on 0.5.x, but changing the
// ordering, placing `B` before `a` would be a compile error.
const B @if($defined(foo)) = 1;
const B @if(!$defined(foo)) = 2;
int a @if(B == 1);

In 0.6.0 the above code is an error, as "a" depends on the conditional "B".

This change helps preventing the user from accidentally building code that depends on top level ordering.

assert(false) disallowed

Aside from (compile time known) unused branches and tests, asserts that are compile time evaluated to be false are now compile time errors. This allows asserts to detect more broken code at compile time.

const A = 1;
if (A == 0)
{
  assert(false); // Ok, dead branch.
}
else
{
  assert(false); // This is a compile time error
}
assert(A == 0); // Also a compile time error.

More permissive function definitions

Functions definitions may now be recursive (e.g. a function type taking as argument a pointer to itself).

Better errors for inlined macros

The code now provides a backtrace to where the macro was first inlined when detecting an error.

Improved debug information

The debug information has gotten an overhaul, in particular debug information for macros are much improved.

Changes to the stdlib

Various fixes, but perhaps most importantly list types now consistently use push rather than add, and pop now always return an Optional result.

The full change list 0.5.5 -> 0.6.0

Changes / improvements

  • @default implementations for interfaces removed.
  • any* => any, same for interfaces.
  • Private / local globals now have internal visibility in LLVM.
  • Updated enum syntax.
  • 'rgba' also available for swizzling.
  • The name "subarray" has been replaced by the more well known name "slice' across the codebase.
  • Improved alignment handling.
  • Add --output-dir to command line. #1155
  • Allow making distinct types out of "void", "typeid", "anyfault" and faults.
  • Removed --system-linker setting.
  • "Try" expressions may not be any binary or unary expressions. So for example try foo() + 1 is disallowed.
  • Added $$REGISTER_SIZE for int register size.
  • assert(false) only allowed in unused branches or in tests. Compile time failed asserts is a compile time error.
  • Require expression blocks returning values to have the value used.
  • Detect "unsigned >= 0" as errors.
  • Improve callstack debug information #1184.
  • Request jump table using @jump for switches.
  • Improved inline debug information.
  • Improved error messages on inlined macros.
  • Introduce MSVC compatible SIMD ABI.
  • $foreach doesn't create an implicit syntactic scope.
  • Error of @if depends on @if
  • Support defer (catch err)
  • Added print-input command argument to print all files used for compilation
  • Allow recursive function definitions as long as they are pointers. #1182
  • Default CPU to native if less than AVX, otherwise use AVX.
  • Bounds checking on length for foo[1:2] slicing #1191.
  • Foreach uses non-wrapping add/dec.

Fixes

  • Fixed issue in safe mode when converting enums.
  • Better checking of operator methods.
  • Bug when assigning an optional from an optional.
  • Lambdas were not type checked thoroughly #1185.
  • Fix problems using reflection on interface types #1203.
  • @param with unnamed macro varargs could crash the compiler.
  • Compiler crash using enum nameof from different module #1205.
  • Incorrect length passed to scratch buffer printf.
  • Casting to a bitstruct would be allowed even if the type was the wrong size.
  • Generic modules parameterized with constants would sometimes get the wrong parameterized module name causing conversion errors #1192.
  • Duplicate emit of expressions on negation would incorrectly compile negated macros.
  • Casting a slice address to its pointer type should not compile #1193.
  • Union is not properly zero-initialized with designated initializer #1194.
  • Compile time fmod evaluates to 0 #1195.
  • Assertion failed when casting (unsigned) argument to enum #1196
  • Correct debug info on parameters without storage.
  • Fix location on foreach debug output.
  • Compiler crash on designated initializer for structs with bitstruct.

Stdlib changes

  • "init_new/init_temp" removed.
  • LinkedList API rewritten.
  • List "pop" and "remove" function now return Optionals.
  • RingBuffer API rewritten. Allocator interface changed.
  • Deprecated Allocator, DString and mem functions removed.
  • "identity" functions are now constants for Matrix and Complex numbers.
  • Removed 'append' from Object and List, replaced by 'push'.
  • GenericList renamed AnyList.
  • Proper handling of '.' and Win32 '//server' paths.
  • Path normalization - fix possible null terminator out of bounds.
  • Add 'zstr' variants for string::new_format / string::tformat.
  • Fix mutex and wait signatures for Win32.

0.6 has feature stability guarantees, code written for 0.6.0 will work with all of 0.6.x.

If you want to read more about C3, check out the documentation: https://c3-lang.org or download it and try it out: https://github.com/c3lang/c3c