Skip to content

Commit c56f8ed

Browse files
committed
Add impl type parameters to trait map.
1 parent 5b0f5ab commit c56f8ed

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

sway-core/src/engine_threading.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,19 @@ impl<T: PartialEqWithEngines> PartialEqWithEngines for [T] {
388388
}
389389
impl<T: OrdWithEngines> OrdWithEngines for [T] {
390390
fn cmp(&self, other: &Self, ctx: &OrdWithEnginesContext) -> Ordering {
391-
self.iter()
392-
.zip(other.iter())
393-
.map(|(x, y)| x.cmp(y, ctx))
394-
.find(|o| o.is_ne())
395-
.unwrap_or_else(|| self.len().cmp(&other.len()))
391+
let len_cmp = self.len().cmp(&other.len());
392+
if len_cmp != Ordering::Equal {
393+
return len_cmp;
394+
}
395+
396+
for (a, b) in self.iter().zip(other.iter()) {
397+
let cmp = a.cmp(b, ctx);
398+
if cmp != Ordering::Equal {
399+
return cmp;
400+
}
401+
}
402+
403+
Ordering::Equal
396404
}
397405
}
398406

sway-core/src/semantic_analysis/namespace/trait_map.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ use sway_types::{integer_bits::IntegerBits, BaseIdent, Ident, Span, Spanned};
1515
use crate::{
1616
decl_engine::{
1717
parsed_id::ParsedDeclId, DeclEngineGet, DeclEngineGetParsedDeclId, DeclEngineInsert,
18-
}, engine_threading::*, language::{
18+
},
19+
engine_threading::*,
20+
language::{
1921
parsed::{EnumDeclaration, ImplItem, StructDeclaration},
2022
ty::{self, TyDecl, TyImplItem, TyTraitItem},
2123
CallPath,
22-
}, type_system::{SubstTypes, TypeId}, IncludeSelf, SubstTypesContext, TraitConstraint, TypeArgument, TypeEngine, TypeInfo, TypeParameter, TypeSubstMap, UnifyCheck
24+
},
25+
type_system::{SubstTypes, TypeId},
26+
IncludeSelf, SubstTypesContext, TraitConstraint, TypeArgument, TypeEngine, TypeInfo,
27+
TypeParameter, TypeSubstMap, UnifyCheck,
2328
};
2429

2530
use super::Module;
@@ -97,14 +102,18 @@ type TraitName = Arc<CallPath<TraitSuffix>>;
97102
pub(crate) struct TraitKey {
98103
pub(crate) name: TraitName,
99104
pub(crate) type_id: TypeId,
105+
pub(crate) impl_type_parameters: Vec<TypeParameter>,
100106
pub(crate) trait_decl_span: Option<Span>,
101107
}
102108

103109
impl OrdWithEngines for TraitKey {
104110
fn cmp(&self, other: &Self, ctx: &OrdWithEnginesContext) -> std::cmp::Ordering {
105-
self.name
106-
.cmp(&other.name, ctx)
107-
.then_with(|| self.type_id.cmp(&other.type_id))
111+
self.name.cmp(&other.name, ctx).then_with(|| {
112+
self.type_id.cmp(&other.type_id).then_with(|| {
113+
self.impl_type_parameters
114+
.cmp(&other.impl_type_parameters, ctx)
115+
})
116+
})
108117
}
109118
}
110119

@@ -225,7 +234,7 @@ impl TraitMap {
225234
trait_name: CallPath,
226235
trait_type_args: Vec<TypeArgument>,
227236
type_id: TypeId,
228-
_impl_type_parameters: Vec<TypeParameter>,
237+
impl_type_parameters: Vec<TypeParameter>,
229238
items: &[ResolvedTraitImplItem],
230239
impl_span: &Span,
231240
trait_decl_span: Option<Span>,
@@ -272,6 +281,7 @@ impl TraitMap {
272281
name: map_trait_name,
273282
type_id: map_type_id,
274283
trait_decl_span: _,
284+
impl_type_parameters: _,
275285
},
276286
value:
277287
TraitValue {
@@ -439,6 +449,7 @@ impl TraitMap {
439449
impl_span.clone(),
440450
trait_decl_span,
441451
type_id,
452+
impl_type_parameters,
442453
trait_items,
443454
engines,
444455
);
@@ -447,19 +458,22 @@ impl TraitMap {
447458
})
448459
}
449460

461+
#[allow(clippy::too_many_arguments)]
450462
fn insert_inner(
451463
&mut self,
452464
trait_name: TraitName,
453465
impl_span: Span,
454466
trait_decl_span: Option<Span>,
455467
type_id: TypeId,
468+
impl_type_parameters: Vec<TypeParameter>,
456469
trait_methods: TraitItems,
457470
engines: &Engines,
458471
) {
459472
let key = TraitKey {
460473
name: trait_name,
461474
type_id,
462475
trait_decl_span,
476+
impl_type_parameters,
463477
};
464478
let value = TraitValue {
465479
trait_items: trait_methods,
@@ -639,6 +653,7 @@ impl TraitMap {
639653
name: map_trait_name,
640654
type_id: map_type_id,
641655
trait_decl_span: map_trait_decl_span,
656+
impl_type_parameters: map_impl_type_parameters,
642657
},
643658
value:
644659
TraitValue {
@@ -654,6 +669,7 @@ impl TraitMap {
654669
impl_span.clone(),
655670
map_trait_decl_span.clone(),
656671
*type_id,
672+
map_impl_type_parameters.clone(),
657673
map_trait_items.clone(),
658674
engines,
659675
);
@@ -663,6 +679,7 @@ impl TraitMap {
663679
impl_span.clone(),
664680
map_trait_decl_span.clone(),
665681
*map_type_id,
682+
map_impl_type_parameters.clone(),
666683
Self::filter_dummy_methods(
667684
map_trait_items.clone(),
668685
*type_id,

sway-core/src/semantic_analysis/type_check_context.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@
22
use std::collections::{HashMap, HashSet, VecDeque};
33

44
use crate::{
5-
decl_engine::{DeclEngineGet, DeclRefFunction}, engine_threading::*, language::{
5+
decl_engine::{DeclEngineGet, DeclRefFunction},
6+
engine_threading::*,
7+
language::{
68
parsed::TreeType,
79
ty::{self, TyDecl},
810
CallPath, QualifiedCallPath, Visibility,
9-
}, monomorphization::{monomorphize_with_modpath, MonomorphizeHelper}, namespace::{
11+
},
12+
monomorphization::{monomorphize_with_modpath, MonomorphizeHelper},
13+
namespace::{
1014
IsExtendingExistingImpl, IsImplSelf, ModulePath, ResolvedDeclaration,
1115
ResolvedTraitImplItem, TraitMap,
12-
}, semantic_analysis::{
16+
},
17+
semantic_analysis::{
1318
ast_node::{AbiMode, ConstShadowingMode},
1419
Namespace,
15-
}, type_system::{SubstTypes, TypeArgument, TypeId, TypeInfo}, EnforceTypeArguments, SubstTypesContext, TraitConstraint, TypeParameter, TypeSubstMap, UnifyCheck
20+
},
21+
type_system::{SubstTypes, TypeArgument, TypeId, TypeInfo},
22+
EnforceTypeArguments, SubstTypesContext, TraitConstraint, TypeParameter, TypeSubstMap,
23+
UnifyCheck,
1624
};
1725
use sway_error::{
1826
error::CompileError,

0 commit comments

Comments
 (0)