Skip to content

Generated C++ for auto casts to non-class types can name an ambiguous or hidden typedef #635

In review

Description

@daveedvdv-nvidia

Issue Summary

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

inline namespace {
  using INT = int;
  using PTR = int*;
  enum E { e0 };
  using EN = E;
  auto f(INT i) { return i; }
  auto p(PTR q) { return q; }
  auto g(EN x) { return x; }
  INT gi = 1;
}
using INT = void;
using PTR = void;
using EN = 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.

Reproducing (Standard) Configuration(s)

linux-gcc-debug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions