Skip to content

Commit edb3b44

Browse files
committed
[NFC] [C++20] [Modules] Add more lambda tests
Add more lambda tests in modules. This is useful when we started to work on #57222.
1 parent 5f68c41 commit edb3b44

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

clang/test/Modules/lambdas.cppm

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: split-file %s %t
4+
//
5+
// RUN: %clang_cc1 -std=c++20 %t/lambdas.cppm -emit-module-interface \
6+
// RUN: -o %t/lambdas.pcm
7+
// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/Use.cpp -fsyntax-only \
8+
// RUN: -verify
9+
//
10+
// RUN: %clang_cc1 -std=c++20 %t/lambdas2.cppm -emit-module-interface \
11+
// RUN: -o %t/lambdas2.pcm
12+
// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/Use.cpp -fsyntax-only \
13+
// RUN: -verify -DUSE_LAMBDA2
14+
15+
//--- lambdas.h
16+
auto l1 = []() constexpr -> int {
17+
return 43;
18+
};
19+
//
20+
auto l2 = []() constexpr -> double {
21+
return 3.0;
22+
};
23+
//
24+
auto l3 = [](auto i) constexpr -> int {
25+
return int(i);
26+
};
27+
//
28+
auto l4 = [](auto i, auto u) constexpr -> int {
29+
return i + u;
30+
};
31+
32+
//--- lambdas.cppm
33+
module;
34+
#include "lambdas.h"
35+
export module lambdas;
36+
export using ::l1;
37+
export using ::l2;
38+
export using ::l3;
39+
export using ::l4;
40+
41+
//--- lambdas2.cppm
42+
export module lambdas2;
43+
export {
44+
#include "lambdas.h"
45+
}
46+
47+
//--- Use.cpp
48+
// expected-no-diagnostics
49+
#ifndef USE_LAMBDA2
50+
import lambdas;
51+
#else
52+
import lambdas2;
53+
#endif
54+
55+
static_assert(l1.operator()() == 43);
56+
57+
static_assert(l2.operator()() == 3.0);
58+
59+
static_assert(l3.operator()(8.4) == 8);
60+
61+
static_assert(l4(4, 12) == 16);
62+
static_assert(l4(5, 20) == 25);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: split-file %s %t
4+
//
5+
// RUN: %clang_cc1 -std=c++20 %t/template_lambdas.cppm -emit-module-interface \
6+
// RUN: -o %t/lambdas.pcm
7+
// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/Use.cpp -fsyntax-only \
8+
// RUN: -verify
9+
//
10+
// RUN: %clang_cc1 -std=c++20 %t/template_lambdas2.cppm -emit-module-interface \
11+
// RUN: -o %t/lambdas2.pcm
12+
// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/Use.cpp -fsyntax-only \
13+
// RUN: -verify -DUSE_LAMBDA2
14+
15+
//--- lambdas.h
16+
auto l1 = []<int I>() constexpr -> int {
17+
return I;
18+
};
19+
20+
auto l2 = []<auto I>() constexpr -> decltype(I) {
21+
return I;
22+
};
23+
24+
auto l3 = []<class T>(auto i) constexpr -> T {
25+
return T(i);
26+
};
27+
28+
auto l4 = []<template<class> class T, class U>(T<U>, auto i) constexpr -> U {
29+
return U(i);
30+
};
31+
32+
//--- template_lambdas.cppm
33+
module;
34+
#include "lambdas.h"
35+
export module lambdas;
36+
export using ::l1;
37+
export using ::l2;
38+
export using ::l3;
39+
export using ::l4;
40+
41+
//--- template_lambdas2.cppm
42+
export module lambdas2;
43+
export {
44+
#include "lambdas.h"
45+
}
46+
47+
//--- Use.cpp
48+
// expected-no-diagnostics
49+
#ifndef USE_LAMBDA2
50+
import lambdas;
51+
#else
52+
import lambdas2;
53+
#endif
54+
55+
static_assert(l1.operator()<5>() == 5);
56+
static_assert(l1.operator()<6>() == 6);
57+
58+
static_assert(l2.operator()<7>() == 7);
59+
static_assert(l2.operator()<nullptr>() == nullptr);
60+
61+
static_assert(l3.operator()<int>(8.4) == 8);
62+
static_assert(l3.operator()<int>(9.9) == 9);
63+
64+
template<typename T>
65+
struct DummyTemplate { };
66+
67+
static_assert(l4(DummyTemplate<float>(), 12) == 12.0);
68+
static_assert(l4(DummyTemplate<int>(), 19.8) == 19);

0 commit comments

Comments
 (0)