Skip to content

Commit d5be8ea

Browse files
committed
Minor code improvements to trait implementation auto-selection from calling function
1 parent 5842ac0 commit d5be8ea

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

Diff for: src/resolve/expr/call/mod.rs

+35-33
Original file line numberDiff line numberDiff line change
@@ -85,54 +85,56 @@ pub fn call_callee(
8585
continue;
8686
}
8787

88-
// NOTE: PERFORMANCE: TODO: This could probably be optimized
89-
if let Some(caller) = ctx
88+
let Some(caller) = ctx
9089
.func_ref
9190
.map(|caller_func_ref| ctx.asg.funcs.get(caller_func_ref).unwrap())
92-
{
93-
let from_env = caller.impl_params.params.iter().filter(|(_, param_trait)| {
94-
callee
91+
else {
92+
continue;
93+
};
94+
95+
// NOTE: PERFORMANCE: TODO: This could probably be optimized
96+
let from_env = caller.impl_params.params.iter().filter(|(_, param_trait)| {
97+
callee
98+
.recipe
99+
.resolve_trait(expected_trait)
100+
.map_or(false, |expected_trait| **param_trait == expected_trait)
101+
});
102+
103+
match from_env.exactly_one() {
104+
Ok((param_name, _)) => {
105+
if callee
95106
.recipe
96-
.resolve_trait(expected_trait)
97-
.map_or(false, |expected_trait| **param_trait == expected_trait)
98-
});
99-
100-
match from_env.exactly_one() {
101-
Ok((param_name, _)) => {
102-
if callee
103-
.recipe
104-
.polymorphs
105-
.insert(expected_name.into(), PolyValue::PolyImpl(param_name.into()))
106-
.is_some()
107-
{
108-
return Err(ResolveError::other(
107+
.polymorphs
108+
.insert(expected_name.into(), PolyValue::PolyImpl(param_name.into()))
109+
.is_some()
110+
{
111+
return Err(ResolveError::other(
109112
format!(
110113
"Could not automatically supply trait implementation for '${} {}' required by function call, since the polymorph is already in use",
111114
expected_name,
112115
expected_trait.display(&ctx.asg),
113116
),
114117
source,
115118
));
116-
}
117119
}
118-
Err(mut non_unique) => {
119-
return Err(ResolveError::other(
120-
if non_unique.next().is_some() {
121-
format!(
120+
}
121+
Err(mut non_unique) => {
122+
return Err(ResolveError::other(
123+
if non_unique.next().is_some() {
124+
format!(
122125
"Ambiguous trait implementation for '${} {}' required by function call, please specify manually",
123126
expected_name,
124127
expected_trait.display(&ctx.asg),
125128
)
126-
} else {
127-
format!(
128-
"Missing '${} {}' trait implementation required by function call",
129-
expected_name,
130-
expected_trait.display(&ctx.asg),
131-
)
132-
},
133-
source,
134-
));
135-
}
129+
} else {
130+
format!(
131+
"Missing '${} {}' trait implementation required by function call",
132+
expected_name,
133+
expected_trait.display(&ctx.asg),
134+
)
135+
},
136+
source,
137+
));
136138
}
137139
}
138140
}

0 commit comments

Comments
 (0)