Skip to content

Commit 695f87f

Browse files
committed
Adds TypeFilter to TraitMap.
1 parent 0f5f033 commit 695f87f

File tree

2 files changed

+117
-14
lines changed

2 files changed

+117
-14
lines changed

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

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,36 @@ enum TypeRootFilter {
200200
TraitType(String),
201201
}
202202

203+
#[derive(Clone, Hash, Eq, PartialEq, Debug)]
204+
enum TypeFilter {
205+
Unknown,
206+
Never,
207+
Placeholder,
208+
TypeParam(usize),
209+
StringSlice,
210+
StringArray(usize),
211+
U8,
212+
U16,
213+
U32,
214+
U64,
215+
U256,
216+
Bool,
217+
Custom(String),
218+
B256,
219+
Contract,
220+
ErrorRecovery,
221+
Tuple(usize, Vec<TypeFilter>),
222+
Enum(ParsedDeclId<EnumDeclaration>, Vec<TypeFilter>),
223+
Struct(ParsedDeclId<StructDeclaration>, Vec<TypeFilter>),
224+
ContractCaller(String),
225+
Array(usize, Box<TypeFilter>),
226+
RawUntypedPtr,
227+
RawUntypedSlice,
228+
Ptr(Box<TypeFilter>),
229+
Slice(Box<TypeFilter>),
230+
TraitType(String),
231+
}
232+
203233
/// Map holding trait implementations for types.
204234
///
205235
/// Note: "impl self" blocks are considered traits and are stored in the
@@ -208,7 +238,7 @@ enum TypeRootFilter {
208238
pub struct TraitMap {
209239
trait_impls: TraitImpls,
210240
satisfied_cache: HashSet<u64>,
211-
insert_for_type_cache: HashMap<TypeRootFilter, im::Vector<TypeId>>,
241+
insert_for_type_cache: HashMap<TypeFilter, im::Vector<TypeId>>,
212242
}
213243

214244
pub(crate) enum IsImplSelf {
@@ -538,7 +568,8 @@ impl TraitMap {
538568

539569
let trait_map = TraitMap {
540570
trait_impls,
541-
satisfied_cache: HashMap::<TypeRootFilter, im::Vector<TypeId>>::new(),
571+
satisfied_cache: HashSet::default(),
572+
insert_for_type_cache: HashMap::<TypeRootFilter, im::Vector<TypeId>>::new(),
542573
};
543574

544575
self.extend(trait_map, engines);
@@ -1623,4 +1654,88 @@ impl TraitMap {
16231654
} => Self::get_type_root_filter(engines, referenced_type.type_id),
16241655
}
16251656
}
1657+
1658+
// This is used by the trait map to filter the entries into a HashMap with the return type string as key.
1659+
fn get_type_filter(engines: &Engines, type_id: TypeId) -> TypeFilter {
1660+
use TypeInfo::*;
1661+
match &*engines.te().get(type_id) {
1662+
Unknown => TypeFilter::Unknown,
1663+
Never => TypeFilter::Never,
1664+
UnknownGeneric { .. } | Placeholder(_) => TypeFilter::Placeholder,
1665+
TypeParam(n) => TypeFilter::TypeParam(*n),
1666+
StringSlice => TypeFilter::StringSlice,
1667+
StringArray(x) => TypeFilter::StringArray(x.val()),
1668+
UnsignedInteger(x) => match x {
1669+
IntegerBits::Eight => TypeFilter::U8,
1670+
IntegerBits::Sixteen => TypeFilter::U16,
1671+
IntegerBits::ThirtyTwo => TypeFilter::U32,
1672+
IntegerBits::SixtyFour => TypeFilter::U64,
1673+
IntegerBits::V256 => TypeFilter::U256,
1674+
},
1675+
Boolean => TypeFilter::Bool,
1676+
Custom {
1677+
qualified_call_path: call_path,
1678+
..
1679+
} => TypeFilter::Custom(call_path.call_path.suffix.to_string()),
1680+
B256 => TypeFilter::B256,
1681+
Numeric => TypeFilter::U64, // u64 is the default
1682+
Contract => TypeFilter::Contract,
1683+
ErrorRecovery(_) => TypeFilter::ErrorRecovery,
1684+
Tuple(fields) => TypeFilter::Tuple(
1685+
fields.len(),
1686+
fields
1687+
.iter()
1688+
.map(|f| Self::get_type_filter(engines, f.type_id))
1689+
.collect::<Vec<_>>(),
1690+
),
1691+
UntypedEnum(_) => unreachable!(),
1692+
UntypedStruct(_) => unreachable!(),
1693+
Enum(decl_id) => {
1694+
// TODO Remove unwrap once #6475 is fixed
1695+
TypeFilter::Enum(
1696+
engines.de().get_parsed_decl_id(decl_id).unwrap(),
1697+
engines
1698+
.de()
1699+
.get_enum(decl_id)
1700+
.type_parameters
1701+
.iter()
1702+
.map(|f| Self::get_type_filter(engines, f.type_id))
1703+
.collect::<Vec<_>>(),
1704+
)
1705+
}
1706+
Struct(decl_id) => {
1707+
// TODO Remove unwrap once #6475 is fixed
1708+
TypeFilter::Struct(
1709+
engines.de().get_parsed_decl_id(decl_id).unwrap(),
1710+
engines
1711+
.de()
1712+
.get_struct(decl_id)
1713+
.type_parameters
1714+
.iter()
1715+
.map(|f| Self::get_type_filter(engines, f.type_id))
1716+
.collect::<Vec<_>>(),
1717+
)
1718+
}
1719+
ContractCaller { abi_name, .. } => TypeFilter::ContractCaller(abi_name.to_string()),
1720+
Array(type_argument, length) => TypeFilter::Array(
1721+
length.val(),
1722+
Box::new(Self::get_type_filter(engines, type_argument.type_id)),
1723+
),
1724+
RawUntypedPtr => TypeFilter::RawUntypedPtr,
1725+
RawUntypedSlice => TypeFilter::RawUntypedSlice,
1726+
Ptr(type_argument) => TypeFilter::Ptr(Box::new(Self::get_type_filter(
1727+
engines,
1728+
type_argument.type_id,
1729+
))),
1730+
Slice(type_argument) => TypeFilter::Slice(Box::new(Self::get_type_filter(
1731+
engines,
1732+
type_argument.type_id,
1733+
))),
1734+
Alias { ty, .. } => Self::get_type_filter(engines, ty.type_id),
1735+
TraitType { name, .. } => TypeFilter::TraitType(name.to_string()),
1736+
Ref {
1737+
referenced_type, ..
1738+
} => Self::get_type_filter(engines, referenced_type.type_id),
1739+
}
1740+
}
16261741
}

sway-core/src/semantic_analysis/type_check_context.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,18 +1246,6 @@ impl<'a> TypeCheckContext<'a> {
12461246
.values()
12471247
.filter_map(|method_ref| {
12481248
let method = decl_engine.get_function(method_ref);
1249-
1250-
//if method.name.clone().as_str() == "new" {
1251-
println!(
1252-
"find_method_for_type {:?} {:?}",
1253-
method.implementing_for_typeid.map(|t| {
1254-
self.engines
1255-
.help_out((*self.engines.te().get(t)).clone())
1256-
}),
1257-
method.name.clone(),
1258-
);
1259-
//}
1260-
12611249
method
12621250
.span()
12631251
.to_string_path_with_line_col(self.engines().se())

0 commit comments

Comments
 (0)