Skip to content

Commit

Permalink
clippy: Reduce the size of the Error(s) enums
Browse files Browse the repository at this point in the history
  • Loading branch information
CohenArthur committed Nov 13, 2024
1 parent 526f964 commit 5ce2d74
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
11 changes: 8 additions & 3 deletions error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ impl ErrKind {
pub struct Error {
kind: ErrKind,
msg: Option<String>,
loc: Option<SpanTuple>,
// We box the `SpanTuple` here to reduce the size of the `Error` enum.
// This makes clippy happy.
loc: Option<Box<SpanTuple>>,
hints: Vec<Error>,
}

Expand Down Expand Up @@ -265,12 +267,15 @@ impl Error {
// FIXME: Should this really take an Option<Location>?
#[deprecated]
pub fn with_opt_loc(self, loc: Option<SpanTuple>) -> Error {
Error { loc, ..self }
Error {
loc: loc.map(Box::new),
..self
}
}

pub fn with_loc(self, loc: SpanTuple) -> Error {
Error {
loc: Some(loc),
loc: Some(Box::new(loc)),
..self
}
}
Expand Down
10 changes: 5 additions & 5 deletions include_code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn get_final_path(
base: &Path,
location: &SpanTuple,
path: &Path,
) -> Result<PathBuf, (Error, Error)> {
) -> Result<PathBuf, Box<(Error, Error)>> {
// Check the local path first
let local_err = match load_local_library(base, location, path) {
Ok(path) => return Ok(path),
Expand All @@ -86,7 +86,7 @@ pub fn get_final_path(
Err(e) => e,
};

Err((local_err, home_err))
Err(Box::new((local_err, home_err)))
}

fn get_base(location: &SpanTuple) -> PathBuf {
Expand Down Expand Up @@ -123,9 +123,9 @@ impl Visitor for IncludeCtx {

let final_path = match get_final_path(&base, &location, &to_include) {
Ok(path) => path,
Err((e1, e2)) => {
e1.emit();
e2.emit();
Err(errs) => {
errs.0.emit();
errs.1.emit();
return Err(Error::new(ErrKind::Include));
}
};
Expand Down
8 changes: 6 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ impl ErrKind {
pub struct Error {
kind: ErrKind,
msg: Option<String>,
loc: Option<SpanTuple>,
// Boxing to reduce the error's size to make clippy happy. This will eventually be removed anyway.
loc: Option<Box<SpanTuple>>,
hints: Vec<Error>,
}

Expand Down Expand Up @@ -224,7 +225,10 @@ impl Error {

// FIXME: Should this really take an Option<Location>?
pub fn with_loc(self, loc: Option<SpanTuple>) -> Error {
Error { loc, ..self }
Error {
loc: loc.map(Box::new),
..self
}
}

// Add a hint to emit alongside the error
Expand Down
10 changes: 5 additions & 5 deletions src/instruction/incl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl Incl {
self.check_base(base)
}

pub fn get_final_path(&self, base: &Path) -> Result<PathBuf, (Error, Error)> {
pub fn get_final_path(&self, base: &Path) -> Result<PathBuf, Box<(Error, Error)>> {
// Check the local path first
let local_err = match self.load_local_library(base) {
Ok(path) => return Ok(path),
Expand All @@ -130,7 +130,7 @@ impl Incl {
Err(e) => e,
};

Err((local_err, home_err))
Err(Box::new((local_err, home_err)))
}
}

Expand Down Expand Up @@ -188,9 +188,9 @@ impl TypeCheck for Incl {

let final_path = match self.get_final_path(&base) {
Ok(path) => path,
Err((e1, e2)) => {
ctx.error(e1);
ctx.error(e2);
Err(errs) => {
ctx.error(errs.0);
ctx.error(errs.1);
return Err(Error::new(ErrKind::TypeChecker));
}
};
Expand Down

0 comments on commit 5ce2d74

Please sign in to comment.