From 85922fe50fc6e08c257c3400a90bc96e778370a4 Mon Sep 17 00:00:00 2001 From: Devyn Cairns Date: Tue, 9 Jul 2024 18:00:46 -0700 Subject: [PATCH] move IR compile errors into own section in StateWorkingSet --- crates/nu-cli/src/eval_cmds.rs | 5 ++++ crates/nu-cli/src/util.rs | 5 ++++ crates/nu-command/src/debug/view_ir.rs | 25 ++++++++----------- crates/nu-parser/src/parser.rs | 13 +++------- .../src/engine/state_working_set.rs | 6 +++-- .../nu-protocol/src/errors/parse_warning.rs | 17 +------------ crates/nu-protocol/src/errors/shell_error.rs | 19 ++------------ 7 files changed, 32 insertions(+), 58 deletions(-) diff --git a/crates/nu-cli/src/eval_cmds.rs b/crates/nu-cli/src/eval_cmds.rs index 13141f6174..ad3a15304d 100644 --- a/crates/nu-cli/src/eval_cmds.rs +++ b/crates/nu-cli/src/eval_cmds.rs @@ -70,6 +70,11 @@ pub fn evaluate_commands( std::process::exit(1); } + if let Some(err) = working_set.compile_errors.first() { + report_error(&working_set, err); + // Not a fatal error, for now + } + (output, working_set.render()) }; diff --git a/crates/nu-cli/src/util.rs b/crates/nu-cli/src/util.rs index e4912e012f..fa567b3027 100644 --- a/crates/nu-cli/src/util.rs +++ b/crates/nu-cli/src/util.rs @@ -265,6 +265,11 @@ fn evaluate_source( return Ok(Some(1)); } + if let Some(err) = working_set.compile_errors.first() { + report_error(&working_set, err); + // Not a fatal error, for now + } + (output, working_set.render()) }; diff --git a/crates/nu-command/src/debug/view_ir.rs b/crates/nu-command/src/debug/view_ir.rs index c18f30d92a..df4f6cad6b 100644 --- a/crates/nu-command/src/debug/view_ir.rs +++ b/crates/nu-command/src/debug/view_ir.rs @@ -1,6 +1,4 @@ -use std::borrow::Cow; - -use nu_engine::{command_prelude::*, compile}; +use nu_engine::command_prelude::*; use nu_protocol::engine::Closure; #[derive(Clone)] @@ -41,17 +39,16 @@ impl Command for ViewIr { let json = call.has_flag(engine_state, stack, "json")?; let block = engine_state.get_block(closure.block_id); - // Use the pre-compiled block if available, otherwise try to compile it - // This helps display the actual compilation error - let ir_block = match &block.ir_block { - Some(ir_block) => Cow::Borrowed(ir_block), - None => Cow::Owned(compile(&StateWorkingSet::new(engine_state), block).map_err( - |err| ShellError::IrCompileError { - span: block.span, - errors: vec![err], - }, - )?), - }; + let ir_block = block + .ir_block + .as_ref() + .ok_or_else(|| ShellError::GenericError { + error: "Can't view IR for this block".into(), + msg: "block is missing compiled representation".into(), + span: block.span, + help: Some("the IrBlock is probably missing due to a compilation error".into()), + inner: vec![], + })?; let formatted = if json { let formatted_instructions = ir_block diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index dd5ca3cd5f..46f0951c56 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -12,8 +12,8 @@ use log::trace; use nu_engine::DIR_VAR_PARSER_INFO; use nu_protocol::{ ast::*, engine::StateWorkingSet, eval_const::eval_constant, BlockId, DidYouMean, Flag, - ParseError, ParseWarning, PositionalArg, Signature, Span, Spanned, SyntaxShape, Type, VarId, - ENV_VARIABLE_ID, IN_VARIABLE_ID, + ParseError, PositionalArg, Signature, Span, Spanned, SyntaxShape, Type, VarId, ENV_VARIABLE_ID, + IN_VARIABLE_ID, }; use std::{ collections::{HashMap, HashSet}, @@ -5847,18 +5847,13 @@ pub fn parse_block( block } -/// Compile an IR block for the `Block`, adding a parse warning on failure +/// Compile an IR block for the `Block`, adding a compile error on failure fn compile_block(working_set: &mut StateWorkingSet<'_>, block: &mut Block) { match nu_engine::compile(working_set, block) { Ok(ir_block) => { block.ir_block = Some(ir_block); } - Err(err) => working_set - .parse_warnings - .push(ParseWarning::IrCompileError { - span: block.span.unwrap_or(Span::unknown()), - errors: vec![err], - }), + Err(err) => working_set.compile_errors.push(err), } } diff --git a/crates/nu-protocol/src/engine/state_working_set.rs b/crates/nu-protocol/src/engine/state_working_set.rs index 33ca9b7ae3..8c3968a824 100644 --- a/crates/nu-protocol/src/engine/state_working_set.rs +++ b/crates/nu-protocol/src/engine/state_working_set.rs @@ -4,8 +4,8 @@ use crate::{ usage::build_usage, CachedFile, Command, CommandType, EngineState, OverlayFrame, StateDelta, Variable, VirtualPath, Visibility, }, - BlockId, Category, Config, DeclId, FileId, GetSpan, Module, ModuleId, ParseError, ParseWarning, - Span, SpanId, Type, Value, VarId, VirtualPathId, + BlockId, Category, CompileError, Config, DeclId, FileId, GetSpan, Module, ModuleId, ParseError, + ParseWarning, Span, SpanId, Type, Value, VarId, VirtualPathId, }; use core::panic; use std::{ @@ -31,6 +31,7 @@ pub struct StateWorkingSet<'a> { pub search_predecls: bool, pub parse_errors: Vec, pub parse_warnings: Vec, + pub compile_errors: Vec, } impl<'a> StateWorkingSet<'a> { @@ -50,6 +51,7 @@ impl<'a> StateWorkingSet<'a> { search_predecls: true, parse_errors: vec![], parse_warnings: vec![], + compile_errors: vec![], } } diff --git a/crates/nu-protocol/src/errors/parse_warning.rs b/crates/nu-protocol/src/errors/parse_warning.rs index e8c3736e5f..a30bc731d8 100644 --- a/crates/nu-protocol/src/errors/parse_warning.rs +++ b/crates/nu-protocol/src/errors/parse_warning.rs @@ -1,4 +1,4 @@ -use crate::{CompileError, Span}; +use crate::Span; use miette::Diagnostic; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -14,27 +14,12 @@ pub enum ParseWarning { span: Span, url: 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("IR compile error")] - IrCompileError { - #[label("failed to compile this code to IR instructions")] - span: Span, - #[related] - errors: Vec, - }, } impl ParseWarning { pub fn span(&self) -> Span { match self { ParseWarning::DeprecatedWarning { span, .. } => *span, - ParseWarning::IrCompileError { span, .. } => *span, } } } diff --git a/crates/nu-protocol/src/errors/shell_error.rs b/crates/nu-protocol/src/errors/shell_error.rs index cf93b16147..192ecb98e8 100644 --- a/crates/nu-protocol/src/errors/shell_error.rs +++ b/crates/nu-protocol/src/errors/shell_error.rs @@ -4,8 +4,8 @@ use std::io; use thiserror::Error; use crate::{ - ast::Operator, engine::StateWorkingSet, format_error, CompileError, LabeledError, ParseError, - Span, Spanned, Value, + ast::Operator, engine::StateWorkingSet, format_error, LabeledError, ParseError, Span, Spanned, + Value, }; /// The fundamental error type for the evaluation engine. These cases represent different kinds of errors @@ -1370,21 +1370,6 @@ On Windows, this would be %USERPROFILE%\AppData\Roaming"# )] 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("IR compile error")] - #[diagnostic(code(nu::shell::ir_compile_error))] - IrCompileError { - #[label("failed to compile this code to IR instructions")] - span: Option, - #[related] - errors: Vec, - }, - /// An unexpected error occurred during IR evaluation. /// /// ## Resolution