Skip to content

Commit

Permalink
Const strings and bytes were not properly converted to compile time b…
Browse files Browse the repository at this point in the history
…ools.

Contracts @require/@Ensure are no longer treated as conditionals, but must be explicitly bool.
  • Loading branch information
lerno committed Jan 18, 2025
1 parent 5a36f0b commit c3f5806
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 27 deletions.
6 changes: 3 additions & 3 deletions lib/std/collections/linkedlist.c3
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn LinkedList* LinkedList.temp_init(&self)
}

<*
@require self.allocator
@require self.allocator != null
*>
macro void LinkedList.free_node(&self, Node* node) @private
{
Expand Down Expand Up @@ -200,7 +200,7 @@ fn void LinkedList.link_before(&self, Node *succ, Type value) @private
}

<*
@require self._first
@require self._first != null
*>
fn void LinkedList.unlink_first(&self) @private
{
Expand Down Expand Up @@ -296,7 +296,7 @@ fn bool LinkedList.remove_last_match(&self, Type t) @if(ELEMENT_IS_EQUATABLE)
return false;
}
<*
@require self._last
@require self._last != null
*>
fn void LinkedList.unlink_last(&self) @inline @private
{
Expand Down
2 changes: 1 addition & 1 deletion lib/std/collections/list.c3
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ macro void List.pre_free(&self) @private
}

<*
@require self.capacity
@require self.capacity > 0
*>
macro void List.post_alloc(&self) @private
{
Expand Down
6 changes: 3 additions & 3 deletions lib/std/core/allocators/dynamic_arena.c3
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ struct DynamicArenaChunk @local
}

<*
@require ptr
@require self.page `tried to free pointer on invalid allocator`
@require ptr != null
@require self.page != null `tried to free pointer on invalid allocator`
*>
fn void DynamicArenaAllocator.release(&self, void* ptr, bool) @dynamic
{
Expand All @@ -77,7 +77,7 @@ fn void DynamicArenaAllocator.release(&self, void* ptr, bool) @dynamic
<*
@require size > 0 `Resize doesn't support zeroing`
@require old_pointer != null `Resize doesn't handle null pointers`
@require self.page `tried to realloc pointer on invalid allocator`
@require self.page != null `tried to realloc pointer on invalid allocator`
*>
fn void*! DynamicArenaAllocator.resize(&self, void* old_pointer, usz size, usz alignment) @dynamic
{
Expand Down
2 changes: 1 addition & 1 deletion lib/std/core/allocators/heap_allocator.c3
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct SimpleHeapAllocator (Allocator)
}

<*
@require allocator "An underlying memory provider must be given"
@require allocator != null "An underlying memory provider must be given"
@require !self.free_list "The allocator may not be already initialized"
*>
fn void SimpleHeapAllocator.init(&self, MemoryAllocFn allocator)
Expand Down
2 changes: 1 addition & 1 deletion lib/std/core/allocators/on_stack_allocator.c3
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct OnStackAllocatorHeader
}

<*
@require old_pointer
@require old_pointer != null
*>
fn void OnStackAllocator.release(&self, void* old_pointer, bool aligned) @dynamic
{
Expand Down
4 changes: 2 additions & 2 deletions lib/std/core/dstring.c3
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn String DString.str_view(self)

<*
@require index < self.len()
@require self.data() "Empty string"
@require self.data() != null "Empty string"
*>
fn char DString.char_at(self, usz index) @operator([])
{
Expand All @@ -166,7 +166,7 @@ fn char DString.char_at(self, usz index) @operator([])

<*
@require index < self.len()
@require self.data() "Empty string"
@require self.data() != null "Empty string"
*>
fn char* DString.char_ref(&self, usz index) @operator(&[])
{
Expand Down
2 changes: 1 addition & 1 deletion lib/std/encoding/csv.c3
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn CsvRow! CsvReader.read_temp_row(self)
}

<*
@require self.allocator `Row already freed`
@require self.allocator != null `Row already freed`
*>
fn void CsvRow.free(&self)
{
Expand Down
6 changes: 3 additions & 3 deletions lib/std/io/file.c3
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn void! File.close(&self) @inline @dynamic
}

<*
@require self.file
@require self.file != null
*>
fn bool File.eof(&self) @inline
{
Expand All @@ -123,7 +123,7 @@ fn usz! File.read(&self, char[] buffer) @dynamic

<*
@param [out] buffer
@require self.file `File must be initialized`
@require self.file != null `File must be initialized`
*>
fn usz! File.write(&self, char[] buffer) @dynamic
{
Expand Down Expand Up @@ -194,7 +194,7 @@ fn void! save(String filename, char[] data)
}

<*
@require self.file `File must be initialized`
@require self.file != null `File must be initialized`
*>
fn void! File.flush(&self) @dynamic
{
Expand Down
2 changes: 1 addition & 1 deletion lib/std/io/os/rmtree.c3
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module std::io::os @if(env::POSIX);
import std::io, std::os, libc;

<*
@require dir.str_view()
@require dir.str_view().len > 0
*>
fn void! native_rmtree(Path dir)
{
Expand Down
3 changes: 2 additions & 1 deletion releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
## 0.6.7 Change list

### Changes / improvements
- None yet.
- Contracts @require/@ensure are no longer treated as conditionals, but must be explicitly bool.

### 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.

### Stdlib changes
- Added '%h' and '%H' for printing out binary data in hexadecimal using the formatter.
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/sema_casts.c
Original file line number Diff line number Diff line change
Expand Up @@ -1966,6 +1966,11 @@ static void cast_ptr_to_bool(SemaContext *context, Expr *expr, Type *type)

static void cast_slice_to_bool(SemaContext *context, Expr *expr, Type *type)
{
if (expr_is_const_string(expr) || expr_is_const_bytes(expr))
{
expr_rewrite_const_bool(expr, type, expr->const_expr.bytes.len > 0);
return;
}
if (expr_is_const_slice(expr))
{
expr_rewrite_const_bool(expr, type, expr->const_expr.slice_init != NULL);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/sema_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -9697,7 +9697,7 @@ bool sema_analyse_cond_expr(SemaContext *context, Expr *expr, CondResult *result
type_quoted_error_string(expr->type));
}
if (!cast_explicit(context, expr, type_bool)) return false;
if (expr_is_const_bool(expr))
if (sema_cast_const(expr) && expr_is_const_bool(expr))
{
*result = expr->const_expr.b ? COND_TRUE : COND_FALSE;
}
Expand Down
14 changes: 5 additions & 9 deletions src/compiler/sema_stmts.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,19 +361,15 @@ static inline bool assert_create_from_contract(SemaContext *context, Ast *direct
{
if (expr->expr_kind == EXPR_DECL) RETURN_SEMA_ERROR(expr, "Only expressions are allowed in contracts.");
CondResult result = COND_MISSING;
if (!sema_analyse_cond_expr(context, expr, &result)) return false;
if (!sema_analyse_expr_rhs(context, type_bool, expr, false, NULL, NULL)) return false;

const char *comment = directive->contract_stmt.contract.comment;
if (!comment) comment = directive->contract_stmt.contract.expr_string;
switch (result)
if (expr_is_const_bool(expr))
{
case COND_TRUE:
continue;
case COND_FALSE:
sema_error_at(context, evaluation_location.a ? evaluation_location : expr->span, "%s", comment);
return false;
case COND_MISSING:
break;
if (expr->const_expr.b) continue;
sema_error_at(context, evaluation_location.a ? evaluation_location : expr->span, "%s", comment);
return false;
}
Ast *assert = new_ast(AST_ASSERT_STMT, expr->span);
assert->assert_stmt.is_ensure = true;
Expand Down

0 comments on commit c3f5806

Please sign in to comment.