Skip to content

Commit

Permalink
Add missing unknown type annotation. Clean up some formatting.
Browse files Browse the repository at this point in the history
Also adds some resetting of the help text and type annotation (to
unknown) in many cases to better match the original behaviour. It's
difficult to tell which locations these values were originally used as
placeholders, and which locations they're a necessity for correctness.
This at least appears to fix an issue with expression return type
inference.
  • Loading branch information
mitchmindtree committed Jun 20, 2022
1 parent 3dd1474 commit e5d9533
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl TypedExpression {
};
let arguments = VecDeque::from(arguments);
let method = check!(
resolve_method_name(ctx, &method_name, arguments.clone(), vec![], span.clone(),),
resolve_method_name(ctx, &method_name, arguments.clone(), vec![], span.clone()),
return err(warnings, errors),
warnings,
errors
Expand Down Expand Up @@ -399,7 +399,7 @@ impl TypedExpression {
r#else,
span,
} => Self::type_check_if_expression(
ctx.by_ref(),
ctx.by_ref().with_help_text(""),
*condition,
*then,
r#else.map(|e| *e),
Expand All @@ -409,7 +409,12 @@ impl TypedExpression {
value,
branches,
span,
} => Self::type_check_match_expression(ctx.by_ref(), *value, branches, span),
} => Self::type_check_match_expression(
ctx.by_ref().with_help_text(""),
*value,
branches,
span,
),
Expression::AsmExpression { asm, span, .. } => {
Self::type_check_asm_expression(ctx.by_ref(), asm, span)
}
Expand Down Expand Up @@ -480,13 +485,15 @@ impl TypedExpression {
} => {
let ctx = ctx
.by_ref()
.with_type_annotation(insert_type(TypeInfo::Unknown));
.with_type_annotation(insert_type(TypeInfo::Unknown))
.with_help_text("");
Self::type_check_array_index(ctx, *prefix, *index, span)
}
Expression::StorageAccess { field_names, .. } => {
let ctx = ctx
.by_ref()
.with_type_annotation(insert_type(TypeInfo::Unknown));
.with_type_annotation(insert_type(TypeInfo::Unknown))
.with_help_text("");
Self::type_check_storage_load(ctx, field_names, &expr_span)
}
Expression::IntrinsicFunction { kind, span } => {
Expand Down Expand Up @@ -806,12 +813,15 @@ impl TypedExpression {
.collect::<Vec<_>>();

// type check the match expression and create a TypedMatchExpression object
let typed_match_expression = check!(
TypedMatchExpression::type_check(ctx.by_ref(), typed_value, branches, span.clone()),
return err(warnings, errors),
warnings,
errors
);
let typed_match_expression = {
let ctx = ctx.by_ref().with_help_text("");
check!(
TypedMatchExpression::type_check(ctx, typed_value, branches, span.clone()),
return err(warnings, errors),
warnings,
errors
)
};

// check to see if the match expression is exhaustive and if all match arms are reachable
let (witness_report, arms_reachability) = check!(
Expand Down Expand Up @@ -950,7 +960,7 @@ impl TypedExpression {

// monomorphize the struct definition
let mut struct_decl = check!(
ctx.monomorphize(struct_decl, type_arguments, EnforceTypeArguments::No, None,),
ctx.monomorphize(struct_decl, type_arguments, EnforceTypeArguments::No, None),
return err(warnings, errors),
warnings,
errors
Expand Down Expand Up @@ -1219,7 +1229,7 @@ impl TypedExpression {
return err(warnings, errors);
}
check!(
instantiate_enum(ctx, enum_decl, call_path.suffix, args, type_arguments,),
instantiate_enum(ctx, enum_decl, call_path.suffix, args, type_arguments),
return err(warnings, errors),
warnings,
errors
Expand All @@ -1241,7 +1251,7 @@ impl TypedExpression {
match decl {
TypedDeclaration::EnumDeclaration(enum_decl) => {
check!(
instantiate_enum(ctx, enum_decl, call_path.suffix, args, type_arguments,),
instantiate_enum(ctx, enum_decl, call_path.suffix, args, type_arguments),
return err(warnings, errors),
warnings,
errors
Expand Down Expand Up @@ -1458,13 +1468,10 @@ impl TypedExpression {

let elem_type = typed_contents[0].return_type;
for typed_elem in &typed_contents[1..] {
let (mut new_warnings, new_errors) = unify_with_self(
typed_elem.return_type,
elem_type,
ctx.self_type(),
&typed_elem.span,
"",
);
let (mut new_warnings, new_errors) = ctx
.by_ref()
.with_type_annotation(elem_type)
.unify_with_self(typed_elem.return_type, &typed_elem.span);
let no_warnings = new_warnings.is_empty();
let no_errors = new_errors.is_empty();
warnings.append(&mut new_warnings);
Expand Down Expand Up @@ -1676,8 +1683,8 @@ mod tests {

fn do_type_check(expr: Expression, type_annotation: TypeId) -> CompileResult<TypedExpression> {
let mut namespace = Namespace::init_root(namespace::Module::default());
let ctx = Context::from_module_namespace(&mut namespace)
.with_type_annotation(type_annotation);
let ctx =
Context::from_module_namespace(&mut namespace).with_type_annotation(type_annotation);
TypedExpression::type_check(ctx, expr)
}

Expand Down
13 changes: 11 additions & 2 deletions sway-core/src/semantic_analysis/ast_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ impl TypedAstNode {
decl
}
Declaration::Reassignment(Reassignment { lhs, rhs, span }) => {
let ctx = ctx
.with_type_annotation(insert_type(TypeInfo::Unknown))
.with_help_text("");
check!(
reassignment(ctx, lhs, rhs, span),
return err(warnings, errors),
Expand Down Expand Up @@ -462,6 +465,9 @@ impl TypedAstNode {
})
}
AstNodeContent::Expression(expr) => {
let ctx = ctx
.with_type_annotation(insert_type(TypeInfo::Unknown))
.with_help_text("");
let inner = check!(
TypedExpression::type_check(ctx, expr.clone()),
error_recovery_expr(expr.span()),
Expand Down Expand Up @@ -635,7 +641,7 @@ fn reassignment(
errors
);
// type check the reassignment
let ctx = ctx.with_type_annotation(ty_of_field);
let ctx = ctx.with_type_annotation(ty_of_field).with_help_text("");
let rhs = check!(
TypedExpression::type_check(ctx, rhs),
error_recovery_expr(span),
Expand All @@ -655,7 +661,10 @@ fn reassignment(
)
}
ReassignmentTarget::StorageField(fields) => {
{ reassign_storage_subfield(ctx, fields, rhs, span) }
let ctx = ctx
.with_type_annotation(insert_type(TypeInfo::Unknown))
.with_help_text("");
reassign_storage_subfield(ctx, fields, rhs, span)
.map(TypedDeclaration::StorageReassignment)
}
}
Expand Down

0 comments on commit e5d9533

Please sign in to comment.