Wrong diagnostic for ternary operator

This is a simplification of some code I let clang loose on:

#include <stdio.h>

int
main(void) {
        int test = 0;

        printf("Type is %s\n", (test >= 1 ? "short" : "char"));

        return (0);
}

It comes up with a diagnostic that's misleading upon first read.

t.c:7:36: error: incompatible operand types ('char *' and 'char *')
        printf("Type is %s\n", (test >= 1 ? "short" : "char"));
                                          ^ ~~~~~~~ ~~~~~~
1 diagnostic generated.

I think the average developer will wonder why "short" and "char" are
incompatible, especially since the tildes draw attention to that. The accent
circumflex gets lost in the noise.

The code in question used this to toggle which text to use in a %s argument to
a printf() call and is quite valid.

Jeroen Ruigrok van der Werven wrote:-

This is a simplification of some code I let clang loose on:

#include <stdio.h>

int
main(void) {
        int test = 0;

        printf("Type is %s\n", (test >= 1 ? "short" : "char"));

        return (0);
}

It comes up with a diagnostic that's misleading upon first read.

t.c:7:36: error: incompatible operand types ('char *' and 'char *')
        printf("Type is %s\n", (test >= 1 ? "short" : "char"));
                                          ^ ~~~~~~~ ~~~~~~
1 diagnostic generated.

I think the average developer will wonder why "short" and "char" are
incompatible, especially since the tildes draw attention to that. The accent
circumflex gets lost in the noise.

The code in question used this to toggle which text to use in a %s argument to
a printf() call and is quite valid.

I'd guess operand decay isn't happening.

Neil.

Jeroen Ruigrok van der Werven wrote:-

This is a simplification of some code I let clang loose on:

#include <stdio.h>

int
main(void) {
        int test = 0;

        printf("Type is %s\n", (test >= 1 ? "short" : "char"));

        return (0);
}

It comes up with a diagnostic that's misleading upon first read.

t.c:7:36: error: incompatible operand types ('char *' and 'char *')
        printf("Type is %s\n", (test >= 1 ? "short" : "char"));
                                          ^ ~~~~~~~ ~~~~~~
1 diagnostic generated.

I think the average developer will wonder why "short" and "char" are
incompatible, especially since the tildes draw attention to that. The accent
circumflex gets lost in the noise.

The code in question used this to toggle which text to use in a %s argument to
a printf() call and is quite valid.

I'd guess operand decay isn't happening.

The bug was more primitive...I was missing a return statement.

There is still a FIXME (need to return the "composite" type).

snaroff

Thanks for the bug report. Just checked in a fix...

snaroff

Thanks Steve,

still need to dig around the code more and grok it. So in the mean time I will
keep pestering you guys with test cases like this. :slight_smile:

Thanks for the bug report. Just checked in a fix...

Thanks Steve,

still need to dig around the code more and grok it. So in the mean time I will
keep pestering you guys with test cases like this. :slight_smile:

Much appreciated (I don't feel pestered:-).

I fully expect more type checking bugs (the code is fairly new and hasn't
received much air time).

Keep the test cases coming!

snaroff