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 regression caused by enum and struct callpaths. #4007

Merged
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
32 changes: 20 additions & 12 deletions sway-core/src/type_system/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ use sway_error::{
type_error::TypeError,
warning::{CompileWarning, Warning},
};
use sway_types::{integer_bits::IntegerBits, Span, Spanned};
use sway_types::{integer_bits::IntegerBits, Ident, Span, Spanned};

use crate::{
engine_threading::*,
language::{ty, CallPath},
type_system::*,
};
use crate::{engine_threading::*, language::ty, type_system::*};

/// Helper struct to aid in type unification.
pub(super) struct Unifier<'a> {
Expand Down Expand Up @@ -120,7 +116,13 @@ impl<'a> Unifier<'a> {
type_parameters: etps,
fields: efs,
},
) => self.unify_structs(received, expected, span, (rn, rpts, rfs), (en, etps, efs)),
) => self.unify_structs(
received,
expected,
span,
(rn.suffix, rpts, rfs),
(en.suffix, etps, efs),
),
// Let empty enums to coerce to any other type. This is useful for Never enum.
(
Enum {
Expand All @@ -139,7 +141,13 @@ impl<'a> Unifier<'a> {
type_parameters: etps,
variant_types: evs,
},
) => self.unify_enums(received, expected, span, (rn, rtps, rvs), (en, etps, evs)),
) => self.unify_enums(
received,
expected,
span,
(rn.suffix, rtps, rvs),
(en.suffix, etps, evs),
),

// For integers and numerics, we (potentially) unify the numeric
// with the integer.
Expand Down Expand Up @@ -339,8 +347,8 @@ impl<'a> Unifier<'a> {
received: TypeId,
expected: TypeId,
span: &Span,
r: (CallPath, Vec<TypeParameter>, Vec<ty::TyStructField>),
e: (CallPath, Vec<TypeParameter>, Vec<ty::TyStructField>),
r: (Ident, Vec<TypeParameter>, Vec<ty::TyStructField>),
e: (Ident, Vec<TypeParameter>, Vec<ty::TyStructField>),
) -> (Vec<CompileWarning>, Vec<TypeError>) {
let mut warnings = vec![];
let mut errors = vec![];
Expand Down Expand Up @@ -388,8 +396,8 @@ impl<'a> Unifier<'a> {
received: TypeId,
expected: TypeId,
span: &Span,
r: (CallPath, Vec<TypeParameter>, Vec<ty::TyEnumVariant>),
e: (CallPath, Vec<TypeParameter>, Vec<ty::TyEnumVariant>),
r: (Ident, Vec<TypeParameter>, Vec<ty::TyEnumVariant>),
e: (Ident, Vec<TypeParameter>, Vec<ty::TyEnumVariant>),
) -> (Vec<CompileWarning>, Vec<TypeError>) {
let mut warnings = vec![];
let mut errors = vec![];
Expand Down
8 changes: 6 additions & 2 deletions sway-core/src/type_system/unify_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ impl<'a> UnifyCheck<'a> {
.iter()
.map(|x| x.type_id)
.collect::<Vec<_>>();
l_name == r_name && l_names == r_names && self.check_multiple(&l_types, &r_types)
l_name.suffix == r_name.suffix
&& l_names == r_names
&& self.check_multiple(&l_types, &r_types)
}
(
Struct {
Expand All @@ -228,7 +230,9 @@ impl<'a> UnifyCheck<'a> {
.iter()
.map(|x| x.type_id)
.collect::<Vec<_>>();
l_name == r_name && l_names == r_names && self.check_multiple(&l_types, &r_types)
l_name.suffix == r_name.suffix
&& l_names == r_names
&& self.check_multiple(&l_types, &r_types)
}

// For contract callers, they can be coerced if they have the same
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
category = "fail"
category = "disabled"

# check: $()x = y;
# nextln: $()Mismatched types.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
category = "fail"
category = "disabled"

# check: $()x = y;
# nextln: $()Mismatched types.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'core'
source = 'path+from-root-9E94934D4E529F7D'

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

[[package]]
name = 'std'
source = 'path+from-root-9E94934D4E529F7D'
dependencies = ['core']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
license = "Apache-2.0"
name = "import_with_different_callpaths"
entry = "main.sw"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"configurables": [],
"functions": [
{
"attributes": null,
"inputs": [],
"name": "main",
"output": {
"name": "",
"type": 0,
"typeArguments": null
}
}
],
"loggedTypes": [],
"messagesTypes": [],
"types": [
{
"components": [],
"type": "()",
"typeId": 0,
"typeParameters": null
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
library data_structures;

pub struct SomeStruct<T> {
a: T,
}

pub enum SomeEnum<T> {
a: T,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
library eq_impls;

dep data_structures;

use data_structures::{SomeEnum, SomeStruct};
use core::ops::Eq;

impl Eq for SomeEnum<u32> {
fn eq(self, other: Self) -> bool {
match self {
SomeEnum::a(val) => {
match other {
SomeEnum::a(other_val) => {
val == other_val
}
}
}
}
}
}

impl Eq for SomeStruct<u32> {
fn eq(self, other: Self) -> bool {
self.a == other.a
}
}

impl Eq for Vec<SomeStruct<u32>> {
fn eq(self, other: Self) -> bool {
if self.len() != other.len() {
return false;
}
let mut i = 0;
while i < self.len() {
if self.get(i).unwrap() != other.get(i).unwrap() {
return false;
}
i += 1;
}
true
}
}

impl Eq for Vec<SomeEnum<u32>> {
fn eq(self, other: Self) -> bool {
if self.len() != other.len() {
return false;
}

let mut i = 0;
while i < self.len() {
if self.get(i).unwrap() != other.get(i).unwrap() {
return false;
}
i += 1;
}
true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
script;

dep eq_impls;
dep data_structures;

use eq_impls::*;
use data_structures::*;

fn main() {
let mut expected = Vec::new();
expected.push(SomeEnum::a(0u32));
expected.push(SomeEnum::a(1u32));

assert(expected == expected);

let mut expected = Vec::new();
expected.push(SomeStruct { a: 0u32 });
expected.push(SomeStruct { a: 1u32 });

assert(expected == expected);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "run"
expected_result = { action = "return", value = 0 }
validate_abi = true