Skip to content

assert() cannot be used in expression context #686

Description

@dhalbert

@kamtom480: a question for you:

The implementation of assert() in spresense comes from nuttx. assert() is defined as ASSERT() when NDEBUG is not defined. See https://github.com/sonydevworld/spresense-nuttx/blob/new-master/include/assert.h:

#define ASSERT(f)        do { if (!(f)) PANIC(); } while (0)

This version of assert() cannot be used in an expression context, such as a comma expression. It can only be used in statement context.

We are merging recent changes from upstream MicroPython into CircuitPython, and there are several places where MicroPython uses assert() in a comma expression here:
https://github.com/micropython/micropython/blob/05cb1406ad1b421a238faf763e19f4119f5f6bb2/py/obj.h#L921-L926

#define mp_type_assert_not_bool_int_str_nonetype(t) (                                     \
    MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_bool), assert((t) != &mp_type_bool),         \
    MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_int), assert((t) != &mp_type_int),           \
    MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_str), assert((t) != &mp_type_str),           \
    MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_NoneType), assert((t) != &mp_type_NoneType), \
    1)

This doesn't work with the statement-only do ... while(0) version of assert() above, and so we can't build the spresense port in in our merge branch right now.

man 3 assert says assert() has the signature void assert(scalar expression);, though it might be implemented as a macro.

The other versions of assert() in the other CircuitPython and MicroPython can be used in expressions.

I am not sure how to fix this: the particular macro above is tricky and hard to rewrite with the nuttx assert. The nuttx assert could be modified, but that is a change in that submodule. Or a new version of assert() could be introduced to substitute for the nuttx version.

What do you think? Thanks.

We don't have a merge PR yet, but @tannewt and I are working in https://github.com/dhalbert/circuitpython/tree/v1.20-merge

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions