Skip to content

Commit 137331f

Browse files
committed
Adjust TypeCheckContext::find_method_for_tyoe
1 parent 5803c18 commit 137331f

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

sway-core/src/semantic_analysis/type_check_context.rs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
engine_threading::*,
88
language::{
99
parsed::{MethodName, TreeType},
10-
ty::{self, TyDecl, TyExpression},
10+
ty::{self, TyDecl, TyExpression, TyFunctionDisplay},
1111
CallPath, QualifiedCallPath, Visibility,
1212
},
1313
monomorphization::{monomorphize_with_modpath, MonomorphizeHelper},
@@ -23,6 +23,7 @@ use crate::{
2323
EnforceTypeArguments, SubstTypesContext, TraitConstraint, TypeParameter, TypeSubstMap,
2424
UnifyCheck,
2525
};
26+
use itertools::Itertools;
2627
use sway_error::{
2728
error::CompileError,
2829
handler::{ErrorEmitted, Handler},
@@ -915,10 +916,10 @@ impl<'a> TypeCheckContext<'a> {
915916
// While collecting unifications we don't decay numeric and will ignore this error.
916917
if self.collecting_unifications {
917918
return Err(handler.emit_err(CompileError::MethodNotFound {
918-
method: method_name.clone().as_str().to_string(),
919+
called_method: method_name.into(),
920+
expected_signature: method_name.clone().as_str().to_string(),
919921
type_name: self.engines.help_out(type_id).to_string(),
920-
matching_method_strings: vec![],
921-
span: method_name.span(),
922+
matching_methods: vec![],
922923
}));
923924
}
924925
type_engine.decay_numeric(handler, self.engines, type_id, &method_name.span())?;
@@ -1515,7 +1516,7 @@ impl<'a> TypeCheckContext<'a> {
15151516
continue;
15161517
}
15171518

1518-
let key: GroupingKey = (impl_trait.decl_id, method.implementing_for_typeid);
1519+
let key: GroupingKey = (impl_trait.decl_id, method.implementing_for);
15191520

15201521
// Prefer the method that is type-check finalized when conflicting.
15211522
match trait_methods.get_mut(&key) {
@@ -1603,7 +1604,7 @@ impl<'a> TypeCheckContext<'a> {
16031604
let mut exact = vec![];
16041605
for r in trait_methods.values() {
16051606
let m = decl_engine.get_function(r);
1606-
if let Some(impl_for) = m.implementing_for_typeid {
1607+
if let Some(impl_for) = m.implementing_for {
16071608
if eq_check.with_unify_ref_mut(false).check(impl_for, type_id) {
16081609
exact.push(r.clone());
16091610
}
@@ -1647,41 +1648,36 @@ impl<'a> TypeCheckContext<'a> {
16471648
}
16481649

16491650
#[inline]
1650-
fn format_candidate_summaries_for_error(&self, decl_refs: &[DeclRefFunction]) -> Vec<String> {
1651-
let de = self.engines.de();
1651+
fn format_candidate_summaries_for_error(engines: &Engines, decl_refs: &[DeclRefFunction]) -> Vec<String> {
1652+
let de = engines.de();
1653+
let fn_display = TyFunctionDisplay::full().without_self_param_type();
16521654

1653-
let mut out: Vec<String> = decl_refs
1655+
let mut out = decl_refs
16541656
.iter()
16551657
.map(|r| {
16561658
let m = de.get_function(r);
1657-
let params = m
1658-
.parameters
1659-
.iter()
1660-
.map(|p| self.engines.help_out(p.type_argument.type_id()).to_string())
1661-
.collect::<Vec<_>>()
1662-
.join(", ");
1663-
let ret = self.engines.help_out(m.return_type.type_id());
1664-
let in_impl = if let Some(for_ty) = m.implementing_for_typeid {
1665-
format!(" in {}", self.engines.help_out(for_ty))
1666-
} else {
1667-
String::new()
1668-
};
1669-
format!("{}({}) -> {}{}", m.name.as_str(), params, ret, in_impl)
1659+
fn_display.display(&m, engines)
16701660
})
1671-
.collect();
1661+
.collect_vec();
16721662

16731663
out.sort();
16741664
out
16751665
}
16761666

16771667
/// Given a `method_name` and a `type_id`, find that method on that type in the namespace.
1678-
/// `annotation_type` is the expected method return type. Requires `argument_types` because:
1668+
///
1669+
/// `annotation_type` is the expected method return type.
1670+
///
1671+
/// Requires `argument_types` because:
16791672
/// - standard operations like +, <=, etc. are called like "std::ops::<operation>" and the
16801673
/// actual self type of the trait implementation is determined by the passed argument type.
16811674
/// - we can have several implementations of generic traits for different types, that can
16821675
/// result in a method of a same name, but with different type arguments.
16831676
///
16841677
/// This function will emit a [CompileError::MethodNotFound] if the method is not found.
1678+
///
1679+
/// Note that _method_ here means **any function associated to a type**, with or without
1680+
/// the `self` argument.
16851681
#[allow(clippy::too_many_arguments)]
16861682
pub(crate) fn find_method_for_type(
16871683
&self,
@@ -1714,7 +1710,7 @@ impl<'a> TypeCheckContext<'a> {
17141710
annotation_type,
17151711
);
17161712

1717-
let mut matching_method_strings = HashSet::<String>::new();
1713+
let mut matching_methods = Vec::<String>::new();
17181714

17191715
let mut qualified_call_path: Option<QualifiedCallPath> = None;
17201716

@@ -1758,8 +1754,8 @@ impl<'a> TypeCheckContext<'a> {
17581754
}
17591755
} else {
17601756
// No signature-compatible candidates.
1761-
matching_method_strings
1762-
.extend(self.format_candidate_summaries_for_error(&matching_method_decl_refs));
1757+
matching_methods
1758+
.append(&mut Self::format_candidate_summaries_for_error(self.engines, &matching_method_decl_refs));
17631759
}
17641760

17651761
// Forward an ErrorRecovery from the first argument if present.
@@ -1782,7 +1778,8 @@ impl<'a> TypeCheckContext<'a> {
17821778

17831779
// Final: MethodNotFound with formatted signature and candidates.
17841780
Err(handler.emit_err(CompileError::MethodNotFound {
1785-
method: format!(
1781+
called_method: method_ident.into(),
1782+
expected_signature: format!(
17861783
"{}({}){}",
17871784
method_ident.clone(),
17881785
arguments_types
@@ -1800,8 +1797,7 @@ impl<'a> TypeCheckContext<'a> {
18001797
}
18011798
),
18021799
type_name,
1803-
matching_method_strings: matching_method_strings.iter().cloned().collect::<Vec<_>>(),
1804-
span: method_ident.span(),
1800+
matching_methods,
18051801
}))
18061802
}
18071803

0 commit comments

Comments
 (0)