From f814d0ca55f9bcca87dc400e928b5ddde9f2ac80 Mon Sep 17 00:00:00 2001 From: IsaacShelton Date: Sun, 26 Jan 2025 02:04:16 -0600 Subject: [PATCH] Added error test case for #[using] on trait functions, and made minor code improvements --- src/resolve/expr/call/infer_impl.rs | 35 +++++++++++-------- src/resolve/polymorph/matcher.rs | 6 ++-- .../using_trait_on_trait_func/main.adept | 16 +++++++++ 3 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 tests/error/using_trait_on_trait_func/main.adept diff --git a/src/resolve/expr/call/infer_impl.rs b/src/resolve/expr/call/infer_impl.rs index 8d70a94..274758e 100644 --- a/src/resolve/expr/call/infer_impl.rs +++ b/src/resolve/expr/call/infer_impl.rs @@ -25,24 +25,29 @@ pub fn infer_callee_missing_impl_args( continue; }; - let from_env = caller.impl_params.params.iter().filter(|(_, param_trait)| { - if catalog - .extend_if_match_all_types(ctx, &expected_trait.args, ¶m_trait.args) - .is_err() - { - return false; - } + let from_env = caller + .impl_params + .params + .iter() + .filter_map(|(param_name, param_trait)| { + if catalog + .extend_if_match_all_types(ctx, &expected_trait.args, ¶m_trait.args) + .is_err() + { + return None; + } - catalog - .resolver() - .resolve_trait(expected_trait) - .map_or(false, |expected_trait| { - param_trait.trait_ref == expected_trait.trait_ref - }) - }); + catalog + .resolver() + .resolve_trait(expected_trait) + .ok() + .and_then(|expected_trait| { + (param_trait.trait_ref == expected_trait.trait_ref).then_some(param_name) + }) + }); match from_env.exactly_one() { - Ok((param_name, _)) => { + Ok(param_name) => { if catalog .polymorphs .insert(expected_name.into(), PolyValue::PolyImpl(param_name.into())) diff --git a/src/resolve/polymorph/matcher.rs b/src/resolve/polymorph/matcher.rs index 45ec929..400af10 100644 --- a/src/resolve/polymorph/matcher.rs +++ b/src/resolve/polymorph/matcher.rs @@ -162,8 +162,10 @@ impl<'a, 'b, 'c> TypeMatcher<'a, 'b, 'c> { } } - self.partial - .insert(name.to_string(), PolyValue::Type(new_type.clone())); + assert!(self + .partial + .insert(name.to_string(), PolyValue::Type(new_type.clone())) + .is_none()); Ok(()) } diff --git a/tests/error/using_trait_on_trait_func/main.adept b/tests/error/using_trait_on_trait_func/main.adept new file mode 100644 index 0000000..3f2b9d6 --- /dev/null +++ b/tests/error/using_trait_on_trait_func/main.adept @@ -0,0 +1,16 @@ + +pragma => adept(c"3.0") + +trait Example<$T> { + #[using Speak<$T>] + func invalidFunction(self $T) void +} + +trait Speak<$T> { + func speak(self $T) void +} + +func main { + +} +