xref: /llvm-project/clang/test/Modules/template-lambdas.cppm (revision da00c60dae0040185dc45039c4397f6e746548e9)
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// Test again with reduced BMI
16// RUN: rm -rf %t
17// RUN: mkdir -p %t
18// RUN: split-file %s %t
19//
20// RUN: %clang_cc1 -std=c++20 %t/template_lambdas.cppm -emit-reduced-module-interface \
21// RUN:    -o %t/lambdas.pcm
22// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/Use.cpp -fsyntax-only \
23// RUN:    -verify
24//
25// RUN: %clang_cc1 -std=c++20 %t/template_lambdas2.cppm -emit-reduced-module-interface \
26// RUN:    -o %t/lambdas2.pcm
27// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/Use.cpp -fsyntax-only \
28// RUN:    -verify -DUSE_LAMBDA2
29
30//--- lambdas.h
31auto l1 = []<int I>() constexpr -> int {
32    return I;
33};
34
35auto l2 = []<auto I>() constexpr -> decltype(I) {
36    return I;
37};
38
39auto l3 = []<class T>(auto i) constexpr -> T {
40  return T(i);
41};
42
43auto l4 = []<template<class> class T, class U>(T<U>, auto i) constexpr -> U {
44  return U(i);
45};
46
47//--- template_lambdas.cppm
48module;
49#include "lambdas.h"
50export module lambdas;
51export using ::l1;
52export using ::l2;
53export using ::l3;
54export using ::l4;
55
56//--- template_lambdas2.cppm
57export module lambdas2;
58export {
59#include "lambdas.h"
60}
61
62//--- Use.cpp
63// expected-no-diagnostics
64#ifndef USE_LAMBDA2
65import lambdas;
66#else
67import lambdas2;
68#endif
69
70static_assert(l1.operator()<5>() == 5);
71static_assert(l1.operator()<6>() == 6);
72
73static_assert(l2.operator()<7>() == 7);
74static_assert(l2.operator()<nullptr>() == nullptr);
75
76static_assert(l3.operator()<int>(8.4) == 8);
77static_assert(l3.operator()<int>(9.9) == 9);
78
79template<typename T>
80struct DummyTemplate { };
81
82static_assert(l4(DummyTemplate<float>(), 12) == 12.0);
83static_assert(l4(DummyTemplate<int>(), 19.8) == 19);
84