Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes error while using generic From and Into traits. #5218

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{
decl_engine::{
engine::DeclEngineReplace, DeclEngineInsert, DeclRefFunction, ReplaceDecls,
UpdateConstantExpression,
engine::{DeclEngineGet, DeclEngineReplace},
DeclEngineInsert, DeclRefFunction, ReplaceDecls, UpdateConstantExpression,
},
language::{
parsed::*,
ty::{self},
ty::{self, TyDecl},
*,
},
namespace::TryInsertingTraitImplOnFailure,
Expand Down Expand Up @@ -552,10 +552,14 @@ pub(crate) fn monomorphize_method_application(
ref call_path,
ref mut arguments,
ref mut type_binding,
call_path_typeid,
..
} = expr
{
let decl_engine = ctx.engines.de();
let type_engine = ctx.engines.te();
let engines = ctx.engines();

*fn_ref = monomorphize_method(
handler,
ctx.by_ref(),
Expand All @@ -568,6 +572,41 @@ pub(crate) fn monomorphize_method_application(
*arguments =
unify_arguments_and_parameters(handler, ctx.by_ref(), arguments, &method.parameters)?;

// unify method return type with current ctx.type_annotation().
handler.scope(|handler| {
type_engine.unify(
handler,
engines,
method.return_type.type_id,
ctx.type_annotation(),
&method.return_type.span(),
"Function return type does not match up with local type annotation.",
None,
);
Ok(())
})?;

// This handles the case of substituting the generic blanket type by call_path_typeid.
if let Some(TyDecl::ImplTrait(t)) = method.clone().implementing_type {
let t = engines.de().get(&t.decl_id).implementing_for;
if let TypeInfo::Custom {
qualified_call_path,
type_arguments: _,
root_type_id: _,
} = type_engine.get(t.initial_type_id)
{
for p in method.type_parameters.clone() {
if p.name_ident.as_str() == qualified_call_path.call_path.suffix.as_str() {
let type_subst = TypeSubstMap::from_type_parameters_and_type_arguments(
vec![t.initial_type_id],
vec![call_path_typeid.unwrap()],
);
method.subst(&type_subst, engines);
}
}
}
}

// Handle the trait constraints. This includes checking to see if the trait
// constraints are satisfied and replacing old decl ids based on the
// constraint with new decl ids based on the new type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ fn main() -> bool {
assert_eq(s2.data_a.my_add(1,2),3);
assert_eq(s2.data_b.my_add(1,2),3);

// TODO Uncomment this after #5208 is fixed
//let _i: Struct3 = 1_u64.into();
let _i: Struct3 = 1_u64.into();

true
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
category = "run"
expected_result = { action = "return", value = 1 }
validate_abi = false
expected_warnings = 3
expected_warnings = 1
Loading