diff --git a/sway-core/src/control_flow_analysis/dead_code_analysis.rs b/sway-core/src/control_flow_analysis/dead_code_analysis.rs index 8d7c379bfab..024a1b58902 100644 --- a/sway-core/src/control_flow_analysis/dead_code_analysis.rs +++ b/sway-core/src/control_flow_analysis/dead_code_analysis.rs @@ -821,6 +821,7 @@ fn connect_expression( StructExpression { struct_name, fields, + .. } => { let decl = match graph.namespace.find_struct_decl(struct_name.as_str()) { Some(ix) => *ix, diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs index 8da53ddcea8..e7023864252 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs @@ -954,7 +954,7 @@ impl TypedExpression { // extract the struct name and fields from the type info let type_info = look_up_type_id(type_id); - let struct_fields = check!( + let (struct_name, struct_fields) = check!( type_info.expect_struct(&span), return err(warnings, errors), warnings, @@ -962,8 +962,6 @@ impl TypedExpression { ); let mut struct_fields = struct_fields.clone(); - let struct_name = Ident::new(call_path_binding.inner.suffix.1.clone()); - // match up the names with their type annotations from the declaration let mut typed_fields_buf = vec![]; for def_field in struct_fields.iter_mut() { @@ -1021,8 +1019,9 @@ impl TypedExpression { } let exp = TypedExpression { expression: TypedExpressionVariant::StructExpression { - struct_name, + struct_name: struct_name.clone(), fields: typed_fields_buf, + span: call_path_binding.inner.suffix.1.clone(), }, return_type: type_id, is_constant: IsConstant::No, diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression_variant.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression_variant.rs index 9cf8bc7d4be..e2bc6371a52 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression_variant.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression_variant.rs @@ -49,6 +49,7 @@ pub enum TypedExpressionVariant { StructExpression { struct_name: Ident, fields: Vec, + span: Span, }, CodeBlock(TypedCodeBlock), // a flag that this value will later be provided as a parameter, but is currently unknown @@ -177,12 +178,18 @@ impl PartialEq for TypedExpressionVariant { Self::StructExpression { struct_name: l_struct_name, fields: l_fields, + span: l_span, }, Self::StructExpression { struct_name: r_struct_name, fields: r_fields, + span: r_span, }, - ) => l_struct_name == r_struct_name && l_fields.clone() == r_fields.clone(), + ) => { + l_struct_name == r_struct_name + && l_fields.clone() == r_fields.clone() + && l_span == r_span + } (Self::CodeBlock(l0), Self::CodeBlock(r0)) => l0 == r0, ( Self::IfExp { diff --git a/sway-core/src/type_engine/type_info.rs b/sway-core/src/type_engine/type_info.rs index eed72f554f9..889c83f996e 100644 --- a/sway-core/src/type_engine/type_info.rs +++ b/sway-core/src/type_engine/type_info.rs @@ -1276,11 +1276,14 @@ impl TypeInfo { /// and return its contents. /// /// Returns an error if `self` is not a `TypeInfo::Struct`. - pub(crate) fn expect_struct(&self, debug_span: &Span) -> CompileResult<&Vec> { + pub(crate) fn expect_struct( + &self, + debug_span: &Span, + ) -> CompileResult<(&Ident, &Vec)> { let warnings = vec![]; let errors = vec![]; match self { - TypeInfo::Struct { fields, .. } => ok(fields, warnings, errors), + TypeInfo::Struct { name, fields, .. } => ok((name, fields), warnings, errors), TypeInfo::ErrorRecovery => err(warnings, errors), a => err( vec![], diff --git a/sway-lsp/src/core/traverse_typed_tree.rs b/sway-lsp/src/core/traverse_typed_tree.rs index 3487fac24cf..47bd769de03 100644 --- a/sway-lsp/src/core/traverse_typed_tree.rs +++ b/sway-lsp/src/core/traverse_typed_tree.rs @@ -230,11 +230,8 @@ fn handle_expression(expression: &TypedExpression, tokens: &TokenMap) { handle_expression(prefix, tokens); handle_expression(index, tokens); } - TypedExpressionVariant::StructExpression { - ref struct_name, - ref fields, - } => { - if let Some(mut token) = tokens.get_mut(&to_ident_key(struct_name)) { + TypedExpressionVariant::StructExpression { fields, span, .. } => { + if let Some(mut token) = tokens.get_mut(&to_ident_key(&Ident::new(span.clone()))) { token.typed = Some(TypedAstToken::TypedExpression(expression.clone())); }