Skip to content

Commit

Permalink
Fixes missing error when using trait constraint. (#5882)
Browse files Browse the repository at this point in the history
## Description

When using trait constraints a blanket implementation is added for the
trait constraint generic parameter.

This changes avoid the generation of other implementations from the type
parameter generic implementation.

Fixes #5873

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.
  • Loading branch information
esdrubal committed Apr 22, 2024
1 parent e12d985 commit d092929
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ fn type_check_trait_implementation(
name: Ident::new_with_override("Self".into(), Span::dummy()),
trait_constraints: VecSet(vec![]),
parent: None,
is_from_type_parameter: false,
},
None,
),
Expand Down
39 changes: 29 additions & 10 deletions sway-core/src/semantic_analysis/namespace/trait_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,18 @@ impl TraitMap {
engines,
);
} else if decider(*type_id, *map_type_id) {
let mut insertable = true;
if let TypeInfo::UnknownGeneric {
is_from_type_parameter,
..
} = *engines.te().get(*map_type_id)
{
insertable = !is_from_type_parameter
|| matches!(
*engines.te().get(*type_id),
TypeInfo::UnknownGeneric { .. }
);
}
let type_mapping = TypeSubstMap::from_superset_and_subset(
type_engine,
decl_engine,
Expand All @@ -809,34 +821,41 @@ impl TraitMap {
let trait_items: TraitItems = map_trait_items
.clone()
.into_iter()
.map(|(name, item)| match &item {
.filter_map(|(name, item)| match &item {
ResolvedTraitImplItem::Parsed(_item) => todo!(),
ResolvedTraitImplItem::Typed(item) => match item {
ty::TyTraitItem::Fn(decl_ref) => {
let mut decl = (*decl_engine.get(decl_ref.id())).clone();
decl.subst(&type_mapping, engines);
let new_ref = decl_engine
.insert(decl)
.with_parent(decl_engine, decl_ref.id().into());
(name, ResolvedTraitImplItem::Typed(TyImplItem::Fn(new_ref)))
if decl.is_trait_method_dummy && !insertable {
None
} else {
decl.subst(&type_mapping, engines);
let new_ref = decl_engine
.insert(decl)
.with_parent(decl_engine, decl_ref.id().into());
Some((
name,
ResolvedTraitImplItem::Typed(TyImplItem::Fn(new_ref)),
))
}
}
ty::TyTraitItem::Constant(decl_ref) => {
let mut decl = (*decl_engine.get(decl_ref.id())).clone();
decl.subst(&type_mapping, engines);
let new_ref = decl_engine.insert(decl);
(
Some((
name,
ResolvedTraitImplItem::Typed(TyImplItem::Constant(new_ref)),
)
))
}
ty::TyTraitItem::Type(decl_ref) => {
let mut decl = (*decl_engine.get(decl_ref.id())).clone();
decl.subst(&type_mapping, engines);
let new_ref = decl_engine.insert(decl);
(
Some((
name,
ResolvedTraitImplItem::Typed(TyImplItem::Type(new_ref)),
)
))
}
},
})
Expand Down
7 changes: 7 additions & 0 deletions sway-core/src/type_system/ast_elements/type_parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl TypeParameter {
name: name.clone(),
trait_constraints: VecSet(vec![]),
parent: None,
is_from_type_parameter: true,
},
span.source_id(),
);
Expand Down Expand Up @@ -306,6 +307,7 @@ impl TypeParameter {
name: _,
trait_constraints: _,
parent,
is_from_type_parameter: _,
} = &*type_engine.get(type_id)
{
*parent
Expand All @@ -321,6 +323,7 @@ impl TypeParameter {
name: name_ident.clone(),
trait_constraints: VecSet(trait_constraints_with_supertraits.clone()),
parent,
is_from_type_parameter: true,
},
name_ident.span().source_id(),
);
Expand Down Expand Up @@ -366,6 +369,7 @@ impl TypeParameter {
name: _,
trait_constraints: _,
parent,
is_from_type_parameter: _,
} = &*type_engine.get(type_parameter.type_id)
{
*parent
Expand All @@ -381,6 +385,7 @@ impl TypeParameter {
name: type_parameter.name_ident.clone(),
trait_constraints: VecSet(trait_constraints_with_supertraits.clone()),
parent,
is_from_type_parameter: true,
}
.into(),
source_id: type_parameter.name_ident.span().source_id().cloned(),
Expand Down Expand Up @@ -455,6 +460,7 @@ impl TypeParameter {
name,
trait_constraints,
parent,
is_from_type_parameter,
} = &*ctx.engines().te().get(*type_id)
{
if parent.is_some() {
Expand All @@ -468,6 +474,7 @@ impl TypeParameter {
name: name.clone(),
trait_constraints: trait_constraints.clone(),
parent: Some(*parent_type_id),
is_from_type_parameter: *is_from_type_parameter,
}
.into(),
source_id: name.span().source_id().cloned(),
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/type_system/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ impl TypeId {
name: _,
trait_constraints,
parent: _,
is_from_type_parameter: _,
} => {
found.insert(*self, trait_constraints.to_vec());
for trait_constraint in trait_constraints.iter() {
Expand Down
7 changes: 7 additions & 0 deletions sway-core/src/type_system/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ pub enum TypeInfo {
trait_constraints: VecSet<TraitConstraint>,
// Methods can have type parameters with unkown generic that extend the trait constraints of a parent unkown generic.
parent: Option<TypeId>,
// This is true when the UnknownGeneric is used in a type parameter.
is_from_type_parameter: bool,
},
/// Represents a type that will be inferred by the Sway compiler. This type
/// is created when the user writes code that creates a new ADT that has
Expand Down Expand Up @@ -219,6 +221,7 @@ impl HashWithEngines for TypeInfo {
name,
trait_constraints: _,
parent: _,
is_from_type_parameter: _,
} => {
name.hash(state);
// Do not hash trait_constraints as those can point back to this type_info
Expand Down Expand Up @@ -296,11 +299,13 @@ impl PartialEqWithEngines for TypeInfo {
name: l,
trait_constraints: ltc,
parent: _,
is_from_type_parameter: _,
},
Self::UnknownGeneric {
name: r,
trait_constraints: rtc,
parent: _,
is_from_type_parameter: _,
},
) => l == r && ltc.eq(rtc, ctx),
(Self::Placeholder(l), Self::Placeholder(r)) => l.eq(r, ctx),
Expand Down Expand Up @@ -443,11 +448,13 @@ impl OrdWithEngines for TypeInfo {
name: l,
trait_constraints: ltc,
parent: _,
is_from_type_parameter: _,
},
Self::UnknownGeneric {
name: r,
trait_constraints: rtc,
parent: _,
is_from_type_parameter: _,
},
) => l.cmp(r).then_with(|| ltc.cmp(rtc, ctx)),
(Self::Placeholder(l), Self::Placeholder(r)) => l.cmp(r, ctx),
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/type_system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ fn generic_enum_resolution() {
name: generic_name.clone(),
trait_constraints: VecSet(Vec::new()),
parent: None,
is_from_type_parameter: false,
},
None,
);
Expand Down
2 changes: 2 additions & 0 deletions sway-core/src/type_system/unify/unifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,13 @@ impl<'a> Unifier<'a> {
name: rn,
trait_constraints: rtc,
parent: _,
is_from_type_parameter: _,
},
UnknownGeneric {
name: en,
trait_constraints: etc,
parent: _,
is_from_type_parameter: _,
},
) if rn.as_str() == en.as_str()
&& rtc.eq(etc, &PartialEqWithEnginesContext::new(self.engines)) => {}
Expand Down
6 changes: 6 additions & 0 deletions sway-core/src/type_system/unify/unify_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,13 @@ impl<'a> UnifyCheck<'a> {
name: ln,
trait_constraints: ltc,
parent: _,
is_from_type_parameter: _,
},
UnknownGeneric {
name: rn,
trait_constraints: rtc,
parent: _,
is_from_type_parameter: _,
},
) => ln == rn && rtc.eq(ltc, &PartialEqWithEnginesContext::new(self.engines)),
// any type can be coerced into a generic,
Expand Down Expand Up @@ -450,11 +452,13 @@ impl<'a> UnifyCheck<'a> {
name: _,
trait_constraints: ltc,
parent: _,
is_from_type_parameter: _,
},
UnknownGeneric {
name: _,
trait_constraints: rtc,
parent: _,
is_from_type_parameter: _,
},
) => rtc.eq(ltc, &PartialEqWithEnginesContext::new(self.engines)),

Expand Down Expand Up @@ -498,11 +502,13 @@ impl<'a> UnifyCheck<'a> {
name: rn,
trait_constraints: rtc,
parent: _,
is_from_type_parameter: _,
},
TypeInfo::UnknownGeneric {
name: en,
trait_constraints: etc,
parent: _,
is_from_type_parameter: _,
},
) => {
rn.as_str() == en.as_str()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = "core"
source = "path+from-root-379FCEFFEC451D4E"

[[package]]
name = "std"
source = "path+from-root-379FCEFFEC451D4E"
dependencies = ["core"]

[[package]]
name = "struct_generic_abi_encode"
source = "member"
dependencies = ["std"]
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 = "struct_generic_abi_encode"
entry = "main.sw"

[dependencies]
std = { path = "../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
script;

#[allow(dead_code)]
struct SomeStruct<T> {
ptr: raw_ptr,
cap: u64,
}

trait AbiEncode2 {
fn abi_encode2(self, ref mut buffer: Buffer);
}


impl AbiEncode2 for u64
{
fn abi_encode2(self, ref mut buffer: Buffer) {

}
}

impl<T> AbiEncode2 for SomeStruct<T> where T: AbiEncode2
{
#[allow(dead_code)]
fn abi_encode2(self, ref mut buffer: Buffer) {
self.ptr.abi_encode2(buffer);
self.cap.abi_encode2(buffer);
}
}


fn main() {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
category = "fail"

#check: $()self.ptr.abi_encode2(buffer);
#nextln: $()No method named "abi_encode2" found for type "pointer".

0 comments on commit d092929

Please sign in to comment.