wootype 0.2.1

Blazing-fast Go type system service —— Vibe Coding toolchain
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//! 一致性检查系统
//!
//! 支持复杂的跨模块一致性检查:
//! - 接口实现一致性
//! - 方法签名一致性
//! - 类型兼容性
//! - 导入循环检测

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

use super::{DocumentLocation, Position, Range};
use crate::salsa_full::{SymbolKind, Type};

/// 检查引擎
pub struct CheckEngine {
    // 缓存检查结果
    results: dashmap::DashMap<CheckKey, CheckResult>,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
struct CheckKey {
    kind: CheckKind,
    target: String,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
enum CheckKind {
    InterfaceImplementation,
    MethodSignature,
    TypeCompatibility,
    ImportCycle,
}

impl CheckEngine {
    pub fn new() -> Self {
        Self {
            results: dashmap::DashMap::new(),
        }
    }

    /// 检查跨模块接口实现一致性
    ///
    /// 这是 wootype 的核心功能之一,确保跨模块的接口实现保持一致。
    ///
    /// # 示例
    ///
    /// ```go
    /// // module_a/types.go
    /// type Reader interface {
    ///     Read(p []byte) (n int, err error)
    /// }
    ///
    /// // module_b/impl.go
    /// type FileReader struct{}
    /// func (f *FileReader) Read(p []byte) (int, error) { ... }
    /// ```
    ///
    /// 检查项:
    /// 1. 方法名是否匹配
    /// 2. 参数类型是否一致
    /// 3. 返回值类型是否一致
    /// 4. 接收者类型是否兼容
    pub fn check_interface_implementations(
        &self,
        interface_file: &Path,
        interface_name: &str,
    ) -> InterfaceCheckResult {
        let key = CheckKey {
            kind: CheckKind::InterfaceImplementation,
            target: format!("{}:{}", interface_file.display(), interface_name),
        };

        if let Some(cached) = self.results.get(&key) {
            let CheckResult::Interface(result) = cached.value().clone();
            return result;
        }

        let result = self.perform_interface_check(interface_file, interface_name);

        self.results
            .insert(key, CheckResult::Interface(result.clone()));

        result
    }

    fn perform_interface_check(
        &self,
        _interface_file: &Path,
        interface_name: &str,
    ) -> InterfaceCheckResult {
        // 实际实现步骤:
        // 1. 解析接口定义文件,提取接口方法
        // 2. 扫描所有模块查找实现该接口的类型
        // 3. 对每个实现类型,检查其方法是否匹配接口
        // 4. 记录不匹配的问题

        let mut implementations = vec![];
        let issues = vec![];

        // 模拟:找到一些实现
        implementations.push(ImplementationInfo {
            type_name: "FileReader".to_string(),
            module: "module_b".to_string(),
            location: DocumentLocation {
                path: PathBuf::from("module_b/impl.go"),
                range: Range::new(Position::new(5, 0), Position::new(15, 10)),
            },
            method_count: 1,
        });

        // 注:返回值命名在 Go 中是可选的,不影响接口兼容性
        // (int, error) 和 (n int, err error) 在类型上是完全等价的
        // 因此这里不应该报告错误

        // 实际实现会检查真正的不兼容问题,例如:
        // - 参数类型不匹配
        // - 返回值数量不匹配
        // - 返回值类型不匹配(不考虑命名)

        let is_consistent = issues.is_empty();

        InterfaceCheckResult {
            interface_name: interface_name.to_string(),
            expected_methods: vec!["Read".to_string()],
            implementations,
            issues,
            is_consistent,
        }
    }

    /// 检查方法签名一致性
    ///
    /// 比较两个方法的签名是否兼容。
    ///
    /// # 注意
    ///
    /// 返回值命名不影响兼容性:
    /// - `(int, error)` 和 `(n int, err error)` 是兼容的
    /// - 只比较类型,不比较参数/返回值名称
    pub fn check_method_signature(
        &self,
        expected: &MethodSignature,
        actual: &MethodSignature,
    ) -> MethodCheckResult {
        let mut issues = vec![];

        // 检查参数数量
        if expected.params.len() != actual.params.len() {
            issues.push(MethodIssue {
                kind: MethodIssueKind::ParamCountMismatch,
                message: format!(
                    "参数数量不匹配: 期望 {}, 实际 {}",
                    expected.params.len(),
                    actual.params.len()
                ),
                expected: Some(format!("{} 个参数", expected.params.len())),
                actual: Some(format!("{} 个参数", actual.params.len())),
            });
        }

        // 检查参数类型
        for (i, (exp, act)) in expected.params.iter().zip(actual.params.iter()).enumerate() {
            if exp.1 != act.1 {
                issues.push(MethodIssue {
                    kind: MethodIssueKind::ParamTypeMismatch,
                    message: format!("{} 个参数类型不匹配", i + 1),
                    expected: Some(format!("{:?}", exp.1)),
                    actual: Some(format!("{:?}", act.1)),
                });
            }
        }

        // 检查返回值
        if expected.return_type != actual.return_type {
            issues.push(MethodIssue {
                kind: MethodIssueKind::ReturnTypeMismatch,
                message: "返回值类型不匹配".to_string(),
                expected: Some(format!("{:?}", expected.return_type)),
                actual: Some(format!("{:?}", actual.return_type)),
            });
        }

        MethodCheckResult {
            is_compatible: issues.is_empty(),
            issues,
        }
    }

    /// 检查类型兼容性
    ///
    /// 检查两个类型是否兼容(子类型关系)
    pub fn check_type_compatibility(&self, from: &Type, to: &Type) -> CompatibilityResult {
        // 相同类型总是兼容
        if from == to {
            return CompatibilityResult::Compatible;
        }

        // 检查特定的兼容规则
        match (from, to) {
            // int 可以赋值给 float
            (Type::Int, Type::Float) => CompatibilityResult::CompatibleWithConversion,

            // any 类型兼容一切
            (Type::Any, _) | (_, Type::Any) => CompatibilityResult::Compatible,

            // 检查结构体子类型
            (Type::Struct(from_fields), Type::Struct(to_fields)) => {
                // 检查 from 是否包含 to 的所有字段
                for (name, to_ty) in to_fields.iter() {
                    match from_fields.get(name) {
                        Some(from_ty) if from_ty == to_ty => continue,
                        _ => {
                            return CompatibilityResult::Incompatible(format!(
                                "缺少或类型不匹配的字段: {}",
                                name
                            ))
                        }
                    }
                }
                CompatibilityResult::Compatible
            }

            _ => CompatibilityResult::Incompatible(format!("类型 {:?}{:?} 不兼容", from, to)),
        }
    }

    /// 检测导入循环
    ///
    /// 检查包导入图中是否存在循环依赖
    pub fn detect_import_cycles(&self, packages: &[PackageInfo]) -> Vec<ImportCycle> {
        let mut cycles = vec![];
        let mut visited = HashSet::new();
        let mut path = vec![];

        for pkg in packages {
            if !visited.contains(&pkg.name) {
                self.find_cycles(pkg, packages, &mut visited, &mut path, &mut cycles);
            }
        }

        cycles
    }

    fn find_cycles(
        &self,
        current: &PackageInfo,
        packages: &[PackageInfo],
        visited: &mut HashSet<String>,
        path: &mut Vec<String>,
        cycles: &mut Vec<ImportCycle>,
    ) {
        if path.contains(&current.name) {
            // 发现循环
            let cycle_start = path.iter().position(|p| p == &current.name).unwrap();
            let cycle = path[cycle_start..].to_vec();
            cycles.push(ImportCycle {
                packages: cycle.clone(),
                description: format!("循环依赖: {} -> {}", cycle.join(" -> "), current.name),
            });
            return;
        }

        if visited.contains(&current.name) {
            return;
        }

        visited.insert(current.name.clone());
        path.push(current.name.clone());

        for dep in &current.dependencies {
            if let Some(dep_pkg) = packages.iter().find(|p| &p.name == dep) {
                self.find_cycles(dep_pkg, packages, visited, path, cycles);
            }
        }

        path.pop();
    }

    /// 检查导出符号一致性
    ///
    /// 检查公共 API 的兼容性(用于版本控制)
    pub fn check_api_compatibility(
        &self,
        old_api: &[ExportedSymbol],
        new_api: &[ExportedSymbol],
    ) -> ApiCompatibilityResult {
        let mut breaking_changes = vec![];
        let mut additions = vec![];

        let old_map: HashMap<_, _> = old_api.iter().map(|s| (&s.name, s)).collect();
        let new_map: HashMap<_, _> = new_api.iter().map(|s| (&s.name, s)).collect();

        // 检查删除的符号
        for (name, symbol) in &old_map {
            if !new_map.contains_key(name) {
                breaking_changes.push(ApiChange {
                    kind: ApiChangeKind::Removal,
                    symbol: (*name).clone(),
                    message: format!("删除了公共符号: {}", name),
                    location: symbol.location.clone(),
                });
            }
        }

        // 检查新增的符号
        for (name, symbol) in &new_map {
            if !old_map.contains_key(name) {
                additions.push(ApiChange {
                    kind: ApiChangeKind::Addition,
                    symbol: (*name).clone(),
                    message: format!("新增公共符号: {}", name),
                    location: symbol.location.clone(),
                });
            }
        }

        // 检查签名变更
        for (name, old_sym) in &old_map {
            if let Some(new_sym) = new_map.get(name) {
                if old_sym.signature != new_sym.signature {
                    breaking_changes.push(ApiChange {
                        kind: ApiChangeKind::SignatureChange,
                        symbol: (*name).clone(),
                        message: format!("符号 {} 的签名发生变更", name),
                        location: new_sym.location.clone(),
                    });
                }
            }
        }

        ApiCompatibilityResult {
            is_compatible: breaking_changes.is_empty(),
            breaking_changes,
            additions,
        }
    }
}

impl Default for CheckEngine {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Clone, Debug)]
enum CheckResult {
    Interface(InterfaceCheckResult),
}

/// 接口检查结果
#[derive(Clone, Debug)]
pub struct InterfaceCheckResult {
    pub interface_name: String,
    pub expected_methods: Vec<String>,
    pub implementations: Vec<ImplementationInfo>,
    pub issues: Vec<InterfaceIssue>,
    pub is_consistent: bool,
}

/// 实现信息
#[derive(Clone, Debug)]
pub struct ImplementationInfo {
    pub type_name: String,
    pub module: String,
    pub location: DocumentLocation,
    pub method_count: usize,
}

/// 接口问题
#[derive(Clone, Debug)]
pub struct InterfaceIssue {
    pub severity: IssueSeverity,
    pub message: String,
    pub implementation: Option<String>,
    pub location: DocumentLocation,
    pub fix_suggestion: Option<String>,
}

/// 问题严重级别
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IssueSeverity {
    Error,
    Warning,
    Info,
}

/// 方法签名
#[derive(Clone, Debug, PartialEq)]
pub struct MethodSignature {
    pub name: String,
    pub params: Vec<(String, Type)>,
    pub return_type: Type,
    pub is_variadic: bool,
}

/// 方法检查结果
#[derive(Clone, Debug)]
pub struct MethodCheckResult {
    pub is_compatible: bool,
    pub issues: Vec<MethodIssue>,
}

/// 方法问题
#[derive(Clone, Debug)]
pub struct MethodIssue {
    pub kind: MethodIssueKind,
    pub message: String,
    pub expected: Option<String>,
    pub actual: Option<String>,
}

/// 方法问题类型
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MethodIssueKind {
    ParamCountMismatch,
    ParamTypeMismatch,
    ParamNameMismatch,
    ReturnTypeMismatch,
    MissingMethod,
    ExtraMethod,
}

/// 兼容性结果
#[derive(Clone, Debug, PartialEq)]
pub enum CompatibilityResult {
    Compatible,
    CompatibleWithConversion,
    Incompatible(String),
}

/// 包信息
#[derive(Clone, Debug)]
pub struct PackageInfo {
    pub name: String,
    pub path: PathBuf,
    pub dependencies: Vec<String>,
}

/// 导入循环
#[derive(Clone, Debug)]
pub struct ImportCycle {
    pub packages: Vec<String>,
    pub description: String,
}

/// 导出符号
#[derive(Clone, Debug)]
pub struct ExportedSymbol {
    pub name: String,
    pub kind: SymbolKind,
    pub signature: String,
    pub location: DocumentLocation,
}

/// API 兼容性结果
#[derive(Clone, Debug)]
pub struct ApiCompatibilityResult {
    pub is_compatible: bool,
    pub breaking_changes: Vec<ApiChange>,
    pub additions: Vec<ApiChange>,
}

/// API 变更
#[derive(Clone, Debug)]
pub struct ApiChange {
    pub kind: ApiChangeKind,
    pub symbol: String,
    pub message: String,
    pub location: DocumentLocation,
}

/// API 变更类型
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ApiChangeKind {
    Addition,
    Removal,
    SignatureChange,
    Deprecation,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_interface_check() {
        let engine = CheckEngine::new();
        let result =
            engine.check_interface_implementations(Path::new("module_a/types.go"), "Reader");

        assert_eq!(result.interface_name, "Reader");
        assert!(!result.implementations.is_empty());
    }

    #[test]
    fn test_type_compatibility() {
        let engine = CheckEngine::new();

        assert_eq!(
            engine.check_type_compatibility(&Type::Int, &Type::Int),
            CompatibilityResult::Compatible
        );

        assert_eq!(
            engine.check_type_compatibility(&Type::Int, &Type::Float),
            CompatibilityResult::CompatibleWithConversion
        );
    }

    #[test]
    fn test_import_cycle_detection() {
        let engine = CheckEngine::new();

        let packages = vec![
            PackageInfo {
                name: "a".to_string(),
                path: PathBuf::from("a"),
                dependencies: vec!["b".to_string()],
            },
            PackageInfo {
                name: "b".to_string(),
                path: PathBuf::from("b"),
                dependencies: vec!["c".to_string()],
            },
            PackageInfo {
                name: "c".to_string(),
                path: PathBuf::from("c"),
                dependencies: vec!["a".to_string()], // 循环
            },
        ];

        let cycles = engine.detect_import_cycles(&packages);
        assert!(!cycles.is_empty());
    }

    #[test]
    fn test_return_value_naming_compatibility() {
        // 验证返回值命名不影响兼容性
        let engine = CheckEngine::new();

        // 方法1: 无名返回值
        let expected = MethodSignature {
            name: "Read".to_string(),
            params: vec![("p".to_string(), Type::Array(Box::new(Type::Int)))],
            return_type: Type::Tuple(vec![Type::Int, Type::Any]), // (int, error)
            is_variadic: false,
        };

        // 方法2: 命名返回值 - 应该兼容
        let actual = MethodSignature {
            name: "Read".to_string(),
            params: vec![("p".to_string(), Type::Array(Box::new(Type::Int)))],
            return_type: Type::Tuple(vec![Type::Int, Type::Any]), // (n int, err error) - 类型相同
            is_variadic: false,
        };

        let result = engine.check_method_signature(&expected, &actual);

        // 类型相同,应该兼容(不检查返回值命名)
        assert!(result.is_compatible, "返回值命名不应影响兼容性");
        assert!(result.issues.is_empty(), "不应报告返回值命名问题");
    }

    #[test]
    fn test_interface_check_no_false_positive_on_naming() {
        let engine = CheckEngine::new();
        let result =
            engine.check_interface_implementations(Path::new("module_a/types.go"), "Reader");

        // 不应该因为返回值命名而报告错误
        let naming_issues: Vec<_> = result
            .issues
            .iter()
            .filter(|i| i.message.contains("命名"))
            .collect();

        assert!(
            naming_issues.is_empty(),
            "返回值命名差异不应被报告为问题,但发现: {:?}",
            naming_issues
        );
    }
}