move IR compile errors into own section in StateWorkingSet

This commit is contained in:
Devyn Cairns 2024-07-09 18:00:46 -07:00
parent 55a4772f4c
commit 85922fe50f
7 changed files with 32 additions and 58 deletions

View File

@ -70,6 +70,11 @@ pub fn evaluate_commands(
std::process::exit(1); 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()) (output, working_set.render())
}; };

View File

@ -265,6 +265,11 @@ fn evaluate_source(
return Ok(Some(1)); 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()) (output, working_set.render())
}; };

View File

@ -1,6 +1,4 @@
use std::borrow::Cow; use nu_engine::command_prelude::*;
use nu_engine::{command_prelude::*, compile};
use nu_protocol::engine::Closure; use nu_protocol::engine::Closure;
#[derive(Clone)] #[derive(Clone)]
@ -41,17 +39,16 @@ impl Command for ViewIr {
let json = call.has_flag(engine_state, stack, "json")?; let json = call.has_flag(engine_state, stack, "json")?;
let block = engine_state.get_block(closure.block_id); let block = engine_state.get_block(closure.block_id);
// Use the pre-compiled block if available, otherwise try to compile it let ir_block = block
// This helps display the actual compilation error .ir_block
let ir_block = match &block.ir_block { .as_ref()
Some(ir_block) => Cow::Borrowed(ir_block), .ok_or_else(|| ShellError::GenericError {
None => Cow::Owned(compile(&StateWorkingSet::new(engine_state), block).map_err( error: "Can't view IR for this block".into(),
|err| ShellError::IrCompileError { msg: "block is missing compiled representation".into(),
span: block.span, span: block.span,
errors: vec![err], help: Some("the IrBlock is probably missing due to a compilation error".into()),
}, inner: vec![],
)?), })?;
};
let formatted = if json { let formatted = if json {
let formatted_instructions = ir_block let formatted_instructions = ir_block

View File

@ -12,8 +12,8 @@ use log::trace;
use nu_engine::DIR_VAR_PARSER_INFO; use nu_engine::DIR_VAR_PARSER_INFO;
use nu_protocol::{ use nu_protocol::{
ast::*, engine::StateWorkingSet, eval_const::eval_constant, BlockId, DidYouMean, Flag, ast::*, engine::StateWorkingSet, eval_const::eval_constant, BlockId, DidYouMean, Flag,
ParseError, ParseWarning, PositionalArg, Signature, Span, Spanned, SyntaxShape, Type, VarId, ParseError, PositionalArg, Signature, Span, Spanned, SyntaxShape, Type, VarId, ENV_VARIABLE_ID,
ENV_VARIABLE_ID, IN_VARIABLE_ID, IN_VARIABLE_ID,
}; };
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
@ -5847,18 +5847,13 @@ pub fn parse_block(
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) { fn compile_block(working_set: &mut StateWorkingSet<'_>, block: &mut Block) {
match nu_engine::compile(working_set, block) { match nu_engine::compile(working_set, block) {
Ok(ir_block) => { Ok(ir_block) => {
block.ir_block = Some(ir_block); block.ir_block = Some(ir_block);
} }
Err(err) => working_set Err(err) => working_set.compile_errors.push(err),
.parse_warnings
.push(ParseWarning::IrCompileError {
span: block.span.unwrap_or(Span::unknown()),
errors: vec![err],
}),
} }
} }

View File

@ -4,8 +4,8 @@ use crate::{
usage::build_usage, CachedFile, Command, CommandType, EngineState, OverlayFrame, usage::build_usage, CachedFile, Command, CommandType, EngineState, OverlayFrame,
StateDelta, Variable, VirtualPath, Visibility, StateDelta, Variable, VirtualPath, Visibility,
}, },
BlockId, Category, Config, DeclId, FileId, GetSpan, Module, ModuleId, ParseError, ParseWarning, BlockId, Category, CompileError, Config, DeclId, FileId, GetSpan, Module, ModuleId, ParseError,
Span, SpanId, Type, Value, VarId, VirtualPathId, ParseWarning, Span, SpanId, Type, Value, VarId, VirtualPathId,
}; };
use core::panic; use core::panic;
use std::{ use std::{
@ -31,6 +31,7 @@ pub struct StateWorkingSet<'a> {
pub search_predecls: bool, pub search_predecls: bool,
pub parse_errors: Vec<ParseError>, pub parse_errors: Vec<ParseError>,
pub parse_warnings: Vec<ParseWarning>, pub parse_warnings: Vec<ParseWarning>,
pub compile_errors: Vec<CompileError>,
} }
impl<'a> StateWorkingSet<'a> { impl<'a> StateWorkingSet<'a> {
@ -50,6 +51,7 @@ impl<'a> StateWorkingSet<'a> {
search_predecls: true, search_predecls: true,
parse_errors: vec![], parse_errors: vec![],
parse_warnings: vec![], parse_warnings: vec![],
compile_errors: vec![],
} }
} }

View File

@ -1,4 +1,4 @@
use crate::{CompileError, Span}; use crate::Span;
use miette::Diagnostic; use miette::Diagnostic;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use thiserror::Error; use thiserror::Error;
@ -14,27 +14,12 @@ pub enum ParseWarning {
span: Span, span: Span,
url: String, 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<CompileError>,
},
} }
impl ParseWarning { impl ParseWarning {
pub fn span(&self) -> Span { pub fn span(&self) -> Span {
match self { match self {
ParseWarning::DeprecatedWarning { span, .. } => *span, ParseWarning::DeprecatedWarning { span, .. } => *span,
ParseWarning::IrCompileError { span, .. } => *span,
} }
} }
} }

View File

@ -4,8 +4,8 @@ use std::io;
use thiserror::Error; use thiserror::Error;
use crate::{ use crate::{
ast::Operator, engine::StateWorkingSet, format_error, CompileError, LabeledError, ParseError, ast::Operator, engine::StateWorkingSet, format_error, LabeledError, ParseError, Span, Spanned,
Span, Spanned, Value, Value,
}; };
/// The fundamental error type for the evaluation engine. These cases represent different kinds of errors /// 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 }, 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<Span>,
#[related]
errors: Vec<CompileError>,
},
/// An unexpected error occurred during IR evaluation. /// An unexpected error occurred during IR evaluation.
/// ///
/// ## Resolution /// ## Resolution