Repository navigation
Expand file tree
/
Copy pathreflection.rs
More file actions
100 lines (94 loc) · 3.96 KB
/
Copy pathreflection.rs
File metadata and controls
100 lines (94 loc) · 3.96 KB
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
//! **C++26 static reflection** (P2996), end to end: the file parses, nothing is reported, and `^^T` has the type
//! the standard gives it.
//!
//! No GCC 16 exists on this machine, so the syntax is the proposal's ([P2996R13], as adjusted by [P3687] for C++26:
//! splice *template arguments* were removed, so a bare `[: r :]` in a template argument list is an expression and a
//! type there needs its `typename`). What can be checked here is what a language server must get right without a
//! compiler: the tokens, the tree, the absence of false diagnostics, and the one type that is known by definition.
//!
//! [P2996R13]: https://isocpp.org/files/papers/P2996R13.html
//! [P3687]: https://open-std.org/jtc1/sc22/WG21/docs/papers/2025/p3687r0.html
use cpp_code_analysis::{
CompilerConfig, Known, MemoryFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
file::coordinates::FileOffset,
};
/// The shapes the proposal's own examples write, in one file.
const REFLECTION: &str = "\
struct Widget { int x; double y; };
template <class T> struct Box { T value; };
constexpr auto r = ^^Widget;
constexpr auto m = ^^Widget::x;
using T = typename[:r:];
using S = typename[:^^Box<int>:]<int>;
int main() {
Widget w{};
auto& field = w.[:^^Widget::x:];
auto n = std::meta::identifier_of(^^Widget::x);
return field + n;
}
";
fn session() -> Session<MemoryFiles> {
let files = MemoryFiles::new().with_file("/p/a.cpp", REFLECTION);
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.add_project_files([std::path::PathBuf::from("/p/a.cpp")]);
session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
session.index_everything();
session
}
/// **The file is read, not refused**: no syntax errors at all, in the cooked reading as well as its own text.
#[test]
fn a_reflection_file_has_no_diagnostics() {
let session = session();
let found = session
.diagnostics("/p/a.cpp")
.expect("the file has a reading");
assert_eq!(found.errors.len(), 0, "{:?}", found.errors);
assert!(
found.checks.is_empty(),
"no check findings: {:?}",
found.checks
);
}
/// **`^^T` is a `std::meta::info`**, whatever it reflects — the one expression whose type does not depend on its
/// operand. Read on `^^Widget`'s own token, through the rendering, because that is the reading a request is answered
/// from.
#[test]
fn a_reflection_is_a_std_meta_info() {
let session = session();
let view = session.view("/p/a.cpp").expect("the file is cooked");
let at = REFLECTION
.find("^^Widget;")
.expect("the fixture reflects a type");
let reading = view
.reading_offset_of(FileOffset::new(at))
.expect("the reflection is in the rendering");
match session.type_at(&view, reading) {
Known::Yes(found) => assert_eq!(
found.type_of, "std::meta::info",
"`^^` has one type, whichever operand it takes"
),
other => panic!("a reflection is an expression with a type: {other:?}"),
}
}
/// **A member splice is where a member is**, so a cursor on `w.[: r :]` sits in an expression that the layer reads
/// as a member access — not as an index or a lambda, which is what the two tokens could otherwise be.
#[test]
fn a_member_splice_is_an_expression() {
let session = session();
let view = session.view("/p/a.cpp").expect("the file is cooked");
let at = REFLECTION
.find("w.[:")
.expect("the fixture splices a member");
let reading = view
.reading_offset_of(FileOffset::new(at + 2))
.expect("the splice is in the rendering");
// The question that must not panic or crash: the tree's shape is pinned by the parser's own tests; this asks
// the layer that walks it for an answer at that position.
let _ = session.type_at(&view, reading);
}