Skip to content

Commit

Permalink
Fixes error while using generic From and Into traits.
Browse files Browse the repository at this point in the history
This commit fixes 2 errors:
First error:
```
60 |     let _i: Struct3 = 1_u64.into();
   |                             ^^^^ Trait "MyFrom" is not implemented for type "U".
```
Second error:
```
60 |     let _i: Struct3 = 1_u64.into();
   |                             ^^^^ Trait "MyFrom" is not implemented for type "Struct3".
```

First fix is to unify method return type with current ctx.type_annotation().
Second fix is to substituting the generic blanket type by call_path_typeid.

Closes #5208
  • Loading branch information
esdrubal committed Oct 23, 2023
1 parent f894ba6 commit 33735e3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
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,35 @@ 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;
let implementing_for_name = engines.help_out(t.initial_type_id).to_string();
for p in method.type_parameters.clone() {
if p.name_ident.as_str() == implementing_for_name {
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

0 comments on commit 33735e3

Please sign in to comment.