Skip to main content

nu_protocol/errors/
compile_error.rs

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