Skip to content

Commit

Permalink
clippy and fmt issues
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj committed Aug 9, 2024
1 parent 93d5aa9 commit e0cf288
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ fn type_check_slice(
};

// start index argument
let start_ty_expr = if let Some(v) = arguments.get(1) {
let start_ty_expr = if arguments.get(1).is_some() {
let start_type = type_engine.insert(
engines,
TypeInfo::UnsignedInteger(IntegerBits::SixtyFour),
Expand All @@ -259,7 +259,7 @@ fn type_check_slice(
};

// end index argument
let end_ty_expr = if let Some(v) = arguments.get(2) {
let end_ty_expr = if arguments.get(2).is_some() {
let end_type = type_engine.insert(
engines,
TypeInfo::UnsignedInteger(IntegerBits::SixtyFour),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1876,85 +1876,6 @@ impl ty::TyExpression {
})
}

fn type_check_array_index(
handler: &Handler,
mut ctx: TypeCheckContext,
prefix: &Expression,
index: &Expression,
span: Span,
) -> Result<Self, ErrorEmitted> {
let type_engine = ctx.engines.te();
let engines = ctx.engines();

let mut current_prefix_te = Box::new({
let ctx = ctx
.by_ref()
.with_help_text("")
.with_type_annotation(type_engine.insert(engines, TypeInfo::Unknown, None));

ty::TyExpression::type_check(handler, ctx, prefix)?
});

let mut current_type = type_engine.get_unaliased(current_prefix_te.return_type);

let prefix_type_id = current_prefix_te.return_type;
let prefix_span = current_prefix_te.span.clone();

// Create the prefix part of the final array index expression.
// This might be an expression that directly evaluates to an array type,
// or an arbitrary number of dereferencing expressions where the last one
// dereference to an array type.
//
// We will either hit an array at the end or return an error, so the
// loop cannot be endless.
while !current_type.is_array() {
match &*current_type {
TypeInfo::Ref {
referenced_type, ..
} => {
let referenced_type_id = referenced_type.type_id;

current_prefix_te = Box::new(ty::TyExpression {
expression: ty::TyExpressionVariant::Deref(current_prefix_te),
return_type: referenced_type_id,
span: prefix_span.clone(),
});

current_type = type_engine.get_unaliased(referenced_type_id);
}
TypeInfo::ErrorRecovery(err) => return Err(*err),
_ => {
return Err(handler.emit_err(CompileError::NotIndexable {
actually: engines.help_out(prefix_type_id).to_string(),
span: prefix_span,
}))
}
};
}

let TypeInfo::Array(array_type_argument, _) = &*current_type else {
panic!("The current type must be an array.");
};

let index_te = {
let type_info_u64 = TypeInfo::UnsignedInteger(IntegerBits::SixtyFour);
let ctx = ctx
.with_help_text("Array index must be of type \"u64\".")
.with_type_annotation(type_engine.insert(engines, type_info_u64, None));

ty::TyExpression::type_check(handler, ctx, index)?
};

Ok(ty::TyExpression {
expression: ty::TyExpressionVariant::ArrayIndex {
prefix: current_prefix_te,
index: Box::new(index_te),
},
return_type: array_type_argument.type_id,
span,
})
}

fn type_check_intrinsic_function(
handler: &Handler,
ctx: TypeCheckContext,
Expand Down
45 changes: 22 additions & 23 deletions sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use sway_types::{
use sway_types::{Ident, Span, Spanned};

use std::{
collections::HashSet, convert::TryFrom, iter, mem::MaybeUninit, str::FromStr, sync::Arc
collections::HashSet, convert::TryFrom, iter, mem::MaybeUninit, str::FromStr, sync::Arc,
};

pub fn convert_parse_tree(
Expand Down Expand Up @@ -2183,7 +2183,7 @@ fn expr_to_expression(
kind: ExpressionKind::Deref(Box::new(expr)),
span,
}
},
}
Expr::MethodCall {
target,
path_seg,
Expand Down Expand Up @@ -2269,21 +2269,25 @@ fn expr_to_expression(
},
Expr::Ref {
mut_token, expr, ..
} => {
match *expr {
Expr::Index { target, arg } => {
let target = expr_to_expression(context, handler, engines, *target)?;
let arg = expr_to_expression(context, handler, engines, *arg.inner)?;
desugar_into_index_trait(mut_token.is_some(), arg.span(), span.clone(), target, arg)?
},
expr => Expression {
kind: ExpressionKind::Ref(RefExpression {
to_mutable_value: mut_token.is_some(),
value: Box::new(expr_to_expression(context, handler, engines, expr)?),
}),
span,
}
} => match *expr {
Expr::Index { target, arg } => {
let target = expr_to_expression(context, handler, engines, *target)?;
let arg = expr_to_expression(context, handler, engines, *arg.inner)?;
desugar_into_index_trait(
mut_token.is_some(),
arg.span(),
span.clone(),
target,
arg,
)?
}
expr => Expression {
kind: ExpressionKind::Ref(RefExpression {
to_mutable_value: mut_token.is_some(),
value: Box::new(expr_to_expression(context, handler, engines, expr)?),
}),
span,
},
},
Expr::Deref { expr, .. } => Expression {
kind: ExpressionKind::Deref(Box::new(expr_to_expression(
Expand Down Expand Up @@ -2540,11 +2544,7 @@ fn desugar_into_index_trait(
span: span.clone(),
};

let name = if mutable {
"index_mut"
} else {
"index"
};
let name = if mutable { "index_mut" } else { "index" };

let call_path_binding = TypeBinding {
inner: CallPath {
Expand Down Expand Up @@ -4504,8 +4504,7 @@ fn assignable_to_expression(
let target =
element_access_to_expression(context, handler, engines, *target, span.clone())?;
let arg = expr_to_expression(context, handler, engines, *arg.inner)?;
let call =
desugar_into_index_trait(true, arg.span(), span.clone(), target, arg)?;
let call = desugar_into_index_trait(true, arg.span(), span.clone(), target, arg)?;
Expression {
kind: ExpressionKind::Deref(Box::new(call)),
span,
Expand Down
31 changes: 13 additions & 18 deletions sway-lsp/src/traverse/parsed_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ use sway_core::{
decl_engine::parsed_id::ParsedDeclId,
language::{
parsed::{
AbiCastExpression, AbiDeclaration, AmbiguousPathExpression, ArrayExpression,
ArrayIndexExpression, AstNode, AstNodeContent, ConfigurableDeclaration,
ConstantDeclaration, Declaration, DelineatedPathExpression, EnumDeclaration,
EnumVariant, Expression, ExpressionKind, ForLoopExpression,
FunctionApplicationExpression, FunctionDeclaration, FunctionParameter, IfExpression,
ImplItem, ImplSelfOrTrait, ImportType, IncludeStatement, IntrinsicFunctionExpression,
LazyOperatorExpression, MatchExpression, MethodApplicationExpression, MethodName,
ParseModule, ParseProgram, ParseSubmodule, QualifiedPathType, ReassignmentExpression,
ReassignmentTarget, RefExpression, Scrutinee, StorageAccessExpression,
StorageDeclaration, StorageEntry, StorageField, StorageNamespace, StructDeclaration,
StructExpression, StructExpressionField, StructField, StructScrutineeField,
SubfieldExpression, Supertrait, TraitDeclaration, TraitFn, TraitItem,
TraitTypeDeclaration, TupleIndexExpression, TypeAliasDeclaration, UseStatement,
VariableDeclaration, WhileLoopExpression,
AbiCastExpression, AbiDeclaration, AmbiguousPathExpression, ArrayExpression, AstNode,
AstNodeContent, ConfigurableDeclaration, ConstantDeclaration, Declaration,
DelineatedPathExpression, EnumDeclaration, EnumVariant, Expression, ExpressionKind,
ForLoopExpression, FunctionApplicationExpression, FunctionDeclaration,
FunctionParameter, IfExpression, ImplItem, ImplSelfOrTrait, ImportType,
IncludeStatement, IntrinsicFunctionExpression, LazyOperatorExpression, MatchExpression,
MethodApplicationExpression, MethodName, ParseModule, ParseProgram, ParseSubmodule,
QualifiedPathType, ReassignmentExpression, ReassignmentTarget, RefExpression,
Scrutinee, StorageAccessExpression, StorageDeclaration, StorageEntry, StorageField,
StorageNamespace, StructDeclaration, StructExpression, StructExpressionField,
StructField, StructScrutineeField, SubfieldExpression, Supertrait, TraitDeclaration,
TraitFn, TraitItem, TraitTypeDeclaration, TupleIndexExpression, TypeAliasDeclaration,
UseStatement, VariableDeclaration, WhileLoopExpression,
},
CallPathTree, HasSubmodules, Literal,
},
Expand Down Expand Up @@ -308,10 +307,6 @@ impl Parse for Expression {
ExpressionKind::AbiCast(abi_cast_expression) => {
abi_cast_expression.parse(ctx);
}
ExpressionKind::ArrayIndex(ArrayIndexExpression { prefix, index, .. }) => {
prefix.parse(ctx);
index.parse(ctx);
}
ExpressionKind::StorageAccess(StorageAccessExpression {
field_names,
namespace_names,
Expand Down

0 comments on commit e0cf288

Please sign in to comment.