microcad_lang/resolve/
resolve_error.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Resolve error
5
6use miette::Diagnostic;
7use thiserror::Error;
8
9use crate::{diag::*, parse::*, syntax::*};
10use crate::src_ref::{SrcRef, SrcReferrer};
11
12/// Resolve error.
13#[derive(Debug, Error, Diagnostic)]
14pub enum ResolveError {
15    /// Parse Error.
16    #[error("Parse Error: {0}")]
17    ParseError(#[from] ParseErrorWithSource),
18
19    /// Can't find a project file by hash.
20    #[error("Could not find a file with hash {0}")]
21    UnknownHash(u64),
22
23    /// Hash is zero
24    #[error("Hash is zero")]
25    NulHash,
26
27    /// Name of external symbol is unknown.
28    #[error("External symbol `{0}` not found")]
29    ExternalSymbolNotFound(QualifiedName),
30
31    /// Path of external file is unknown.
32    #[error("External path `{0}` not found")]
33    ExternalPathNotFound(std::path::PathBuf),
34
35    /// Can't find a project file by it's path.
36    #[error("Could not find a file with path {0}")]
37    FileNotFound(std::path::PathBuf),
38
39    /// Symbol not found.
40    #[error("Symbol {0} not found while resolving.")]
41    SymbolNotFound(QualifiedName),
42
43    /// Symbol not found (retry to load from external).
44    #[error("Symbol {0} must be loaded from {1}")]
45    SymbolMustBeLoaded(QualifiedName, std::path::PathBuf),
46
47    /// Symbol is not a value
48    #[error("Symbol {0} is not a value")]
49    NotAValue(QualifiedName),
50
51    /// Declaration of property not allowed here
52    #[error("Declaration of {0} not allowed within {1}")]
53    DeclNotAllowed(Identifier, QualifiedName),
54
55    /// Sternal module file not found
56    #[error("Ambiguous external module files found {0:?}")]
57    AmbiguousExternals(Vec<std::path::PathBuf>),
58
59    /// Ambiguous symbol was found
60    #[error("Symbol {0} already defined")]
61    SymbolAlreadyDefined(QualifiedName),
62
63    /// Ambiguous symbol was found
64    #[error("Ambiguous symbol found: {0}")]
65    AmbiguousSymbol(QualifiedName, QualifiedNames),
66
67    /// ScanDir Error
68    #[error("{0}")]
69    ScanDirError(#[from] scan_dir::Error),
70
71    /// Invalid path.
72    #[error("Invalid path: {0:?}")]
73    InvalidPath(std::path::PathBuf),
74
75    /// Diagnostic error
76    #[error("Diagnostic error: {0}")]
77    DiagError(#[from] DiagError),
78
79    /// Statement is not supported in this context.
80    #[error("{0} is not available within {1}")]
81    StatementNotSupported(String, String),
82
83    /// Resolve check failed
84    #[error("Resolve failed")]
85    ResolveCheckFailed,
86
87    /// Symbol is private
88    #[error("Symbol {0} is private")]
89    SymbolIsPrivate(QualifiedName),
90
91    /// ScanDir Error
92    #[error("{0}")]
93    IoError(#[from] std::io::Error),
94
95    /// Invalid path.
96    #[error(
97        "Source of module '{0}' could not be found in {1:?} (expecting a file '{0}.µcad' or '{0}/mod.µcad')"
98    )]
99    SourceFileNotFound(
100        #[label("module not found")]
101        Identifier,
102        std::path::PathBuf
103    ),
104
105    /// Wrong lookup target
106    #[error("Wrong lookup target")]
107    WrongTarget,
108
109    /// Statement not allowed within workbenches
110    #[error("Statement not allowed within workbenches")]
111    IllegalWorkbenchStatement,
112
113    /// Code Between initializers
114    #[error("Code between initializers is not allowed")]
115    CodeBetweenInitializers,
116
117    /// Statement not allowed prior initializers
118    #[error("Statement not allowed prior initializers")]
119    StatementNotAllowedPriorInitializers,
120}
121
122impl SrcReferrer for ResolveError {
123    fn src_ref(&self) -> SrcRef {
124        match self {
125            ResolveError::SourceFileNotFound(identifier, _) => identifier.src_ref(),
126            _ => SrcRef(None),
127        }
128    }
129}
130
131/// Result type of any resolve.
132pub type ResolveResult<T> = std::result::Result<T, ResolveError>;