ryo-analysis 0.1.0

Code graph and discovery engine for the RYO project
Documentation
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! Reference Integrity Checker - Validates reference consistency after mutations.
//!
//! Checks that no dangling references exist after symbol deletion, renaming,
//! or structural changes (field additions/removals).
//!
//! # Capabilities
//!
//! - Detect references to deleted/renamed symbols
//! - Find struct literals with missing field assignments
//! - Check call site compatibility after method signature changes
//!
//! # Performance
//!
//! Target: < 10ms per mutation check.

use super::{CodeGraphV2, TypeFlowGraphV2};
use crate::symbol::SymbolRegistry;
use crate::SymbolId;
use crate::SymbolKind;

/// Result of reference integrity analysis.
#[derive(Debug, Clone)]
pub struct ReferenceIntegrityResult {
    /// The symbol being analyzed.
    pub target_symbol: SymbolId,

    /// Issues found during analysis.
    pub issues: Vec<ReferenceIntegrityIssue>,
}

impl ReferenceIntegrityResult {
    /// Check if there are any issues.
    pub fn has_issues(&self) -> bool {
        !self.issues.is_empty()
    }

    /// Get the count of errors (not warnings).
    pub fn error_count(&self) -> usize {
        self.issues.iter().filter(|i| i.is_error()).count()
    }

    /// Get the count of warnings.
    pub fn warning_count(&self) -> usize {
        self.issues.iter().filter(|i| !i.is_error()).count()
    }
}

/// Specific issue found during reference integrity analysis.
#[derive(Debug, Clone)]
pub enum ReferenceIntegrityIssue {
    /// Reference to a symbol that will be deleted.
    DanglingReference {
        /// The symbol with the dangling reference.
        referrer: SymbolId,
        /// The deleted symbol being referenced.
        deleted_symbol: SymbolId,
        /// Number of references found.
        reference_count: usize,
    },

    /// Struct literal missing required fields after field addition.
    MissingFieldInLiteral {
        /// Location of the struct literal (containing function/method).
        location: SymbolId,
        /// The struct type.
        struct_type: SymbolId,
        /// The added field that's missing.
        missing_field: String,
    },

    /// Struct literal has field that will be deleted.
    RemovedFieldInLiteral {
        /// Location of the struct literal (containing function/method).
        location: SymbolId,
        /// The struct type.
        struct_type: SymbolId,
        /// The field being removed.
        removed_field: String,
    },

    /// Method call with incompatible arguments after signature change.
    IncompatibleMethodCall {
        /// The caller with incompatible call.
        caller: SymbolId,
        /// The method being called.
        method: SymbolId,
        /// Expected argument count.
        expected_args: usize,
        /// Actual argument count at call site.
        actual_args: usize,
    },

    /// Symbol rename would break references.
    RenameWouldBreakReferences {
        /// The symbol being renamed.
        symbol: SymbolId,
        /// Referrers that would break.
        referrers: Vec<SymbolId>,
    },

    /// Unused symbol after mutation (warning).
    UnusedAfterMutation {
        /// The symbol that became unused.
        symbol: SymbolId,
    },
}

impl ReferenceIntegrityIssue {
    /// Check if this issue is an error (vs warning).
    pub fn is_error(&self) -> bool {
        !matches!(self, ReferenceIntegrityIssue::UnusedAfterMutation { .. })
    }
}

/// Reference Integrity Checker using CodeGraphV2.
///
/// Validates that mutations don't leave dangling references or break
/// existing code that depends on the changed symbols.
///
/// # Example
///
/// ```rust,ignore
/// let checker = ReferenceIntegrityChecker::new(&code_graph, &registry);
///
/// // Check impact of deleting a symbol
/// let result = checker.check_deletion_impact(symbol_to_delete);
/// if result.has_issues() {
///     for issue in &result.issues {
///         println!("Issue: {:?}", issue);
///     }
/// }
/// ```
pub struct ReferenceIntegrityChecker<'a> {
    graph: &'a CodeGraphV2,
    typeflow: &'a TypeFlowGraphV2,
    registry: &'a SymbolRegistry,
}

impl<'a> ReferenceIntegrityChecker<'a> {
    /// Create a new ReferenceIntegrityChecker.
    pub fn new(
        graph: &'a CodeGraphV2,
        typeflow: &'a TypeFlowGraphV2,
        registry: &'a SymbolRegistry,
    ) -> Self {
        Self {
            graph,
            typeflow,
            registry,
        }
    }

    /// Check the impact of deleting a symbol.
    ///
    /// Returns all references that would become dangling.
    pub fn check_deletion_impact(&self, symbol_id: SymbolId) -> ReferenceIntegrityResult {
        let mut issues = Vec::new();

        // Find all symbols that reference the to-be-deleted symbol
        let referrers: Vec<SymbolId> = self.typeflow.type_users(symbol_id).collect();
        let callers: Vec<SymbolId> = self.graph.callers_of(symbol_id).collect();

        // Combine all references
        let mut all_referrers: Vec<SymbolId> = referrers.clone();
        all_referrers.extend(callers.iter().copied());
        all_referrers.sort();
        all_referrers.dedup();

        if !all_referrers.is_empty() {
            issues.push(ReferenceIntegrityIssue::DanglingReference {
                referrer: all_referrers[0], // Representative referrer
                deleted_symbol: symbol_id,
                reference_count: all_referrers.len(),
            });
        }

        // Check if this symbol has children (struct fields, enum variants, methods)
        // If so, deleting the parent affects all children
        let children: Vec<SymbolId> = self.graph.children_of(symbol_id).collect();
        for child_id in children {
            let child_result = self.check_deletion_impact(child_id);
            issues.extend(child_result.issues);
        }

        ReferenceIntegrityResult {
            target_symbol: symbol_id,
            issues,
        }
    }

    /// Check the impact of renaming a symbol.
    ///
    /// Returns all locations that would need to be updated.
    pub fn check_rename_impact(&self, symbol_id: SymbolId) -> ReferenceIntegrityResult {
        let mut issues = Vec::new();

        // Find all referrers (users + callers)
        let mut referrers: Vec<SymbolId> = self.typeflow.type_users(symbol_id).collect();
        referrers.extend(self.graph.callers_of(symbol_id));
        referrers.sort();
        referrers.dedup();

        if !referrers.is_empty() {
            issues.push(ReferenceIntegrityIssue::RenameWouldBreakReferences {
                symbol: symbol_id,
                referrers: referrers.clone(),
            });
        }

        ReferenceIntegrityResult {
            target_symbol: symbol_id,
            issues,
        }
    }

    /// Check if adding a field would affect struct literals.
    ///
    /// This checks all callers that might construct the struct.
    pub fn check_field_addition_impact(
        &self,
        struct_id: SymbolId,
        field_name: &str,
    ) -> ReferenceIntegrityResult {
        let mut issues = Vec::new();

        // Find all functions that use (construct) this struct
        let users: Vec<SymbolId> = self.typeflow.type_users(struct_id).collect();

        for user_id in users {
            // Check if the user is a function that might construct the struct
            if let Some(kind) = self.registry.kind(user_id) {
                if matches!(kind, SymbolKind::Function | SymbolKind::Method) {
                    issues.push(ReferenceIntegrityIssue::MissingFieldInLiteral {
                        location: user_id,
                        struct_type: struct_id,
                        missing_field: field_name.to_string(),
                    });
                }
            }
        }

        ReferenceIntegrityResult {
            target_symbol: struct_id,
            issues,
        }
    }

    /// Check if removing a field would affect struct literals.
    pub fn check_field_removal_impact(
        &self,
        struct_id: SymbolId,
        field_name: &str,
    ) -> ReferenceIntegrityResult {
        let mut issues = Vec::new();

        // Find all functions that use (construct) this struct
        let users: Vec<SymbolId> = self.typeflow.type_users(struct_id).collect();

        for user_id in users {
            if let Some(kind) = self.registry.kind(user_id) {
                if matches!(kind, SymbolKind::Function | SymbolKind::Method) {
                    issues.push(ReferenceIntegrityIssue::RemovedFieldInLiteral {
                        location: user_id,
                        struct_type: struct_id,
                        removed_field: field_name.to_string(),
                    });
                }
            }
        }

        ReferenceIntegrityResult {
            target_symbol: struct_id,
            issues,
        }
    }

    /// Check if a method signature change would break call sites.
    ///
    /// Compares expected argument count against actual call sites.
    pub fn check_method_signature_change(
        &self,
        method_id: SymbolId,
        new_arg_count: usize,
    ) -> ReferenceIntegrityResult {
        let mut issues = Vec::new();

        // Get current parameter count
        let current_param_count = self
            .graph
            .children_of(method_id)
            .filter(|child_id| {
                self.registry
                    .kind(*child_id)
                    .map(|k| matches!(k, SymbolKind::Parameter))
                    .unwrap_or(false)
            })
            .count();

        // Only check if the count actually changed
        if current_param_count != new_arg_count {
            // Find all callers
            let callers: Vec<SymbolId> = self.graph.callers_of(method_id).collect();

            for caller_id in callers {
                issues.push(ReferenceIntegrityIssue::IncompatibleMethodCall {
                    caller: caller_id,
                    method: method_id,
                    expected_args: new_arg_count,
                    actual_args: current_param_count, // Assuming caller uses current count
                });
            }
        }

        ReferenceIntegrityResult {
            target_symbol: method_id,
            issues,
        }
    }

    /// Get all referrers of a symbol (type_users + callers).
    pub fn get_all_referrers(&self, symbol_id: SymbolId) -> Vec<SymbolId> {
        let mut referrers: Vec<SymbolId> = self.typeflow.type_users(symbol_id).collect();
        referrers.extend(self.graph.callers_of(symbol_id));
        referrers.sort();
        referrers.dedup();
        referrers
    }

    /// Check if a symbol is unused (no call references and no type references).
    pub fn is_symbol_unused(&self, symbol_id: SymbolId) -> bool {
        self.graph.reference_count(symbol_id) == 0
            && self.typeflow.type_users(symbol_id).next().is_none()
    }

    /// Get total reference count for a symbol (calls + type references).
    pub fn reference_count(&self, symbol_id: SymbolId) -> usize {
        self.graph.reference_count(symbol_id) + self.typeflow.usage_count(symbol_id)
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::query::{GraphBuilderV2, TypeFlowGraphV2};
    use crate::symbol::SymbolPath;

    fn create_test_setup() -> (CodeGraphV2, TypeFlowGraphV2, SymbolRegistry) {
        let mut registry = SymbolRegistry::new();
        let mut builder = GraphBuilderV2::new(&mut registry);

        // Create a struct with fields
        let config = builder
            .add_symbol(
                SymbolPath::parse("test::Config").unwrap(),
                SymbolKind::Struct,
            )
            .unwrap();
        let name_field = builder
            .add_symbol(
                SymbolPath::parse("test::Config::name").unwrap(),
                SymbolKind::Field,
            )
            .unwrap();
        let value_field = builder
            .add_symbol(
                SymbolPath::parse("test::Config::value").unwrap(),
                SymbolKind::Field,
            )
            .unwrap();
        builder.add_contains(config, name_field);
        builder.add_contains(config, value_field);

        // Create a function that uses Config
        let create_config = builder
            .add_symbol(
                SymbolPath::parse("test::create_config").unwrap(),
                SymbolKind::Function,
            )
            .unwrap();
        // create_config -> Config type usage registered in TypeFlow below

        // Create another function that calls create_config
        let init = builder
            .add_symbol(
                SymbolPath::parse("test::init").unwrap(),
                SymbolKind::Function,
            )
            .unwrap();
        builder.add_call(init, create_config);

        let graph = builder.build();
        let mut typeflow = TypeFlowGraphV2::new();
        // Register create_config -> Config type usage in TypeFlow
        typeflow.add_usage(
            crate::query::UsageContext::ReturnType,
            crate::query::RefKind::Owned,
            Some(config),
            Some(create_config),
        );
        (graph, typeflow, registry)
    }

    #[test]
    fn test_check_deletion_impact_with_references() {
        let (graph, typeflow, registry) = create_test_setup();
        let checker = ReferenceIntegrityChecker::new(&graph, &typeflow, &registry);

        // Find create_config function
        let create_config_id = registry.lookup_by_name("create_config").unwrap();

        let result = checker.check_deletion_impact(create_config_id);

        // Should have dangling reference issue (init calls create_config)
        assert!(result.has_issues());
        assert!(result.error_count() > 0);
    }

    #[test]
    fn test_check_deletion_impact_no_references() {
        let (graph, typeflow, registry) = create_test_setup();
        let checker = ReferenceIntegrityChecker::new(&graph, &typeflow, &registry);

        // Find init function (no one calls it)
        let init_id = registry.lookup_by_name("init").unwrap();

        let result = checker.check_deletion_impact(init_id);

        // Should have no issues (init is not referenced by anyone)
        assert!(!result.has_issues());
    }

    #[test]
    fn test_check_rename_impact() {
        let (graph, typeflow, registry) = create_test_setup();
        let checker = ReferenceIntegrityChecker::new(&graph, &typeflow, &registry);

        // Find Config struct
        let config_id = registry.lookup_by_name("Config").unwrap();

        let result = checker.check_rename_impact(config_id);

        // Should have rename issue (create_config uses Config)
        assert!(result.has_issues());
    }

    #[test]
    fn test_check_field_addition_impact() {
        let (graph, typeflow, registry) = create_test_setup();
        let checker = ReferenceIntegrityChecker::new(&graph, &typeflow, &registry);

        // Find Config struct
        let config_id = registry.lookup_by_name("Config").unwrap();

        let result = checker.check_field_addition_impact(config_id, "timeout");

        // Should have issue (create_config constructs Config)
        assert!(result.has_issues());
    }

    #[test]
    fn test_get_all_referrers() {
        let (graph, typeflow, registry) = create_test_setup();
        let checker = ReferenceIntegrityChecker::new(&graph, &typeflow, &registry);

        // Find create_config function
        let create_config_id = registry.lookup_by_name("create_config").unwrap();

        let referrers = checker.get_all_referrers(create_config_id);

        // init should be in the referrers
        let init_id = registry.lookup_by_name("init").unwrap();
        assert!(referrers.contains(&init_id));
    }

    #[test]
    fn test_is_symbol_unused() {
        let (graph, typeflow, registry) = create_test_setup();
        let checker = ReferenceIntegrityChecker::new(&graph, &typeflow, &registry);

        // init is unused (no callers)
        let init_id = registry.lookup_by_name("init").unwrap();
        assert!(checker.is_symbol_unused(init_id));

        // create_config is used by init
        let create_config_id = registry.lookup_by_name("create_config").unwrap();
        assert!(!checker.is_symbol_unused(create_config_id));
    }
}