1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
mod function_parameter;
pub use function_parameter::*;
use sway_error::warning::{CompileWarning, Warning};
use crate::{
error::*,
language::{parsed::*, ty, Visibility},
semantic_analysis::*,
type_system::*,
};
use sway_types::{style::is_snake_case, Spanned};
impl ty::TyFunctionDeclaration {
pub fn type_check(
mut ctx: TypeCheckContext,
fn_decl: FunctionDeclaration,
is_method: bool,
is_in_impl_self: bool,
) -> CompileResult<Self> {
let mut warnings = Vec::new();
let mut errors = Vec::new();
let FunctionDeclaration {
name,
body,
parameters,
span,
attributes,
return_type,
type_parameters,
return_type_span,
visibility,
purity,
} = fn_decl;
if !is_snake_case(name.as_str()) {
warnings.push(CompileWarning {
span: name.span(),
warning_content: Warning::NonSnakeCaseFunctionName { name: name.clone() },
})
}
let mut fn_namespace = ctx.namespace.clone();
let mut fn_ctx = ctx.by_ref().scoped(&mut fn_namespace).with_purity(purity);
let mut new_type_parameters = vec![];
for type_parameter in type_parameters.into_iter() {
new_type_parameters.push(check!(
TypeParameter::type_check(fn_ctx.by_ref(), type_parameter),
continue,
warnings,
errors
));
}
if !errors.is_empty() {
return err(warnings, errors);
}
let mut new_parameters = vec![];
for parameter in parameters.into_iter() {
new_parameters.push(check!(
ty::TyFunctionParameter::type_check(fn_ctx.by_ref(), parameter, is_method),
continue,
warnings,
errors
));
}
if !errors.is_empty() {
return err(warnings, errors);
}
let initial_return_type = insert_type(return_type);
let return_type = check!(
fn_ctx.resolve_type_with_self(
initial_return_type,
&return_type_span,
EnforceTypeArguments::Yes,
None
),
insert_type(TypeInfo::ErrorRecovery),
warnings,
errors,
);
let (body, _implicit_block_return) = {
let fn_ctx = fn_ctx
.by_ref()
.with_purity(purity)
.with_help_text("Function body's return type does not match up with its return type annotation.")
.with_type_annotation(return_type);
check!(
ty::TyCodeBlock::type_check(fn_ctx, body),
(
ty::TyCodeBlock { contents: vec![] },
insert_type(TypeInfo::ErrorRecovery)
),
warnings,
errors
)
};
let return_statements: Vec<&ty::TyExpression> = body
.contents
.iter()
.flat_map(|node| node.gather_return_statements())
.collect();
for stmt in return_statements {
append!(
fn_ctx
.by_ref()
.with_type_annotation(return_type)
.with_help_text(
"Return statement must return the declared function return type."
)
.unify_with_self(stmt.return_type, &stmt.span),
warnings,
errors
);
}
let (visibility, is_contract_call) = if is_method {
if is_in_impl_self {
(visibility, false)
} else {
(Visibility::Public, false)
}
} else {
(visibility, fn_ctx.mode() == Mode::ImplAbiFn)
};
let function_decl = ty::TyFunctionDeclaration {
name,
body,
parameters: new_parameters,
span,
attributes,
return_type,
initial_return_type,
type_parameters: new_type_parameters,
return_type_span,
visibility,
is_contract_call,
purity,
};
let mut return_type_namespace = fn_ctx
.namespace
.implemented_traits
.filter_by_type(function_decl.return_type);
for type_param in function_decl.type_parameters.iter() {
return_type_namespace.filter_against_type(type_param.type_id);
}
ctx.namespace
.implemented_traits
.extend(return_type_namespace);
ok(function_decl, warnings, errors)
}
}
#[test]
fn test_function_selector_behavior() {
use crate::language::Visibility;
use sway_types::{integer_bits::IntegerBits, Ident, Span};
let decl = ty::TyFunctionDeclaration {
purity: Default::default(),
name: Ident::new_no_span("foo"),
body: ty::TyCodeBlock { contents: vec![] },
parameters: vec![],
span: Span::dummy(),
attributes: Default::default(),
return_type: 0.into(),
initial_return_type: 0.into(),
type_parameters: vec![],
return_type_span: Span::dummy(),
visibility: Visibility::Public,
is_contract_call: false,
};
let selector_text = match decl.to_selector_name().value {
Some(value) => value,
_ => panic!("test failure"),
};
assert_eq!(selector_text, "foo()".to_string());
let decl = ty::TyFunctionDeclaration {
purity: Default::default(),
name: Ident::new_with_override("bar", Span::dummy()),
body: ty::TyCodeBlock { contents: vec![] },
parameters: vec![
ty::TyFunctionParameter {
name: Ident::new_no_span("foo"),
is_reference: false,
is_mutable: false,
mutability_span: Span::dummy(),
type_id: crate::type_system::insert_type(TypeInfo::Str(5)),
initial_type_id: crate::type_system::insert_type(TypeInfo::Str(5)),
type_span: Span::dummy(),
},
ty::TyFunctionParameter {
name: Ident::new_no_span("baz"),
is_reference: false,
is_mutable: false,
mutability_span: Span::dummy(),
type_id: insert_type(TypeInfo::UnsignedInteger(IntegerBits::ThirtyTwo)),
initial_type_id: crate::type_system::insert_type(TypeInfo::Str(5)),
type_span: Span::dummy(),
},
],
span: Span::dummy(),
attributes: Default::default(),
return_type: 0.into(),
initial_return_type: 0.into(),
type_parameters: vec![],
return_type_span: Span::dummy(),
visibility: Visibility::Public,
is_contract_call: false,
};
let selector_text = match decl.to_selector_name().value {
Some(value) => value,
_ => panic!("test failure"),
};
assert_eq!(selector_text, "bar(str[5],u32)".to_string());
}