llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.69k stars 11.86k forks source link

Improve diagnostics with incompatible VLA types #99914

Open AaronBallman opened 3 months ago

AaronBallman commented 3 months ago

Consider an example like the following, compiled in C++:

void func(int n) {
    int grp[n][n];
    int (*ptr)[n];

    for (int i = 0; i < n; i++)
        ptr = &grp[i];
}

https://godbolt.org/z/oET6jjjzj

This emits a very confusing diagnostic: error: incompatible pointer types assigning to 'int (*)[n]' from 'int (*)[n]' and users are understandably confused by it: https://discourse.llvm.org/t/clang-pointer-assignment-error-message/80265

It would be better to identify when both types are VLAs with the same bounds, and instead of repeating identical types, issue a diagnostic specific to VLAs. e.g., error: incompatible assignment of pointers of variable-length array type 'int (*)[n]'; consider using a typedef to use the same variable-length array type for both operands or something along those lines.

sookach commented 3 months ago

I'll gladly work on this issue, feel free to assign to me.

AaronBallman commented 3 months ago

I'll gladly work on this issue, feel free to assign to me.

Thank you! There may be several diagnostics that could use the same kind of updating. Bonus points if we can find a way to make this automated by the diagnostics engine when printing type differences so that folks writing diagnostics don't have to think about VLAs as a special wording case.