nu_protocol/errors/
compile_error.rs

1use crate::{RegId, Span};
2use miette::Diagnostic;
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
5
6/// An internal compiler error, generally means a Nushell bug rather than an issue with user error
7/// since parsing and typechecking has already passed.
8#[derive(Debug, Clone, Error, Diagnostic, PartialEq, Serialize, Deserialize)]
9pub enum CompileError {
10    #[error("Register overflow.")]
11    #[diagnostic(code(nu::compile::register_overflow))]
12    RegisterOverflow {
13        #[label("the code being compiled is probably too large")]
14        block_span: Option<Span>,
15    },
16
17    #[error("Register {reg_id} was uninitialized when used, possibly reused.")]
18    #[diagnostic(
19        code(nu::compile::register_uninitialized),
20        help(
21            "this is a compiler bug. Please report it at https://github.com/nushell/nushell/issues/new\nfrom: {caller}"
22        )
23    )]
24    RegisterUninitialized { reg_id: RegId, caller: String },
25
26    #[error("Register {reg_id} was uninitialized when used, possibly reused.")]
27    #[diagnostic(
28        code(nu::compile::register_uninitialized),
29        help(
30            "this is a compiler bug. Please report it at https://github.com/nushell/nushell/issues/new\nfrom: {caller}"
31        )
32    )]
33    RegisterUninitializedWhilePushingInstruction {
34        reg_id: RegId,
35        caller: String,
36        instruction: String,
37        #[label("while adding this instruction: {instruction}")]
38        span: Span,
39    },
40
41    #[error("Block contains too much string data: maximum 4 GiB exceeded.")]
42    #[diagnostic(
43        code(nu::compile::data_overflow),
44        help("try loading the string data from a file instead")
45    )]
46    DataOverflow {
47        #[label("while compiling this block")]
48        block_span: Option<Span>,
49    },
50
51    #[error("Block contains too many files.")]
52    #[diagnostic(
53        code(nu::compile::register_overflow),
54        help("try using fewer file redirections")
55    )]
56    FileOverflow {
57        #[label("while compiling this block")]
58        block_span: Option<Span>,
59    },
60
61    #[error("Invalid redirect mode: File should not be specified by commands.")]
62    #[diagnostic(
63        code(nu::compile::invalid_redirect_mode),
64        help(
65            "this is a command bug. Please report it at https://github.com/nushell/nushell/issues/new"
66        )
67    )]
68    InvalidRedirectMode {
69        #[label("while compiling this expression")]
70        span: Span,
71    },
72
73    #[error("Encountered garbage, likely due to parse error.")]
74    #[diagnostic(code(nu::compile::garbage))]
75    Garbage {
76        #[label("garbage found here")]
77        span: Span,
78    },
79
80    #[error("Unsupported operator expression.")]
81    #[diagnostic(code(nu::compile::unsupported_operator_expression))]
82    UnsupportedOperatorExpression {
83        #[label("this expression is in operator position but is not an operator")]
84        span: Span,
85    },
86
87    #[error("Attempted access of $env by integer path.")]
88    #[diagnostic(code(nu::compile::access_env_by_int))]
89    AccessEnvByInt {
90        #[label("$env keys should be strings")]
91        span: Span,
92    },
93
94    #[error("Encountered invalid `{keyword}` keyword call.")]
95    #[diagnostic(code(nu::compile::invalid_keyword_call))]
96    InvalidKeywordCall {
97        keyword: String,
98        #[label("this call is not properly formed")]
99        span: Span,
100    },
101
102    #[error("Attempted to set branch target of non-branch instruction.")]
103    #[diagnostic(
104        code(nu::compile::set_branch_target_of_non_branch_instruction),
105        help(
106            "this is a compiler bug. Please report it at https://github.com/nushell/nushell/issues/new"
107        )
108    )]
109    SetBranchTargetOfNonBranchInstruction {
110        instruction: String,
111        #[label("tried to modify: {instruction}")]
112        span: Span,
113    },
114
115    /// You're trying to run an unsupported external command.
116    ///
117    /// ## Resolution
118    ///
119    /// Make sure there's an appropriate `run-external` declaration for this external command.
120    #[error("External calls are not supported.")]
121    #[diagnostic(
122        code(nu::compile::run_external_not_found),
123        help("`run-external` was not found in scope")
124    )]
125    RunExternalNotFound {
126        #[label("can't be run in this context")]
127        span: Span,
128    },
129
130    /// Invalid assignment left-hand side
131    ///
132    /// ## Resolution
133    ///
134    /// Assignment requires that you assign to a variable or variable cell path.
135    #[error("Assignment operations require a variable.")]
136    #[diagnostic(
137        code(nu::compile::assignment_requires_variable),
138        help("try assigning to a variable or a cell path of a variable")
139    )]
140    AssignmentRequiresVar {
141        #[label("needs to be a variable")]
142        span: Span,
143    },
144
145    /// Invalid assignment left-hand side
146    ///
147    /// ## Resolution
148    ///
149    /// Assignment requires that you assign to a mutable variable or cell path.
150    #[error("Assignment to an immutable variable.")]
151    #[diagnostic(
152        code(nu::compile::assignment_requires_mutable_variable),
153        help("declare the variable with `mut`, or shadow it again with `let`")
154    )]
155    AssignmentRequiresMutableVar {
156        #[label("needs to be a mutable variable")]
157        span: Span,
158    },
159
160    /// This environment variable cannot be set manually.
161    ///
162    /// ## Resolution
163    ///
164    /// This environment variable is set automatically by Nushell and cannot not be set manually.
165    #[error("{envvar_name} cannot be set manually.")]
166    #[diagnostic(
167        code(nu::compile::automatic_env_var_set_manually),
168        help(
169            r#"The environment variable '{envvar_name}' is set automatically by Nushell and cannot be set manually."#
170        )
171    )]
172    AutomaticEnvVarSetManually {
173        envvar_name: String,
174        #[label("cannot set '{envvar_name}' manually")]
175        span: Span,
176    },
177
178    /// It is not possible to replace the entire environment at once
179    ///
180    /// ## Resolution
181    ///
182    /// Setting the entire environment is not allowed. Change environment variables individually
183    /// instead.
184    #[error("Cannot replace environment.")]
185    #[diagnostic(
186        code(nu::compile::cannot_replace_env),
187        help("Assigning a value to '$env' is not allowed.")
188    )]
189    CannotReplaceEnv {
190        #[label("setting '$env' not allowed")]
191        span: Span,
192    },
193
194    #[error("Unexpected expression.")]
195    #[diagnostic(code(nu::compile::unexpected_expression))]
196    UnexpectedExpression {
197        expr_name: String,
198        #[label("{expr_name} is not allowed in this context")]
199        span: Span,
200    },
201
202    #[error("Missing required declaration: `{decl_name}`")]
203    #[diagnostic(code(nu::compile::missing_required_declaration))]
204    MissingRequiredDeclaration {
205        decl_name: String,
206        #[label("`{decl_name}` must be in scope to compile this expression")]
207        span: Span,
208    },
209
210    #[error("Invalid literal")]
211    #[diagnostic(code(nu::compile::invalid_literal))]
212    InvalidLiteral {
213        msg: String,
214        #[label("{msg}")]
215        span: Span,
216    },
217
218    #[error("{msg}")]
219    #[diagnostic(code(nu::compile::not_in_a_loop))]
220    NotInALoop {
221        msg: String,
222        #[label("can't be used outside of a loop")]
223        span: Option<Span>,
224    },
225
226    #[error("Incoherent loop state: the loop that ended was not the one we were expecting.")]
227    #[diagnostic(
228        code(nu::compile::incoherent_loop_state),
229        help(
230            "this is a compiler bug. Please report it at https://github.com/nushell/nushell/issues/new"
231        )
232    )]
233    IncoherentLoopState {
234        #[label("while compiling this block")]
235        block_span: Option<Span>,
236    },
237
238    #[error("Undefined label `{label_id}`.")]
239    #[diagnostic(
240        code(nu::compile::undefined_label),
241        help(
242            "this is a compiler bug. Please report it at https://github.com/nushell/nushell/issues/new"
243        )
244    )]
245    UndefinedLabel {
246        label_id: usize,
247        #[label("label was used while compiling this code")]
248        span: Option<Span>,
249    },
250}