Skip to content

[Clang] Unhelpful ambiguous __attribute__((overloadable)) diagnostic #157353

@Sirraide

Description

@Sirraide

Consider (https://godbolt.org/z/b3jGaMPPb):

struct a {};
struct b {};

__attribute__((overloadable)) void f(struct a*) { return; }
__attribute__((overloadable)) void f(struct b*) { return; }

void g() {
    const struct a x = {};
    f(&x);
}

Because passing incompatible pointers to a function and discarding const are not errors in C, we consider both of these to be ambiguous; the result of this is a diagnostic that is rather unhelpful:

<source>:9:5: error: call to 'f' is ambiguous
    9 |     f(&x);
      |     ^
<source>:4:36: note: candidate function
    4 | __attribute__((overloadable)) void f(struct a*) { return; }
      |                                    ^
<source>:5:36: note: candidate function
    5 | __attribute__((overloadable)) void f(struct b*) { return; }
      |                                    ^

I wonder if we could just disallow such conversions if __attribute__((overloadable)) is used.

Ideally, we’d make -Wincompatible-pointer-types-discards-qualifiers default to an error, but I have a feeling this would run into the same problems that -Wreturn-type did, that is there being too much code that doesn’t care and just does this anyway irrespective of the warnings.

However, GCC 14 did make -Wincompatible-pointer-types an error, and I think it’d be reasonable for us to do the same since that’s a lot more egregious (whereas -Wincompatible-pointer-types-discards-qualifiers could simply happen because a lot of C code isn’t const-correct).

Metadata

Metadata

Assignees

No one assigned

    Labels

    cclang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerclang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions