You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Generated C++ for auto casts to non-class types can name an ambiguous or hidden typedef #635
A C++23 cast written with auto to a non-class type, as in auto{x} or auto(x), is put out by the C++-generating back end as a C-style cast to the deduced type. If that type was written through a typedef, the cast names the typedef, which may not denote the same type (or may be ambiguous) where the cast appears. The generated C++ is then ill-formed, even though the source is valid.
PR #579 (for #576) makes the back end put out auto casts as written, but only for those that have a dynamic initialization, i.e., casts to class types. Casts to non-class types are not recorded as auto casts in the IL.
Reproducing Source Code
inlinenamespace {
usingINT = int;
usingPTR = int*;
enum E { e0 };
usingEN = E;
autof(INT i) { return i; }
autop(PTR q) { return q; }
autog(EN x) { return x; }
INT gi = 1;
}
usingINT = void;
usingPTR = void;
usingEN = void;
auto v1 = auto{f(0)};
auto v2 = auto(f(0));
auto v3 = auto{p(nullptr)};
auto v4 = auto{g(e0)};
auto v5 = auto{gi};
Command-line Options
cpfe-cp --c++23 --gen_c_file_name t.int.cpp t.cpp
Type of Issue
[Back End] There is a problem with the generated C++ code.
Additional Details
GCC and Clang accept the source, as does cpfe --c++23 (without C++ generation).
The generated C++ for the last five lines is:
auto v1 = (INT)f(0);
auto v2 = (INT)f(0);
auto v3 = (PTR)p(nullptr);
auto v4 = (EN)g(e0);
auto v5 = (INT)gi;
which GCC rejects (reference to 'INT' is ambiguous, etc.): At file scope, INT names both ::INT and the INT of the inline unnamed namespace.
When the deduced type is not written through a typedef, the result is fine. For example, int h(int n) { return auto{n} + auto(n); } becomes int h(int n) { return (int)n + (int)n; }.
The problem occurs on main and with PR #579. Reported by @nv-cmeerw in the review of PR #579.
Activity
Generate auto casts to non-class types as written [GH #635]