Skip to content

Commit

Permalink
Fixes issue of trait implementation not beeing used.
Browse files Browse the repository at this point in the history
When constraint is used in generic functions the trait name does not
contain any prefixes this was causing
get_methods_for_type_and_trait_name to return no methods to be used.

Closes FuelLabs#3326
  • Loading branch information
esdrubal committed Nov 24, 2022
1 parent 412a4cd commit f4e758e
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 1 deletion.
5 changes: 4 additions & 1 deletion sway-core/src/semantic_analysis/namespace/trait_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,10 @@ impl TraitMap {
suffix: e.key.name.suffix.name.clone(),
is_absolute: e.key.name.is_absolute,
};
if &map_trait_name == trait_name
let are_same_name_traits = (trait_name.prefixes.is_empty()
&& map_trait_name.suffix == trait_name.suffix)
|| &map_trait_name == trait_name;
if are_same_name_traits
&& are_equal_minus_dynamic_types(type_engine, type_id, e.key.type_id)
{
let mut trait_methods = e.value.values().cloned().into_iter().collect::<Vec<_>>();
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 <contact@fuel.sh>"]
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 f4e758e

Please sign in to comment.