1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! **What a concept requires of its parameter** — the member list a constrained template parameter has.
//!
//! `auto f(A auto a) { a. }` has no class to list members of: `a` is a template parameter, and the only statement of
//! what it can do is the concept's own `requires` body. These tests pin that the completion reads it — and that a
//! parameter with no constraint still offers nothing, which is the difference between "the concept says so" and a
//! guess.
use cpp_code_analysis::file::coordinates::FileOffset;
use cpp_code_analysis::{
CompilerConfig, ItemKind, MemoryFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
};
const SOURCE: &str = "\
template <class T>
concept B = requires(T b) {
{ b.bar() };
};
template <class T>
concept A = requires(T a) {
{ a.foo() } -> B;
a.count;
};
template <class T>
concept HasFoo = requires(T t) {
{ t.foo() };
};
template <class T>
concept HasBar = requires(T t) {
t.bar;
};
template <class T>
concept Both = HasFoo<T> && HasBar<T>;
template <class T>
concept LoopA = LoopB<T>;
template <class T>
concept LoopB = LoopA<T>;
template <A T>
auto by_head(T t) {
t.
}
auto by_auto(A auto a) {
a.
}
auto by_result(A auto a) {
a.foo().
}
template <Both T>
auto by_both(T t) {
t.
}
template <class T>
requires HasFoo<T>
auto by_clause(T t) {
t.
}
template <LoopA T>
auto by_loop(T t) {
t.
}
template <class T>
concept Shiny = requires(T s) {
s.shine;
};
template <class T>
concept HasGet = requires(T b) {
{ b.get() } -> Shiny;
};
template <class T>
concept HasChain = requires(T a) {
{ a.foo() } -> HasGet;
};
auto by_chain(HasChain auto a) {
a.foo().get().
}
template <class U>
auto unconstrained(U u) {
u.
}
";
fn session_of(source: &str) -> Session<MemoryFiles> {
let memory = MemoryFiles::new().with_file("/p/a.cpp", source);
let providers = SessionFiles::new(OpenDocuments::new(), memory);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.add_project_files([std::path::PathBuf::from("/p/a.cpp")]);
// **A completion is answered from a rendering**, and a rendering is built because something asked — the same
// three steps every test in `tests/completion.rs` states once in its helper.
session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
session.index_everything();
session
}
/// The labels the completion offers after the `.` of `needle`.
fn offered_after(needle: &str) -> Vec<(String, ItemKind)> {
let session = session_of(SOURCE);
let view = session.view("/p/a.cpp").expect("the file has a reading");
let at = view
.written_text()
.expect("the rendering has the file's text")
.find(needle)
.unwrap_or_else(|| panic!("the fixture writes {needle:?}"))
+ needle.len();
let offered = session.completions(&view, FileOffset::new(at));
offered
.items
.into_iter()
.map(|item| (item.label, item.kind))
.collect()
}
/// **`A auto a` gives the members `A` requires** — the abbreviated function template's parameter is a constrained
/// template parameter, and `a.foo()` in the concept is the whole of what says `a` has a `foo`.
#[test]
fn a_constrained_auto_parameter_offers_what_the_concept_requires() {
assert_eq!(
offered_after("auto by_auto(A auto a) {\n a."),
[
("foo".to_string(), ItemKind::Method),
("count".to_string(), ItemKind::Field),
]
);
}
/// **`template <A T>` is the same question one level up** — the parameter's type is a template parameter whose
/// constraint is written in the head, and the answer must not depend on the spelling a file happens to use.
#[test]
fn a_constrained_template_parameter_offers_the_same() {
assert_eq!(
offered_after("auto by_head(T t) {\n t."),
[
("foo".to_string(), ItemKind::Method),
("count".to_string(), ItemKind::Field),
]
);
}
/// **An unconstrained parameter offers nothing**, and that is a fact rather than a gap: no concept has said what its
/// type has, so a list would be invented.
#[test]
fn an_unconstrained_parameter_offers_nothing() {
assert!(offered_after("auto unconstrained(U u) {\n u.").is_empty());
}
/// **The result of a call a concept constrains has the members the result's concept requires** —
/// { a.foo() } -> B is the only statement anywhere of what oo returns, so .foo(). lists B's names.
#[test]
fn the_result_of_a_required_call_offers_the_result_concept() {
assert_eq!(
offered_after("auto by_result(A auto a) {\n a.foo()."),
[("bar".to_string(), ItemKind::Method)]
);
}
/// **A chain of required calls is walked to its end** — `{ a.foo() } -> HasGet` and `{ b.get() } -> Shiny` say what
/// each hop returns, so `a.foo().get().` lists `Shiny`'s names. One hop was the first version; the walk is recursive
/// on the receiver, which is what makes the second one answer.
#[test]
fn a_chain_of_required_calls_is_followed() {
assert_eq!(
offered_after("auto by_chain(HasChain auto a) {\n a.foo().get()."),
[("shine".to_string(), ItemKind::Field)]
);
}
/// **A requires-clause is the same constraint beside the head** — `template <class T> requires HasFoo<T>` says what
/// `template <HasFoo T>` says, and both spellings must answer the same list.
#[test]
fn a_requires_clause_offers_the_same_as_a_head_constraint() {
assert_eq!(
offered_after("auto by_clause(T t) {\n t."),
[("foo".to_string(), ItemKind::Method)]
);
}
/// **A concept written as a conjunction of concepts requires both sets of names** — `concept Both = HasFoo<T> &&
/// HasBar<T>` is how a library states two constraints at once, and answering only the first would list half of what a
/// type has to provide.
#[test]
fn a_concept_made_of_two_concepts_offers_both() {
assert_eq!(
offered_after("auto by_both(T t) {\n t."),
[
("foo".to_string(), ItemKind::Method),
("bar".to_string(), ItemKind::Field),
]
);
}
/// **A cycle between two concepts is not a hang** — `LoopA = LoopB<T>` and `LoopB = LoopA<T>` is a program no
/// compiler accepts, and a completion asked in the meantime must answer (with nothing) rather than walk for ever.
#[test]
fn a_cycle_between_concepts_offers_nothing() {
assert!(offered_after("auto by_loop(T t) {\n t.").is_empty());
}