ink-analyzer 0.11.2

A library for semantic analysis of ink! smart contracts.
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Utilities for item resolution.

use ink_analyzer_ir::ast;
use ink_analyzer_ir::ast::{AstNode, HasName};
use ink_analyzer_ir::syntax::SyntaxNode;
use itertools::Itertools;
use std::collections::{HashMap, HashSet};
use std::iter;

pub const SCALE_QUALIFIERS: [&str; 3] = ["scale", "ink::scale", "parity_scale_codec"];
pub const SCALE_INFO_QUALIFIERS: [&str; 2] = ["scale_info", "ink::scale_info"];

/// Returns the full qualified path for the item (if possible).
pub fn item_path<T>(item: &T) -> Option<String>
where
    T: HasName,
{
    item.name().map(|name| {
        iter::once(String::from("crate"))
            .chain(item.syntax().ancestors().filter_map(|node| {
                ast::Module::cast(node)
                    .as_ref()
                    .and_then(HasName::name)
                    .as_ref()
                    .map(ToString::to_string)
            }))
            .chain(iter::once(name.to_string()))
            .join("::")
    })
}

/// Finds first external trait implementations (if any).
pub fn external_trait_impls<'a>(
    trait_name: &'a str,
    crate_qualifiers: &'a [&'a str],
    ref_node: &'a SyntaxNode,
) -> impl Iterator<Item = ast::Impl> + 'a {
    ref_node
        .descendants()
        .filter(|it| ast::Impl::can_cast(it.kind()))
        .filter_map(move |node| {
            ast::Impl::cast(node).filter(|impl_item| {
                impl_item
                    .trait_()
                    .as_ref()
                    .and_then(ink_analyzer_ir::path_from_type)
                    .is_some_and(|path| {
                        is_external_crate_item(
                            trait_name,
                            &path,
                            crate_qualifiers,
                            impl_item.syntax(),
                        )
                    })
            })
        })
}

/// Finds first external trait implementation given an implementation name.
pub fn external_trait_impl_by_name(
    impl_name: &str,
    trait_name: &str,
    crate_qualifiers: &[&str],
    ref_node: &SyntaxNode,
) -> Option<ast::Impl> {
    external_trait_impls(trait_name, crate_qualifiers, ref_node).find(|impl_item| {
        impl_item
            .self_ty()
            .as_ref()
            .and_then(ink_analyzer_ir::path_from_type)
            .is_some_and(|path| is_path_target(impl_name, &path))
    })
}

/// Checks that the `path` resolves to the item `name` with one of the crate `qualifiers` (or any of their aliases).
pub fn is_external_crate_item(
    name: &str,
    path: &ast::Path,
    qualifiers: &[&str],
    ref_node: &SyntaxNode,
) -> bool {
    let name_is_path_target = is_path_target(name, path);

    // Checks if path's qualifier matches one of the specified qualifiers.
    // Matches exactly when strict is true, otherwise adds fully qualified variants
    // See `make_qualifiers_exhaustive` doc above.
    let path_has_qualifier = |qualifiers: &[&str], strict: bool| {
        path.qualifier().is_some_and(|qualifier| {
            let qualifier_str = ink_analyzer_ir::path_to_string(&qualifier);
            if strict {
                qualifiers.contains(&qualifier_str.as_str())
            } else {
                exhaustive_qualifiers(qualifiers).contains(&qualifier_str)
            }
        })
    };

    // Checks `name` or any of its aliases is the target of `path` (including scope considerations).
    (name_is_path_target && path_has_qualifier(qualifiers, false))
        || ink_analyzer_ir::resolve_current_module(ref_node).is_some_and(|root_node| {
            let crates: Vec<_> = qualifiers
                .iter()
                .map(|qualifier| qualifier.split("::").next().unwrap_or(qualifier))
                .collect();
            let (use_paths, item_aliases) =
                external_crate_uses_and_aliases_in_scope(&crates, &root_node);

            // Reverse `alias -> path` map to `path -> [alias]`.
            let mut item_path_to_aliases: HashMap<String, HashSet<String>> = HashMap::new();
            for (alias, item_path) in item_aliases {
                match item_path_to_aliases.get_mut(&item_path) {
                    Some(path_aliases) => {
                        path_aliases.insert(alias);
                    }
                    None => {
                        item_path_to_aliases.insert(item_path, HashSet::from([alias]));
                    }
                }
            }

            // Checks whether an item with given qualifiers is in scope.
            let is_item_in_scope = |item_name: &str, qualifiers: &[&str]| {
                exhaustive_qualifiers(qualifiers)
                    .into_iter()
                    .flat_map(|prefix| [format!("{prefix}::{item_name}"), format!("{prefix}::*")])
                    .any(|use_path| use_paths.contains(&use_path))
            };

            macro_rules! item_aliases {
                ($name: expr, $qualifiers: expr) => {
                    exhaustive_qualifiers($qualifiers)
                        .into_iter()
                        .flat_map(|prefix| [format!("{prefix}::{}", $name), format!("{prefix}::*")])
                        .filter_map(|item_path| item_path_to_aliases.get(&item_path))
                        .flatten()
                };
            }

            // Checks for scope and aliased name.
            let unqualified_target_name_in_scope =
                || path.qualifier().is_none() && is_item_in_scope(name, qualifiers);
            let target_is_name_alias = || {
                path.qualifier().is_none()
                    && item_aliases!(name, qualifiers).any(|alias| is_path_target(alias, path))
            };
            let sub_qualifier_or_alias_in_scope = || {
                qualifiers.iter().any(|qualifier| {
                    let qualifiers: Vec<_> = qualifier.split("::").collect();
                    let n_qualifiers = qualifiers.len();
                    (0..n_qualifiers).any(|idx| {
                        let anchor_name = qualifiers[idx];
                        let pre_anchor_qualifier = qualifiers[0..idx].join("::");
                        let post_anchor_qualifier = if idx < n_qualifiers {
                            let post_anchor = qualifiers[idx + 1..].join("::");
                            (!post_anchor.is_empty()).then_some(post_anchor)
                        } else {
                            None
                        };

                        let is_top_qualifier = idx == 0;
                        let anchor_and_post_qualifier = |alias: &str| {
                            format!(
                                "{alias}{}{}",
                                if post_anchor_qualifier
                                    .as_ref()
                                    .is_some_and(|it| !it.is_empty())
                                {
                                    "::"
                                } else {
                                    ""
                                },
                                post_anchor_qualifier.as_deref().unwrap_or("")
                            )
                        };
                        let anchor_qualifier_in_scope = || {
                            path_has_qualifier(
                                &[anchor_and_post_qualifier(anchor_name).as_str()],
                                !is_top_qualifier,
                            ) && (is_top_qualifier
                                || is_item_in_scope(anchor_name, &[pre_anchor_qualifier.as_str()]))
                        };
                        let anchor_alias_qualifier_in_scope = || {
                            let anchor_aliases: Vec<_> = if is_top_qualifier {
                                exhaustive_qualifiers(&[anchor_name])
                                    .into_iter()
                                    .filter_map(|item_path| item_path_to_aliases.get(&item_path))
                                    .flatten()
                                    .map(|alias| anchor_and_post_qualifier(alias))
                                    .collect()
                            } else {
                                item_aliases!(anchor_name, &[pre_anchor_qualifier.as_str()])
                                    .map(|alias| anchor_and_post_qualifier(alias))
                                    .collect()
                            };

                            path_has_qualifier(
                                &anchor_aliases
                                    .iter()
                                    .map(|it| it.as_str())
                                    .collect::<Vec<_>>(),
                                !is_top_qualifier,
                            )
                        };

                        anchor_qualifier_in_scope() || anchor_alias_qualifier_in_scope()
                    })
                })
            };
            let path_is_type_alias_for_name = || {
                ink_analyzer_ir::resolve_item::<ast::TypeAlias>(path, &root_node).is_some_and(
                    |type_alias| {
                        type_alias
                            .ty()
                            .as_ref()
                            .and_then(ink_analyzer_ir::path_from_type)
                            .is_some_and(|path| {
                                is_external_crate_item(name, &path, qualifiers, type_alias.syntax())
                            })
                    },
                )
            };

            (name_is_path_target
                && (unqualified_target_name_in_scope() || sub_qualifier_or_alias_in_scope()))
                || target_is_name_alias()
                || path_is_type_alias_for_name()
        })
}

/// Finds an ADT by either resolving the path or searching for the first ADT that implements an external trait.
pub fn candidate_adt_by_name_or_external_trait_impl(
    trait_name: &str,
    crate_qualifiers: &[&str],
    ref_node: &SyntaxNode,
    path_option: Option<&ast::Path>,
) -> Option<ast::Adt> {
    path_option
        // Finds a struct, enum or union with the target name.
        .and_then(|path| find_adt_by_name(path, ref_node))
        .or_else(|| {
            // Otherwise finds a struct that implements the external trait (if any).
            let root_node_opt = ref_node.ancestors().last();
            let root_node = root_node_opt.as_ref().unwrap_or(ref_node);
            let adt = external_trait_impls(trait_name, crate_qualifiers, root_node)
                .next()
                .and_then(|impl_item| find_adt_for_impl(&impl_item, ref_node));
            // Explicit binding due to temporaries.
            adt
        })
}

/// Finds a struct, enum or union with the target name.
pub fn find_adt_by_name(path: &ast::Path, ref_node: &SyntaxNode) -> Option<ast::Adt> {
    path.segment()
        .as_ref()
        .and_then(ast::PathSegment::name_ref)
        .and_then(|target_name| {
            let root_node_opt = ref_node.ancestors().last();
            let root_node = root_node_opt.as_ref().unwrap_or(ref_node);
            root_node
                .descendants()
                .filter(|it| ast::Adt::can_cast(it.kind()))
                .find_map(|node| {
                    ast::Adt::cast(node).filter(|item| {
                        item.name()
                            .is_some_and(|item_name| item_name.text() == target_name.text())
                    })
                })
        })
}

/// Finds a struct, enum or union for the `impl` item.
pub fn find_adt_for_impl(impl_item: &ast::Impl, ref_node: &SyntaxNode) -> Option<ast::Adt> {
    impl_item
        .self_ty()
        .as_ref()
        .and_then(ink_analyzer_ir::path_from_type)
        // Finds a struct, enum or union with the custom type name.
        .and_then(|path| find_adt_by_name(&path, ref_node))
}

// Checks if an item named `name` is the target of the `path`.
fn is_path_target(name: &str, path: &ast::Path) -> bool {
    path.segment()
        .as_ref()
        .and_then(ast::PathSegment::name_ref)
        .is_some_and(|name_ref| name_ref.to_string() == name)
}

// Add fully qualified variants to paths (e.g. [`ink`] becomes [`ink`, `::ink`]).
fn exhaustive_qualifiers(paths: &[&str]) -> Vec<String> {
    paths
        .iter()
        .flat_map(|qualifier| [format!("::{qualifier}"), String::from(*qualifier)])
        .collect()
}

// Checks if the path references an item in one of the crates.
fn is_crate_item_path(path: &str, crates: &[&str]) -> bool {
    exhaustive_qualifiers(crates)
        .iter()
        .any(|qualifier| path == qualifier || path.starts_with(&format!("{qualifier}::")))
}

// Collects crate use paths and aliases in the current scope.
fn external_crate_uses_and_aliases_in_scope(
    crates: &[&str],
    ref_node: &SyntaxNode,
) -> (HashSet<String>, HashMap<String, String>) {
    let (mut use_paths, mut item_aliases) =
        ink_analyzer_ir::simple_use_paths_and_aliases_in_scope(ref_node);

    while let Some(use_path_str) = use_paths
        .iter()
        .find_map(|use_path| (!is_crate_item_path(use_path, crates)).then(|| use_path.to_owned()))
    {
        // Removes path.
        use_paths.remove(&use_path_str);

        // Resolves path if it points to a use declaration for one of the specified external crates.
        let result = match_path_to_external_crate_in_scope(&use_path_str, crates, ref_node);
        use_paths.extend(result.0);
        item_aliases.extend(result.1);
    }

    while let Some((alias, item_path_str)) = item_aliases.iter().find_map(|(alias, item_path)| {
        (!is_crate_item_path(item_path, crates)).then(|| (alias.to_owned(), item_path.to_owned()))
    }) {
        // Removes alias.
        item_aliases.remove(&alias);

        // Resolves path if it points to a use declaration for one of the specified external crates.
        if let Some(target) = ink_analyzer_ir::path_from_str(&item_path_str)
            .as_ref()
            .and_then(ast::Path::segment)
        {
            let result = match_path_to_external_crate_in_scope(&item_path_str, crates, ref_node);
            if let Some(resolved_item_path) = result
                .0
                .iter()
                .next()
                .or_else(|| result.1.get(&target.to_string()))
            {
                item_aliases.insert(alias, resolved_item_path.to_owned());
            }
        }
    }

    (use_paths, item_aliases)
}

// Matches a path to an external crate item (if possible) using `use` declarations and aliases in the current scope.
fn match_path_to_external_crate_in_scope(
    path: &str,
    crates: &[&str],
    ref_node: &SyntaxNode,
) -> (HashSet<String>, HashMap<String, String>) {
    let mut use_paths = HashSet::new();
    let mut item_aliases = HashMap::new();
    let item_path_option = ink_analyzer_ir::path_from_str(path).and_then(|path| {
        path.qualifier().and_then(|qualifier| {
            let target_name_option = path
                .segment()
                .as_ref()
                .and_then(ast::PathSegment::name_ref)
                .as_ref()
                .map(ToString::to_string)
                .or_else(|| {
                    (ink_analyzer_ir::path_to_string(&path)
                        == format!("{}::*", ink_analyzer_ir::path_to_string(&qualifier)))
                    .then(|| String::from("*"))
                });

            target_name_option.zip(ink_analyzer_ir::resolve_qualifier(&qualifier, ref_node))
        })
    });
    if let Some((target_name, qualifier_ref_node)) = item_path_option {
        if target_name == "*" {
            let result = external_crate_uses_and_aliases_in_scope(crates, &qualifier_ref_node);
            use_paths.extend(result.0);
            item_aliases.extend(result.1);
        } else {
            for resolved_path in ink_analyzer_ir::resolve_item_path_from_use_scope_and_aliases!(
                target_name,
                &qualifier_ref_node
            ) {
                let resolved_path_str = ink_analyzer_ir::path_to_string(&resolved_path);
                if is_crate_item_path(&resolved_path_str, crates) {
                    if resolved_path_str.ends_with(&format!("::{target_name}")) {
                        use_paths.insert(resolved_path_str.clone());
                    } else {
                        item_aliases.insert(target_name.clone(), resolved_path_str);
                    }
                } else if resolved_path_str != path {
                    // Only recurse if the path resolved from use scope and aliases
                    // is different from the current path argument.
                    let result = match_path_to_external_crate_in_scope(
                        &resolved_path_str,
                        crates,
                        &qualifier_ref_node,
                    );
                    use_paths.extend(result.0);
                    item_aliases.extend(result.1);
                }
            }
        }
    }

    (use_paths, item_aliases)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::{parse_first_ast_node_of_type, parse_source};
    use ink_analyzer_ir::ast::HasName;
    use ink_analyzer_ir::{InkEntity, InkFile};
    use quote::quote;
    use test_utils::quote_as_str;

    #[test]
    fn external_crate_item_path_resolution_works() {
        let ref_name = quote! { ref_node };
        for (code, path_str) in [
            // Simple paths.
            ("", "ink::env::Environment"),
            ("", "::ink::env::Environment"),
            ("", "ink_env::Environment"),
            ("", "::ink_env::Environment"),
            // Scoped paths.
            ("use ink::env::Environment;", "Environment"),
            ("use ink::env::*", "Environment;"),
            ("use ink::{env::Environment, primitives};", "Environment"),
            (
                "use ink::{env::{Environment, DefaultEnvironment}, primitives};",
                "Environment",
            ),
            ("use ink::env", "env::Environment;"),
            ("use ink::{env, primitives};", "env::Environment"),
            (
                quote_as_str! {
                    use ink::env::Environment;

                    mod #ref_name {
                        use super::Environment;
                    }
                },
                "Environment",
            ),
            (
                quote_as_str! {
                    use ink::env::Environment;

                    mod #ref_name {
                        use super::*;
                    }
                },
                "Environment",
            ),
            // Use aliases.
            (
                "use ink::env::Environment as ChainEnvironment;",
                "ChainEnvironment",
            ),
            (
                "use ink::{env::Environment as ChainEnvironment, primitives};",
                "ChainEnvironment",
            ),
            ("use ink::env as chain_env;", "chain_env::Environment"),
            (
                "use ink::{env as chain_env, primitives};",
                "chain_env::Environment",
            ),
            ("use ink as ink_lang;", "ink_lang::env::Environment"),
            ("use ink_env as ink_lang_env;", "ink_lang_env::Environment"),
            (
                quote_as_str! {
                    use ink::env::Environment as ChainEnvironment;

                    mod #ref_name {
                        use super::ChainEnvironment;
                    }
                },
                "ChainEnvironment",
            ),
            (
                quote_as_str! {
                    use ink::env::Environment as ChainEnvironment;

                    mod #ref_name {
                        use super::*;
                    }
                },
                "ChainEnvironment",
            ),
            (
                quote_as_str! {
                    use ink::env::Environment as InkEnvironment;

                    mod #ref_name {
                        use super::InkEnvironment as ChainEnvironment;
                    }
                },
                "ChainEnvironment",
            ),
            (
                quote_as_str! {
                    use ink::env::Environment as ChainEnvironment1;
                    use ink::env::Environment as ChainEnvironment2;
                },
                "ChainEnvironment1",
            ),
            (
                quote_as_str! {
                    use ink::env::Environment as ChainEnvironment1;
                    use ink::env::Environment as ChainEnvironment2;
                },
                "ChainEnvironment2",
            ),
            (
                quote_as_str! {
                    use ink::env::Environment as ChainEnvironment1;
                    use ink::env::Environment as ChainEnvironment2;

                    mod #ref_name {
                        use super::ChainEnvironment1;
                    }
                },
                "ChainEnvironment1",
            ),
            (
                quote_as_str! {
                    use ink::env::Environment as ChainEnvironment1;
                    use ink::env::Environment as ChainEnvironment2;

                    mod #ref_name {
                        use super::ChainEnvironment2;
                    }
                },
                "ChainEnvironment2",
            ),
            (
                quote_as_str! {
                    use ink::env::Environment as ChainEnvironment1;
                    use ink::env::Environment as ChainEnvironment2;

                    mod #ref_name {
                        use super::*;
                    }
                },
                "ChainEnvironment1",
            ),
            (
                quote_as_str! {
                    use ink::env::Environment as ChainEnvironment1;
                    use ink::env::Environment as ChainEnvironment2;

                    mod #ref_name {
                        use super::*;
                    }
                },
                "ChainEnvironment2",
            ),
            // Type aliases.
            (
                "type ChainEnvironment = ink::env::Environment;",
                "ChainEnvironment",
            ),
            (
                "type ChainEnvironment = ink::env::Environment;",
                "self::ChainEnvironment",
            ),
            (
                quote_as_str! {
                    type ChainEnvironment = ink::env::Environment;

                    mod #ref_name {
                    }
                },
                "super::ChainEnvironment",
            ),
            (
                quote_as_str! {
                    type ChainEnvironment = ink::env::Environment;

                    mod #ref_name {
                        type RenamedEnvironment = super::ChainEnvironment;
                    }
                },
                "RenamedEnvironment",
            ),
            (
                quote_as_str! {
                    type ChainEnvironment = ink::env::Environment;

                    mod #ref_name {
                        type RenamedEnvironment = super::ChainEnvironment;
                    }
                },
                "self::RenamedEnvironment",
            ),
        ] {
            let file = InkFile::parse(code);
            let path: ast::Path = parse_first_ast_node_of_type(path_str);
            let ref_module_option = parse_source(code).syntax().descendants().find_map(|node| {
                ast::Module::cast(node).filter(|item| {
                    item.name()
                        .is_some_and(|name| name.to_string() == ref_name.to_string())
                })
            });

            assert!(
                is_external_crate_item(
                    "Environment",
                    &path,
                    &["ink::env", "ink_env"],
                    ref_module_option
                        .as_ref()
                        .map(AstNode::syntax)
                        .unwrap_or(file.syntax())
                ),
                "code: {code} | path: {path_str}"
            );
        }
    }
}