Skip to content

Commit

Permalink
Fixes unexpected error while using generic Into trait. (#5211)
Browse files Browse the repository at this point in the history
## Description

Most of the changes in this commit are moving the TypeInfo extract
methods into TypeId, this was required by the fix which requires to work
with a TypeId instead of TypeInfo.

This commit also improves multiple Display and Debug traits.

The fix changes are in
https://github.com/FuelLabs/sway/compare/esdrubal/fix_into_trait_error?expand=1#diff-1b4791151cae4e7986e4d18bcb97e88ec47dfb10231ed1cf50dd247fc5f0bc80R334-R341

After fixing this issue this one appeared #5208

Closes #5209

## Checklist

- [x] I have linked to any relevant issues.
- [x] 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).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] 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).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
esdrubal authored Oct 20, 2023
1 parent dfa3fe5 commit b59aae9
Show file tree
Hide file tree
Showing 14 changed files with 347 additions and 283 deletions.
10 changes: 2 additions & 8 deletions sway-core/src/language/ty/declaration/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,11 @@ impl UnconstrainedTypeParameters for TyFunctionDecl {
.map(|type_param| type_param.type_id)
.collect();
all_types.extend(self.parameters.iter().flat_map(|param| {
let mut inner = type_engine
.get(param.type_argument.type_id)
.extract_inner_types(engines);
let mut inner = param.type_argument.type_id.extract_inner_types(engines);
inner.insert(param.type_argument.type_id);
inner
}));
all_types.extend(
type_engine
.get(self.return_type.type_id)
.extract_inner_types(engines),
);
all_types.extend(self.return_type.type_id.extract_inner_types(engines));
all_types.insert(self.return_type.type_id);
let type_parameter_info = type_engine.get(type_parameter.type_id);
all_types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1325,14 +1325,9 @@ fn check_for_unconstrained_type_parameters(
// create a list of the generics in use in the impl signature
let mut generics_in_use = HashSet::new();
for type_arg in trait_type_arguments.iter() {
generics_in_use.extend(
engines
.te()
.get(type_arg.type_id)
.extract_nested_generics(engines),
);
generics_in_use.extend(type_arg.type_id.extract_nested_generics(engines));
}
generics_in_use.extend(engines.te().get(self_type).extract_nested_generics(engines));
generics_in_use.extend(self_type.extract_nested_generics(engines));

// TODO: add a lookup in the trait constraints here and add it to
// generics_in_use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(crate) struct ConstructorFactory {

impl ConstructorFactory {
pub(crate) fn new(engines: &Engines, type_id: TypeId) -> Self {
let possible_types = engines.te().get(type_id).extract_nested_types(engines);
let possible_types = type_id.extract_nested_types(engines);
ConstructorFactory { possible_types }
}

Expand Down
9 changes: 2 additions & 7 deletions sway-core/src/semantic_analysis/namespace/trait_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,13 +591,11 @@ impl TraitMap {
/// `Data<T, T>: get_second(self) -> T`, and we can create a new [TraitMap]
/// with those entries for `Data<T, T>`.
pub(crate) fn filter_by_type(&self, type_id: TypeId, engines: &Engines) -> TraitMap {
let type_engine = engines.te();

let unify_checker = UnifyCheck::constraint_subset(engines);

// a curried version of the decider protocol to use in the helper functions
let decider = |left: TypeId, right: TypeId| unify_checker.check(left, right);
let mut all_types = type_engine.get(type_id).extract_inner_types(engines);
let mut all_types = type_id.extract_inner_types(engines);
all_types.insert(type_id);
let all_types = all_types.into_iter().collect::<Vec<_>>();
self.filter_by_type_inner(engines, all_types, decider)
Expand Down Expand Up @@ -666,8 +664,6 @@ impl TraitMap {
type_id: TypeId,
engines: &Engines,
) -> TraitMap {
let type_engine = engines.te();

let unify_checker = UnifyCheck::constraint_subset(engines);
let unify_checker_for_item_import = UnifyCheck::non_generic_constraint_subset(engines);

Expand All @@ -676,8 +672,7 @@ impl TraitMap {
unify_checker.check(left, right) || unify_checker_for_item_import.check(right, left)
};
let mut trait_map = self.filter_by_type_inner(engines, vec![type_id], decider);
let all_types = type_engine
.get(type_id)
let all_types = type_id
.extract_inner_types(engines)
.into_iter()
.collect::<Vec<_>>();
Expand Down
21 changes: 21 additions & 0 deletions sway-core/src/type_system/ast_elements/trait_constraint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
cmp::Ordering,
fmt,
hash::{Hash, Hasher},
};

Expand Down Expand Up @@ -56,6 +57,26 @@ impl OrdWithEngines for TraitConstraint {
}
}

impl DisplayWithEngines for TraitConstraint {
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result {
write!(f, "{:?}", engines.help_out(self))
}
}

impl DebugWithEngines for TraitConstraint {
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result {
let mut res = write!(f, "{}", self.trait_name);
if !self.type_arguments.is_empty() {
write!(f, "<")?;
for ty_arg in self.type_arguments.clone() {
write!(f, "{:?}", engines.help_out(ty_arg))?;
}
res = write!(f, ">");
}
res
}
}

impl Spanned for TraitConstraint {
fn span(&self) -> sway_types::Span {
self.trait_name.span()
Expand Down
19 changes: 13 additions & 6 deletions sway-core/src/type_system/ast_elements/type_parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,19 @@ impl Spanned for TypeParameter {

impl DebugWithEngines for TypeParameter {
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result {
write!(
f,
"{}: {:?}",
self.name_ident,
engines.help_out(self.type_id)
)
write!(f, "{}", self.name_ident)?;
if !self.trait_constraints.is_empty() {
write!(
f,
":{}",
self.trait_constraints
.iter()
.map(|c| format!("{:?}", engines.help_out(c)))
.collect::<Vec<_>>()
.join("+")
)?;
}
Ok(())
}
}

Expand Down
Loading

0 comments on commit b59aae9

Please sign in to comment.