[libcxx-commits] [clang] [libcxx] Reapply "[Clang] Implement resolution for CWG1835 (#92957, #98547)" (PR #100425)

Krystian Stasiowski via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jul 26 09:33:25 PDT 2024


sdkrystian wrote:

@AaronBallman @cor3ntin @mizvekov So, I've made improvements to our implementation of `isTemplateArgumentList` that allow us to issue a warning instead of an error for most of the cases where applying the DR breaks existing code:
```cpp
template<int I>
struct A { int x; };

template<int I, bool J>
struct B { int x; };

template<int I, typename T, int J>
struct C { int x; };

template<int I>
constexpr inline int V = I;

int y;

template<typename T>
void f(T t)
{
    t.A<0>::x;               // warning: use 'template' keyword to treat 'A' as a dependent template name
    t.B<1, true>::x;         // warning: use 'template' keyword to treat 'B' as a dependent template name
    t.C<2, int, 4>::x;       // warning: use 'template' keyword to treat 'C' as a dependent template name
    t.A<V<0>>::x;            // warning: use 'template' keyword to treat 'A' as a dependent template name
    t.B<1, V<1>>::x;         // warning: use 'template' keyword to treat 'B' as a dependent template name
    t.C<V<2>, int, V<3>>::x; // warning: use 'template' keyword to treat 'C' as a dependent template name

    t.A<(1 > 2)>::x;         // warning: use 'template' keyword to treat 'A' as a dependent template name
    t.A<(1 < 3)>::x;         // warning: use 'template' keyword to treat 'A' as a dependent template name

    t.A<1 < 4>::x;           // error: no member named 'x' in the global namespace
                             // error: missing 'template' keyword prior to dependent template name 'A'

    t.A<1 > 4>::x;           // error: no member named 'x' in the global namespace

    t.A<0>::y;               // ok, parsed as '((t.A) < 0) > ::y'
}
```
This is accomplished by (attempting to) look past the end of the potential _template-argument-list_, and if the token following the `>` is `::`, we try to parse it as an _id-expression_/type. If it ends up being invalid, then we consider it being an intended template name and we issue the warning.

Since we can look past the potential _template-argument-list_ in most cases, we could probably apply CWG1835 in all language modes with minimal impact on existing code. 

https://github.com/llvm/llvm-project/pull/100425


More information about the libcxx-commits mailing list