Improve type display in error messages.

This commit is contained in:
Stephen Chung 2023-05-13 10:47:39 +08:00
parent b1fbfcbc07
commit db9980ce3a
6 changed files with 47 additions and 11 deletions

View File

@ -9,6 +9,7 @@ Enhancements
* Expressions involving `this` should now run slightly faster due to a dedicated `AST` node `ThisPtr`. * Expressions involving `this` should now run slightly faster due to a dedicated `AST` node `ThisPtr`.
* A `take` function is added to the standard library to take ownership of any data (replacing with `()`) in order to avoid cloning. * A `take` function is added to the standard library to take ownership of any data (replacing with `()`) in order to avoid cloning.
* `EvalAltResult::ErrorMismatchOutputType` now gives a better name for the requested generic type (e.g. `&str` is now `&str` and not `string`).
Version 1.14.0 Version 1.14.0

View File

@ -190,8 +190,11 @@ impl Engine {
let typ = self.map_type_name(result.type_name()); let typ = self.map_type_name(result.type_name());
result.try_cast().ok_or_else(|| { result.try_cast().ok_or_else(|| {
let t = self.map_type_name(type_name::<T>()).into(); let typename = match type_name::<T>() {
ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into() typ @ _ if typ.contains("::") => self.map_type_name(typ),
typ @ _ => typ,
};
ERR::ErrorMismatchOutputType(typename.into(), typ.into(), Position::NONE).into()
}) })
}) })
} }

View File

@ -213,8 +213,13 @@ impl Engine {
} }
result.try_cast_raw::<T>().map_err(|v| { result.try_cast_raw::<T>().map_err(|v| {
let typename = match type_name::<T>() {
typ @ _ if typ.contains("::") => self.map_type_name(typ),
typ @ _ => typ,
};
ERR::ErrorMismatchOutputType( ERR::ErrorMismatchOutputType(
self.map_type_name(type_name::<T>()).into(), typename.into(),
self.map_type_name(v.type_name()).into(), self.map_type_name(v.type_name()).into(),
Position::NONE, Position::NONE,
) )

View File

@ -321,8 +321,11 @@ impl<'a> NativeCallContext<'a> {
let typ = self.engine().map_type_name(result.type_name()); let typ = self.engine().map_type_name(result.type_name());
result.try_cast().ok_or_else(|| { result.try_cast().ok_or_else(|| {
let t = self.engine().map_type_name(type_name::<T>()).into(); let typename = match type_name::<T>() {
ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into() typ @ _ if typ.contains("::") => self.engine.map_type_name(typ),
typ @ _ => typ,
};
ERR::ErrorMismatchOutputType(typename.into(), typ.into(), Position::NONE).into()
}) })
}) })
} }
@ -352,8 +355,11 @@ impl<'a> NativeCallContext<'a> {
let typ = self.engine().map_type_name(result.type_name()); let typ = self.engine().map_type_name(result.type_name());
result.try_cast().ok_or_else(|| { result.try_cast().ok_or_else(|| {
let t = self.engine().map_type_name(type_name::<T>()).into(); let typename = match type_name::<T>() {
ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into() typ @ _ if typ.contains("::") => self.engine.map_type_name(typ),
typ @ _ => typ,
};
ERR::ErrorMismatchOutputType(typename.into(), typ.into(), Position::NONE).into()
}) })
}) })
} }

View File

@ -226,8 +226,11 @@ impl FnPtr {
let typ = engine.map_type_name(result.type_name()); let typ = engine.map_type_name(result.type_name());
result.try_cast().ok_or_else(|| { result.try_cast().ok_or_else(|| {
let t = engine.map_type_name(type_name::<T>()).into(); let typename = match type_name::<T>() {
ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into() typ @ _ if typ.contains("::") => engine.map_type_name(typ),
typ @ _ => typ,
};
ERR::ErrorMismatchOutputType(typename.into(), typ.into(), Position::NONE).into()
}) })
}) })
} }
@ -255,8 +258,11 @@ impl FnPtr {
let typ = context.engine().map_type_name(result.type_name()); let typ = context.engine().map_type_name(result.type_name());
result.try_cast().ok_or_else(|| { result.try_cast().ok_or_else(|| {
let t = context.engine().map_type_name(type_name::<T>()).into(); let typename = match type_name::<T>() {
ERR::ErrorMismatchOutputType(t, typ.into(), Position::NONE).into() typ @ _ if typ.contains("::") => context.engine().map_type_name(typ),
typ @ _ => typ,
};
ERR::ErrorMismatchOutputType(typename.into(), typ.into(), Position::NONE).into()
}) })
}) })
} }

View File

@ -10,6 +10,21 @@ fn test_mismatched_op() {
)); ));
} }
#[test]
fn test_mismatched_op_name() {
let engine = Engine::new();
assert!(matches!(
*engine.eval::<String>("true").expect_err("expects error"),
EvalAltResult::ErrorMismatchOutputType(need, actual, ..) if need == "string" && actual == "bool"
));
assert!(matches!(
*engine.eval::<&str>("true").expect_err("expects error"),
EvalAltResult::ErrorMismatchOutputType(need, actual, ..) if need == "&str" && actual == "bool"
));
}
#[test] #[test]
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
fn test_mismatched_op_custom_type() -> Result<(), Box<EvalAltResult>> { fn test_mismatched_op_custom_type() -> Result<(), Box<EvalAltResult>> {