From 037ea618c7879ae470611174b4287672c0b947a5 Mon Sep 17 00:00:00 2001 From: Devyn Cairns Date: Tue, 18 Jun 2024 22:21:59 -0700 Subject: [PATCH] give ICE a proper error type --- crates/nu-engine/src/compile/mod.rs | 27 ++++++-------------- crates/nu-protocol/src/errors/shell_error.rs | 17 ++++++++++++ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/crates/nu-engine/src/compile/mod.rs b/crates/nu-engine/src/compile/mod.rs index 1f66bf3131..d82b065d0c 100644 --- a/crates/nu-engine/src/compile/mod.rs +++ b/crates/nu-engine/src/compile/mod.rs @@ -591,36 +591,25 @@ enum CompileError { impl CompileError { fn to_shell_error(self, mut span: Option) -> ShellError { - let ice = "internal compiler error: "; let message = match self { - CompileError::RegisterOverflow => format!("{ice}register overflow"), + CompileError::RegisterOverflow => format!("register overflow"), CompileError::RegisterUninitialized(reg_id) => { - format!("{ice}register {reg_id} is uninitialized when used, possibly reused") + format!("register {reg_id} is uninitialized when used, possibly reused") } CompileError::InvalidRedirectMode => { - format!("{ice}invalid redirect mode: File should not be specified by commands") - } - CompileError::Garbage => { - format!("{ice}encountered garbage, likely due to parse error") - } - CompileError::UnsupportedOperatorExpression => { - format!("{ice}unsupported operator expression") + "invalid redirect mode: File should not be specified by commands".into() } + CompileError::Garbage => "encountered garbage, likely due to parse error".into(), + CompileError::UnsupportedOperatorExpression => "unsupported operator expression".into(), CompileError::AccessEnvByInt(local_span) => { span = Some(local_span); - format!("{ice}attempted access of $env by integer path") + "attempted access of $env by integer path".into() } CompileError::Todo(msg) => { - format!("{ice}TODO: {msg}") + format!("TODO: {msg}") } }; - ShellError::GenericError { - error: message, - msg: "while compiling this code".into(), - span, - help: Some("this is a bug, please report it at https://github.com/nushell/nushell/issues/new along with the code you were compiling if able".into()), - inner: vec![] - } + ShellError::IrCompileError { message, span } } } diff --git a/crates/nu-protocol/src/errors/shell_error.rs b/crates/nu-protocol/src/errors/shell_error.rs index e201915fcf..61f967ae5f 100644 --- a/crates/nu-protocol/src/errors/shell_error.rs +++ b/crates/nu-protocol/src/errors/shell_error.rs @@ -1369,6 +1369,23 @@ On Windows, this would be %USERPROFILE%\AppData\Roaming"# help("Set XDG_CONFIG_HOME to an absolute path, or set it to an empty string to ignore it") )] InvalidXdgConfig { xdg: String, default: String }, + + /// An error occurred with the IR compiler. + /// + /// ## Resolution + /// + /// The IR compiler is in very early development, so code that can't be compiled is quite + /// expected. If you think it should be working, please report it to us. + #[error("internal compiler error: {message}")] + #[diagnostic( + code(nu::shell::ir_compile_error), + help("this is a bug, please report it at https://github.com/nushell/nushell/issues/new along with the code you were compiling if able") + )] + IrCompileError { + message: String, + #[label = "while compiling this code"] + span: Option, + }, } // TODO: Implement as From trait