Skip to content

Commit

Permalink
Add impl type parameters to trait map.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Mar 3, 2025
1 parent 5b0f5ab commit 36468bc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
5 changes: 5 additions & 0 deletions sway-core/src/engine_threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,11 @@ impl<T: PartialEqWithEngines> PartialEqWithEngines for [T] {
}
impl<T: OrdWithEngines> OrdWithEngines for [T] {
fn cmp(&self, other: &Self, ctx: &OrdWithEnginesContext) -> Ordering {
let order = self.len().cmp(&other.len());
if order != Ordering::Equal {
return order;
}

self.iter()
.zip(other.iter())
.map(|(x, y)| x.cmp(y, ctx))
Expand Down
29 changes: 23 additions & 6 deletions sway-core/src/semantic_analysis/namespace/trait_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ use sway_types::{integer_bits::IntegerBits, BaseIdent, Ident, Span, Spanned};
use crate::{
decl_engine::{
parsed_id::ParsedDeclId, DeclEngineGet, DeclEngineGetParsedDeclId, DeclEngineInsert,
}, engine_threading::*, language::{
},
engine_threading::*,
language::{
parsed::{EnumDeclaration, ImplItem, StructDeclaration},
ty::{self, TyDecl, TyImplItem, TyTraitItem},
CallPath,
}, type_system::{SubstTypes, TypeId}, IncludeSelf, SubstTypesContext, TraitConstraint, TypeArgument, TypeEngine, TypeInfo, TypeParameter, TypeSubstMap, UnifyCheck
},
type_system::{SubstTypes, TypeId},
IncludeSelf, SubstTypesContext, TraitConstraint, TypeArgument, TypeEngine, TypeInfo,
TypeParameter, TypeSubstMap, UnifyCheck,
};

use super::Module;
Expand Down Expand Up @@ -97,14 +102,18 @@ type TraitName = Arc<CallPath<TraitSuffix>>;
pub(crate) struct TraitKey {
pub(crate) name: TraitName,
pub(crate) type_id: TypeId,
pub(crate) impl_type_parameters: Vec<TypeParameter>,
pub(crate) trait_decl_span: Option<Span>,
}

impl OrdWithEngines for TraitKey {
fn cmp(&self, other: &Self, ctx: &OrdWithEnginesContext) -> std::cmp::Ordering {
self.name
.cmp(&other.name, ctx)
.then_with(|| self.type_id.cmp(&other.type_id))
self.name.cmp(&other.name, ctx).then_with(|| {
self.type_id.cmp(&other.type_id).then_with(|| {
self.impl_type_parameters
.cmp(&other.impl_type_parameters, ctx)
})
})
}
}

Expand Down Expand Up @@ -225,7 +234,7 @@ impl TraitMap {
trait_name: CallPath,
trait_type_args: Vec<TypeArgument>,
type_id: TypeId,
_impl_type_parameters: Vec<TypeParameter>,
impl_type_parameters: Vec<TypeParameter>,
items: &[ResolvedTraitImplItem],
impl_span: &Span,
trait_decl_span: Option<Span>,
Expand Down Expand Up @@ -272,6 +281,7 @@ impl TraitMap {
name: map_trait_name,
type_id: map_type_id,
trait_decl_span: _,
impl_type_parameters: _,
},
value:
TraitValue {
Expand Down Expand Up @@ -439,6 +449,7 @@ impl TraitMap {
impl_span.clone(),
trait_decl_span,
type_id,
impl_type_parameters,
trait_items,
engines,
);
Expand All @@ -447,19 +458,22 @@ impl TraitMap {
})
}

#[allow(clippy::too_many_arguments)]
fn insert_inner(
&mut self,
trait_name: TraitName,
impl_span: Span,
trait_decl_span: Option<Span>,
type_id: TypeId,
impl_type_parameters: Vec<TypeParameter>,
trait_methods: TraitItems,
engines: &Engines,
) {
let key = TraitKey {
name: trait_name,
type_id,
trait_decl_span,
impl_type_parameters,
};
let value = TraitValue {
trait_items: trait_methods,
Expand Down Expand Up @@ -639,6 +653,7 @@ impl TraitMap {
name: map_trait_name,
type_id: map_type_id,
trait_decl_span: map_trait_decl_span,
impl_type_parameters: map_impl_type_parameters,
},
value:
TraitValue {
Expand All @@ -654,6 +669,7 @@ impl TraitMap {
impl_span.clone(),
map_trait_decl_span.clone(),
*type_id,
map_impl_type_parameters.clone(),
map_trait_items.clone(),
engines,
);
Expand All @@ -663,6 +679,7 @@ impl TraitMap {
impl_span.clone(),
map_trait_decl_span.clone(),
*map_type_id,
map_impl_type_parameters.clone(),
Self::filter_dummy_methods(
map_trait_items.clone(),
*type_id,
Expand Down
16 changes: 12 additions & 4 deletions sway-core/src/semantic_analysis/type_check_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@
use std::collections::{HashMap, HashSet, VecDeque};

use crate::{
decl_engine::{DeclEngineGet, DeclRefFunction}, engine_threading::*, language::{
decl_engine::{DeclEngineGet, DeclRefFunction},
engine_threading::*,
language::{
parsed::TreeType,
ty::{self, TyDecl},
CallPath, QualifiedCallPath, Visibility,
}, monomorphization::{monomorphize_with_modpath, MonomorphizeHelper}, namespace::{
},
monomorphization::{monomorphize_with_modpath, MonomorphizeHelper},
namespace::{
IsExtendingExistingImpl, IsImplSelf, ModulePath, ResolvedDeclaration,
ResolvedTraitImplItem, TraitMap,
}, semantic_analysis::{
},
semantic_analysis::{
ast_node::{AbiMode, ConstShadowingMode},
Namespace,
}, type_system::{SubstTypes, TypeArgument, TypeId, TypeInfo}, EnforceTypeArguments, SubstTypesContext, TraitConstraint, TypeParameter, TypeSubstMap, UnifyCheck
},
type_system::{SubstTypes, TypeArgument, TypeId, TypeInfo},
EnforceTypeArguments, SubstTypesContext, TraitConstraint, TypeParameter, TypeSubstMap,
UnifyCheck,
};
use sway_error::{
error::CompileError,
Expand Down

0 comments on commit 36468bc

Please sign in to comment.