xref: /llvm-project/clang/test/Modules/concept.cppm (revision da00c60dae0040185dc45039c4397f6e746548e9)
1// RUN: rm -rf %t
2// RUN: mkdir %t
3// RUN: split-file %s %t
4//
5// RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-module-interface -o %t/A.pcm
6// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t -I%t -DDIFFERENT %t/B.cppm -verify
7// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t -I%t %t/B.cppm -verify
8//
9// Testing the behavior of `-fskip-odr-check-in-gmf`
10// RUN: %clang_cc1 -std=c++20 -fskip-odr-check-in-gmf %t/A.cppm -emit-module-interface -o %t/A.pcm
11// RUN: %clang_cc1 -std=c++20 -fskip-odr-check-in-gmf -fprebuilt-module-path=%t -I%t  \
12// RUN:    -DDIFFERENT -DSKIP_ODR_CHECK_IN_GMF %t/B.cppm -verify
13
14//--- foo.h
15#ifndef FOO_H
16#define FOO_H
17
18template <class T>
19concept Range = requires(T &t) { t.begin(); };
20
21template<class _Tp>
22concept __integer_like = true;
23
24template <class _Tp>
25concept __member_size = requires(_Tp &&t) { t.size(); };
26
27template <class First, class Second>
28concept C = requires(First x, Second y) { x + y; };
29
30struct A {
31public:
32  template <Range T>
33  using range_type = T;
34};
35
36struct __fn {
37  template <__member_size _Tp>
38  constexpr __integer_like auto operator()(_Tp&& __t) const {
39    return __t.size();
40  }
41
42  template <__integer_like _Tp, C<_Tp> Sentinel>
43  constexpr _Tp operator()(_Tp &&__t, Sentinel &&last) const {
44    return __t;
45  }
46
47  template <template <class> class H, class S, C<H<S>> Sentinel>
48  constexpr H<S> operator()(H<S> &&__s, Sentinel &&last) const {
49    return __s;
50  }
51
52// Tests that we could find different concept definition indeed.
53#ifndef DIFFERENT
54  template <__integer_like _Tp, __integer_like _Up, C<_Tp> Sentinel>
55  constexpr _Tp operator()(_Tp &&__t, _Up _u, Sentinel &&last) const {
56    return __t;
57  }
58#else
59  template <__integer_like _Tp, __integer_like _Up, C<_Up> Sentinel>
60  constexpr _Tp operator()(_Tp &&__t, _Up _u, Sentinel &&last) const {
61    return __t;
62  }
63#endif
64};
65#endif
66
67//--- A.cppm
68module;
69#include "foo.h"
70export module A;
71
72//--- B.cppm
73module;
74#include "foo.h"
75export module B;
76import A;
77
78#ifdef SKIP_ODR_CHECK_IN_GMF
79// expected-error@B.cppm:* {{call to object of type '__fn' is ambiguous}}
80// expected-note@* 1+{{candidate function}}
81#elif defined(DIFFERENT)
82// expected-error@foo.h:41 {{'__fn::operator()' from module 'A.<global>' is not present in definition of '__fn' provided earlier}}
83// expected-note@* 1+{{declaration of 'operator()' does not match}}
84#else
85// expected-no-diagnostics
86#endif
87
88template <class T>
89struct U {
90  auto operator+(U) { return 0; }
91};
92
93void foo() {
94    A a;
95    struct S {
96        int size() { return 0; }
97        auto operator+(S s) { return 0; }
98    };
99    __fn{}(S());
100    __fn{}(S(), S());
101    __fn{}(S(), S(), S());
102
103    __fn{}(U<int>(), U<int>());
104}
105