Skip to content

Commit

Permalink
refactor: migrate syntax errors to diagnostic repository (#30)
Browse files Browse the repository at this point in the history
Starts migrating error definitions to eight-diagnostic
  • Loading branch information
junlarsen authored Feb 4, 2025
1 parent 5138fd8 commit 3ce825f
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 50 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions compiler/eight-diagnostics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ serde = ["dep:serde"]

[dependencies]
eight-macros = { path = "../eight-macros" }
eight-span = { path = "../eight-span" }

miette = { workspace = true }
serde = { workspace = true, optional = true }
thiserror = { workspace = true }
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ declare_error_type! {
}
}

/// Handy type alias for all HIR-related errors.
pub type HirResult<T> = Result<T, HirError>;

#[derive(Error, Diagnostic, Debug)]
#[diagnostic(code(sema::unknown_type))]
#[error("{name} does not name a known type")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@ use eight_macros::declare_error_type;

declare_error_type! {
#[error("mir lowering error: {0}")]
pub enum MirError {
}
pub enum MirError {}
}

pub type MirResult<T> = Result<T, MirError>;
3 changes: 3 additions & 0 deletions compiler/eight-diagnostics/src/errors/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod hir;
pub mod mir;
pub mod syntax;
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
//! of future lexer-related syntax errors. These will only be triggered by the parser upon
//! attempting to consume further tokens.
use crate::tok::Token;
use eight_macros::declare_error_type;
use eight_span::Span;
use miette::Diagnostic;
Expand All @@ -25,9 +24,6 @@ declare_error_type! {
}
}

/// Handy type alias for all parsing-related errors.
pub type ParseResult<T> = Result<T, ParseError>;

/// Signals that the parser has reached the end of the input stream.
///
/// This error is only emitted to the diagnostic engine when the parser produces it. See the note
Expand Down Expand Up @@ -86,7 +82,7 @@ pub struct UnexpectedCharacterError {
#[diagnostic(code(syntax::unexpected_token))]
#[error("found unexpected token during parsing")]
pub struct UnexpectedTokenError {
pub token: Token,
pub token: String,
#[label("was not expecting to find '{token}' in this position")]
pub span: Span,
}
2 changes: 2 additions & 0 deletions compiler/eight-diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod errors;

#[macro_export]
macro_rules! ice {
($($arg:tt)*) => {{
Expand Down
6 changes: 3 additions & 3 deletions compiler/eight-driver/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use crate::operations::syntax_lower::SyntaxLowerOperation;
use crate::operations::type_check::TypeCheckOperation;
use crate::query::EmitQuery;
use eight_codegen_llvm::error::LLVMBackendError;
use eight_diagnostics::errors::hir::HirError;
use eight_diagnostics::errors::mir::MirError;
use eight_diagnostics::errors::syntax::ParseError;
use eight_diagnostics::ice;
use eight_middle::context::CompileContext;
use eight_middle::hir_error::HirError;
use eight_middle::mir_error::MirError;
use eight_syntax::arena::AstArena;
use eight_syntax::error::ParseError;
use miette::Diagnostic;
use std::mem::ManuallyDrop;
use thiserror::Error;
Expand Down
7 changes: 4 additions & 3 deletions compiler/eight-middle/src/ast_lowering_pass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use crate::hir::{
HirTraitSignature, HirType, HirTypeParameterSignature, HirTypeSignature,
};
use crate::hir_builder::HirBuilder;
use crate::hir_error::{
BreakOutsideLoopError, ContinueOutsideLoopError, HirError, HirResult, UnknownIntrinsicTypeError,
};
use crate::scope::Scope;
use crate::HirResult;
use crate::LinkageType;
use eight_diagnostics::errors::hir::{
BreakOutsideLoopError, ContinueOutsideLoopError, HirError, UnknownIntrinsicTypeError,
};
use eight_diagnostics::ice;
use eight_span::Span;
use eight_syntax::ast::{
Expand Down
7 changes: 3 additions & 4 deletions compiler/eight-middle/src/hir_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ use crate::hir::{
HirOffsetIndexExpr, HirReferenceExpr, HirReturnStmt, HirStmt, HirTrait, HirTraitSignature,
};
use crate::hir::{HirCallableReferenceExpr, HirCallableSymbol, HirTy};
use crate::hir_error::HirError;
use crate::LinkageType;
use crate::{HirResult, LinkageType};
use eight_span::Span;
use std::collections::BTreeMap;

Expand All @@ -36,8 +35,8 @@ impl<'hir> HirBuilder {
/// collect something into a vector.
pub fn build_vec<A, B>(
iter: impl IntoIterator<Item = A>,
f: impl FnMut(A) -> Result<B, HirError>,
) -> Result<Vec<B>, HirError> {
f: impl FnMut(A) -> HirResult<B>,
) -> HirResult<Vec<B>> {
iter.into_iter().map(f).collect()
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/eight-middle/src/hir_lowering_pass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use crate::mir::MirModule;
use crate::mir::MirTy;
use crate::mir::MirValueId;
use crate::mir_builder::{MirFunctionBuilder, MirModuleContext};
use crate::mir_error::MirResult;
use crate::scope::Scope;
use crate::LinkageType;
use crate::MirResult;
use eight_diagnostics::ice;

pub struct MirModuleLoweringPass<'mir> {
Expand Down
5 changes: 3 additions & 2 deletions compiler/eight-middle/src/hir_type_check_pass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use crate::hir::{
HirPointerTy, HirReturnStmt, HirStmt, HirTy,
};
use crate::hir_builder::HirBuilder;
use crate::hir_error::{
HirError, HirResult, InvalidReferenceError, TypeFieldInfiniteRecursionError, UnknownTypeError,
use crate::HirResult;
use eight_diagnostics::errors::hir::{
HirError, InvalidReferenceError, TypeFieldInfiniteRecursionError, UnknownTypeError,
WrongTraitTypeArgumentCount,
};
use eight_diagnostics::ice;
Expand Down
15 changes: 8 additions & 7 deletions compiler/eight-middle/src/hir_type_check_pass/typing_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ use crate::hir::{
HirFunctionTy, HirGroupExpr, HirIntegerLiteralExpr, HirMetaTy, HirModuleSignature,
HirOffsetIndexExpr, HirReferenceExpr, HirTy,
};
use crate::hir_error::{
BindingReDeclaresName, ConstructingNonStructTypeError, ConstructingPointerTypeError,
DereferenceOfNonPointerError, FunctionTypeMismatchError, HirError, HirResult,
InvalidFieldReferenceOfNonStructError, InvalidStructFieldReferenceError, MissingFieldError,
SelfReferentialTypeError, TraitDoesNotExistError, TraitMissingInstanceError, TypeMismatchError,
TypeParameterShadowsExisting, UnknownFieldError, WrongFunctionTypeArgumentCount,
};
use crate::hir_type_check_pass::{
Constraint, DereferenceableConstraint, EqualityConstraint, FieldProjectionConstraint,
InstanceConstraint,
};
use crate::scope::Scope;
use crate::HirResult;
use eight_diagnostics::errors::hir::{
BindingReDeclaresName, ConstructingNonStructTypeError, ConstructingPointerTypeError,
DereferenceOfNonPointerError, FunctionTypeMismatchError, HirError,
InvalidFieldReferenceOfNonStructError, InvalidStructFieldReferenceError, MissingFieldError,
SelfReferentialTypeError, TraitDoesNotExistError, TraitMissingInstanceError, TypeMismatchError,
TypeParameterShadowsExisting, UnknownFieldError, WrongFunctionTypeArgumentCount,
};
use eight_diagnostics::ice;
use eight_span::Span;
use std::collections::{HashMap, HashSet, VecDeque};
Expand Down
8 changes: 6 additions & 2 deletions compiler/eight-middle/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
//! Common types and functions for the middle-end of the compiler.
use eight_diagnostics::errors::hir::HirError;
use eight_diagnostics::errors::mir::MirError;

pub mod ast_lowering_pass;
pub mod builtin;
pub mod context;
pub mod hir;
pub mod hir_builder;
pub mod hir_error;
pub mod hir_lowering_pass;
pub mod hir_textual_pass;
pub mod hir_type_check_pass;
pub mod intern;
pub mod mir;
pub mod mir_builder;
pub mod mir_error;
pub mod mir_textual_pass;
pub mod scope;

pub type HirResult<T> = Result<T, HirError>;
pub type MirResult<T> = Result<T, MirError>;

#[derive(Debug, PartialEq, Eq)]
pub enum LinkageType {
/// The symbol is to be resolved at link-time. Typically used for symbols that are marked as
Expand Down
15 changes: 8 additions & 7 deletions compiler/eight-syntax/src/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::error::{
InvalidIntegerLiteralError, ParseError, ParseResult, UnexpectedCharacterError,
UnexpectedEndOfFileError, UnfinishedTokenError,
};
use crate::tok::{Token, TokenType};
use crate::ParseResult;
use eight_diagnostics::errors::syntax::{
InvalidIntegerLiteralError, ParseError, UnexpectedCharacterError, UnexpectedEndOfFileError,
UnfinishedTokenError,
};
use eight_diagnostics::ice;
use eight_span::{SourcePosition, Span};
use std::iter::Peekable;
Expand Down Expand Up @@ -304,11 +305,11 @@ impl<'a> Lexer<'a> {

#[cfg(test)]
mod tests {
use crate::error::{
InvalidIntegerLiteralError, UnexpectedCharacterError, UnfinishedTokenError,
};
use crate::lexer::{Lexer, ParseError};
use crate::tok::{Token, TokenType};
use eight_diagnostics::errors::syntax::{
InvalidIntegerLiteralError, UnexpectedCharacterError, UnfinishedTokenError,
};
use eight_span::Span;

macro_rules! assert_lexer_parse {
Expand Down
5 changes: 4 additions & 1 deletion compiler/eight-syntax/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use eight_diagnostics::errors::syntax::ParseError;

pub mod arena;
pub mod ast;
pub mod error;
pub mod lexer;
pub mod parser;
pub mod tok;

pub type ParseResult<T> = Result<T, ParseError>;
19 changes: 11 additions & 8 deletions compiler/eight-syntax/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ use crate::ast::{
AstTraitFunctionItem, AstTraitItem, AstTranslationUnit, AstType, AstTypeItem,
AstTypeParameterItem, AstUnaryOp, AstUnaryOpExpr, AstUnitType,
};
use crate::error::{ParseError, ParseResult, UnexpectedEndOfFileError, UnexpectedTokenError};
use crate::lexer::Lexer;
use crate::tok::{Token, TokenType};
use crate::ParseResult;
use eight_diagnostics::errors::syntax::{
ParseError, UnexpectedEndOfFileError, UnexpectedTokenError,
};
use eight_span::Span;

pub struct ParserInput<'a> {
Expand Down Expand Up @@ -127,7 +130,7 @@ impl<'a, 'ast> Parser<'a, 'ast> {
token if token.ty == *ty => Ok(token),
_ => Err(ParseError::UnexpectedToken(UnexpectedTokenError {
span: token.span,
token,
token: token.to_string(),
})),
}
}
Expand Down Expand Up @@ -237,7 +240,7 @@ impl<'ast> Parser<'_, 'ast> {
let token = self.eat()?;
return Err(ParseError::UnexpectedToken(UnexpectedTokenError {
span: token.span,
token,
token: token.to_string(),
}));
}
};
Expand Down Expand Up @@ -561,7 +564,7 @@ impl<'ast> Parser<'_, 'ast> {
TokenType::KeywordIntrinsicFn => Ok(p.parse_intrinsic_fn_item()?),
_ => Err(ParseError::UnexpectedToken(UnexpectedTokenError {
span: token.span,
token: token.clone(),
token: token.to_string(),
})),
}
})?;
Expand Down Expand Up @@ -1233,7 +1236,7 @@ impl<'ast> Parser<'_, 'ast> {
let token = self.eat()?;
Err(ParseError::UnexpectedToken(UnexpectedTokenError {
span: token.span,
token,
token: token.to_string(),
}))
}

Expand All @@ -1254,7 +1257,7 @@ impl<'ast> Parser<'_, 'ast> {
}
_ => Err(ParseError::from(UnexpectedTokenError {
span: token.span,
token,
token: token.to_string(),
})),
}
}
Expand Down Expand Up @@ -1294,7 +1297,7 @@ impl<'ast> Parser<'_, 'ast> {
let token = self.eat()?;
Err(ParseError::from(UnexpectedTokenError {
span: token.span,
token,
token: token.to_string(),
}))
}
}
Expand Down Expand Up @@ -1335,7 +1338,7 @@ mod tests {
use crate::ast::{
AstBinaryOp, AstBreakStmt, AstContinueStmt, AstExpr, AstIdentifier, AstType, AstUnaryOp,
};
use crate::error::{InvalidIntegerLiteralError, ParseError};
use eight_diagnostics::errors::syntax::{InvalidIntegerLiteralError, ParseError};

use eight_macros::{assert_err, assert_matches, assert_none, assert_ok, assert_some};
use eight_span::Span;
Expand Down

0 comments on commit 3ce825f

Please sign in to comment.