1use crate::{RegId, Span};
2use miette::Diagnostic;
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
56/// 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))]
12RegisterOverflow {
13#[label("the code being compiled is probably too large")]
14block_span: Option<Span>,
15 },
1617#[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 )]
24RegisterUninitialized { reg_id: RegId, caller: String },
2526#[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 )]
33RegisterUninitializedWhilePushingInstruction {
34 reg_id: RegId,
35 caller: String,
36 instruction: String,
37#[label("while adding this instruction: {instruction}")]
38span: Span,
39 },
4041#[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 )]
46DataOverflow {
47#[label("while compiling this block")]
48block_span: Option<Span>,
49 },
5051#[error("Block contains too many files.")]
52 #[diagnostic(
53 code(nu::compile::register_overflow),
54 help("try using fewer file redirections")
55 )]
56FileOverflow {
57#[label("while compiling this block")]
58block_span: Option<Span>,
59 },
6061#[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 )]
68InvalidRedirectMode {
69#[label("while compiling this expression")]
70span: Span,
71 },
7273#[error("Encountered garbage, likely due to parse error.")]
74 #[diagnostic(code(nu::compile::garbage))]
75Garbage {
76#[label("garbage found here")]
77span: Span,
78 },
7980#[error("Unsupported operator expression.")]
81 #[diagnostic(code(nu::compile::unsupported_operator_expression))]
82UnsupportedOperatorExpression {
83#[label("this expression is in operator position but is not an operator")]
84span: Span,
85 },
8687#[error("Attempted access of $env by integer path.")]
88 #[diagnostic(code(nu::compile::access_env_by_int))]
89AccessEnvByInt {
90#[label("$env keys should be strings")]
91span: Span,
92 },
9394#[error("Encountered invalid `{keyword}` keyword call.")]
95 #[diagnostic(code(nu::compile::invalid_keyword_call))]
96InvalidKeywordCall {
97 keyword: String,
98#[label("this call is not properly formed")]
99span: Span,
100 },
101102#[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 )]
109SetBranchTargetOfNonBranchInstruction {
110 instruction: String,
111#[label("tried to modify: {instruction}")]
112span: Span,
113 },
114115/// 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 )]
125RunExternalNotFound {
126#[label("can't be run in this context")]
127span: Span,
128 },
129130/// 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 )]
140AssignmentRequiresVar {
141#[label("needs to be a variable")]
142span: Span,
143 },
144145/// 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 )]
155AssignmentRequiresMutableVar {
156#[label("needs to be a mutable variable")]
157span: Span,
158 },
159160/// 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(
169r#"The environment variable '{envvar_name}' is set automatically by Nushell and cannot be set manually."#
170)
171 )]
172AutomaticEnvVarSetManually {
173 envvar_name: String,
174#[label("cannot set '{envvar_name}' manually")]
175span: Span,
176 },
177178/// 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 )]
189CannotReplaceEnv {
190#[label("setting '$env' not allowed")]
191span: Span,
192 },
193194#[error("Unexpected expression.")]
195 #[diagnostic(code(nu::compile::unexpected_expression))]
196UnexpectedExpression {
197 expr_name: String,
198#[label("{expr_name} is not allowed in this context")]
199span: Span,
200 },
201202#[error("Missing required declaration: `{decl_name}`")]
203 #[diagnostic(code(nu::compile::missing_required_declaration))]
204MissingRequiredDeclaration {
205 decl_name: String,
206#[label("`{decl_name}` must be in scope to compile this expression")]
207span: Span,
208 },
209210#[error("Invalid literal")]
211 #[diagnostic(code(nu::compile::invalid_literal))]
212InvalidLiteral {
213 msg: String,
214#[label("{msg}")]
215span: Span,
216 },
217218#[error("{msg}")]
219 #[diagnostic(code(nu::compile::not_in_a_loop))]
220NotInALoop {
221 msg: String,
222#[label("can't be used outside of a loop")]
223span: Option<Span>,
224 },
225226#[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 )]
233IncoherentLoopState {
234#[label("while compiling this block")]
235block_span: Option<Span>,
236 },
237238#[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 )]
245UndefinedLabel {
246 label_id: usize,
247#[label("label was used while compiling this code")]
248span: Option<Span>,
249 },
250}