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);
}
if let Some(err) = working_set.compile_errors.first() {
report_error(&working_set, err);
// Not a fatal error, for now
}
(output, working_set.render())
};

View File

@ -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())
};

View File

@ -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 {
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,
errors: vec![err],
},
)?),
};
help: Some("the IrBlock is probably missing due to a compilation error".into()),
inner: vec![],
})?;
let formatted = if json {
let formatted_instructions = ir_block

View File

@ -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),
}
}

View File

@ -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<ParseError>,
pub parse_warnings: Vec<ParseWarning>,
pub compile_errors: Vec<CompileError>,
}
impl<'a> StateWorkingSet<'a> {
@ -50,6 +51,7 @@ impl<'a> StateWorkingSet<'a> {
search_predecls: true,
parse_errors: vec![],
parse_warnings: vec![],
compile_errors: vec![],
}
}

View File

@ -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<CompileError>,
},
}
impl ParseWarning {
pub fn span(&self) -> Span {
match self {
ParseWarning::DeprecatedWarning { span, .. } => *span,
ParseWarning::IrCompileError { span, .. } => *span,
}
}
}

View File

@ -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<Span>,
#[related]
errors: Vec<CompileError>,
},
/// An unexpected error occurred during IR evaluation.
///
/// ## Resolution