Skip to content

Commit

Permalink
fix: move error message generation to compiler_base_error
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe committed Oct 16, 2023
1 parent 39a2c4d commit 3ab1bc4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 28 deletions.
1 change: 1 addition & 0 deletions compiler_base/error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod emitter;
mod tests;

pub mod errors;
pub mod unit_type;

pub use diagnostic::{
components, diagnostic_handler, style::DiagnosticStyle, Component, Diagnostic, StyledBuffer,
Expand Down
31 changes: 31 additions & 0 deletions compiler_base/error/src/unit_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! This file provides some of the self-encapsulated types used in handling error messages.

/// [`TyeWithUnit`] is a trait for types that can be converted into a string with a unit.
pub trait TypeWithUnit {
fn into_string_with_unit(self) -> String;
}

/// [`UnitUsize`] is a [`usize`] type that can be converted into a string with a unit.
pub struct UnitUsize(pub usize, pub String);

impl TypeWithUnit for UnitUsize {
/// [`into_string_with_unit`] converts [`UnitUsize`] into a string with a unit.
///
/// # Examples
///
/// ```
/// use compiler_base_error::unit_type::{TypeWithUnit, UnitUsize};
///
/// let unit_usize = UnitUsize(1, "byte".to_string());
/// assert_eq!(unit_usize.into_string_with_unit(), "1 byte");
/// let unit_usize = UnitUsize(2, "byte".to_string());
/// assert_eq!(unit_usize.into_string_with_unit(), "2 bytes");
/// ```
fn into_string_with_unit(self) -> String {
if self.0 > 1 {
format!("{} {}s", self.0, self.1)
} else {
format!("{} {}", self.0, self.1)
}
}
}
12 changes: 5 additions & 7 deletions kclvm/Cargo.lock

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

2 changes: 1 addition & 1 deletion kclvm/sema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ kclvm-span = {path = "../span"}
compiler_base_span = {path = "../../compiler_base/span", version = "0.0.2"}
compiler_base_session = {path = "../../compiler_base/session"}
compiler_base_macros = "0.0.1"
compiler_base_error = "0.0.8"
compiler_base_error = {path = "../../compiler_base/error"}
suggestions = "0.1.1"

[dev-dependencies]
Expand Down
34 changes: 14 additions & 20 deletions kclvm/sema/src/resolver/arg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::resolver::Resolver;
use crate::ty::{FunctionType, Type};
use compiler_base_error::unit_type::{TypeWithUnit, UnitUsize};
use indexmap::IndexSet;
use kclvm_ast::ast;
use std::rc::Rc;
Expand Down Expand Up @@ -66,13 +67,13 @@ impl<'ctx> Resolver<'ctx> {
}
got_count += args.len();
if got_count < expect_count {
let expect_err_msg = if expect_count > 1 {
format!("expected {} positional arguments", expect_count)
} else {
format!("expected {} positional argument", expect_count)
};
self.handler.add_compile_error(
&format!("{}, found {}", expect_err_msg, got_count),
&format!(
"expected {}, found {}",
UnitUsize(expect_count, "positional argument".to_string())
.into_string_with_unit(),
got_count
),
func.get_span_pos(),
);
}
Expand All @@ -83,21 +84,14 @@ impl<'ctx> Resolver<'ctx> {
Some(param) => param.ty.clone(),
None => {
if !func_ty.is_variadic {
let expect_err_msg = if func_ty.params.len() > 1 {
format!(
"{} takes {} positional arguments",
func_name,
func_ty.params.len()
)
} else {
format!(
"{} takes {} positional argument",
func_name,
func_ty.params.len()
)
};
self.handler.add_compile_error(
&format!("{} but {} were given", expect_err_msg, args.len(),),
&format!(
"{} takes {} but {} were given",
func_name,
UnitUsize(func_ty.params.len(), "positional argument".to_string())
.into_string_with_unit(),
args.len(),
),
args[i].get_span_pos(),
);
}
Expand Down

0 comments on commit 3ab1bc4

Please sign in to comment.