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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! Per-file declaration-scope facts extracted from a [`ParsedFile`].
//!
//! Collects each scope's items, raw reference relations, `callee-before-caller` ordering edges,
//! public-root ownership, recursion groups, topological levels, and ordering findings for a single
//! parsed file. Multi-file project resolution lives in `crate::code_lint::graph`.
use super::call_clusters::{
CallClusterRule, DeclarationScope, DeclarationScopeKind, OrderingEdge, OrderingReason,
PlacementAnchor, RelationFact, ScopeGraph, ScopeItem, ScopeItemKind, Visibility,
collect_declaration_scopes, scope_findings,
};
use super::tree::ParsedFile;
/// Everything the analyzer knows about one scope (`Module`, `Class`, or `Impl`) in a file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScopeFacts {
/// Which syntactic container this scope covers.
pub scope: DeclarationScopeKind,
/// Declarations in source order. Every index in [`ScopeFacts`] (`roots`, `source`, `target`,
/// `higher`, `lower`) indexes into this slice.
pub items: Vec<ItemFacts>,
/// Direct intra-scope references, labeled by where the reference occurs.
pub relations: Vec<RelationFact>,
/// Downward `callee-before-caller` ordering edges (`higher -> lower`), labeled by why `higher`
/// sits above `lower`.
pub ordering: Vec<OrderingEdge>,
}
/// One declaration in [`ScopeFacts::items`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ItemFacts {
/// Identifier name of the declaration.
pub name: String,
/// Callable (`fn` / `def`) or type (`struct`, `enum`, `union`, `trait`, `class`).
pub kind: ScopeItemKind,
/// Public or private visibility tier.
pub visibility: Visibility,
/// True if this callable is a lifecycle constructor (`__init__`, `pub fn new`, etc.).
pub is_constructor: bool,
/// 1-based line of the declaration's name identifier.
pub line: usize,
/// 1-based last line of the declaration, including its attached `impl` blocks or grouped
/// overloads and accessors.
pub end_line: usize,
/// 1-based column of the declaration's name identifier.
pub column: usize,
/// Compact one-line signature header (parameters + return type for callables; public fields or
/// variants for types), when available.
pub signature: Option<String>,
/// Sorted indices in [`ScopeFacts::items`] of the public entrypoint units that reach this item
/// through private references (`Roots(f)` with constructors collapsed to the scope's first
/// constructor).
pub roots: Vec<usize>,
/// Strongly connected component id over [`ScopeFacts::ordering`]. Mutual recursion shares an id.
pub recursion_group: usize,
/// Topological level in [`ScopeFacts::ordering`]: `0` for leaves, `1 + max(callee.level)` above
/// them, with a recursion group sharing one level.
pub level: usize,
/// Call-cluster ordering findings reported on this declaration, in rule priority order.
pub findings: Vec<FindingFact>,
}
/// A call-cluster lint finding attached to an [`ItemFacts`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FindingFact {
/// Call-cluster placement rule violated by this item.
pub rule: CallClusterRule,
/// Name of the owning or calling declaration the finding relates to.
pub related: String,
/// Why `related` sits above this item (`callee-before-caller` only).
pub reason: Option<OrderingReason>,
/// Sibling declaration next to which this item belongs.
pub anchor: PlacementAnchor,
}
/// Collects [`ScopeFacts`] for every declaration scope in `file`, in source order.
#[must_use]
pub fn collect_scope_facts(file: &ParsedFile) -> Vec<ScopeFacts> {
collect_declaration_scopes(file)
.iter()
.map(scope_facts)
.collect()
}
fn scope_facts(scope: &DeclarationScope<'_>) -> ScopeFacts {
let items = &scope.items;
let graph = ScopeGraph::new(items);
let mut item_facts: Vec<ItemFacts> = items
.iter()
.enumerate()
.map(|(idx, item)| {
let coordinate = item.name_node.start_coordinate();
ItemFacts {
name: item.name.clone(),
kind: item.kind,
visibility: item.visibility,
is_constructor: item.is_constructor,
line: coordinate.line,
end_line: item.end_line,
column: coordinate.column,
signature: item.signature.clone(),
roots: graph.unit_owners[idx].clone(),
recursion_group: graph.recursion_group[idx],
level: graph.level[idx],
findings: Vec::new(),
}
})
.collect();
for finding in scope_findings(scope, &graph) {
item_facts[finding.item].findings.push(FindingFact {
rule: finding.rule,
related: items[finding.related].name.clone(),
reason: finding.reason,
anchor: finding.anchor,
});
}
ScopeFacts {
scope: scope.kind.clone(),
items: item_facts,
relations: raw_relations(items),
ordering: graph.edges,
}
}
/// Direct intra-scope references (`source != target`), sorted by `(source, target, kind)`.
fn raw_relations(items: &[ScopeItem<'_>]) -> Vec<RelationFact> {
let mut relations: Vec<RelationFact> = items
.iter()
.enumerate()
.flat_map(|(source, item)| {
item.references
.iter()
.filter(move |&&(target, _)| target != source && target < items.len())
.map(move |&(target, kind)| RelationFact {
source,
target,
kind,
})
})
.collect();
relations.sort();
relations.dedup();
relations
}
#[cfg(test)]
mod tests {
use super::super::call_clusters::RelationKind;
use super::*;
use crate::diagnostic::Language;
const SANDWICH: &str = indoc::indoc! {r"
pub fn parse_config(raw: &str) -> usize {
measure(raw)
}
fn measure(raw: &str) -> usize {
let frame = Frame { len: raw.len() };
frame_len(&frame)
}
fn frame_len(frame: &Frame) -> usize {
frame.len
}
struct Frame {
len: usize,
}
"};
const MUTUAL_RECURSION: &str = indoc::indoc! {r"
pub fn evaluate(n: usize) -> bool {
is_even(n)
}
fn is_even(n: usize) -> bool {
if n == 0 { true } else { is_odd(n - 1) }
}
fn is_odd(n: usize) -> bool {
if n == 0 { false } else { is_even(n - 1) }
}
"};
fn only_scope(source: &str) -> ScopeFacts {
let mut scopes = collect_scope_facts(&ParsedFile::rust(source));
assert_eq!(scopes.len(), 1, "expected exactly one scope");
scopes.remove(0)
}
#[test]
fn module_scope_items_roots_and_levels() {
let facts = only_scope(SANDWICH);
let summary: Vec<_> = facts
.items
.iter()
.map(|item| {
(
item.name.as_str(),
item.kind,
item.visibility,
item.roots.as_slice(),
item.level,
)
})
.collect();
assert_eq!(facts.scope, DeclarationScopeKind::Module);
assert_eq!(
summary,
[
(
"parse_config",
ScopeItemKind::Callable,
Visibility::Public,
&[0][..],
3
),
(
"measure",
ScopeItemKind::Callable,
Visibility::Private,
&[0][..],
2
),
(
"frame_len",
ScopeItemKind::Callable,
Visibility::Private,
&[0][..],
0
),
(
"Frame",
ScopeItemKind::Type,
Visibility::Private,
&[0][..],
1
),
]
);
}
#[test]
fn items_carry_their_name_location() {
let facts = only_scope(SANDWICH);
assert_eq!(facts.items[2].line, 10);
assert_eq!(facts.items[2].column, 4);
}
#[test]
fn module_scope_relations_and_ordering() {
let facts = only_scope(SANDWICH);
assert_eq!(
facts.relations,
[
RelationFact {
source: 0,
target: 1,
kind: RelationKind::Body
},
RelationFact {
source: 1,
target: 2,
kind: RelationKind::Body
},
RelationFact {
source: 1,
target: 3,
kind: RelationKind::Body
},
RelationFact {
source: 2,
target: 3,
kind: RelationKind::Parameter
},
]
);
assert_eq!(
facts.ordering,
[
OrderingEdge {
higher: 0,
lower: 1,
reason: OrderingReason::Call
},
OrderingEdge {
higher: 1,
lower: 2,
reason: OrderingReason::Call
},
OrderingEdge {
higher: 1,
lower: 3,
reason: OrderingReason::Encapsulation
},
OrderingEdge {
higher: 3,
lower: 2,
reason: OrderingReason::SignatureContract
},
]
);
}
#[test]
fn findings_attach_to_their_item() {
let facts = only_scope(SANDWICH);
assert_eq!(
facts.items[2].findings,
[FindingFact {
rule: CallClusterRule::CalleeBeforeCaller,
related: "Frame".to_owned(),
reason: Some(OrderingReason::SignatureContract),
anchor: PlacementAnchor {
name: "Frame".to_owned(),
line: 16,
},
}]
);
assert!(
facts
.items
.iter()
.enumerate()
.all(|(idx, item)| idx == 2 || item.findings.is_empty())
);
}
#[test]
fn mutual_recursion_shares_a_group() {
let facts = only_scope(MUTUAL_RECURSION);
assert_eq!(
facts.items[1].recursion_group,
facts.items[2].recursion_group
);
assert_ne!(
facts.items[0].recursion_group,
facts.items[1].recursion_group
);
}
#[test]
fn recursion_group_counts_as_one_level() {
let facts = only_scope(MUTUAL_RECURSION);
assert_eq!(facts.items[1].level, 0);
assert_eq!(facts.items[2].level, 0);
assert_eq!(facts.items[0].level, 1);
}
const RUST_TYPE_PARTS: &str = indoc::indoc! {r"
pub struct Config {
header: Header,
}
struct Header {
len: usize,
}
impl Config {
pub fn size(&self) -> usize {
measure(&self.header)
}
}
fn measure(header: &Header) -> usize {
header.len
}
"};
const PYTHON_TYPE_PARTS: &str = indoc::indoc! {r"
from __future__ import annotations
class Config:
header: Header
def size(self) -> int:
return _measure(self.header)
class Header:
length: int
def _measure(header: Header) -> int:
return header.length
"};
fn relations_from(facts: &ScopeFacts, source: usize) -> Vec<(usize, RelationKind)> {
facts
.relations
.iter()
.filter(|relation| relation.source == source)
.map(|relation| (relation.target, relation.kind))
.collect()
}
#[test]
fn rust_type_splits_composition_from_behavior() {
let facts = collect_scope_facts(&ParsedFile::rust(RUST_TYPE_PARTS)).remove(0);
assert_eq!(
relations_from(&facts, 0),
[(1, RelationKind::Composition), (2, RelationKind::Behavior)]
);
}
#[test]
fn python_class_splits_composition_from_behavior() {
let mut scopes = collect_scope_facts(&ParsedFile::new(PYTHON_TYPE_PARTS, Language::Python));
let facts = scopes.remove(0);
assert_eq!(
relations_from(&facts, 0),
[(1, RelationKind::Composition), (2, RelationKind::Behavior)]
);
}
#[test]
fn type_reached_only_from_impl_is_behavior_not_containment() {
let facts = collect_scope_facts(&ParsedFile::rust(indoc::indoc! {r"
pub struct Report;
impl Report {
pub fn render(&self) -> usize {
Cache::default().len
}
}
#[derive(Default)]
struct Cache {
len: usize,
}
"}))
.remove(0);
assert_eq!(
facts.ordering,
[OrderingEdge {
higher: 0,
lower: 1,
reason: OrderingReason::ImplBehavior
}]
);
}
#[test]
fn cycle_through_public_entrypoint_terminates_with_one_level() {
let facts = only_scope(indoc::indoc! {r"
pub fn evaluate(depth: usize) -> usize {
if depth == 0 { 0 } else { step_one(depth) }
}
fn step_two(depth: usize) -> usize {
evaluate(depth - 1)
}
fn step_one(depth: usize) -> usize {
step_two(depth)
}
"});
assert!(facts.items.iter().all(|item| item.level == 0));
assert!(
facts
.items
.iter()
.all(|item| item.recursion_group == facts.items[0].recursion_group)
);
}
}