Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix extract_type_parameters regarding primitive types #6965

Merged
merged 4 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions sway-core/src/language/ty/declaration/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,15 @@ impl DeclRefFunction {
let matches = type_id_type_parameters
.iter()
.filter(|(_, orig_tp)| {
engines.te().get(orig_tp.type_id).eq(
engines.te().get(*orig_tp).eq(
&*engines.te().get(impl_type_parameter.type_id),
&PartialEqWithEnginesContext::new(engines),
)
})
.collect::<Vec<_>>();
if !matches.is_empty() {
// Adds type substitution for first match only as we can apply only one.
type_id_type_subst_map
.insert(impl_type_parameter.type_id, matches[0].0.type_id);
type_id_type_subst_map.insert(impl_type_parameter.type_id, matches[0].0);
} else if engines
.te()
.get(impl_self_or_trait.implementing_for.initial_type_id)
Expand Down
24 changes: 19 additions & 5 deletions sway-core/src/type_system/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,15 @@ impl TypeId {
found
}

/// Returns all pairs of type parameters and its
/// concrete types.
/// This includes primitive types that have "implicit"
/// type parameters such as tuples, arrays and others...
pub(crate) fn extract_type_parameters(
self,
engines: &Engines,
depth: usize,
type_parameters: &mut Vec<(TypeParameter, TypeParameter)>,
type_parameters: &mut Vec<(TypeId, TypeId)>,
orig_type_id: TypeId,
) {
if depth >= EXTRACT_ANY_MAX_DEPTH {
Expand Down Expand Up @@ -230,7 +234,7 @@ impl TypeId {
.iter()
.zip(orig_enum_decl.type_parameters.iter())
{
type_parameters.push((type_param.clone(), orig_type_param.clone()));
type_parameters.push((type_param.type_id, orig_type_param.type_id));
type_param.type_id.extract_type_parameters(
engines,
depth + 1,
Expand All @@ -251,7 +255,7 @@ impl TypeId {
.iter()
.zip(orig_struct_decl.type_parameters.iter())
{
type_parameters.push((type_param.clone(), orig_type_param.clone()));
type_parameters.push((type_param.type_id, orig_type_param.type_id));
type_param.type_id.extract_type_parameters(
engines,
depth + 1,
Expand All @@ -272,7 +276,7 @@ impl TypeId {
.iter()
.zip(orig_enum_decl.type_parameters.iter())
{
type_parameters.push((type_param.clone(), orig_type_param.clone()));
type_parameters.push((type_param.type_id, orig_type_param.type_id));
type_param.type_id.extract_type_parameters(
engines,
depth + 1,
Expand All @@ -293,7 +297,7 @@ impl TypeId {
.iter()
.zip(orig_struct_decl.type_parameters.iter())
{
type_parameters.push((type_param.clone(), orig_type_param.clone()));
type_parameters.push((type_param.type_id, orig_type_param.type_id));
type_param.type_id.extract_type_parameters(
engines,
depth + 1,
Expand All @@ -302,9 +306,11 @@ impl TypeId {
);
}
}
// Primitive types have "implicit" type parameters
(TypeInfo::Tuple(elems), TypeInfo::Tuple(orig_elems)) => {
assert_eq!(elems.len(), orig_elems.len());
for (elem, orig_elem) in elems.iter().zip(orig_elems.iter()) {
type_parameters.push((elem.type_id, orig_elem.type_id));
elem.type_id.extract_type_parameters(
engines,
depth + 1,
Expand Down Expand Up @@ -356,7 +362,9 @@ impl TypeId {
}
}
}
// Primitive types have "implicit" type parameters
(TypeInfo::Array(ty, _), TypeInfo::Array(orig_ty, _)) => {
type_parameters.push((ty.type_id, orig_ty.type_id));
ty.type_id.extract_type_parameters(
engines,
depth + 1,
Expand All @@ -376,22 +384,27 @@ impl TypeId {
self.extract_type_parameters(engines, depth + 1, type_parameters, ty.type_id);
}
(TypeInfo::UnknownGeneric { .. }, TypeInfo::UnknownGeneric { .. }) => {}
// Primitive types have "implicit" type parameters
(TypeInfo::Ptr(ty), TypeInfo::Ptr(orig_ty)) => {
type_parameters.push((ty.type_id, orig_ty.type_id));
ty.type_id.extract_type_parameters(
engines,
depth + 1,
type_parameters,
orig_ty.type_id,
);
}
// Primitive types have "implicit" type parameters
(TypeInfo::Slice(ty), TypeInfo::Slice(orig_ty)) => {
type_parameters.push((ty.type_id, orig_ty.type_id));
ty.type_id.extract_type_parameters(
engines,
depth + 1,
type_parameters,
orig_ty.type_id,
);
}
// Primitive types have "implicit" type parameters
(
TypeInfo::Ref {
referenced_type, ..
Expand All @@ -401,6 +414,7 @@ impl TypeId {
..
},
) => {
type_parameters.push((referenced_type.type_id, orig_referenced_type.type_id));
referenced_type.type_id.extract_type_parameters(
engines,
depth + 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = "core"
source = "path+from-root-8D9646CA7E6F9448"

[[package]]
name = "method_on_primitives"
source = "member"
dependencies = ["std"]

[[package]]
name = "std"
source = "path+from-root-8D9646CA7E6F9448"
dependencies = ["core"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
license = "Apache-2.0"
name = "method_on_primitives"
entry = "main.sw"

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

use std::assert::*;

fn main() {
assert(__slice(&[1u8], 0, 1).len() == 1);
}
Loading