Skip to content

Commit 9d97d46

Browse files
committed
Refactor resolve_method_name.
1 parent ace60ce commit 9d97d46

File tree

1 file changed

+38
-82
lines changed

1 file changed

+38
-82
lines changed

sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs

Lines changed: 38 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -906,55 +906,48 @@ pub(crate) fn resolve_method_name(
906906
let type_engine = ctx.engines.te();
907907
let engines = ctx.engines();
908908

909-
// retrieve the function declaration using the components of the method name
910-
let (decl_ref, type_id) = match &method_name_binding.inner {
909+
// Helper: first argument type or an unknown placeholder.
910+
let first_arg_or_unknown = || {
911+
arguments_types
912+
.first()
913+
.cloned()
914+
.unwrap_or_else(|| type_engine.new_unknown())
915+
};
916+
917+
let (type_id, module_path, method_ident) = match &method_name_binding.inner {
911918
MethodName::FromType {
912919
call_path_binding,
913920
method_name,
914921
} => {
915-
// type check the call path
916922
let type_id = call_path_binding
917923
.type_check_with_type_info(handler, &mut ctx)
918924
.unwrap_or_else(|err| type_engine.id_of_error_recovery(err));
919925

920-
// find the module that the symbol is in
921-
let type_info_prefix = &call_path_binding
926+
let type_info_path = call_path_binding
922927
.inner
923-
.to_fullpath(engines, ctx.namespace())
924-
.prefixes;
928+
.to_fullpath(engines, ctx.namespace());
929+
let module_path = type_info_path.prefixes.clone();
925930
ctx.namespace()
926-
.require_module_from_absolute_path(handler, type_info_prefix)?;
931+
.require_module_from_absolute_path(handler, &module_path)?;
927932

928-
// find the method
929-
let decl_ref = ctx.find_method_for_type(
930-
handler,
931-
type_id,
932-
type_info_prefix,
933-
method_name,
934-
ctx.type_annotation(),
935-
arguments_types,
936-
Some(&method_name_binding.inner),
937-
)?;
938-
939-
(decl_ref, type_id)
933+
(type_id, module_path, method_name)
940934
}
935+
941936
MethodName::FromTrait { call_path } => {
942-
// find the module that the symbol is in
943-
let module_path = match call_path.callpath_type {
937+
let module_path: Vec<_> = match call_path.callpath_type {
944938
CallPathType::RelativeToPackageRoot => {
945939
let mut path = vec![ctx.namespace().current_package_name().clone()];
946-
for ident in call_path.prefixes.iter() {
947-
path.push(ident.clone())
948-
}
940+
path.extend(call_path.prefixes.iter().cloned());
949941
path
950942
}
951943
CallPathType::Full => call_path.prefixes.clone(),
952944
CallPathType::Ambiguous => {
945+
let first = call_path.prefixes.first().expect("prefixes is non-empty");
953946
if ctx
954947
.namespace()
955948
.current_module()
956949
.submodules()
957-
.contains_key(call_path.prefixes.first().unwrap().as_str())
950+
.contains_key(first.as_str())
958951
{
959952
ctx.namespace().prepend_module_path(&call_path.prefixes)
960953
} else {
@@ -963,72 +956,35 @@ pub(crate) fn resolve_method_name(
963956
}
964957
};
965958

966-
// find the type of the first argument
967-
let type_id = arguments_types
968-
.first()
969-
.cloned()
970-
.unwrap_or_else(|| type_engine.new_unknown());
971-
972-
// find the method
973-
let decl_ref = ctx.find_method_for_type(
974-
handler,
975-
type_id,
976-
&module_path,
977-
&call_path.suffix,
978-
ctx.type_annotation(),
979-
arguments_types,
980-
Some(&method_name_binding.inner),
981-
)?;
982-
983-
(decl_ref, type_id)
959+
let type_id = first_arg_or_unknown();
960+
(type_id, module_path, &call_path.suffix)
984961
}
985-
MethodName::FromModule { method_name } => {
986-
// find the module that the symbol is in
987-
let module_path = ctx.namespace().current_mod_path();
988962

989-
// find the type of the first argument
990-
let type_id = arguments_types
991-
.first()
992-
.cloned()
993-
.unwrap_or_else(|| type_engine.new_unknown());
994-
995-
// find the method
996-
let decl_ref = ctx.find_method_for_type(
997-
handler,
998-
type_id,
999-
module_path.as_slice(),
1000-
method_name,
1001-
ctx.type_annotation(),
1002-
arguments_types,
1003-
Some(&method_name_binding.inner),
1004-
)?;
1005-
1006-
(decl_ref, type_id)
963+
MethodName::FromModule { method_name } => {
964+
let module_path = ctx.namespace().current_mod_path().to_vec();
965+
let type_id = first_arg_or_unknown();
966+
(type_id, module_path, method_name)
1007967
}
968+
1008969
MethodName::FromQualifiedPathRoot {
1009970
ty, method_name, ..
1010971
} => {
1011-
// type check the call path
1012972
let type_id = ty.type_id();
1013-
1014-
// find the module that the symbol is in
1015-
let module_path = ctx.namespace().current_mod_path();
1016-
1017-
// find the method
1018-
let decl_ref = ctx.find_method_for_type(
1019-
handler,
1020-
type_id,
1021-
module_path,
1022-
method_name,
1023-
ctx.type_annotation(),
1024-
arguments_types,
1025-
Some(&method_name_binding.inner),
1026-
)?;
1027-
1028-
(decl_ref, type_id)
973+
let module_path = ctx.namespace().current_mod_path().to_vec();
974+
(type_id, module_path, method_name)
1029975
}
1030976
};
1031977

978+
let decl_ref = ctx.find_method_for_type(
979+
handler,
980+
type_id,
981+
module_path.as_slice(),
982+
method_ident,
983+
ctx.type_annotation(),
984+
arguments_types,
985+
Some(&method_name_binding.inner),
986+
)?;
987+
1032988
ctx.engines.obs().raise_on_after_method_resolution(
1033989
&ctx,
1034990
method_name_binding,

0 commit comments

Comments
 (0)