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 changed manually.
162    ///
163    /// ## Resolution
164    ///
165    /// This environment variable is set automatically by Nushell and cannot be set manually.
166    #[error("'{}' cannot be changed manually.", .envvar_name.to_ascii_uppercase())]
167    #[diagnostic(
168        code(nu::compile::automatic_env_var_set_manually),
169        help(
170            "The environment variable '{}' is set automatically by Nushell and cannot be changed manually.",
171            .envvar_name.to_ascii_uppercase()
172        )
173    )]
174    AutomaticEnvVarSetManually {
175        envvar_name: String,
176        #[label("cannot change '{}' manually", .envvar_name.to_ascii_uppercase())]
177        span: Span,
178    },
179
180    /// It is not possible to replace the entire environment at once
181    ///
182    /// ## Resolution
183    ///
184    /// Setting the entire environment is not allowed. Change environment variables individually
185    /// instead.
186    #[error("Cannot replace environment.")]
187    #[diagnostic(
188        code(nu::compile::cannot_replace_env),
189        help("Assigning a value to '$env' is not allowed.")
190    )]
191    CannotReplaceEnv {
192        #[label("setting '$env' not allowed")]
193        span: Span,
194    },
195
196    #[error("Unexpected expression.")]
197    #[diagnostic(code(nu::compile::unexpected_expression))]
198    UnexpectedExpression {
199        expr_name: String,
200        #[label("{expr_name} is not allowed in this context")]
201        span: Span,
202    },
203
204    #[error("Missing required declaration: `{decl_name}`")]
205    #[diagnostic(code(nu::compile::missing_required_declaration))]
206    MissingRequiredDeclaration {
207        decl_name: String,
208        #[label("`{decl_name}` must be in scope to compile this expression")]
209        span: Span,
210    },
211
212    #[error("Invalid literal")]
213    #[diagnostic(code(nu::compile::invalid_literal))]
214    InvalidLiteral {
215        msg: String,
216        #[label("{msg}")]
217        span: Span,
218    },
219
220    #[error("{msg}")]
221    #[diagnostic(code(nu::compile::not_in_a_try))]
222    NotInATry {
223        msg: String,
224        #[label("can't be used outside of a try block")]
225        span: Option<Span>,
226    },
227
228    #[error("{msg}")]
229    #[diagnostic(code(nu::compile::not_in_a_loop))]
230    NotInALoop {
231        msg: String,
232        #[label("can't be used outside of a loop")]
233        span: Option<Span>,
234    },
235
236    #[error("Incoherent loop state: the loop that ended was not the one we were expecting.")]
237    #[diagnostic(
238        code(nu::compile::incoherent_loop_state),
239        help(
240            "this is a compiler bug. Please report it at https://github.com/nushell/nushell/issues/new"
241        )
242    )]
243    IncoherentLoopState {
244        #[label("while compiling this block")]
245        block_span: Option<Span>,
246    },
247
248    #[error("Undefined label `{label_id}`.")]
249    #[diagnostic(
250        code(nu::compile::undefined_label),
251        help(
252            "this is a compiler bug. Please report it at https://github.com/nushell/nushell/issues/new"
253        )
254    )]
255    UndefinedLabel {
256        label_id: usize,
257        #[label("label was used while compiling this code")]
258        span: Option<Span>,
259    },
260}