Skip to content

static assertions #294

Open
Open
@YashasSamaga

Description

@YashasSamaga

Is this a BUG REPORT, FEATURE REQUEST or QUESTION?:

  • Bug Report
  • Feature Request
  • Question

Design concept:
static_assert takes a constant expression as the first parameter which must evaluate to true (non-zero number) for successful compilation and an optional error string as the second parameter.

static_assert(cond, errstr="")

The compiler would trigger a fatal error if cond evaluates to false. If an error string was given, the error string would be shown to the user; otherwise, the expression will be shown.

The compiler should allow static_assert to be embedded inside other constant expressions. For example,

new arr[(static_assert(sz % 32 == 0, "array size must be a multiple of 32"), sz)];
const expr = 10 + !static_assert(size > 0);

This can be achieved by making static_assert always return a value (say 1).

Error messages:

test.pwn (123): fatal error 123: assertion failed: [expression]
test.pwn (123): fatal error 123: assertion failed: array size must be a multiple of 32
test.pwn (123): error 195: static_assert can evaluate constant expressions only

Why is it needed?

  • allows libraries to place assertions in the user's code (where #error isn't of much use)
  • allows descriptive error messages

Issues:

  • Macro replacement for the old compiler

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions