assura-resolve 0.4.3

Name resolution and symbol table for the Assura contract language
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
//! Unused import detection (A02007).

use std::collections::HashSet;

use assura_parser::ast::{
    BindDecl, BlockKind, Clause, CodecRegistryDecl, ContractDecl, DeclVisitor, EnumDef, Expr,
    ExternDecl, FnDef, ProphecyDecl, ServiceDecl, ServiceItem, SourceFile, SpExpr, TypeBody,
    TypeDef, TypeExpr, walk_decls,
};

use crate::errors::ResolutionError;
use crate::imports::{ImportStatus, ResolvedImport};
use crate::type_refs::TYPE_SYNTAX_TOKENS;

/// Collect all identifier-like names referenced in the AST. This includes
/// type annotations, expression identifiers, and field/param type tokens.
///
/// Uses [`DeclVisitor`] so new `Decl` variants only need a `visit_*` arm here
/// (and in `walk_decl`), not another open-coded match in this pass.
pub(crate) fn collect_referenced_names(source: &SourceFile) -> HashSet<String> {
    struct CollectNames<'a> {
        names: &'a mut HashSet<String>,
    }

    impl DeclVisitor for CollectNames<'_> {
        fn visit_type_def(&mut self, t: &TypeDef) {
            collect_type_body_names(&t.body, self.names);
        }
        fn visit_fn_def(&mut self, f: &FnDef) {
            collect_fn_names(f, self.names);
        }
        fn visit_extern(&mut self, ex: &ExternDecl) {
            for p in &ex.params {
                collect_type_expr_names(p.ty.as_ref(), self.names);
            }
            collect_type_expr_names(ex.return_ty.as_ref(), self.names);
            for clause in &ex.clauses {
                collect_expr_names(&clause.body, self.names);
            }
        }
        fn visit_bind(&mut self, b: &BindDecl) {
            for p in &b.params {
                collect_type_expr_names(p.ty.as_ref(), self.names);
            }
            collect_type_expr_names(b.return_ty.as_ref(), self.names);
            for clause in &b.clauses {
                collect_expr_names(&clause.body, self.names);
            }
        }
        fn visit_contract(&mut self, c: &ContractDecl) {
            for clause in &c.clauses {
                collect_expr_names(&clause.body, self.names);
            }
        }
        fn visit_service(&mut self, s: &ServiceDecl) {
            for item in &s.items {
                match item {
                    ServiceItem::TypeDef(t) => collect_type_body_names(&t.body, self.names),
                    ServiceItem::EnumDef(e) => {
                        for v in &e.variants {
                            for f in &v.fields {
                                let toks: Vec<String> =
                                    f.split_whitespace().map(String::from).collect();
                                collect_type_token_names(&toks, self.names);
                            }
                        }
                    }
                    ServiceItem::Operation { clauses, .. } | ServiceItem::Query { clauses, .. } => {
                        for clause in clauses {
                            collect_expr_names(&clause.body, self.names);
                        }
                    }
                    ServiceItem::Invariant(expr) => collect_expr_names(expr, self.names),
                    ServiceItem::Other { body, .. } => collect_expr_names(body, self.names),
                    ServiceItem::States(_) => {}
                }
            }
        }
        fn visit_enum_def(&mut self, e: &EnumDef) {
            for v in &e.variants {
                for f in &v.fields {
                    let toks: Vec<String> = f.split_whitespace().map(String::from).collect();
                    collect_type_token_names(&toks, self.names);
                }
            }
        }
        fn visit_prophecy(&mut self, p: &ProphecyDecl) {
            collect_type_expr_names(p.ty.as_ref(), self.names);
        }
        fn visit_codec_registry(&mut self, cr: &CodecRegistryDecl) {
            for tok in &cr.output_type {
                if tok.chars().next().is_some_and(|c| c.is_uppercase()) {
                    self.names.insert(tok.clone());
                }
            }
            for codec in &cr.codecs {
                for clause in &codec.contracts {
                    collect_expr_names(&clause.body, self.names);
                }
            }
        }
        fn visit_block(
            &mut self,
            _kind: &BlockKind,
            _name: &str,
            _value: &Option<Vec<String>>,
            body: &[Clause],
        ) {
            for clause in body {
                collect_expr_names(&clause.body, self.names);
            }
        }
    }

    let mut names = HashSet::new();
    let mut visitor = CollectNames { names: &mut names };
    walk_decls(&mut visitor, &source.decls);
    names
}

fn collect_type_body_names(body: &TypeBody, names: &mut HashSet<String>) {
    match body {
        TypeBody::Struct(fields) => {
            for f in fields {
                collect_type_expr_names(f.ty.as_ref(), names);
            }
        }
        TypeBody::Alias(tokens) | TypeBody::Refined(tokens) => {
            collect_type_token_names(tokens, names);
        }
        TypeBody::Empty => {}
    }
}

fn collect_fn_names(f: &FnDef, names: &mut HashSet<String>) {
    for p in &f.params {
        collect_type_expr_names(p.ty.as_ref(), names);
    }
    collect_type_expr_names(f.return_ty.as_ref(), names);
    for clause in &f.clauses {
        collect_expr_names(&clause.body, names);
    }
}

fn collect_type_token_names(tokens: &[String], names: &mut HashSet<String>) {
    for tok in tokens {
        if !TYPE_SYNTAX_TOKENS.contains(&tok.as_str())
            && !tok.starts_with(|c: char| c.is_ascii_digit())
        {
            names.insert(tok.clone());
        }
    }
}

/// Collect referenced names from an `Option<TypeExpr>` by converting to tokens.
fn collect_type_expr_names(te: Option<&TypeExpr>, names: &mut HashSet<String>) {
    if let Some(te) = te {
        collect_type_token_names(&te.to_tokens(), names);
    }
}

fn collect_expr_names(expr: &SpExpr, names: &mut HashSet<String>) {
    match &expr.node {
        Expr::Ident(name) => {
            names.insert(name.clone());
        }
        Expr::Field(receiver, field) => {
            collect_expr_names(receiver, names);
            names.insert(field.clone());
        }
        Expr::BinOp { lhs, rhs, .. } => {
            collect_expr_names(lhs, names);
            collect_expr_names(rhs, names);
        }
        Expr::UnaryOp { expr: inner, .. } | Expr::Old(inner) | Expr::Ghost(inner) => {
            collect_expr_names(inner, names);
        }
        Expr::Call { func, args } => {
            collect_expr_names(func, names);
            for arg in args {
                collect_expr_names(arg, names);
            }
        }
        Expr::MethodCall {
            receiver,
            args,
            method,
        } => {
            collect_expr_names(receiver, names);
            names.insert(method.clone());
            for arg in args {
                collect_expr_names(arg, names);
            }
        }
        Expr::Index { expr: base, index } => {
            collect_expr_names(base, names);
            collect_expr_names(index, names);
        }
        Expr::If {
            cond,
            then_branch,
            else_branch,
        } => {
            collect_expr_names(cond, names);
            collect_expr_names(then_branch, names);
            if let Some(e) = else_branch {
                collect_expr_names(e, names);
            }
        }
        Expr::Forall {
            var, domain, body, ..
        }
        | Expr::Exists {
            var, domain, body, ..
        } => {
            names.insert(var.clone());
            collect_expr_names(domain, names);
            collect_expr_names(body, names);
        }
        Expr::List(items) | Expr::Tuple(items) | Expr::Block(items) => {
            for item in items {
                collect_expr_names(item, names);
            }
        }
        Expr::Cast { expr: inner, ty } => {
            collect_expr_names(inner, names);
            names.insert(ty.clone());
        }
        Expr::Apply { lemma_name, args } => {
            names.insert(lemma_name.clone());
            for arg in args {
                collect_expr_names(arg, names);
            }
        }
        Expr::Match { scrutinee, arms } => {
            collect_expr_names(scrutinee, names);
            for arm in arms {
                collect_expr_names(&arm.body, names);
            }
        }
        Expr::Let { name, value, body } => {
            names.insert(name.clone());
            collect_expr_names(value, names);
            collect_expr_names(body, names);
        }
        Expr::Raw(tokens) => {
            for tok in tokens {
                if tok
                    .chars()
                    .next()
                    .is_some_and(|c| c.is_alphabetic() || c == '_')
                {
                    names.insert(tok.clone());
                }
            }
        }
        Expr::Literal(_) => {}
    }
}

/// Check which imports introduced names that are never referenced in the AST.
///
/// An import is "unused" when none of the names it introduces appear in
/// the set of referenced names collected from the AST.
pub(crate) fn check_unused_imports(
    imports: &[ResolvedImport],
    referenced: &HashSet<String>,
    errors: &mut Vec<ResolutionError>,
) {
    for imp in imports {
        // Unresolved imports are not "unused"; they failed to load (A02010).
        // Circular imports already have A02005.
        if imp.status == ImportStatus::Circular || imp.status == ImportStatus::Unresolved {
            continue;
        }
        let introduced: Vec<&str> = if !imp.items.is_empty() {
            imp.items.iter().map(|s| s.as_str()).collect()
        } else if let Some(alias) = &imp.alias {
            vec![alias.as_str()]
        } else if let Some(last) = imp.path.last() {
            vec![last.as_str()]
        } else {
            continue;
        };

        // An import is unused if none of its introduced names appear in references
        if introduced.iter().all(|name| !referenced.contains(*name)) {
            let path_str = imp.path.join(".");
            errors.push(ResolutionError {
                code: "A02007".into(),
                message: format!("unused import `{path_str}`"),
                span: imp.span.clone(),
                secondary: None,
                suggestion: None,
            });
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::imports::{ImportStatus, ResolvedImport};

    fn make_import(path: &[&str], items: &[&str]) -> ResolvedImport {
        ResolvedImport {
            path: path.iter().map(|s| s.to_string()).collect(),
            items: items.iter().map(|s| s.to_string()).collect(),
            alias: None,
            status: ImportStatus::Unresolved,
            span: 0..1,
        }
    }

    #[test]
    fn collect_referenced_names_empty() {
        let source = assura_parser::parse_unwrap("");
        let names = collect_referenced_names(&source);
        assert!(names.is_empty());
    }

    #[test]
    fn collect_referenced_names_contract() {
        let source = assura_parser::parse_unwrap("contract C { input(n: Nat) requires { n > 0 } }");
        let names = collect_referenced_names(&source);
        assert!(names.contains("n"));
    }

    #[test]
    fn collect_referenced_names_fn_types_and_body() {
        let source = assura_parser::parse_unwrap("fn f(x: Int) -> Bool { ensures { x > 0 } }");
        let names = collect_referenced_names(&source);
        // Type tokens from params/return are collected
        assert!(names.contains("Int"));
        assert!(names.contains("Bool"));
    }

    #[test]
    fn check_unused_imports_used_name() {
        let mut referenced = HashSet::new();
        referenced.insert("Map".to_string());
        let imports = vec![make_import(&["std", "collections"], &["Map"])];
        let mut errors = Vec::new();
        check_unused_imports(&imports, &referenced, &mut errors);
        assert!(errors.is_empty());
    }

    #[test]
    fn check_unused_skips_unresolved_imports() {
        let referenced = HashSet::new();
        let imports = vec![make_import(&["missing_mod"], &[])];
        assert_eq!(imports[0].status, ImportStatus::Unresolved);
        let mut errors = Vec::new();
        check_unused_imports(&imports, &referenced, &mut errors);
        assert!(
            errors.is_empty(),
            "unresolved imports must not be reported as unused: {errors:?}"
        );
    }

    #[test]
    fn check_unused_imports_unused_name() {
        let referenced = HashSet::new();
        // Resolved import that introduces an unused name.
        let mut imports = vec![make_import(&["std", "math"], &[])];
        imports[0].status = ImportStatus::Resolved;
        let mut errors = Vec::new();
        check_unused_imports(&imports, &referenced, &mut errors);
        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].code, "A02007");
        assert!(errors[0].message.contains("std.math"));
    }

    #[test]
    fn check_unused_imports_circular_skipped() {
        let referenced = HashSet::new();
        let imports = vec![ResolvedImport {
            path: vec!["self_ref".into()],
            items: vec![],
            alias: None,
            status: ImportStatus::Circular,
            span: 0..1,
        }];
        let mut errors = Vec::new();
        check_unused_imports(&imports, &referenced, &mut errors);
        assert!(errors.is_empty(), "circular imports should be skipped");
    }

    #[test]
    fn check_unused_imports_alias_used() {
        let mut referenced = HashSet::new();
        referenced.insert("M".to_string());
        let imports = vec![ResolvedImport {
            path: vec!["std".into(), "math".into()],
            items: vec![],
            alias: Some("M".into()),
            status: ImportStatus::Unresolved,
            span: 0..1,
        }];
        let mut errors = Vec::new();
        check_unused_imports(&imports, &referenced, &mut errors);
        assert!(errors.is_empty());
    }
}