Skip to content

Commit

Permalink
Fixes issue of trait implementation not beeing used. (FuelLabs#3391)
Browse files Browse the repository at this point in the history
The issue was caused by constraints used in generic functions having
trait name that does not contain any prefixes. This was causing
`get_methods_for_type_and_trait_name` to return no methods to be used.

This PR changes `get_methods_for_type_and_trait_name` so when a
`trait_name` without prefixes is provided it still tries to match using
the suffixes.

Closes FuelLabs#3326

Co-authored-by: Mohammad Fawaz <[email protected]>
Co-authored-by: Emily Herbert <[email protected]>
  • Loading branch information
3 people authored Dec 8, 2022
1 parent 3a80db6 commit b87843c
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 1 deletion.
28 changes: 27 additions & 1 deletion sway-core/src/type_system/type_parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,34 @@ impl TypeParameter {
type_arguments: trait_type_arguments,
} = trait_constraint;

// Use trait name with module path as this is expected in get_methods_for_type_and_trait_name
let mut full_trait_name = trait_name.clone();
if trait_name.prefixes.is_empty() {
if let Some(use_synonym) = ctx.namespace.use_synonyms.get(&trait_name.suffix) {
let mut prefixes = use_synonym.0.clone();
for mod_path in ctx.namespace.mod_path() {
if prefixes[0].as_str() == mod_path.as_str() {
prefixes.drain(0..1);
} else {
prefixes = use_synonym.0.clone();
break;
}
}
full_trait_name = CallPath {
prefixes,
suffix: trait_name.suffix.clone(),
is_absolute: false,
}
}
}

let (trait_original_method_ids, trait_impld_method_ids) = check!(
handle_trait(ctx.by_ref(), *type_id, trait_name, trait_type_arguments),
handle_trait(
ctx.by_ref(),
*type_id,
&full_trait_name,
trait_type_arguments
),
continue,
warnings,
errors
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'core'
source = 'path+from-root-33945E385D757681'

[[package]]
name = 'eq_custom_type'
source = 'member'
dependencies = ['std']

[[package]]
name = 'std'
source = 'path+from-root-33945E385D757681'
dependencies = ['core']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "eq_custom_type"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"functions": [
{
"inputs": [],
"name": "main",
"output": {
"name": "",
"type": 0,
"typeArguments": null
}
}
],
"loggedTypes": [],
"types": [
{
"components": null,
"type": "bool",
"typeId": 0,
"typeParameters": null
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
script;

use core::ops::*;

enum Error {
BoolError: bool,
U8Error: u8,
}

impl Eq for Error {
fn eq(self, other: Self) -> bool {
match (self, other) {
(Error::BoolError(val1), Error::BoolError(val2)) => val2 == val1,
(Error::U8Error(val1), Error::U8Error(val2)) => val2 == val1,
_ => false,
}
}
}

fn test_none_ok_or<T, E>(_val: T, default: E) where E: Eq {
match Option::None::<T>().ok_or(default) {
Result::Ok(_) => revert(0),
Result::Err(e) => assert(default == e),
}
}

fn main() -> bool {
test_none_ok_or(true, Error::BoolError(true));
return true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "run"
expected_result = { action = "return", value = 1 }
validate_abi = true

0 comments on commit b87843c

Please sign in to comment.