www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22699] New: importC: assignment cannot be used as a condition

https://issues.dlang.org/show_bug.cgi?id=22699

          Issue ID: 22699
           Summary: importC: assignment cannot be used as a condition
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: dave287091 gmail.com

C has the error-prone feature of assignment being an expression.

For example:

// assignexpr.c
int x;
int foo(int y){
    if (x = y) // Error: assignment cannot be used as a condition, perhaps `==`
was meant?
        return 3;
    return 0;
}

The CoreFoundation framework on macOS has a header with an inline function that
uses this feature:

static inline __attribute__((always_inline)) 
void CFStringInitInlineBuffer(CFStringRef str, CFStringInlineBuffer *buf,
CFRange range) {
    buf->theString = str;
    buf->rangeToBuffer = range;
    buf->directCStringBuffer = (buf->directUniCharBuffer =
CFStringGetCharactersPtr(str)) ? ((void*)0) : CFStringGetCStringPtr(str,
kCFStringEncodingASCII);
    buf->bufferedRangeStart = buf->bufferedRangeEnd = 0;
}

--
Jan 22 2022