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 issue with generic trait impls using the same method names. #3676

Merged
merged 6 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
52 changes: 49 additions & 3 deletions sway-core/src/semantic_analysis/namespace/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ use crate::{
CompileResult, Ident,
};

use super::{module::Module, root::Root, submodule_namespace::SubmoduleNamespace, Path, PathBuf};
use super::{
module::Module, root::Root, submodule_namespace::SubmoduleNamespace,
trait_map::are_equal_minus_dynamic_types, Path, PathBuf,
};

use sway_error::error::CompileError;
use sway_types::{span::Span, Spanned};

use std::collections::VecDeque;
use std::{cmp::Ordering, collections::VecDeque};

/// The set of items that represent the namespace context passed throughout type checking.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -217,6 +220,8 @@ impl Namespace {
let mut methods = local_methods;
methods.append(&mut type_methods);

let mut matching_method_decl_ids: Vec<DeclId> = vec![];

for decl_id in methods.into_iter() {
let method = check!(
CompileResult::from(decl_engine.get_function(decl_id.clone(), &decl_id.span())),
Expand All @@ -225,8 +230,49 @@ impl Namespace {
errors
);
if &method.name == method_name {
return ok(decl_id, warnings, errors);
matching_method_decl_ids.push(decl_id);
}
}

let matching_method_decl_id = match matching_method_decl_ids.len().cmp(&1) {
Ordering::Equal => matching_method_decl_ids.get(0).cloned(),
Ordering::Greater => {
// Case where multiple methods exist with the same name
// This is the case of https://github.com/FuelLabs/sway/issues/3633
// where multiple generic trait impls use the same method name but with different parameter types
let mut maybe_method_decl_id: Option<DeclId> = Option::None;
for decl_id in matching_method_decl_ids.clone().into_iter() {
let method = check!(
CompileResult::from(
decl_engine.get_function(decl_id.clone(), &decl_id.span())
),
return err(warnings, errors),
warnings,
errors
);
if method.parameters.len() == args_buf.len()
&& !method.parameters.iter().zip(args_buf.iter()).any(|(p, a)| {
!are_equal_minus_dynamic_types(engines, p.type_id, a.return_type)
})
{
maybe_method_decl_id = Some(decl_id);
break;
}
}
if let Some(matching_method_decl_id) = maybe_method_decl_id {
// In case one or more methods match the parameter types we return the first match.
Some(matching_method_decl_id)
} else {
// When we can't match any method with parameter types we still return the first method found
// This was the behavior before introducing the parameter type matching
matching_method_decl_ids.get(0).cloned()
}
}
Ordering::Less => None,
};

if let Some(method_decl_id) = matching_method_decl_id {
return ok(method_decl_id, warnings, errors);
}

if !args_buf
Expand Down
32 changes: 30 additions & 2 deletions sway-core/src/semantic_analysis/namespace/trait_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl TraitMap {
});
} else if types_are_subset {
mohammadfawaz marked this conversation as resolved.
Show resolved Hide resolved
for (name, decl_id) in trait_methods.iter() {
if map_trait_methods.get(name).is_some() {
if let Some(map_trait_method_decl_id) = map_trait_methods.get(name) {
let method = check!(
CompileResult::from(
decl_engine.get_function(decl_id.clone(), impl_span)
Expand All @@ -201,6 +201,30 @@ impl TraitMap {
warnings,
errors
);
let map_trait_method = check!(
CompileResult::from(
decl_engine
.get_function(map_trait_method_decl_id.clone(), impl_span)
),
return err(warnings, errors),
warnings,
errors
);
if !traits_are_subset
&& !is_impl_self
&& (method.parameters.len() != map_trait_method.parameters.len()
|| method
.parameters
.iter()
.zip(map_trait_method.parameters.iter())
.any(|(p1, p2)| {
!are_equal_minus_dynamic_types(
engines, p1.type_id, p2.type_id,
)
}))
{
continue;
}
errors.push(CompileError::DuplicateMethodsDefinedForType {
func_name: method.name.to_string(),
type_implementing_for: engines.help_out(type_id).to_string(),
Expand Down Expand Up @@ -772,7 +796,11 @@ impl TraitMap {
}
}

fn are_equal_minus_dynamic_types(engines: Engines<'_>, left: TypeId, right: TypeId) -> bool {
pub(crate) fn are_equal_minus_dynamic_types(
engines: Engines<'_>,
left: TypeId,
right: TypeId,
) -> bool {
if left.index() == right.index() {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ impl Data<u8> {
}
}

pub struct A {
a: u64,
}

pub struct B {
b: u64,
}

pub struct C {
c: u64,
}

pub trait Convert<T> {
fn convert(t: u64);
}

impl Convert<B> for A {
fn convert(t: u64) {
}
}

impl Convert<C> for A {
// duplicate definition
fn convert(t: u64) {
}
}

fn main() {

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ category = "fail"

# check: $()fn my_add(self, other: Self) -> Self {
# check: $()Duplicate definitions for the method "my_add" for type "Data<u32>".

# check: $()fn convert(t: u64) {
# check: $()Duplicate definitions for the method "convert" for type "A".
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,38 @@ impl MyTriple<u64> for MyU64 {
}
}

pub struct A {
a: u64,
}

pub struct B {
b: u64,
}

pub struct C {
c: u64,
}

pub trait Convert<T> {
fn convert(t: T) -> Self;
}

impl Convert<B> for A {
fn convert(t: B) -> Self {
A {
a: t.b
}
}
}

impl Convert<C> for A {
fn convert(t: C) -> Self {
A {
a: t.c
}
}
}

fn main() -> u64 {
let a = FooBarData {
value: 1u8
Expand Down Expand Up @@ -170,6 +202,9 @@ fn main() -> u64 {
};
let q = p.my_triple(1);

let r_b = B { b: 42 };
let r_c = C { c: 42 };

if c == 42u8
&& d
&& e == 9u64
Expand All @@ -179,7 +214,9 @@ fn main() -> u64 {
&& l == 50
&& n == 240
&& o == 360
&& q == 93 {
&& q == 93
&& A::convert(r_b).a == 42
&& A::convert(r_c).a == 42 {
42
} else {
7
Expand Down