From 586bd2eec87c7db00ffc8284a0b47ff2fd68500e Mon Sep 17 00:00:00 2001 From: Rangi <35663410+Rangi42@users.noreply.github.com> Date: Wed, 14 Oct 2026 19:24:47 -0400 Subject: [PATCH] Non-constant expressions do not fail a `static_assert` (#2215) --- include/asm/actions.hpp | 2 +- src/asm/actions.cpp | 5 +++-- src/asm/parser.y | 4 ++-- test/asm/assert.err | 2 -- test/asm/const-low.asm | 7 ++++--- test/asm/const-low.err | 5 ----- test/asm/const-low.out.bin | Bin 0 -> 2 bytes test/asm/raw-string-symbol-errors.err | 6 +----- 8 files changed, 11 insertions(+), 20 deletions(-) delete mode 100644 test/asm/const-low.err create mode 100644 test/asm/const-low.out.bin diff --git a/include/asm/actions.hpp b/include/asm/actions.hpp index 8133f122..c40cfa3c 100644 --- a/include/asm/actions.hpp +++ b/include/asm/actions.hpp @@ -29,7 +29,7 @@ void act_Endc(); AlignmentSpec act_Alignment(int32_t alignment, int32_t alignOfs); void act_Assert(AssertionType type, Expression const &expr, std::string const &message); -void act_StaticAssert(AssertionType type, int32_t condition, std::string const &message); +void act_StaticAssert(AssertionType type, Expression const &expr, std::string const &message); std::optional act_ReadFile(std::string const &name, uint32_t maxLen); diff --git a/src/asm/actions.cpp b/src/asm/actions.cpp index 8b771c09..fe9d320e 100644 --- a/src/asm/actions.cpp +++ b/src/asm/actions.cpp @@ -132,8 +132,9 @@ void act_Assert(AssertionType type, Expression const &expr, std::string const &m } } -void act_StaticAssert(AssertionType type, int32_t condition, std::string const &message) { - if (!condition) { +void act_StaticAssert(AssertionType type, Expression const &expr, std::string const &message) { + // If `expr` is not a known constant, `expr.getConstVal()` will print its own error. + if (int32_t condition = expr.getConstVal(); expr.isKnown() && !condition) { failAssert(type, message); } } diff --git a/src/asm/parser.y b/src/asm/parser.y index 8a84f309..7fb25b79 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -761,10 +761,10 @@ assert: | POP_ASSERT assert_type relocexpr COMMA string { act_Assert($2, $3, $5); } - | POP_STATIC_ASSERT assert_type iconst { + | POP_STATIC_ASSERT assert_type relocexpr { act_StaticAssert($2, $3, ""); } - | POP_STATIC_ASSERT assert_type iconst COMMA string { + | POP_STATIC_ASSERT assert_type relocexpr COMMA string { act_StaticAssert($2, $3, $5); } ; diff --git a/test/asm/assert.err b/test/asm/assert.err index 6e47ee91..c2a58807 100644 --- a/test/asm/assert.err +++ b/test/asm/assert.err @@ -6,7 +6,5 @@ warning: Assertion failed [-Wassert] at assert.asm(10) error: Expected constant expression: `FloatingBase` is not constant at assembly time at assert.asm(18) -error: Assertion failed - at assert.asm(18) FATAL: Assertion failed at assert.asm(21) diff --git a/test/asm/const-low.asm b/test/asm/const-low.asm index 7f90bb04..efc774f8 100644 --- a/test/asm/const-low.asm +++ b/test/asm/const-low.asm @@ -1,10 +1,11 @@ -section "good", romx, align[8, 1] +section "known", romx, align[8, 1] Alpha: static_assert LOW(Alpha) == 1 db 99 Beta: static_assert LOW(Beta) == 2 -section "bad", romx, align[7, 3] +section "unknown", romx, align[7, 3] Gamma: -static_assert LOW(Gamma) == 3 +static_assert !ISCONST(LOW(Gamma)) +assert LOW(Gamma) == 3 diff --git a/test/asm/const-low.err b/test/asm/const-low.err deleted file mode 100644 index 9d7981df..00000000 --- a/test/asm/const-low.err +++ /dev/null @@ -1,5 +0,0 @@ -error: Expected constant expression: `Gamma` is not constant at assembly time - at const-low.asm(10) -error: Assertion failed - at const-low.asm(10) -Assembly aborted with 2 errors diff --git a/test/asm/const-low.out.bin b/test/asm/const-low.out.bin new file mode 100644 index 0000000000000000000000000000000000000000..65b7b652228b8eae077bff927402b61233882288 GIT binary patch literal 2 JcmZQb1^@tM0Av6F literal 0 HcmV?d00001 diff --git a/test/asm/raw-string-symbol-errors.err b/test/asm/raw-string-symbol-errors.err index 2fd9daf0..94177e50 100644 --- a/test/asm/raw-string-symbol-errors.err +++ b/test/asm/raw-string-symbol-errors.err @@ -6,8 +6,6 @@ error: `m` is not a numeric symbol at raw-string-symbol-errors.asm(8) error: Expected constant expression: undefined symbol `u` at raw-string-symbol-errors.asm(9) -error: Assertion failed - at raw-string-symbol-errors.asm(9) error: `n` is not a string symbol at raw-string-symbol-errors.asm(11) error: `m` is not a string symbol @@ -32,8 +30,6 @@ error: Undefined symbol `s` was already purged at raw-string-symbol-errors.asm(27) error: Expected constant expression: undefined symbol `s`; it was purged at raw-string-symbol-errors.asm(28) -error: Assertion failed - at raw-string-symbol-errors.asm(28) error: `s` is not a string symbol at raw-string-symbol-errors.asm(29) -Assembly aborted with 17 errors +Assembly aborted with 15 errors