Skip to content

Commit 85d9abe

Browse files
Reduce use of to_typeinfo(..).unwrap() (#2910)
1 parent ce6260c commit 85d9abe

File tree

3 files changed

+107
-19
lines changed

3 files changed

+107
-19
lines changed

sway-core/src/ir_generation/function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ impl FnCompiler {
546546
let val_exp = arguments[1].clone();
547547
// Validate that the val_exp is of the right type. We couldn't do it
548548
// earlier during type checking as the type arguments may not have been resolved.
549-
let val_ty = to_typeinfo(val_exp.return_type, &span).unwrap();
549+
let val_ty = to_typeinfo(val_exp.return_type, &span)?;
550550
if !val_ty.is_copy_type() {
551551
return Err(CompileError::IntrinsicUnsupportedArgType {
552552
name: kind.to_string(),
@@ -569,7 +569,7 @@ impl FnCompiler {
569569
let val_exp = arguments[1].clone();
570570
// Validate that the val_exp is of the right type. We couldn't do it
571571
// earlier during type checking as the type arguments may not have been resolved.
572-
let val_ty = to_typeinfo(val_exp.return_type, &span).unwrap();
572+
let val_ty = to_typeinfo(val_exp.return_type, &span)?;
573573
if val_ty != TypeInfo::UnsignedInteger(IntegerBits::SixtyFour) {
574574
return Err(CompileError::IntrinsicUnsupportedArgType {
575575
name: kind.to_string(),

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

Lines changed: 96 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,15 @@ impl TypedIntrinsicFunctionKind {
142142
return err(warnings, errors);
143143
}
144144
let targ = type_arguments[0].clone();
145-
let initial_type_id = insert_type(to_typeinfo(targ.type_id, &targ.span).unwrap());
145+
let initial_type_info = check!(
146+
CompileResult::from(
147+
to_typeinfo(targ.type_id, &targ.span).map_err(CompileError::from)
148+
),
149+
TypeInfo::ErrorRecovery,
150+
warnings,
151+
errors
152+
);
153+
let initial_type_id = insert_type(initial_type_info);
146154
let type_id = check!(
147155
ctx.resolve_type_with_self(
148156
initial_type_id,
@@ -177,7 +185,15 @@ impl TypedIntrinsicFunctionKind {
177185
return err(warnings, errors);
178186
}
179187
let targ = type_arguments[0].clone();
180-
let initial_type_id = insert_type(to_typeinfo(targ.type_id, &targ.span).unwrap());
188+
let initial_type_info = check!(
189+
CompileResult::from(
190+
to_typeinfo(targ.type_id, &targ.span).map_err(CompileError::from)
191+
),
192+
TypeInfo::ErrorRecovery,
193+
warnings,
194+
errors
195+
);
196+
let initial_type_id = insert_type(initial_type_info);
181197
let type_id = check!(
182198
ctx.resolve_type_with_self(
183199
initial_type_id,
@@ -232,7 +248,14 @@ impl TypedIntrinsicFunctionKind {
232248
);
233249

234250
// Check for supported argument types
235-
let arg_ty = to_typeinfo(lhs.return_type, &lhs.span).unwrap();
251+
let arg_ty = check!(
252+
CompileResult::from(
253+
to_typeinfo(lhs.return_type, &lhs.span).map_err(CompileError::from)
254+
),
255+
TypeInfo::ErrorRecovery,
256+
warnings,
257+
errors
258+
);
236259
let is_valid_arg_ty = matches!(arg_ty, TypeInfo::UnsignedInteger(_))
237260
|| matches!(arg_ty, TypeInfo::Boolean);
238261
if !is_valid_arg_ty {
@@ -307,8 +330,16 @@ impl TypedIntrinsicFunctionKind {
307330
);
308331

309332
// Make sure that the index argument is a `u64`
333+
let index_type_info = check!(
334+
CompileResult::from(
335+
to_typeinfo(index.return_type, &index.span).map_err(CompileError::from)
336+
),
337+
TypeInfo::ErrorRecovery,
338+
warnings,
339+
errors
340+
);
310341
if !matches!(
311-
to_typeinfo(index.return_type, &index.span).unwrap(),
342+
index_type_info,
312343
TypeInfo::UnsignedInteger(IntegerBits::SixtyFour)
313344
) {
314345
errors.push(CompileError::IntrinsicUnsupportedArgType {
@@ -319,8 +350,17 @@ impl TypedIntrinsicFunctionKind {
319350
}
320351

321352
// Make sure that the tx field ID is a `u64`
353+
let tx_field_type_info = check!(
354+
CompileResult::from(
355+
to_typeinfo(tx_field_id.return_type, &tx_field_id.span)
356+
.map_err(CompileError::from)
357+
),
358+
TypeInfo::ErrorRecovery,
359+
warnings,
360+
errors
361+
);
322362
if !matches!(
323-
to_typeinfo(tx_field_id.return_type, &tx_field_id.span).unwrap(),
363+
tx_field_type_info,
324364
TypeInfo::UnsignedInteger(IntegerBits::SixtyFour)
325365
) {
326366
errors.push(CompileError::IntrinsicUnsupportedArgType {
@@ -331,7 +371,15 @@ impl TypedIntrinsicFunctionKind {
331371
}
332372

333373
let targ = type_arguments[0].clone();
334-
let initial_type_id = insert_type(to_typeinfo(targ.type_id, &targ.span).unwrap());
374+
let initial_type_info = check!(
375+
CompileResult::from(
376+
to_typeinfo(targ.type_id, &targ.span).map_err(CompileError::from)
377+
),
378+
TypeInfo::ErrorRecovery,
379+
warnings,
380+
errors
381+
);
382+
let initial_type_id = insert_type(initial_type_info);
335383
let type_id = check!(
336384
ctx.resolve_type_with_self(
337385
initial_type_id,
@@ -376,8 +424,15 @@ impl TypedIntrinsicFunctionKind {
376424
warnings,
377425
errors
378426
);
379-
let copy_ty = to_typeinfo(exp.return_type, &span).unwrap().is_copy_type();
380-
if copy_ty {
427+
let copy_type_info = check!(
428+
CompileResult::from(
429+
to_typeinfo(exp.return_type, &span).map_err(CompileError::from)
430+
),
431+
TypeInfo::ErrorRecovery,
432+
warnings,
433+
errors
434+
);
435+
if copy_type_info.is_copy_type() {
381436
errors.push(CompileError::IntrinsicUnsupportedArgType {
382437
name: kind.to_string(),
383438
span,
@@ -415,7 +470,14 @@ impl TypedIntrinsicFunctionKind {
415470
warnings,
416471
errors
417472
);
418-
let key_ty = to_typeinfo(exp.return_type, &span).unwrap();
473+
let key_ty = check!(
474+
CompileResult::from(
475+
to_typeinfo(exp.return_type, &span).map_err(CompileError::from)
476+
),
477+
TypeInfo::ErrorRecovery,
478+
warnings,
479+
errors
480+
);
419481
if key_ty != TypeInfo::B256 {
420482
errors.push(CompileError::IntrinsicUnsupportedArgType {
421483
name: kind.to_string(),
@@ -461,7 +523,14 @@ impl TypedIntrinsicFunctionKind {
461523
warnings,
462524
errors
463525
);
464-
let key_ty = to_typeinfo(key_exp.return_type, &span).unwrap();
526+
let key_ty = check!(
527+
CompileResult::from(
528+
to_typeinfo(key_exp.return_type, &span).map_err(CompileError::from)
529+
),
530+
TypeInfo::ErrorRecovery,
531+
warnings,
532+
errors
533+
);
465534
if key_ty != TypeInfo::B256 {
466535
errors.push(CompileError::IntrinsicUnsupportedArgType {
467536
name: kind.to_string(),
@@ -485,8 +554,15 @@ impl TypedIntrinsicFunctionKind {
485554
let mut ctx = ctx
486555
.with_help_text("")
487556
.with_type_annotation(insert_type(TypeInfo::Unknown));
488-
let initial_type_id =
489-
insert_type(to_typeinfo(targ.type_id, &targ.span).unwrap());
557+
let initial_type_info = check!(
558+
CompileResult::from(
559+
to_typeinfo(targ.type_id, &targ.span).map_err(CompileError::from)
560+
),
561+
TypeInfo::ErrorRecovery,
562+
warnings,
563+
errors
564+
);
565+
let initial_type_id = insert_type(initial_type_info);
490566
let type_id = check!(
491567
ctx.resolve_type_with_self(
492568
initial_type_id,
@@ -572,7 +648,14 @@ impl TypedIntrinsicFunctionKind {
572648
);
573649

574650
// Check for supported argument types
575-
let arg_ty = to_typeinfo(lhs.return_type, &lhs.span).unwrap();
651+
let arg_ty = check!(
652+
CompileResult::from(
653+
to_typeinfo(lhs.return_type, &lhs.span).map_err(CompileError::from)
654+
),
655+
TypeInfo::ErrorRecovery,
656+
warnings,
657+
errors
658+
);
576659
let is_valid_arg_ty = matches!(arg_ty, TypeInfo::UnsignedInteger(_));
577660
if !is_valid_arg_ty {
578661
errors.push(CompileError::IntrinsicUnsupportedArgType {

sway-core/src/semantic_analysis/storage_only_types.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::declaration_engine::declaration_engine::*;
44
use crate::error::err;
55
use crate::type_system::{is_type_info_storage_only, to_typeinfo, TypeId};
66
use crate::{error::ok, semantic_analysis, CompileError, CompileResult, CompileWarning};
7-
use crate::{TypedDeclaration, TypedFunctionDeclaration};
7+
use crate::{TypeInfo, TypedDeclaration, TypedFunctionDeclaration};
88

99
use crate::semantic_analysis::{
1010
TypedAstNodeContent, TypedConstantDeclaration, TypedExpression, TypedExpressionVariant,
@@ -144,15 +144,20 @@ fn check_type(ty: TypeId, span: Span, ignore_self: bool) -> CompileResult<()> {
144144
let mut warnings: Vec<CompileWarning> = vec![];
145145
let mut errors: Vec<CompileError> = vec![];
146146

147-
let ti = to_typeinfo(ty, &span).unwrap();
147+
let type_info = check!(
148+
CompileResult::from(to_typeinfo(ty, &span).map_err(CompileError::from)),
149+
TypeInfo::ErrorRecovery,
150+
warnings,
151+
errors
152+
);
148153
let nested_types = check!(
149-
ti.clone().extract_nested_types(&span),
154+
type_info.clone().extract_nested_types(&span),
150155
vec![],
151156
warnings,
152157
errors
153158
);
154159
for ty in nested_types {
155-
if ignore_self && ty == ti {
160+
if ignore_self && ty == type_info {
156161
continue;
157162
}
158163
if is_type_info_storage_only(&ty) {

0 commit comments

Comments
 (0)