@@ -54,61 +54,136 @@ impl ty::TyAbiDecl {
54
54
55
55
// A temporary namespace for checking within this scope.
56
56
let mut abi_namespace = ctx. namespace . clone ( ) ;
57
- let mut ctx = ctx
58
- . scoped ( & mut abi_namespace)
59
- . with_abi_mode ( AbiMode :: ImplAbiFn ( name. clone ( ) , None ) )
60
- . with_self_type ( Some ( self_type_id) ) ;
57
+ ctx. with_abi_mode ( AbiMode :: ImplAbiFn ( name. clone ( ) , None ) )
58
+ . with_self_type ( Some ( self_type_id) )
59
+ . scoped ( & mut abi_namespace, |mut ctx| {
60
+ // Insert the "self" type param into the namespace.
61
+ self_type_param. insert_self_type_into_namespace ( handler, ctx. by_ref ( ) ) ;
61
62
62
- // Insert the "self" type param into the namespace.
63
- self_type_param. insert_self_type_into_namespace ( handler, ctx. by_ref ( ) ) ;
63
+ // Recursively make the interface surfaces and methods of the
64
+ // supertraits available to this abi.
65
+ insert_supertraits_into_namespace (
66
+ handler,
67
+ ctx. by_ref ( ) ,
68
+ self_type_id,
69
+ & supertraits,
70
+ & SupertraitOf :: Abi ( span. clone ( ) ) ,
71
+ ) ?;
64
72
65
- // Recursively make the interface surfaces and methods of the
66
- // supertraits available to this abi.
67
- insert_supertraits_into_namespace (
68
- handler,
69
- ctx. by_ref ( ) ,
70
- self_type_id,
71
- & supertraits,
72
- & SupertraitOf :: Abi ( span. clone ( ) ) ,
73
- ) ?;
73
+ // Type check the interface surface.
74
+ let mut new_interface_surface = vec ! [ ] ;
74
75
75
- // Type check the interface surface.
76
- let mut new_interface_surface = vec ! [ ] ;
76
+ let mut ids: HashSet < Ident > = HashSet :: default ( ) ;
77
77
78
- let mut ids: HashSet < Ident > = HashSet :: default ( ) ;
78
+ let error_on_shadowing_superabi_method =
79
+ |method_name : & Ident , ctx : & mut TypeCheckContext | {
80
+ if let Ok ( superabi_impl_method_ref) = ctx. find_method_for_type (
81
+ & Handler :: default ( ) ,
82
+ self_type_id,
83
+ & [ ] ,
84
+ & method_name. clone ( ) ,
85
+ ctx. type_annotation ( ) ,
86
+ & Default :: default ( ) ,
87
+ None ,
88
+ TryInsertingTraitImplOnFailure :: No ,
89
+ ) {
90
+ let superabi_impl_method =
91
+ ctx. engines . de ( ) . get_function ( & superabi_impl_method_ref) ;
92
+ if let Some ( ty:: TyDecl :: AbiDecl ( abi_decl) ) =
93
+ & superabi_impl_method. implementing_type
94
+ {
95
+ handler. emit_err ( CompileError :: AbiShadowsSuperAbiMethod {
96
+ span : method_name. span ( ) ,
97
+ superabi : abi_decl. name . clone ( ) ,
98
+ } ) ;
99
+ }
100
+ }
101
+ } ;
79
102
80
- let error_on_shadowing_superabi_method =
81
- |method_name : & Ident , ctx : & mut TypeCheckContext | {
82
- if let Ok ( superabi_impl_method_ref) = ctx. find_method_for_type (
83
- & Handler :: default ( ) ,
84
- self_type_id,
85
- & [ ] ,
86
- & method_name. clone ( ) ,
87
- ctx. type_annotation ( ) ,
88
- & Default :: default ( ) ,
89
- None ,
90
- TryInsertingTraitImplOnFailure :: No ,
91
- ) {
92
- let superabi_impl_method =
93
- ctx. engines . de ( ) . get_function ( & superabi_impl_method_ref) ;
94
- if let Some ( ty:: TyDecl :: AbiDecl ( abi_decl) ) =
95
- & superabi_impl_method. implementing_type
96
- {
97
- handler. emit_err ( CompileError :: AbiShadowsSuperAbiMethod {
98
- span : method_name. span ( ) ,
99
- superabi : abi_decl. name . clone ( ) ,
103
+ for item in interface_surface. into_iter ( ) {
104
+ let decl_name = match item {
105
+ TraitItem :: TraitFn ( method) => {
106
+ // check that a super-trait does not define a method
107
+ // with the same name as the current interface method
108
+ error_on_shadowing_superabi_method ( & method. name , & mut ctx) ;
109
+ let method = ty:: TyTraitFn :: type_check ( handler, ctx. by_ref ( ) , method) ?;
110
+ for param in & method. parameters {
111
+ if param. is_reference || param. is_mutable {
112
+ handler. emit_err (
113
+ CompileError :: RefMutableNotAllowedInContractAbi {
114
+ param_name : param. name . clone ( ) ,
115
+ span : param. name . span ( ) ,
116
+ } ,
117
+ ) ;
118
+ }
119
+ }
120
+ new_interface_surface. push ( ty:: TyTraitInterfaceItem :: TraitFn (
121
+ ctx. engines . de ( ) . insert ( method. clone ( ) ) ,
122
+ ) ) ;
123
+ method. name . clone ( )
124
+ }
125
+ TraitItem :: Constant ( decl_id) => {
126
+ let const_decl = engines. pe ( ) . get_constant ( & decl_id) . as_ref ( ) . clone ( ) ;
127
+ let const_decl =
128
+ ty:: TyConstantDecl :: type_check ( handler, ctx. by_ref ( ) , const_decl) ?;
129
+ let decl_ref = ctx. engines . de ( ) . insert ( const_decl. clone ( ) ) ;
130
+ new_interface_surface
131
+ . push ( ty:: TyTraitInterfaceItem :: Constant ( decl_ref. clone ( ) ) ) ;
132
+
133
+ let const_name = const_decl. call_path . suffix . clone ( ) ;
134
+ ctx. insert_symbol (
135
+ handler,
136
+ const_name. clone ( ) ,
137
+ ty:: TyDecl :: ConstantDecl ( ty:: ConstantDecl {
138
+ name : const_name. clone ( ) ,
139
+ decl_id : * decl_ref. id ( ) ,
140
+ decl_span : const_decl. span . clone ( ) ,
141
+ } ) ,
142
+ ) ?;
143
+
144
+ const_name
145
+ }
146
+ TraitItem :: Type ( decl_id) => {
147
+ let type_decl = engines. pe ( ) . get_trait_type ( & decl_id) . as_ref ( ) . clone ( ) ;
148
+ handler. emit_err ( CompileError :: AssociatedTypeNotSupportedInAbi {
149
+ span : type_decl. span . clone ( ) ,
150
+ } ) ;
151
+
152
+ let type_decl =
153
+ ty:: TyTraitType :: type_check ( handler, ctx. by_ref ( ) , type_decl) ?;
154
+ let decl_ref = ctx. engines ( ) . de ( ) . insert ( type_decl. clone ( ) ) ;
155
+ new_interface_surface
156
+ . push ( ty:: TyTraitInterfaceItem :: Type ( decl_ref. clone ( ) ) ) ;
157
+
158
+ type_decl. name
159
+ }
160
+ TraitItem :: Error ( _, _) => {
161
+ continue ;
162
+ }
163
+ } ;
164
+
165
+ if !ids. insert ( decl_name. clone ( ) ) {
166
+ handler. emit_err ( CompileError :: MultipleDefinitionsOfName {
167
+ name : decl_name. clone ( ) ,
168
+ span : decl_name. span ( ) ,
100
169
} ) ;
101
170
}
102
171
}
103
- } ;
104
172
105
- for item in interface_surface. into_iter ( ) {
106
- let decl_name = match item {
107
- TraitItem :: TraitFn ( method) => {
108
- // check that a super-trait does not define a method
109
- // with the same name as the current interface method
173
+ // Type check the items.
174
+ let mut new_items = vec ! [ ] ;
175
+ for method_id in methods. into_iter ( ) {
176
+ let method = engines. pe ( ) . get_function ( & method_id) ;
177
+ let method = ty:: TyFunctionDecl :: type_check (
178
+ handler,
179
+ ctx. by_ref ( ) ,
180
+ & method,
181
+ false ,
182
+ false ,
183
+ Some ( self_type_param. type_id ) ,
184
+ )
185
+ . unwrap_or_else ( |_| ty:: TyFunctionDecl :: error ( & method) ) ;
110
186
error_on_shadowing_superabi_method ( & method. name , & mut ctx) ;
111
- let method = ty:: TyTraitFn :: type_check ( handler, ctx. by_ref ( ) , method) ?;
112
187
for param in & method. parameters {
113
188
if param. is_reference || param. is_mutable {
114
189
handler. emit_err ( CompileError :: RefMutableNotAllowedInContractAbi {
@@ -117,100 +192,28 @@ impl ty::TyAbiDecl {
117
192
} ) ;
118
193
}
119
194
}
120
- new_interface_surface. push ( ty:: TyTraitInterfaceItem :: TraitFn (
121
- ctx. engines . de ( ) . insert ( method. clone ( ) ) ,
122
- ) ) ;
123
- method. name . clone ( )
124
- }
125
- TraitItem :: Constant ( decl_id) => {
126
- let const_decl = engines. pe ( ) . get_constant ( & decl_id) . as_ref ( ) . clone ( ) ;
127
- let const_decl =
128
- ty:: TyConstantDecl :: type_check ( handler, ctx. by_ref ( ) , const_decl) ?;
129
- let decl_ref = ctx. engines . de ( ) . insert ( const_decl. clone ( ) ) ;
130
- new_interface_surface
131
- . push ( ty:: TyTraitInterfaceItem :: Constant ( decl_ref. clone ( ) ) ) ;
132
-
133
- let const_name = const_decl. call_path . suffix . clone ( ) ;
134
- ctx. insert_symbol (
135
- handler,
136
- const_name. clone ( ) ,
137
- ty:: TyDecl :: ConstantDecl ( ty:: ConstantDecl {
138
- name : const_name. clone ( ) ,
139
- decl_id : * decl_ref. id ( ) ,
140
- decl_span : const_decl. span . clone ( ) ,
141
- } ) ,
142
- ) ?;
143
-
144
- const_name
145
- }
146
- TraitItem :: Type ( decl_id) => {
147
- let type_decl = engines. pe ( ) . get_trait_type ( & decl_id) . as_ref ( ) . clone ( ) ;
148
- handler. emit_err ( CompileError :: AssociatedTypeNotSupportedInAbi {
149
- span : type_decl. span . clone ( ) ,
150
- } ) ;
151
-
152
- let type_decl = ty:: TyTraitType :: type_check ( handler, ctx. by_ref ( ) , type_decl) ?;
153
- let decl_ref = ctx. engines ( ) . de ( ) . insert ( type_decl. clone ( ) ) ;
154
- new_interface_surface. push ( ty:: TyTraitInterfaceItem :: Type ( decl_ref. clone ( ) ) ) ;
155
-
156
- type_decl. name
157
- }
158
- TraitItem :: Error ( _, _) => {
159
- continue ;
160
- }
161
- } ;
162
-
163
- if !ids. insert ( decl_name. clone ( ) ) {
164
- handler. emit_err ( CompileError :: MultipleDefinitionsOfName {
165
- name : decl_name. clone ( ) ,
166
- span : decl_name. span ( ) ,
167
- } ) ;
168
- }
169
- }
170
-
171
- // Type check the items.
172
- let mut new_items = vec ! [ ] ;
173
- for method_id in methods. into_iter ( ) {
174
- let method = engines. pe ( ) . get_function ( & method_id) ;
175
- let method = ty:: TyFunctionDecl :: type_check (
176
- handler,
177
- ctx. by_ref ( ) ,
178
- & method,
179
- false ,
180
- false ,
181
- Some ( self_type_param. type_id ) ,
182
- )
183
- . unwrap_or_else ( |_| ty:: TyFunctionDecl :: error ( & method) ) ;
184
- error_on_shadowing_superabi_method ( & method. name , & mut ctx) ;
185
- for param in & method. parameters {
186
- if param. is_reference || param. is_mutable {
187
- handler. emit_err ( CompileError :: RefMutableNotAllowedInContractAbi {
188
- param_name : param. name . clone ( ) ,
189
- span : param. name . span ( ) ,
190
- } ) ;
195
+ if !ids. insert ( method. name . clone ( ) ) {
196
+ handler. emit_err ( CompileError :: MultipleDefinitionsOfName {
197
+ name : method. name . clone ( ) ,
198
+ span : method. name . span ( ) ,
199
+ } ) ;
200
+ }
201
+ new_items. push ( TyTraitItem :: Fn ( ctx. engines . de ( ) . insert ( method) ) ) ;
191
202
}
192
- }
193
- if !ids. insert ( method. name . clone ( ) ) {
194
- handler. emit_err ( CompileError :: MultipleDefinitionsOfName {
195
- name : method. name . clone ( ) ,
196
- span : method. name . span ( ) ,
197
- } ) ;
198
- }
199
- new_items. push ( TyTraitItem :: Fn ( ctx. engines . de ( ) . insert ( method) ) ) ;
200
- }
201
203
202
- // Compared to regular traits, we do not insert recursively methods of ABI supertraits
203
- // into the interface surface, we do not want supertrait methods to be available to
204
- // the ABI user, only the contract methods can use supertrait methods
205
- let abi_decl = ty:: TyAbiDecl {
206
- interface_surface : new_interface_surface,
207
- supertraits,
208
- items : new_items,
209
- name,
210
- span,
211
- attributes,
212
- } ;
213
- Ok ( abi_decl)
204
+ // Compared to regular traits, we do not insert recursively methods of ABI supertraits
205
+ // into the interface surface, we do not want supertrait methods to be available to
206
+ // the ABI user, only the contract methods can use supertrait methods
207
+ let abi_decl = ty:: TyAbiDecl {
208
+ interface_surface : new_interface_surface,
209
+ supertraits,
210
+ items : new_items,
211
+ name,
212
+ span,
213
+ attributes,
214
+ } ;
215
+ Ok ( abi_decl)
216
+ } )
214
217
}
215
218
216
219
pub ( crate ) fn insert_interface_surface_and_items_into_namespace (
0 commit comments