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