brintos

brintos / llvm-project-archived public Read only

0
0
Text · 17.0 KiB · 404b928 Raw
621 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -triple x86_64-unknown-linux -Wno-c++11-narrowing -Wno-literal-conversion -std=c++20 -verify %s2 3namespace test1 {4template <typename T>5struct Foo { T t; };6template <typename U>7using Bar = Foo<U>;8 9Bar s = {1};10}  // namespace test111 12namespace test2 {13template <typename X, typename Y>14struct XYpair {15  X x;16  Y y;17};18// A tricky explicit deduction guide that swapping X and Y.19template <typename X, typename Y>20XYpair(X, Y) -> XYpair<Y, X>;21template <typename U, typename V>22using AliasXYpair = XYpair<U, V>;23 24AliasXYpair xy = {1.1, 2};  // XYpair<int, double>25static_assert(__is_same(decltype(xy.x), int));26static_assert(__is_same(decltype(xy.y), double));27}  // namespace test228 29namespace test3 {30template <typename T, class>31struct container {32  // test with default arguments.33  container(T a, T b = T());34};35 36template <class T>37using vector = container<T, int>;38vector v(0, 0);39}  // namespace test340 41namespace test4 {42// Explicit deduction guide.43template <class T>44struct X {45  T t;46  X(T);47};48 49template <class T>50X(T) -> X<double>;51 52template <class T>53using AX = X<T>;54 55AX s = {1};56static_assert(__is_same(decltype(s.t), double)); // explicit one is picked.57}  // namespace test458 59namespace test5 {60template <int B>61struct Foo {};62// Template parameter pack63template <int... C>64using AF = Foo<1>;65auto a = AF{};66}  // namespace test567 68namespace test6 {69// non-type template argument.70template <typename T, bool B = false>71struct Foo {72  Foo(T);73};74template <typename T>75using AF = Foo<T, 1>;76 77AF b{0};78}  // namespace test679 80namespace test7 {81template <typename T>82struct Foo {83  Foo(T);84};85// using alias chain.86template <typename U>87using AF1 = Foo<U>;88template <typename K>89using AF2 = AF1<K>;90AF2 b = 1;91}  // namespace test792 93namespace test8 {94template <typename T, int N>95struct Foo {96  Foo(T const (&)[N]);97};98 99template <typename X, int Y>100using Bar = Foo<X, Y>;101 102Bar s = {{1}};103}  // namespace test8104 105namespace test9 {106template <typename T, int N>107struct Foo {108  Foo(T const (&)[N]);109};110 111template <typename X, int Y>112using Bar = Foo<X, sizeof(X)>; // expected-note {{candidate template ignored: couldn't infer template argument 'X'}} \113                               // expected-note {{implicit deduction guide declared as 'template <typename X> requires __is_deducible(test9::Bar, test9::Foo<X, sizeof(X)>) Bar(test9::Foo<X, sizeof(X)>) -> test9::Foo<X, sizeof(X)>'}} \114                               // expected-note {{implicit deduction guide declared as 'template <typename X> requires __is_deducible(test9::Bar, test9::Foo<X, sizeof(X)>) Bar(const X (&)[sizeof(X)]) -> test9::Foo<X, sizeof(X)>'}} \115                               // expected-note {{candidate template ignored: constraints not satisfied [with X = int]}} \116                               // expected-note {{cannot deduce template arguments for 'test9::Bar' from 'test9::Foo<int, sizeof(int)>'}}117 118 119Bar s = {{1}}; // expected-error {{no viable constructor or deduction guide }}120}  // namespace test9121 122namespace test10 {123template <typename T>124struct Foo {125  template <typename U>126  Foo(U);127};128 129template <typename U>130Foo(U) -> Foo<U*>;131 132template <typename K>133using A = Foo<K>;134A a(2);  // Foo<int*>135}  // namespace test10136 137namespace test11 {138struct A {};139template<class T> struct Foo { T c; };140template<class X, class Y=A>141using AFoo = Foo<Y>; // expected-note {{candidate template ignored: could not match 'test11::Foo<Y>' against 'int'}} \142                    // expected-note {{implicit deduction guide declared as 'template <class Y = A> requires __is_deducible(test11::AFoo, test11::Foo<Y>) AFoo(test11::Foo<Y>) -> test11::Foo<Y>'}} \143                    // expected-note {{candidate template ignored: constraints not satisfied [with Y = int]}} \144                    // expected-note {{cannot deduce template arguments for 'test11::AFoo' from 'test11::Foo<int>'}} \145                    // expected-note {{implicit deduction guide declared as 'template <class Y = A> requires __is_deducible(test11::AFoo, test11::Foo<Y>) AFoo(Y) -> test11::Foo<Y>'}} \146                    // expected-note {{candidate function template not viable: requires 0 arguments, but 1 was provided}} \147                    // expected-note {{implicit deduction guide declared as 'template <class Y = A> requires __is_deducible(test11::AFoo, test11::Foo<Y>) AFoo() -> test11::Foo<Y>'}}148 149AFoo s = {1}; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'AFoo'}}150} // namespace test11151 152namespace test12 {153// no crash on null access attribute154template<typename X>155struct Foo {156  template<typename K>157  struct Bar {158    Bar(K);159  };160 161  template<typename U>162  using ABar = Bar<U>;163  void test() { ABar k = 2; }164};165 166void func(Foo<int> s) {167  s.test();168}169} // namespace test12170 171namespace test13 {172template <typename... Ts>173struct Foo {174  Foo(Ts...);175};176 177template <typename... Ts>178using AFoo = Foo<Ts...>;179 180auto b = AFoo{};181AFoo a(1, 2);182 183template <typename T>184using BFoo = Foo<T, T>;185BFoo b2(1.0, 2.0);186} // namespace test13187 188namespace test14 {189template<typename T>190concept IsInt = __is_same(decltype(T()), int);191 192template<IsInt T, int N>193struct Foo {194  Foo(T const (&)[N]);195};196 197template <int K>198using Bar = Foo<double, K>; // expected-note {{constraints not satisfied for class template 'Foo'}}199// expected-note@-1 {{candidate template ignored: could not match}} expected-note@-1 {{candidate template ignored: constraints not satisfied}}200// expected-note@-2 {{implicit deduction guide declared as 'template <int K> requires __is_deducible(test14::Bar, test14::Foo<double, K>) Bar(test14::Foo<double, K>) -> test14::Foo<double, K>'}}201// expected-note@-3 {{implicit deduction guide declared as 'template <int K> requires __is_deducible(test14::Bar, test14::Foo<double, K>) Bar(const double (&)[K]) -> test14::Foo<double, K>'}}202double abc[3];203Bar s2 = {abc}; // expected-error {{no viable constructor or deduction guide for deduction }}204} // namespace test14205 206namespace test15 {207template <class T> struct Foo { Foo(T); };208 209template<class V> using AFoo = Foo<V *>;210template<typename> concept False = false; // #test15_False211template<False W>212using BFoo = AFoo<W>; // expected-note {{candidate template ignored: constraints not satisfied [with W = int]}} \213                      // expected-note@-1 {{because 'int' does not satisfy 'False'}} \214                      // expected-note@#test15_False {{because 'false' evaluated to false}} \215                      // expected-note {{implicit deduction guide declared as 'template <False<> W> requires __is_deducible(test15::AFoo, test15::Foo<W *>) && __is_deducible(test15::BFoo, test15::Foo<W *>) BFoo(W *) -> test15::Foo<W *>}} \216                      // expected-note {{candidate template ignored: could not match 'test15::Foo<W *>' against 'int *'}} \217                      // expected-note {{template <False<> W> requires __is_deducible(test15::AFoo, test15::Foo<W *>) && __is_deducible(test15::BFoo, test15::Foo<W *>) BFoo(test15::Foo<W *>) -> test15::Foo<W *>}}218int i = 0;219AFoo a1(&i); // OK, deduce Foo<int *>220 221// the W is not deduced from the deduced type Foo<int *>.222BFoo b2(&i); // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'BFoo'}}223} // namespace test15224 225namespace test16 {226struct X { X(int); X(const X&); };227template<class T>228struct Foo {229  T t;230  Foo(T t) : t(t) {}231};232template<class T>233using AFoo = Foo<T>;234int i = 0;235AFoo s{i};236static_assert(__is_same(decltype(s.t), int));237 238template<class T>239using BFoo = AFoo<T>;240 241// template explicit deduction guide.242template<class T>243Foo(T) -> Foo<float>;244static_assert(__is_same(decltype(AFoo(i).t), float));245static_assert(__is_same(decltype(BFoo(i).t), float));246 247// explicit deduction guide.248Foo(int) -> Foo<X>;249static_assert(__is_same(decltype(AFoo(i).t), X));250static_assert(__is_same(decltype(BFoo(i).t), X));251 252Foo(double) -> Foo<int>;253static_assert(__is_same(decltype(AFoo(1.0).t), int));254static_assert(__is_same(decltype(BFoo(1.0).t), int));255} // namespace test16256 257namespace test17 {258template <typename T>259struct Foo { T t; };260 261// CTAD for alias templates only works for the RHS of the alias of form of262//  [typename] [nested-name-specifier] [template] simple-template-id263template <typename U>264using AFoo = Foo<U>*; // expected-note {{template is declared here}}265 266AFoo s = {1}; // expected-error {{alias template 'AFoo' requires template arguments; argument deduction only allowed for}}267} // namespace test17268 269namespace test18 {270template<typename T>271concept False = false; // expected-note {{because 'false' evaluated to false}}272 273template <typename T> struct Foo { T t; };274 275template<typename T> requires False<T> // expected-note {{because 'int' does not satisfy 'False'}}276Foo(T) -> Foo<int>;277 278template <typename U>279using Bar = Foo<U>; // expected-note {{could not match 'test18::Foo<U>' against 'int'}} \280                    // expected-note {{implicit deduction guide declared as 'template <typename U> requires __is_deducible(test18::Bar, test18::Foo<U>) Bar(test18::Foo<U>) -> test18::Foo<U>'}} \281                    // expected-note {{candidate template ignored: constraints not satisfied}} \282                    // expected-note {{implicit deduction guide declared as 'template <typename T> requires False<T> && __is_deducible(test18::Bar, Foo<int>) Bar(T) -> Foo<int>'}} \283                    // expected-note {{candidate function template not viable}} \284                    // expected-note {{implicit deduction guide declared as 'template <typename U> requires __is_deducible(test18::Bar, test18::Foo<U>) Bar() -> test18::Foo<U>'}}285 286Bar s = {1}; // expected-error {{no viable constructor or deduction guide for deduction of template arguments}}287} // namespace test18288 289// GH85406, verify no crash on invalid alias templates.290namespace test19 {291template <typename T>292class Foo {};293 294template <typename T>295template <typename K>296using Bar2 = Foo<K>; // expected-error {{extraneous template parameter list in alias template declaration}}297 298Bar2 b = 1; // expected-error {{no viable constructor or deduction guide for deduction of template arguments}}299} // namespace test19300 301// GH85385302namespace test20 {303template <template <typename> typename T>304struct K {};305 306template <typename U>307class Foo {};308 309// Verify that template template type parameter TTP is referenced/used in the310// template arguments of the RHS.311template <template<typename> typename TTP>312using Bar = Foo<K<TTP>>; // expected-note {{candidate template ignored: could not match 'test20::Foo<K<TTP>>' against 'int'}} \313                        // expected-note {{implicit deduction guide declared as 'template <template <typename> typename TTP> requires __is_deducible(test20::Bar, test20::Foo<K<TTP>>) Bar(test20::Foo<K<TTP>>) -> test20::Foo<K<TTP>>'}}314 315template <class T>316class Container {};317Bar t = Foo<K<Container>>();318 319Bar s = 1; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of}}320} // namespace test20321 322namespace test21 {323template <typename T, unsigned N>324struct Array { const T member[N]; };325template <unsigned N>326using String = Array<char, N>;327 328// Verify no crash on constructing the aggregate deduction guides.329String s("hello");330} // namespace test21331 332// GH89013333namespace test22 {334class Base {};335template <typename T>336class Derived final : public Base {};337 338template <typename T, typename D>339requires __is_base_of(Base, D)340struct Foo {341  explicit Foo(D) {}342};343 344template <typename U>345using AFoo = Foo<int, Derived<U>>;346 347AFoo a(Derived<int>{});348} // namespace test22349 350namespace test23 {351// We have an aggregate deduction guide "G(T) -> G<T>".352template<typename T>353struct G { T t1; };354 355template<typename X = int>356using AG = G<int>;357 358AG ag(1.0);359// Verify that the aggregate deduction guide "AG(int) -> AG<int>" is built and360// choosen.361static_assert(__is_same(decltype(ag.t1), int));362} // namespace test23363 364// GH90177365// verify that the transformed require-clause of the alias deduction gudie has366// the right depth info.367namespace test24 {368class Forward;369class Key {};370 371template <typename D>372constexpr bool C = sizeof(D);373 374// Case1: the alias template and the underlying deduction guide are in the same375// scope.376template <typename T>377struct Case1 {378  template <typename U>379  struct Foo {380    Foo(U);381  };382 383  template <typename V>384  requires (C<V>)385  Foo(V) -> Foo<V>;386 387  template <typename Y>388  using Alias = Foo<Y>;389};390// The require-clause should be evaluated on the type Key.391Case1<Forward>::Alias t2 = Key();392 393 394// Case2: the alias template and underlying deduction guide are in different395// scope.396template <typename T>397struct Foo {398  Foo(T);399};400template <typename U>401requires (C<U>)402Foo(U) -> Foo<U>;403 404template <typename T>405struct Case2 {406  template <typename Y>407  using Alias = Foo<Y>;408};409// The require-caluse should be evaluated on the type Key.410Case2<Forward>::Alias t1 = Key();411 412// Case3: crashes on the constexpr evaluator due to the mixed-up depth in413// require-expr.414template <class T1>415struct A1 {416  template<class T2>417  struct A2 {418    template <class T3>419    struct Foo {420      Foo(T3);421    };422    template <class T3>423    requires C<T3>424    Foo(T3) -> Foo<T3>;425  };426};427template <typename U>428using AFoo = A1<int>::A2<int>::Foo<U>;429AFoo case3(1);430 431// Case4: crashes on the constexpr evaluator due to the mixed-up index for the432// template parameters `V`.433template<class T, typename T2>434struct Case4 {435  template<class V> requires C<V>436  Case4(V, T);437};438 439template<class T2>440using ACase4 = Case4<T2, T2>;441ACase4 case4{0, 1};442 443} // namespace test24444 445namespace test25 {446 447template<typename T, typename...Us>448struct A{449  template<typename V> requires __is_same(V, int)450  A(V);451};452 453template<typename...TS>454using AA = A<int, TS...>;455 456template<typename...US>457using BB = AA<US...>; // #test25_BB458 459BB a{0};460static_assert(__is_same(decltype(a), A<int>));461// FIXME: The template parameter list of generated deduction guide is not strictly conforming,462// as the pack occurs prior to the non-packs.463BB b{0, 1};464// expected-error@-1 {{no viable}}465// expected-note@#test25_BB 2{{not viable}}466// expected-note@#test25_BB {{template <typename ...US, typename V> requires __is_same(V, int) && __is_deducible(test25::AA, test25::A<int, US...>) && __is_deducible(test25::BB, test25::A<int, US...>) BB(V) -> test25::A<int, US...>}}467// expected-note@#test25_BB {{implicit deduction guide}}468 469}470 471namespace GH92212 {472template<typename T, typename...Us>473struct A{474  template<typename V> requires __is_same(V, int)475  A(V);476};477 478template<typename...TS>479using AA = A<int, TS...>;480AA a{0};481}482 483namespace GH94927 {484template <typename T>485struct A {486  A(T);487};488A(int) -> A<char>;489 490template <typename U>491using B1 = A<U>;492B1 b1(100); // deduce to A<char>;493static_assert(__is_same(decltype(b1), A<char>));494 495template <typename U>496requires (!__is_same(U, char)) // filter out the explicit deduction guide.497using B2 = A<U>;498template <typename V>499using B3 = B2<V>;500 501B2 b2(100); // deduced to A<int>;502static_assert(__is_same(decltype(b2), A<int>));503B3 b3(100); // decuded to A<int>;504static_assert(__is_same(decltype(b3), A<int>));505 506 507// the nested case508template <typename T1>509struct Out {510  template <typename T2>511  struct A {512    A(T2);513  };514  A(int) -> A<T1>;515 516  template <typename T3>517  using B = A<T3>;518};519 520Out<float>::B out(100); // deduced to Out<float>::A<float>;521static_assert(__is_same(decltype(out), Out<float>::A<float>));522}523 524namespace GH111508 {525 526template <typename V> struct S {527  using T = V;528  T Data;529};530 531template <typename V> using Alias = S<V>;532 533Alias A(42);534 535} // namespace GH111508536 537namespace GH113518 {538 539template <class T, unsigned N> struct array {540  T value[N];541};542 543template <typename Tp, typename... Up>544array(Tp, Up...) -> array<Tp, 1 + sizeof...(Up)>;545 546template <typename T> struct ArrayType {547  template <unsigned size> using Array = array<T, size>;548};549 550template <ArrayType<int>::Array array> void test() {}551 552void foo() { test<{1, 2, 3}>(); }553 554} // namespace GH113518555 556// FIXME: This is accepted by GCC: https://gcc.godbolt.org/z/f3rMfbacz557namespace GH125821 {558template<typename T>559struct A { A(T){} };560 561template<typename T>562using Proxy = T;563 564template<typename T>565using C = Proxy< A<T> >;566 567C test{ 42 }; // expected-error {{no viable constructor or deduction guide for deduction of template arguments}}568 569} // namespace GH125821570 571namespace GH133132 {572 573template <class T>574struct A {};575 576template <class T>577using Foo = A<A<T>>;578 579template <class T>580using Bar = Foo<T>;581 582template <class T = int>583using Baz = Bar<T>;584 585Baz a{};586static_assert(__is_same(decltype(a), A<A<int>>));587 588} // namespace GH133132589 590namespace GH131408 {591 592struct Node {};593 594template <class T, Node>595struct A {596    A(T) {}597};598 599template <class T>600using AA = A<T, {}>;601 602AA a{0};603 604static_assert(__is_same(decltype(a), A<int, Node{}>));605}606 607namespace GH130604 {608template <typename T> struct A {609    A(T);610};611 612template <typename T, template <typename> class TT = A> using Alias = TT<T>; // #gh130604-alias613template <typename T> using Alias2 = Alias<T>;614 615Alias2 a(42);616// expected-error@-1 {{no viable constructor or deduction guide for deduction of template arguments of 'Alias2'}}617Alias  b(42);618// expected-error@-1 {{alias template 'Alias' requires template arguments; argument deduction only allowed for class templates or alias template}}619// expected-note@#gh130604-alias {{template is declared here}}620}621