Skip to content

Add support for asserts using built-in macros - #555

Open
Jafaral wants to merge 8 commits into
uniconproject:masterfrom
Jafaral:assert
Open

Jafaral wants to merge 8 commits into
uniconproject:masterfrom
Jafaral:assert

Conversation

@Jafaral

@Jafaral Jafaral commented Mar 29, 2026 •

Copy link
Copy Markdown
Member
Add builtin assert helpers (assert, assert_not) to the Unicon preprocessor and route them through the
function-like macro expansion path.

Behavior is controlled by preprocessor symbols:
- __debug__ enables runtime assertion checks.
- without __debug__, builtin asserts expand to nothing
- __test__ implies __debug__ and switches failures to write()+fail
  so tests can continue and observe later checks.
- without __test__, failures use stop() and abort immediately.

Diagnostics include file and line context, failed expression text,
optional labels, and expected/actual values for equality helpers.

Follow up to #554

@Jafaral
Jafaral requested a review from cjeffery March 29, 2026 05:39
@Jafaral
Jafaral requested a review from Don-Ward March 29, 2026 05:40
@greptile-apps

greptile-apps Bot commented Mar 29, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5 · The PR appears safe to merge; no new blocking issue was established.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new blocking issue was established.

Summary

Adds built-in assert and assert_not, with separate release, debug, and test behavior.

  • Unicon assertions now choose release, debug, or test behavior from preprocessor symbols.
  • Function-like macro definitions and calls can now span physical lines.
  • The test runner now counts compile failures and keeps running the suite.

Review conclusions are based on source inspection; tests were not run.

Reviews (15) · Last reviewed commit: "doc: add UTR # 24 The Unicon Preprocesso..." · Reviewed by Greptile

Comment thread uni/unicon/preproce.icn Resolved
Comment thread uni/unicon/preproce.icn Resolved
Comment thread uni/unicon/preproce.icn Resolved
Comment thread uni/unicon/preproce.icn Resolved
Comment thread uni/unicon/preproce.icn Resolved
Comment thread uni/unicon/preproce.icn Resolved
Comment thread src/runtime/rdebug.r Resolved
Comment thread src/runtime/errmsg.r Resolved
Comment thread src/runtime/errmsg.r Resolved
@Jafaral
Jafaral force-pushed the assert branch 2 times, most recently from 0ab316d to 83681a9 Compare April 17, 2026 20:15
@Jafaral
Jafaral marked this pull request as draft April 23, 2026 04:02
@cjeffery

Copy link
Copy Markdown
Contributor

trim() calls to remove whitespace before and after a \ that is "near" the end of a line make me very nervous. Most languages would not accept a \ as a line continuation in a non-final position, and most languages would not quietly concatenate tokens. This is made more complex by the fact that Unicon has a binary backslash operator (limitation), so it is not inconceivable for a Unicon programmer to write
$define f(X) abc \
3
and if you trim everything to the token abc3 they may have expected abc to be limited to at most 3 results.

The error codes 219 and 220 having identical error messages seems bad. Why two error codes if they are the same?

The assert macro syntax ( assert { } vs. C assert() ) throws me for a loop. Is macro syntax more generally not using parentheses but instead using curly brackets? In other situations, Unicon uses { } instead of ( ) very specifically to mean "create co-expressions for each parameter", and overloading the meaning of { } vs. ( ) for asserts or more generally for macros may confuse programmers.

@Jafaral

Jafaral commented Apr 23, 2026

Copy link
Copy Markdown
Member Author

trim() calls to remove whitespace before and after a \ that is "near" the end of a line make me very nervous. Most languages would not accept a \ as a line continuation in a non-final position, and most languages would not quietly concatenate tokens. This is made more complex by the fact that Unicon has a binary backslash operator (limitation), so it is not inconceivable for a Unicon programmer to write $define f(X) abc \ 3 and if you trim everything to the token abc3 they may have expected abc to be limited to at most 3 results.

I initially didn't want to add multi-line support to Macros, but figured it is really useful to be able to split a long macro to multiple lines. But then again, writing long macros is a bad habit in a very high level programming language like Unicon, except I want be able a custom long assert macro and still make it readable. Here is what I possible trade off:

  • don't trim the end of the line,
  • don't treat \ as a continuous unless it is the last char in the line
  • maybe use \\ for continuation instead of just one \.

The error codes 219 and 220 having identical error messages seems bad. Why two error codes if they are the same?

mainly did it so I can differentiate between assert and assert_not. runerr() is used with both to print the stack with assert annotation instead of a regular runtime error. This is an example of what you would see when an assertion happens:

[assert_debugging.icn:7] AssertionFailed: (x +:= 1) = 2, x must be two after an increment
Traceback:
   main()
   deep1() from line 69 in assert_debugging.icn
   deep2() from line 48 in assert_debugging.icn
   deep3() from line 52 in assert_debugging.icn
   deep4() from line 56 in assert_debugging.icn
   assert_with_label() from line 60 in assert_debugging.icn
   assert((x +:= 1) = 2, "x must be two after an increment") from line 7 in assert_debugging.icn

So error codes 219 and 2020 are used for specially handling to runerr() stack trace for the two new assert functions.

The assert macro syntax ( assert { } vs. C assert() ) throws me for a loop. Is macro syntax more generally not using parentheses but instead using curly brackets? In other situations, Unicon uses { } instead of ( ) very specifically to mean "create co-expressions for each parameter", and overloading the meaning of { } vs. ( ) for asserts or more generally for macros may confuse programmers.

The new assert macros I'm adding use (). The ones you see using {} in this PR are actually existing tests added a while back. I just renamed those one from assert to assertSucess` to avoid name collision with the newly added assert macros.

@cjeffery

Copy link
Copy Markdown
Contributor

I initially didn't want to add multi-line support to Macros, but figured it is really useful to be able to split a long macro to multiple lines. But then again, writing long macros is a bad habit in a very high level programming language like Unicon, except I want be able a custom long assert macro and still make it readable. Here is what I possible trade off:

  • don't trim the end of the line,
  • don't treat \ as a continuous unless it is the last char in the line
  • maybe use \\ for continuation instead of just one \.

I guess that we are not the bad habit police, and yeah, if we are doing macros with parameters we will have to accept the fact that some folks will want to use it for long macros like in C. If we are going to support C-like $define macros with parameters we had better have pretty similar behavior to C preprocessor while we are at it.
The C preprocessor does not trim, it only continues if a backslash is the very last char on the line, and I would recommend being consistent with that unless we've got a darned good reason.

The error codes 219 and 220 having identical error messages seems bad. Why two error codes if they are the same?

mainly did it so I can differentiate between assert and assert_not.

I can live with this, but do we really gotta? If everything was 219, what would that not allow you to do?

@Jafaral
Jafaral marked this pull request as ready for review August 6, 2026 23:22
@Jafaral

Jafaral commented Aug 6, 2026

Copy link
Copy Markdown
Member Author

I guess that we are not the bad habit police, and yeah, if we are doing macros with parameters we will have to accept the fact that some folks will want to use it for long macros like in C. If we are going to support C-like $define macros with parameters we had better have pretty similar behavior to C preprocessor while we are at it. The C preprocessor does not trim, it only continues if a backslash is the very last char on the line, and I would recommend being consistent with that unless we've got a darned good reason.

Good point, I adjusted the code to do that; same C semantics.

The error codes 219 and 220 having identical error messages seems bad. Why two error codes if they are the same?

mainly did it so I can differentiate between assert and assert_not.

I can live with this, but do we really gotta? If everything was 219, what would that not allow you to do?

I dropped 219. 220 allowed me to do a shortcut using the runtime which I discovered I can do in the preprocessor.

@Jafaral
Jafaral force-pushed the assert branch 4 times, most recently from 460ce76 to 780c9ce Compare August 21, 2026 04:50
A compile error stopped the suite, and the summary still counted every
test as passed. Record that failure and continue with the rest.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
assert and assert_not expand to one if-expression: &null when the check
passes, and nothing unless __debug__ or __test__ is set. A program's own
assert method is left alone.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Cover debug and test builds, release builds, a program's own assert, and
-D__test__ on the command line.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Comment thread uni/unicon/preproce.icn Resolved
Comment thread src/runtime/errmsg.r Resolved
Report assert and assert_not as error 219, one line with the file and
line. A plain runerr(219) keeps the usual banner and traceback.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Match the single-line 219 format, including &error conversion and a
plain runerr(219).

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Comment thread uni/unicon/preproce.icn Resolved
Let a macro call or a $define value continue onto the next line.
Argument lists respect brackets, and a continuation stays in its file.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Cover multiline macro calls and $define continuations, including the
cases that should fail.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Describe the preprocessor, including multiline macros and asserts, and
index the report.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants