Skip to content

Commit bb4d899

Browse files
committed
- Renamed StorageHandle to StorageKey as per the RFC
- Remove support for type ascription on `self` for now - Instead, we now do `impl StorageKey<StorageMap<K, V>>` which seems to work just as well
1 parent 284c7f6 commit bb4d899

File tree

14 files changed

+70
-262
lines changed

14 files changed

+70
-262
lines changed

sway-ast/src/item/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ pub enum FnArgs {
7979
self_token: SelfToken,
8080
ref_self: Option<RefToken>,
8181
mutable_self: Option<MutToken>,
82-
ty: Option<(ColonToken, Ty)>,
8382
args_opt: Option<(CommaToken, Punctuated<FnArg, CommaToken>)>,
8483
},
8584
}

sway-core/src/language/parsed/declaration/function.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ impl PartialEqWithEngines for FunctionParameter {
4040
}
4141
}
4242

43-
impl FunctionParameter {
44-
pub fn is_self(&self) -> bool {
45-
self.name.as_str() == "self"
46-
}
47-
}
48-
4943
impl FunctionDeclaration {
5044
/// Checks if this `FunctionDeclaration` is a test.
5145
pub(crate) fn is_test(&self) -> bool {

sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs

Lines changed: 15 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ use sway_types::{Named, Spanned};
33
use crate::{
44
decl_engine::{DeclEngineInsert, DeclRef, ReplaceFunctionImplementingType},
55
error::*,
6-
language::{parsed, ty, ty::TyImplItem},
6+
language::{parsed, ty},
77
semantic_analysis::TypeCheckContext,
88
type_system::*,
99
CompileResult,
1010
};
1111

12-
use std::collections::{BTreeMap, HashSet};
13-
1412
impl ty::TyDecl {
1513
pub(crate) fn type_check(
1614
mut ctx: TypeCheckContext,
@@ -197,7 +195,6 @@ impl ty::TyDecl {
197195
warnings,
198196
errors
199197
);
200-
201198
let impl_trait_decl: ty::TyDecl = decl_engine.insert(impl_trait.clone()).into();
202199
impl_trait.items.iter_mut().for_each(|item| {
203200
item.replace_implementing_type(engines, impl_trait_decl.clone());
@@ -212,84 +209,20 @@ impl ty::TyDecl {
212209
warnings,
213210
errors
214211
);
215-
216-
// Set of all type IDs that have corresponding items implemented for them in this
217-
// `impl` block. In most cases, this is a single type ID that is equal to
218-
// `impl_trait.implementing_for.type_id`.
219-
let mut processed_ids = HashSet::new();
220-
221-
// This is a map from type IDs to a vector of impl items. In most cases, all the
222-
// items are available to `impl_trait.implementing_for.type_id` (i.e. `Self`).
223-
// However, there are situations where `self`, in some of the impl methods, has a
224-
// type ascription that is different from `Self`. In that case, we have to keep
225-
// track of which impl items are available to which type ID.
226-
//
227-
// For now, only `std::core::experimental::StorageHandle` is allowed as a type
228-
// ascription for `self`, so check that here as well
229-
let type_id_to_impl_items: BTreeMap<TypeId, Vec<TyImplItem>> = impl_trait
230-
.items
231-
.iter()
232-
.map(|item| match item {
233-
TyImplItem::Fn(func) => {
234-
// For function items, check if the first argument is `self`. If so,
235-
// map the corresponding type ID to this item. Make sure that type IDs
236-
// that are subset of each other are uniqued using `processed_ids`.
237-
let func = decl_engine.get_function(func);
238-
(
239-
match func.parameters.first() {
240-
Some(first_arg) if first_arg.is_self() => {
241-
let self_type_id = first_arg.type_argument.type_id;
242-
match processed_ids.iter().find(|p| {
243-
type_engine
244-
.get(self_type_id)
245-
.is_subset_of(&type_engine.get(**p), engines)
246-
}) {
247-
Some(id) => *id,
248-
_ => {
249-
processed_ids.insert(self_type_id);
250-
self_type_id
251-
}
252-
}
253-
}
254-
_ => impl_trait.implementing_for.type_id,
255-
},
256-
item,
257-
)
258-
}
259-
_ => {
260-
// Other items are only available for `Self`
261-
(impl_trait.implementing_for.type_id, item)
262-
}
263-
})
264-
.fold(
265-
BTreeMap::new(),
266-
|mut acc: BTreeMap<TypeId, Vec<_>>, (type_id, item)| {
267-
acc.entry(type_id)
268-
.and_modify(|v| v.push(item.clone()))
269-
.or_insert(vec![item.clone()]);
270-
acc
271-
},
272-
);
273-
274-
// For each type ID discovered in type_id_to_impl_items, insert the collected items
275-
// into the trait map
276-
for type_id in type_id_to_impl_items.keys() {
277-
check!(
278-
ctx.namespace.insert_trait_implementation(
279-
impl_trait.trait_name.clone(),
280-
impl_trait.trait_type_arguments.clone(),
281-
*type_id,
282-
&type_id_to_impl_items[type_id],
283-
&impl_trait.span,
284-
true,
285-
engines,
286-
),
287-
return err(warnings, errors),
288-
warnings,
289-
errors
290-
);
291-
}
292-
212+
check!(
213+
ctx.namespace.insert_trait_implementation(
214+
impl_trait.trait_name.clone(),
215+
impl_trait.trait_type_arguments.clone(),
216+
impl_trait.implementing_for.type_id,
217+
&impl_trait.items,
218+
&impl_trait.span,
219+
true,
220+
engines,
221+
),
222+
return err(warnings, errors),
223+
warnings,
224+
errors
225+
);
293226
let impl_trait_decl: ty::TyDecl = decl_engine.insert(impl_trait.clone()).into();
294227
impl_trait.items.iter_mut().for_each(|item| {
295228
item.replace_implementing_type(engines, impl_trait_decl.clone())

sway-core/src/semantic_analysis/ast_node/declaration/function/function_parameter.rs

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
};
88

99
use sway_error::error::CompileError;
10-
use sway_types::{Ident, Spanned};
10+
use sway_types::Spanned;
1111

1212
impl ty::TyFunctionParameter {
1313
pub(crate) fn type_check(
@@ -21,8 +21,6 @@ impl ty::TyFunctionParameter {
2121
let type_engine = ctx.type_engine;
2222
let decl_engine = ctx.decl_engine;
2323

24-
let is_self = parameter.is_self();
25-
2624
let FunctionParameter {
2725
name,
2826
is_reference,
@@ -31,9 +29,6 @@ impl ty::TyFunctionParameter {
3129
mut type_argument,
3230
} = parameter;
3331

34-
let is_self_and_is_ascribed =
35-
is_self && !matches!(type_engine.get(type_argument.type_id), TypeInfo::SelfType);
36-
3732
type_argument.type_id = check!(
3833
ctx.resolve_type_with_self(
3934
type_argument.type_id,
@@ -46,37 +41,6 @@ impl ty::TyFunctionParameter {
4641
errors,
4742
);
4843

49-
// If this parameter is `self` and if it is type ascribed, then check that the type
50-
// ascribed is `core::experimental::storage::StorageHandle`. Otherwise, emit an error. In
51-
// the future, we may want to allow more types such as `Self`.
52-
if is_self_and_is_ascribed {
53-
match type_engine.get(type_argument.type_id) {
54-
TypeInfo::Struct(decl_ref) => {
55-
let struct_decl = decl_engine.get_struct(&decl_ref);
56-
if !(struct_decl.call_path.prefixes
57-
== vec![
58-
Ident::new_no_span("core".into()),
59-
Ident::new_no_span("experimental".into()),
60-
Ident::new_no_span("storage".into()),
61-
]
62-
&& struct_decl.call_path.suffix
63-
== Ident::new_no_span("StorageHandle".into()))
64-
{
65-
errors.push(CompileError::InvalidSelfParamterType {
66-
r#type: ctx.engines().help_out(type_argument.type_id).to_string(),
67-
span: name.span(),
68-
});
69-
}
70-
}
71-
_ => {
72-
errors.push(CompileError::InvalidSelfParamterType {
73-
r#type: ctx.engines().help_out(type_argument.type_id).to_string(),
74-
span: name.span(),
75-
});
76-
}
77-
}
78-
}
79-
8044
if !is_from_method {
8145
let mutability = ty::VariableMutability::new_from_ref_mut(is_reference, is_mutable);
8246
if mutability == ty::VariableMutability::Mutable {

sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -911,48 +911,46 @@ impl ty::TyExpression {
911911
);
912912

913913
if ctx.experimental_storage_enabled() {
914-
// The type of a storage access is `core::experimental::StorageHandle`. This is the
915-
// path to it.
916-
let storage_handle_mod_path = vec![
914+
// The type of a storage access is `core::experimental::storage::StorageKey`. This is
915+
// the path to it.
916+
let storage_key_mod_path = vec![
917917
Ident::new_with_override("core".into(), span.clone()),
918918
Ident::new_with_override("experimental".into(), span.clone()),
919919
Ident::new_with_override("storage".into(), span.clone()),
920920
];
921-
let storage_handle_ident =
922-
Ident::new_with_override("StorageHandle".into(), span.clone());
921+
let storage_key_ident = Ident::new_with_override("StorageKey".into(), span.clone());
923922

924923
// Search for the struct declaration with the call path above.
925-
let storage_handle_decl_opt = check!(
924+
let storage_key_decl_opt = check!(
926925
ctx.namespace
927926
.root()
928-
.resolve_symbol(&storage_handle_mod_path, &storage_handle_ident),
927+
.resolve_symbol(&storage_key_mod_path, &storage_key_ident),
929928
return err(warnings, errors),
930929
warnings,
931930
errors
932931
);
933-
let storage_handle_struct_decl_ref = check!(
934-
storage_handle_decl_opt.to_struct_ref(engines),
932+
let storage_key_struct_decl_ref = check!(
933+
storage_key_decl_opt.to_struct_ref(engines),
935934
return err(warnings, errors),
936935
warnings,
937936
errors
938937
);
939-
let mut storage_handle_struct_decl =
940-
decl_engine.get_struct(&storage_handle_struct_decl_ref);
938+
let mut storage_key_struct_decl = decl_engine.get_struct(&storage_key_struct_decl_ref);
941939

942-
// Set the type arguments to `StorageHandle` to the `access_type`, which is represents
943-
// the type of the data that the `StorageHandle` "points" to.
940+
// Set the type arguments to `StorageKey` to the `access_type`, which is represents the
941+
// type of the data that the `StorageKey` "points" to.
944942
let mut type_arguments = vec![TypeArgument {
945943
initial_type_id: access_type,
946944
type_id: access_type,
947945
span: span.clone(),
948946
call_path_tree: None,
949947
}];
950948

951-
// Monomorphize the generic `StorageHandle` type given the type argument specified above
949+
// Monomorphize the generic `StorageKey` type given the type argument specified above
952950
let mut ctx = ctx;
953951
check!(
954952
ctx.monomorphize(
955-
&mut storage_handle_struct_decl,
953+
&mut storage_key_struct_decl,
956954
&mut type_arguments,
957955
EnforceTypeArguments::Yes,
958956
span
@@ -964,14 +962,11 @@ impl ty::TyExpression {
964962

965963
// Update `access_type` to be the type of the monomorphized struct after inserting it
966964
// into the type engine
967-
let storage_handle_struct_decl_ref =
968-
ctx.engines().de().insert(storage_handle_struct_decl);
969-
access_type = type_engine.insert(
970-
decl_engine,
971-
TypeInfo::Struct(storage_handle_struct_decl_ref),
972-
);
965+
let storage_key_struct_decl_ref = ctx.engines().de().insert(storage_key_struct_decl);
966+
access_type =
967+
type_engine.insert(decl_engine, TypeInfo::Struct(storage_key_struct_decl_ref));
973968

974-
// take any trait items that apply to `StorageHandle<T>` and copy them to the
969+
// take any trait items that apply to `StorageKey<T>` and copy them to the
975970
// monomorphized type
976971
ctx.namespace
977972
.insert_trait_implementation_for_type(engines, access_type);

sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,6 @@ fn fn_args_to_function_parameters(
11391139
self_token,
11401140
ref_self,
11411141
mutable_self,
1142-
ty,
11431142
args_opt,
11441143
} => {
11451144
let mutability_span = match (&ref_self, &mutable_self) {
@@ -1148,13 +1147,7 @@ fn fn_args_to_function_parameters(
11481147
(Some(reference), None) => reference.span(),
11491148
(Some(reference), Some(mutable)) => Span::join(reference.span(), mutable.span()),
11501149
};
1151-
let type_id = match ty {
1152-
None => engines.te().insert(engines.de(), TypeInfo::SelfType),
1153-
Some(ty) => engines.te().insert(
1154-
engines.de(),
1155-
ty_to_type_info(context, handler, engines, ty.1)?,
1156-
),
1157-
};
1150+
let type_id = engines.te().insert(engines.de(), TypeInfo::SelfType);
11581151
let mut function_parameters = vec![FunctionParameter {
11591152
name: Ident::new(self_token.span()),
11601153
is_reference: ref_self.is_some(),

sway-error/src/error.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,6 @@ pub enum CompileError {
633633
ConfigurableInLibrary { span: Span },
634634
#[error("The name `{name}` is defined multiple times")]
635635
NameDefinedMultipleTimes { name: String, span: Span },
636-
#[error("invalid `self` parameter type: {r#type}")]
637-
InvalidSelfParamterType { r#type: String, span: Span },
638636
}
639637

640638
impl std::convert::From<TypeError> for CompileError {
@@ -805,7 +803,6 @@ impl Spanned for CompileError {
805803
TraitImplPayabilityMismatch { span, .. } => span.clone(),
806804
ConfigurableInLibrary { span } => span.clone(),
807805
NameDefinedMultipleTimes { span, .. } => span.clone(),
808-
InvalidSelfParamterType { span, .. } => span.clone(),
809806
}
810807
}
811808
}

sway-lib-core/src/experimental/storage.sw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ library;
33
/// Describes a location in storage specified by the `b256` key of a particular storage slot and an
44
/// offset, in words, from the start of the storage slot at `key`. The parameter `T` is the type of
55
/// the data to be read from or written to at `offset`.
6-
pub struct StorageHandle<T> {
6+
pub struct StorageKey<T> {
77
key: b256,
88
offset: u64,
99
}

0 commit comments

Comments
 (0)