sway_core/
error.rs

1//! Tools related to handling/recovering from Sway compile errors and reporting them to the user.
2
3use crate::{language::parsed::VariableDeclaration, namespace::ModulePath, Engines, Namespace};
4
5/// Acts as the result of parsing `Declaration`s, `Expression`s, etc.
6/// Some `Expression`s need to be able to create `VariableDeclaration`s,
7/// so this struct is used to "bubble up" those declarations to a viable
8/// place in the AST.
9#[derive(Debug, Clone)]
10pub struct ParserLifter<T> {
11    pub var_decls: Vec<VariableDeclaration>,
12    pub value: T,
13}
14
15impl<T> ParserLifter<T> {
16    #[allow(dead_code)]
17    pub(crate) fn empty(value: T) -> Self {
18        ParserLifter {
19            var_decls: vec![],
20            value,
21        }
22    }
23}
24
25/// When providing suggestions for errors and warnings, a solution for an issue can sometimes
26/// be changing the code in some other module. We want to provide such suggestions only if
27/// the programmer can actually change the code in that module.
28///
29/// Assuming that the issue occurs in the `issue_namespace` to which the programmer has access,
30/// and that fixing it means changing the code in the module given by the `absolute_module_path`
31/// this function returns true if the programmer can change that module.
32pub(crate) fn module_can_be_changed(
33    _engines: &Engines,
34    issue_namespace: &Namespace,
35    absolute_module_path: &ModulePath,
36) -> bool {
37    // For now, we assume that the programmers can change the module
38    // if the module is in the same package where the issue is.
39    // A bit too restrictive, considering the same workspace might be more appropriate,
40    // but it's a good start.
41    !issue_namespace.module_is_external(absolute_module_path)
42}