From 1cf01c724351f3dd13bd45eb4e58d969434537eb Mon Sep 17 00:00:00 2001 From: Marcos Henrich Date: Fri, 3 Feb 2023 11:41:40 +0000 Subject: [PATCH] Changes struct and enum declarations to contain a call path. (#3900) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With these changes `TyStructDeclaration` and `TyEnumDeclaration` now have a call path instead of a name. This is required before changing the JSON ABI to use enum and struct types with call paths instead of only names which can be duplicate. This also fixes #418 by changing `Unifier` to compare `CallPath`'s instead of `Ident`'s for enums and structs. Closes https://github.com/FuelLabs/sway/issues/418 --------- Co-authored-by: João Matos --- forc-plugins/forc-doc/src/descriptor.rs | 4 +- sway-core/src/abi_generation/evm_json_abi.rs | 8 +-- sway-core/src/abi_generation/fuel_json_abi.rs | 8 +-- .../dead_code_analysis.rs | 51 +++++++++--------- sway-core/src/ir_generation/types.rs | 12 +++-- sway-core/src/language/call_path.rs | 16 ++++-- .../language/parsed/expression/scrutinee.rs | 4 +- .../language/ty/declaration/declaration.rs | 12 +++-- sway-core/src/language/ty/declaration/enum.rs | 18 ++++--- .../src/language/ty/declaration/struct.rs | 18 ++++--- .../ty/expression/expression_variant.rs | 2 +- .../ast_node/declaration/declaration.rs | 8 +-- .../ast_node/declaration/enum.rs | 7 ++- .../ast_node/declaration/struct.rs | 7 ++- .../match_expression/analysis/pattern.rs | 4 +- .../match_expression/typed/typed_scrutinee.rs | 7 ++- .../src/semantic_analysis/namespace/items.rs | 4 +- .../semantic_analysis/namespace/trait_map.rs | 12 ++--- .../to_parsed_lang/convert_parse_tree.rs | 2 +- sway-core/src/type_system/id.rs | 18 ++++--- sway-core/src/type_system/info.rs | 53 +++++++++++-------- sway-core/src/type_system/mod.rs | 8 +-- sway-core/src/type_system/substitute.rs | 8 +-- sway-core/src/type_system/unify.rs | 24 +++++---- sway-core/src/type_system/unify_check.rs | 8 +-- sway-lsp/src/core/token.rs | 8 +-- sway-lsp/src/traverse/dependency.rs | 4 +- sway-lsp/src/traverse/parsed_tree.rs | 2 +- sway-lsp/src/traverse/typed_tree.rs | 12 +++-- .../Forc.lock | 8 +++ .../Forc.toml | 9 ++++ .../json_abi_oracle.json | 16 ++++++ .../src/main.sw | 10 ++++ .../src/module0.sw | 5 ++ .../src/module1.sw | 5 ++ .../test.toml | 4 ++ .../Forc.lock | 8 +++ .../Forc.toml | 9 ++++ .../json_abi_oracle.json | 16 ++++++ .../src/main.sw | 10 ++++ .../src/module0.sw | 9 ++++ .../src/module1.sw | 9 ++++ .../test.toml | 4 ++ 43 files changed, 324 insertions(+), 147 deletions(-) create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/Forc.lock create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/Forc.toml create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/json_abi_oracle.json create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/src/main.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/src/module0.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/src/module1.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/test.toml create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/Forc.lock create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/Forc.toml create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/json_abi_oracle.json create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/src/main.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/src/module0.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/src/module1.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/test.toml diff --git a/forc-plugins/forc-doc/src/descriptor.rs b/forc-plugins/forc-doc/src/descriptor.rs index feca63f99ea..0f36ce457c1 100644 --- a/forc-plugins/forc-doc/src/descriptor.rs +++ b/forc-plugins/forc-doc/src/descriptor.rs @@ -49,7 +49,7 @@ impl Descriptor { if !document_private_items && struct_decl.visibility.is_private() { Ok(Descriptor::NonDocumentable) } else { - let item_name = struct_decl.name; + let item_name = struct_decl.call_path.suffix; let attrs_opt = (!struct_decl.attributes.is_empty()) .then(|| struct_decl.attributes.to_html_string()); let context = (!struct_decl.fields.is_empty()) @@ -81,7 +81,7 @@ impl Descriptor { if !document_private_items && enum_decl.visibility.is_private() { Ok(Descriptor::NonDocumentable) } else { - let item_name = enum_decl.name; + let item_name = enum_decl.call_path.suffix; let attrs_opt = (!enum_decl.attributes.is_empty()) .then(|| enum_decl.attributes.to_html_string()); let context = (!enum_decl.variants.is_empty()) diff --git a/sway-core/src/abi_generation/evm_json_abi.rs b/sway-core/src/abi_generation/evm_json_abi.rs index 3c073993062..84d7f890913 100644 --- a/sway-core/src/abi_generation/evm_json_abi.rs +++ b/sway-core/src/abi_generation/evm_json_abi.rs @@ -96,11 +96,11 @@ pub fn json_abi_str(type_info: &TypeInfo, type_engine: &TypeEngine) -> String { Numeric => "u64".into(), // u64 is the default Contract => "contract".into(), ErrorRecovery => "unknown due to error".into(), - Enum { name, .. } => { - format!("enum {name}") + Enum { call_path, .. } => { + format!("enum {}", call_path.suffix) } - Struct { name, .. } => { - format!("struct {name}") + Struct { call_path, .. } => { + format!("struct {}", call_path.suffix) } ContractCaller { abi_name, .. } => { format!("contract caller {abi_name}") diff --git a/sway-core/src/abi_generation/fuel_json_abi.rs b/sway-core/src/abi_generation/fuel_json_abi.rs index 5ac42c7e7c3..608dbae03ff 100644 --- a/sway-core/src/abi_generation/fuel_json_abi.rs +++ b/sway-core/src/abi_generation/fuel_json_abi.rs @@ -567,11 +567,11 @@ impl TypeInfo { Numeric => "u64".into(), // u64 is the default Contract => "contract".into(), ErrorRecovery => "unknown due to error".into(), - Enum { name, .. } => { - format!("enum {name}") + Enum { call_path, .. } => { + format!("enum {}", call_path.suffix) } - Struct { name, .. } => { - format!("struct {name}") + Struct { call_path, .. } => { + format!("struct {}", call_path.suffix) } ContractCaller { abi_name, .. } => { format!("contract caller {abi_name}") 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 b1cc67e40c4..9c40de5371b 100644 --- a/sway-core/src/control_flow_analysis/dead_code_analysis.rs +++ b/sway-core/src/control_flow_analysis/dead_code_analysis.rs @@ -454,7 +454,7 @@ fn connect_struct_declaration<'eng: 'cfg, 'cfg>( tree_type: &TreeType, ) { let ty::TyStructDeclaration { - name, + call_path, fields, visibility, .. @@ -478,9 +478,11 @@ fn connect_struct_declaration<'eng: 'cfg, 'cfg>( // Now, populate the struct namespace with the location of this struct as well as the indexes // of the field names - graph - .namespace - .insert_struct(name.as_str().to_string(), entry_node, field_nodes); + graph.namespace.insert_struct( + call_path.suffix.as_str().to_string(), + entry_node, + field_nodes, + ); } /// Implementations of traits are top-level things that are not conditional, so @@ -601,10 +603,10 @@ fn connect_abi_declaration( // be used outside of the contract. for fn_decl_id in decl.interface_surface.iter() { let fn_decl = decl_engine.get_trait_fn(fn_decl_id.clone(), &decl.span)?; - if let Some(TypeInfo::Struct { name, .. }) = + if let Some(TypeInfo::Struct { call_path, .. }) = get_struct_type_info_from_type_id(type_engine, fn_decl.return_type)? { - if let Some(ns) = graph.namespace.get_struct(&name).cloned() { + if let Some(ns) = graph.namespace.get_struct(&call_path.suffix).cloned() { for (_, field_ix) in ns.fields.iter() { graph.add_edge(ns.struct_decl_ix, *field_ix, "".into()); } @@ -683,7 +685,7 @@ fn connect_enum_declaration<'eng: 'cfg, 'cfg>( ) { graph .namespace - .insert_enum(enum_decl.name.clone(), entry_node); + .insert_enum(enum_decl.call_path.suffix.clone(), entry_node); // keep a mapping of each variant for variant in enum_decl.variants.iter() { @@ -696,7 +698,7 @@ fn connect_enum_declaration<'eng: 'cfg, 'cfg>( ); graph.namespace.insert_enum_variant( - enum_decl.name.clone(), + enum_decl.call_path.suffix.clone(), entry_node, variant.name.clone(), variant_index, @@ -770,21 +772,22 @@ fn connect_fn_params_struct_enums<'eng: 'cfg, 'cfg>( for fn_param in &fn_decl.parameters { let ty = type_engine.to_typeinfo(fn_param.type_id, &fn_param.type_span)?; match ty { - TypeInfo::Enum { name, .. } => { - let ty_index = match graph.namespace.find_enum(&name) { + TypeInfo::Enum { call_path, .. } => { + let ty_index = match graph.namespace.find_enum(&call_path.suffix) { Some(ix) => *ix, - None => { - graph.add_node(engines, format!("External enum {}", name.as_str()).into()) - } + None => graph.add_node( + engines, + format!("External enum {}", call_path.suffix.as_str()).into(), + ), }; graph.add_edge(fn_decl_entry_node, ty_index, "".into()); } - TypeInfo::Struct { name, .. } => { - let ty_index = match graph.namespace.find_struct_decl(name.as_str()) { + TypeInfo::Struct { call_path, .. } => { + let ty_index = match graph.namespace.find_struct_decl(call_path.suffix.as_str()) { Some(ix) => *ix, None => graph.add_node( engines, - format!("External struct {}", name.as_str()).into(), + format!("External struct {}", call_path.suffix.as_str()).into(), ), }; graph.add_edge(fn_decl_entry_node, ty_index, "".into()); @@ -835,7 +838,7 @@ fn get_trait_fn_node_index<'a>( let struct_decl = decl_engine.get_struct(decl, &expression_span)?; Ok(graph .namespace - .find_trait_method(&struct_decl.name.into(), &fn_decl.name)) + .find_trait_method(&struct_decl.call_path.suffix.into(), &fn_decl.name)) } ty::TyDeclaration::ImplTrait(decl) => { let impl_trait = decl_engine.get_impl_trait(decl, &expression_span)?; @@ -1158,14 +1161,14 @@ fn connect_expression<'eng: 'cfg, 'cfg>( assert!(matches!(resolved_type_of_parent, TypeInfo::Struct { .. })); let resolved_type_of_parent = match resolved_type_of_parent { - TypeInfo::Struct { name, .. } => name, + TypeInfo::Struct { call_path, .. } => call_path, _ => panic!("Called subfield on a non-struct"), }; let field_name = &field_to_access.name; // find the struct field index in the namespace let field_ix = match graph .namespace - .find_struct_field_idx(resolved_type_of_parent.as_str(), field_name.as_str()) + .find_struct_field_idx(resolved_type_of_parent.suffix.as_str(), field_name.as_str()) { Some(ix) => *ix, None => graph.add_node(engines, "external struct".into()), @@ -1563,16 +1566,16 @@ fn connect_enum_instantiation<'eng: 'cfg, 'cfg>( tree_type: &TreeType, options: NodeConnectionOptions, ) -> Result, CompileError> { - let enum_name = &enum_decl.name; + let enum_call_path = enum_decl.call_path.clone(); let (decl_ix, variant_index) = graph .namespace - .find_enum_variant_index(enum_name, variant_name) + .find_enum_variant_index(&enum_call_path.suffix, variant_name) .unwrap_or_else(|| { let node_idx = graph.add_node( engines, format!( "extern enum {}::{}", - enum_name.as_str(), + enum_call_path.suffix.as_str(), variant_name.as_str() ) .into(), @@ -1645,7 +1648,7 @@ fn construct_dead_code_warning_from_node( span, } => { let warning_span = match decl_engine.get_struct(decl_id.clone(), span) { - Ok(ty::TyStructDeclaration { name, .. }) => name.span(), + Ok(ty::TyStructDeclaration { call_path, .. }) => call_path.span(), Err(_) => span.clone(), }; CompileWarning { @@ -1658,7 +1661,7 @@ fn construct_dead_code_warning_from_node( span, } => { let warning_span = match decl_engine.get_enum(decl_id.clone(), span) { - Ok(ty::TyEnumDeclaration { name, .. }) => name.span(), + Ok(ty::TyEnumDeclaration { call_path, .. }) => call_path.span(), Err(_) => span.clone(), }; CompileWarning { diff --git a/sway-core/src/ir_generation/types.rs b/sway-core/src/ir_generation/types.rs index cd025c89107..7ccc2a422b0 100644 --- a/sway-core/src/ir_generation/types.rs +++ b/sway-core/src/ir_generation/types.rs @@ -79,10 +79,12 @@ pub(super) fn get_struct_name_field_index_and_type( .ok()?; match (ty_info, field_kind) { ( - TypeInfo::Struct { name, fields, .. }, + TypeInfo::Struct { + call_path, fields, .. + }, ty::ProjectionKind::StructField { name: field_name }, ) => Some(( - name.as_str().to_owned(), + call_path.suffix.as_str().to_owned(), fields .iter() .enumerate() @@ -149,7 +151,9 @@ pub(super) fn get_indices_for_struct_access( // Get the field index and also its type for the next iteration. match (ty_info, &field_kind) { ( - TypeInfo::Struct { name, fields, .. }, + TypeInfo::Struct { + call_path, fields, .. + }, ty::ProjectionKind::StructField { name: field_name }, ) => { let field_idx_and_type_opt = fields @@ -163,7 +167,7 @@ pub(super) fn get_indices_for_struct_access( format!( "Unknown field '{}' for struct {} in reassignment.", field_kind.pretty_print(), - name, + call_path, ), field_kind.span(), )); diff --git a/sway-core/src/language/call_path.rs b/sway-core/src/language/call_path.rs index 90adc6cb8b5..fb5eaca6559 100644 --- a/sway-core/src/language/call_path.rs +++ b/sway-core/src/language/call_path.rs @@ -1,4 +1,4 @@ -use std::fmt; +use std::{fmt, sync::Arc}; use crate::{Ident, Namespace}; @@ -45,13 +45,21 @@ impl Spanned for CallPath { if self.prefixes.is_empty() { self.suffix.span() } else { - let prefixes_spans = self + let mut prefixes_spans = self .prefixes .iter() .map(|x| x.span()) //LOC below should be removed when #21 goes in - .filter(|x| x.path() == self.suffix.span().path()); - Span::join(Span::join_all(prefixes_spans), self.suffix.span()) + .filter(|x| { + Arc::ptr_eq(x.src(), self.suffix.span().src()) + && x.path() == self.suffix.span().path() + }) + .peekable(); + if prefixes_spans.peek().is_some() { + Span::join(Span::join_all(prefixes_spans), self.suffix.span()) + } else { + self.suffix.span() + } } } } diff --git a/sway-core/src/language/parsed/expression/scrutinee.rs b/sway-core/src/language/parsed/expression/scrutinee.rs index deaeab2359e..564c90deac7 100644 --- a/sway-core/src/language/parsed/expression/scrutinee.rs +++ b/sway-core/src/language/parsed/expression/scrutinee.rs @@ -23,7 +23,7 @@ pub enum Scrutinee { span: Span, }, StructScrutinee { - struct_name: Ident, + struct_name: CallPath, fields: Vec, span: Span, }, @@ -131,7 +131,7 @@ impl Scrutinee { .. } => { let name = vec![TypeInfo::Custom { - name: struct_name.clone(), + name: struct_name.clone().suffix, type_arguments: None, }]; let fields = fields diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index 0023686db06..4e9dac06a1d 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -164,13 +164,15 @@ impl DisplayWithEngines for TyDeclaration { } TyDeclaration::StructDeclaration(decl_id) => { match decl_engine.get_struct(decl_id.clone(), &decl_id.span()) { - Ok(TyStructDeclaration { name, .. }) => name.as_str().into(), + Ok(TyStructDeclaration { call_path, .. }) => { + call_path.suffix.as_str().into() + } Err(_) => "unknown struct".into(), } } TyDeclaration::EnumDeclaration(decl_id) => { match decl_engine.get_enum(decl_id.clone(), &decl_id.span()) { - Ok(TyEnumDeclaration { name, .. }) => name.as_str().into(), + Ok(TyEnumDeclaration { call_path, .. }) => call_path.suffix.as_str().into(), Err(_) => "unknown enum".into(), } } @@ -281,13 +283,15 @@ impl GetDeclIdent for TyDeclaration { decl_engine .get_struct(decl.clone(), &decl.span()) .unwrap() - .name, + .call_path + .suffix, ), TyDeclaration::EnumDeclaration(decl) => Some( decl_engine .get_enum(decl.clone(), &decl.span()) .unwrap() - .name, + .call_path + .suffix, ), TyDeclaration::ImplTrait(decl) => Some( decl_engine diff --git a/sway-core/src/language/ty/declaration/enum.rs b/sway-core/src/language/ty/declaration/enum.rs index 87fce6d6c21..6c4d3c56c43 100644 --- a/sway-core/src/language/ty/declaration/enum.rs +++ b/sway-core/src/language/ty/declaration/enum.rs @@ -3,11 +3,17 @@ use std::hash::{Hash, Hasher}; use sway_error::error::CompileError; use sway_types::{Ident, Span, Spanned}; -use crate::{engine_threading::*, error::*, language::Visibility, transform, type_system::*}; +use crate::{ + engine_threading::*, + error::*, + language::{CallPath, Visibility}, + transform, + type_system::*, +}; #[derive(Clone, Debug)] pub struct TyEnumDeclaration { - pub name: Ident, + pub call_path: CallPath, pub type_parameters: Vec, pub attributes: transform::AttributesMap, pub variants: Vec, @@ -21,7 +27,7 @@ pub struct TyEnumDeclaration { impl EqWithEngines for TyEnumDeclaration {} impl PartialEqWithEngines for TyEnumDeclaration { fn eq(&self, other: &Self, engines: Engines<'_>) -> bool { - self.name == other.name + self.call_path.suffix == other.call_path.suffix && self.type_parameters.eq(&other.type_parameters, engines) && self.variants.eq(&other.variants, engines) && self.visibility == other.visibility @@ -57,7 +63,7 @@ impl CreateTypeId for TyEnumDeclaration { type_engine.insert( decl_engine, TypeInfo::Enum { - name: self.name.clone(), + call_path: self.call_path.clone(), variant_types: self.variants.clone(), type_parameters: self.type_parameters.clone(), }, @@ -77,7 +83,7 @@ impl MonomorphizeHelper for TyEnumDeclaration { } fn name(&self) -> &Ident { - &self.name + &self.call_path.suffix } } @@ -96,7 +102,7 @@ impl TyEnumDeclaration { Some(variant) => ok(variant, warnings, errors), None => { errors.push(CompileError::UnknownEnumVariant { - enum_name: self.name.clone(), + enum_name: self.call_path.suffix.clone(), variant_name: variant_name.clone(), span: variant_name.span(), }); diff --git a/sway-core/src/language/ty/declaration/struct.rs b/sway-core/src/language/ty/declaration/struct.rs index 446d0cad0ba..a33d9474ae6 100644 --- a/sway-core/src/language/ty/declaration/struct.rs +++ b/sway-core/src/language/ty/declaration/struct.rs @@ -3,11 +3,17 @@ use std::hash::{Hash, Hasher}; use sway_error::error::CompileError; use sway_types::{Ident, Span, Spanned}; -use crate::{engine_threading::*, error::*, language::Visibility, transform, type_system::*}; +use crate::{ + engine_threading::*, + error::*, + language::{CallPath, Visibility}, + transform, + type_system::*, +}; #[derive(Clone, Debug)] pub struct TyStructDeclaration { - pub name: Ident, + pub call_path: CallPath, pub fields: Vec, pub type_parameters: Vec, pub visibility: Visibility, @@ -21,7 +27,7 @@ pub struct TyStructDeclaration { impl EqWithEngines for TyStructDeclaration {} impl PartialEqWithEngines for TyStructDeclaration { fn eq(&self, other: &Self, engines: Engines<'_>) -> bool { - self.name == other.name + self.call_path.suffix == other.call_path.suffix && self.fields.eq(&other.fields, engines) && self.type_parameters.eq(&other.type_parameters, engines) && self.visibility == other.visibility @@ -57,7 +63,7 @@ impl CreateTypeId for TyStructDeclaration { type_engine.insert( decl_engine, TypeInfo::Struct { - name: self.name.clone(), + call_path: self.call_path.clone(), fields: self.fields.clone(), type_parameters: self.type_parameters.clone(), }, @@ -77,7 +83,7 @@ impl MonomorphizeHelper for TyStructDeclaration { } fn name(&self) -> &Ident { - &self.name + &self.call_path.suffix } } @@ -100,7 +106,7 @@ impl TyStructDeclaration { .collect::>() .join("\n"), field_name: field_to_access.clone(), - struct_name: self.name.clone(), + struct_name: self.call_path.suffix.clone(), span: field_to_access.span(), }); err(warnings, errors) diff --git a/sway-core/src/language/ty/expression/expression_variant.rs b/sway-core/src/language/ty/expression/expression_variant.rs index e4415332686..b2d9ff34985 100644 --- a/sway-core/src/language/ty/expression/expression_variant.rs +++ b/sway-core/src/language/ty/expression/expression_variant.rs @@ -774,7 +774,7 @@ impl DisplayWithEngines for TyExpressionVariant { } => { format!( "{}::{} enum instantiation (tag: {})", - enum_decl.name.as_str(), + enum_decl.call_path.suffix.as_str(), variant_name.as_str(), tag ) diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs index 8ed85bffa57..e0519a3f3ff 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs @@ -160,10 +160,10 @@ impl ty::TyDeclaration { warnings, errors ); - let name = enum_decl.name.clone(); + let call_path = enum_decl.call_path.clone(); let decl = ty::TyDeclaration::EnumDeclaration(decl_engine.insert(enum_decl)); check!( - ctx.namespace.insert_symbol(name, decl.clone()), + ctx.namespace.insert_symbol(call_path.suffix, decl.clone()), return err(warnings, errors), warnings, errors @@ -290,12 +290,12 @@ impl ty::TyDeclaration { warnings, errors ); - let name = decl.name.clone(); + let call_path = decl.call_path.clone(); let decl_id = decl_engine.insert(decl); let decl = ty::TyDeclaration::StructDeclaration(decl_id); // insert the struct decl into namespace check!( - ctx.namespace.insert_symbol(name, decl.clone()), + ctx.namespace.insert_symbol(call_path.suffix, decl.clone()), return err(warnings, errors), warnings, errors diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/enum.rs b/sway-core/src/semantic_analysis/ast_node/declaration/enum.rs index 53befa4d81b..b4e940dc2d6 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/enum.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/enum.rs @@ -2,7 +2,7 @@ use sway_error::error::CompileError; use crate::{ error::*, - language::{parsed::*, ty}, + language::{parsed::*, ty, CallPath}, semantic_analysis::*, type_system::*, }; @@ -55,9 +55,12 @@ impl ty::TyEnumDeclaration { )); } + let mut call_path: CallPath = name.into(); + call_path = call_path.to_fullpath(ctx.namespace); + // create the enum decl let decl = ty::TyEnumDeclaration { - name, + call_path, type_parameters: new_type_parameters, variants: variants_buf, span, diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/struct.rs b/sway-core/src/semantic_analysis/ast_node/declaration/struct.rs index 5c550d98283..0099dbc5baf 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/struct.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/struct.rs @@ -2,7 +2,7 @@ use sway_error::error::CompileError; use crate::{ error::*, - language::{parsed::*, ty}, + language::{parsed::*, ty, CallPath}, semantic_analysis::*, type_system::*, }; @@ -58,9 +58,12 @@ impl ty::TyStructDeclaration { )); } + let mut path: CallPath = name.into(); + path = path.to_fullpath(ctx.namespace); + // create the struct decl let decl = ty::TyStructDeclaration { - name, + call_path: path, type_parameters: new_type_parameters, fields: new_fields, visibility, diff --git a/sway-core/src/semantic_analysis/ast_node/expression/match_expression/analysis/pattern.rs b/sway-core/src/semantic_analysis/ast_node/expression/match_expression/analysis/pattern.rs index 08d0ab8f1e6..99446486a22 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/match_expression/analysis/pattern.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/match_expression/analysis/pattern.rs @@ -683,12 +683,12 @@ impl Pattern { .. }), TypeInfo::Enum { - name: r_enum_name, + call_path: r_enum_call_path, variant_types, .. }, ) => { - l_enum_name.as_str() == r_enum_name.as_str() + l_enum_name.as_str() == r_enum_call_path.suffix.as_str() && variant_types .iter() .map(|variant_type| variant_type.name.clone()) diff --git a/sway-core/src/semantic_analysis/ast_node/expression/match_expression/typed/typed_scrutinee.rs b/sway-core/src/semantic_analysis/ast_node/expression/match_expression/typed/typed_scrutinee.rs index faaa7da22d4..ff8697fb995 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/match_expression/typed/typed_scrutinee.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/match_expression/typed/typed_scrutinee.rs @@ -45,7 +45,7 @@ impl ty::TyScrutinee { struct_name, fields, span, - } => type_check_struct(ctx, struct_name, fields, span), + } => type_check_struct(ctx, struct_name.suffix, fields, span), Scrutinee::EnumScrutinee { call_path, value, @@ -201,7 +201,10 @@ fn type_check_struct( } let typed_scrutinee = ty::TyScrutinee { - variant: ty::TyScrutineeVariant::StructScrutinee(struct_decl.name.clone(), typed_fields), + variant: ty::TyScrutineeVariant::StructScrutinee( + struct_decl.call_path.suffix.clone(), + typed_fields, + ), type_id: struct_decl.create_type_id(ctx.engines()), span, }; diff --git a/sway-core/src/semantic_analysis/namespace/items.rs b/sway-core/src/semantic_analysis/namespace/items.rs index d1a95a1a4e3..9aa1e6e03be 100644 --- a/sway-core/src/semantic_analysis/namespace/items.rs +++ b/sway-core/src/semantic_analysis/namespace/items.rs @@ -245,7 +245,7 @@ impl Items { match (resolved_type, projection) { ( TypeInfo::Struct { - name: struct_name, + call_path: struct_name, fields, .. }, @@ -277,7 +277,7 @@ impl Items { errors.push(CompileError::FieldNotFound { field_name: field_name.clone(), - struct_name, + struct_name: struct_name.suffix, available_fields: available_fields.join(", "), span: field_name.span(), }); diff --git a/sway-core/src/semantic_analysis/namespace/trait_map.rs b/sway-core/src/semantic_analysis/namespace/trait_map.rs index d8dee601b65..d4035d2f404 100644 --- a/sway-core/src/semantic_analysis/namespace/trait_map.rs +++ b/sway-core/src/semantic_analysis/namespace/trait_map.rs @@ -856,17 +856,17 @@ pub(crate) fn are_equal_minus_dynamic_types( } ( TypeInfo::Enum { - name: l_name, + call_path: l_name, variant_types: l_variant_types, type_parameters: l_type_parameters, }, TypeInfo::Enum { - name: r_name, + call_path: r_name, variant_types: r_variant_types, type_parameters: r_type_parameters, }, ) => { - l_name == r_name + l_name.suffix == r_name.suffix && l_variant_types.iter().zip(r_variant_types.iter()).fold( true, |acc, (left, right)| { @@ -884,17 +884,17 @@ pub(crate) fn are_equal_minus_dynamic_types( } ( TypeInfo::Struct { - name: l_name, + call_path: l_name, fields: l_fields, type_parameters: l_type_parameters, }, TypeInfo::Struct { - name: r_name, + call_path: r_name, fields: r_fields, type_parameters: r_type_parameters, }, ) => { - l_name == r_name + l_name.suffix == r_name.suffix && l_fields .iter() .zip(r_fields.iter()) diff --git a/sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs b/sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs index 91130f0f7b2..170e9413363 100644 --- a/sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs +++ b/sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs @@ -3186,7 +3186,7 @@ fn pattern_to_scrutinee( .collect::>()?; Scrutinee::StructScrutinee { - struct_name: path_expr_to_ident(context, handler, path)?, + struct_name: path_expr_to_ident(context, handler, path)?.into(), fields: { scrutinee_fields }, span, } diff --git a/sway-core/src/type_system/id.rs b/sway-core/src/type_system/id.rs index 4518cea2a79..c3990e51fa9 100644 --- a/sway-core/src/type_system/id.rs +++ b/sway-core/src/type_system/id.rs @@ -83,7 +83,7 @@ impl ReplaceSelfType for TypeId { TypeInfo::Enum { type_parameters, variant_types, - name, + call_path, } => { let mut need_to_create_new = false; let variant_types = variant_types @@ -112,7 +112,7 @@ impl ReplaceSelfType for TypeId { TypeInfo::Enum { variant_types, type_parameters, - name, + call_path, }, )) } else { @@ -122,7 +122,7 @@ impl ReplaceSelfType for TypeId { TypeInfo::Struct { type_parameters, fields, - name, + call_path, } => { let mut need_to_create_new = false; let fields = fields @@ -150,7 +150,7 @@ impl ReplaceSelfType for TypeId { decl_engine, TypeInfo::Struct { fields, - name, + call_path, type_parameters, }, )) @@ -309,15 +309,17 @@ impl TypeId { ( TypeInfo::Custom { name, .. }, TypeInfo::Enum { - name: enum_name, .. + call_path: enum_call_path, + .. }, - ) => name != enum_name, + ) => name != enum_call_path.suffix, ( TypeInfo::Custom { name, .. }, TypeInfo::Struct { - name: struct_name, .. + call_path: struct_call_path, + .. }, - ) => name != struct_name, + ) => name != struct_call_path.suffix, (TypeInfo::Custom { .. }, _) => true, _ => false, } diff --git a/sway-core/src/type_system/info.rs b/sway-core/src/type_system/info.rs index d96e8c0e153..2c64612fdd1 100644 --- a/sway-core/src/type_system/info.rs +++ b/sway-core/src/type_system/info.rs @@ -92,12 +92,12 @@ pub enum TypeInfo { Str(Length), UnsignedInteger(IntegerBits), Enum { - name: Ident, + call_path: CallPath, type_parameters: Vec, variant_types: Vec, }, Struct { - name: Ident, + call_path: CallPath, type_parameters: Vec, fields: Vec, }, @@ -172,22 +172,22 @@ impl HashWithEngines for TypeInfo { state.write_u8(6); } TypeInfo::Enum { - name, + call_path, variant_types, type_parameters, } => { state.write_u8(7); - name.hash(state); + call_path.hash(state); variant_types.hash(state, type_engine); type_parameters.hash(state, type_engine); } TypeInfo::Struct { - name, + call_path, fields, type_parameters, } => { state.write_u8(8); - name.hash(state); + call_path.hash(state); fields.hash(state, type_engine); type_parameters.hash(state, type_engine); } @@ -291,12 +291,12 @@ impl PartialEqWithEngines for TypeInfo { (Self::UnsignedInteger(l), Self::UnsignedInteger(r)) => l == r, ( Self::Enum { - name: l_name, + call_path: l_name, variant_types: l_variant_types, type_parameters: l_type_parameters, }, Self::Enum { - name: r_name, + call_path: r_name, variant_types: r_variant_types, type_parameters: r_type_parameters, }, @@ -307,12 +307,12 @@ impl PartialEqWithEngines for TypeInfo { } ( Self::Struct { - name: l_name, + call_path: l_name, fields: l_fields, type_parameters: l_type_parameters, }, Self::Struct { - name: r_name, + call_path: r_name, fields: r_fields, type_parameters: r_type_parameters, }, @@ -394,21 +394,21 @@ impl DisplayWithEngines for TypeInfo { Contract => "contract".into(), ErrorRecovery => "unknown due to error".into(), Enum { - name, + call_path, type_parameters, .. } => print_inner_types( engines, - name.as_str().to_string(), + call_path.suffix.as_str().to_string(), type_parameters.iter().map(|x| x.type_id), ), Struct { - name, + call_path, type_parameters, .. } => print_inner_types( engines, - name.as_str().to_string(), + call_path.suffix.as_str().to_string(), type_parameters.iter().map(|x| x.type_id), ), ContractCaller { abi_name, address } => { @@ -1441,12 +1441,12 @@ impl TypeInfo { } ( Self::Enum { - name: l_name, + call_path: l_name, variant_types: l_variant_types, type_parameters: l_type_parameters, }, Self::Enum { - name: r_name, + call_path: r_name, variant_types: r_variant_types, type_parameters: r_type_parameters, }, @@ -1473,12 +1473,12 @@ impl TypeInfo { } ( Self::Struct { - name: l_name, + call_path: l_name, fields: l_fields, type_parameters: l_type_parameters, }, Self::Struct { - name: r_name, + call_path: r_name, fields: r_fields, type_parameters: r_type_parameters, }, @@ -1534,7 +1534,12 @@ impl TypeInfo { let type_engine = engines.te(); match (self, subfields.split_first()) { (TypeInfo::Struct { .. }, None) => err(warnings, errors), - (TypeInfo::Struct { name, fields, .. }, Some((first, rest))) => { + ( + TypeInfo::Struct { + call_path, fields, .. + }, + Some((first, rest)), + ) => { let field = match fields .iter() .find(|field| field.name.as_str() == first.as_str()) @@ -1546,7 +1551,7 @@ impl TypeInfo { fields.iter().map(|x| x.name.as_str()).collect::>(); errors.push(CompileError::FieldNotFound { field_name: first.clone(), - struct_name: name.clone(), + struct_name: call_path.suffix.clone(), available_fields: available_fields.join(", "), span: first.span(), }); @@ -1661,10 +1666,10 @@ impl TypeInfo { let errors = vec![]; match self { TypeInfo::Enum { - name, + call_path, variant_types, .. - } => ok((name, variant_types), warnings, errors), + } => ok((&call_path.suffix, variant_types), warnings, errors), TypeInfo::ErrorRecovery => err(warnings, errors), a => err( vec![], @@ -1690,7 +1695,9 @@ impl TypeInfo { let warnings = vec![]; let errors = vec![]; match self { - TypeInfo::Struct { name, fields, .. } => ok((name, fields), warnings, errors), + TypeInfo::Struct { + call_path, fields, .. + } => ok((&call_path.suffix, fields), warnings, errors), TypeInfo::ErrorRecovery => err(warnings, errors), a => err( vec![], diff --git a/sway-core/src/type_system/mod.rs b/sway-core/src/type_system/mod.rs index 7f3e50311a6..30ad0b4b6d7 100644 --- a/sway-core/src/type_system/mod.rs +++ b/sway-core/src/type_system/mod.rs @@ -88,7 +88,7 @@ fn generic_enum_resolution() { let ty_1 = type_engine.insert( &decl_engine, TypeInfo::Enum { - name: result_name.clone(), + call_path: result_name.clone().into(), variant_types, type_parameters: vec![placeholder_type_param], }, @@ -119,7 +119,7 @@ fn generic_enum_resolution() { let ty_2 = type_engine.insert( &decl_engine, TypeInfo::Enum { - name: result_name, + call_path: result_name.into(), variant_types, type_parameters: vec![type_param], }, @@ -130,12 +130,12 @@ fn generic_enum_resolution() { assert!(errors.is_empty()); if let TypeInfo::Enum { - name, + call_path: name, variant_types, .. } = type_engine.get(ty_1) { - assert_eq!(name.as_str(), "Result"); + assert_eq!(name.suffix.as_str(), "Result"); assert!(matches!( type_engine.get(variant_types[0].type_id), TypeInfo::Boolean diff --git a/sway-core/src/type_system/substitute.rs b/sway-core/src/type_system/substitute.rs index 9957312c656..da75c45c33e 100644 --- a/sway-core/src/type_system/substitute.rs +++ b/sway-core/src/type_system/substitute.rs @@ -321,7 +321,7 @@ impl TypeSubstMap { TypeInfo::Placeholder(_) => iter_for_match(engines, self, &type_info), TypeInfo::Struct { fields, - name, + call_path, type_parameters, } => { let mut need_to_create_new = false; @@ -350,7 +350,7 @@ impl TypeSubstMap { decl_engine, TypeInfo::Struct { fields, - name, + call_path, type_parameters, }, )) @@ -360,7 +360,7 @@ impl TypeSubstMap { } TypeInfo::Enum { variant_types, - name, + call_path, type_parameters, } => { let mut need_to_create_new = false; @@ -390,7 +390,7 @@ impl TypeSubstMap { TypeInfo::Enum { variant_types, type_parameters, - name, + call_path, }, )) } else { diff --git a/sway-core/src/type_system/unify.rs b/sway-core/src/type_system/unify.rs index df7ae87d034..6157bde1421 100644 --- a/sway-core/src/type_system/unify.rs +++ b/sway-core/src/type_system/unify.rs @@ -4,9 +4,13 @@ use sway_error::{ type_error::TypeError, warning::{CompileWarning, Warning}, }; -use sway_types::{integer_bits::IntegerBits, Ident, Span, Spanned}; +use sway_types::{integer_bits::IntegerBits, Span, Spanned}; -use crate::{engine_threading::*, language::ty, type_system::*}; +use crate::{ + engine_threading::*, + language::{ty, CallPath}, + type_system::*, +}; /// Helper struct to aid in type unification. pub(super) struct Unifier<'a> { @@ -107,12 +111,12 @@ impl<'a> Unifier<'a> { } ( Struct { - name: rn, + call_path: rn, type_parameters: rpts, fields: rfs, }, Struct { - name: en, + call_path: en, type_parameters: etps, fields: efs, }, @@ -126,12 +130,12 @@ impl<'a> Unifier<'a> { ) if rvs.is_empty() => (vec![], vec![]), ( Enum { - name: rn, + call_path: rn, type_parameters: rtps, variant_types: rvs, }, Enum { - name: en, + call_path: en, type_parameters: etps, variant_types: evs, }, @@ -335,8 +339,8 @@ impl<'a> Unifier<'a> { received: TypeId, expected: TypeId, span: &Span, - r: (Ident, Vec, Vec), - e: (Ident, Vec, Vec), + r: (CallPath, Vec, Vec), + e: (CallPath, Vec, Vec), ) -> (Vec, Vec) { let mut warnings = vec![]; let mut errors = vec![]; @@ -384,8 +388,8 @@ impl<'a> Unifier<'a> { received: TypeId, expected: TypeId, span: &Span, - r: (Ident, Vec, Vec), - e: (Ident, Vec, Vec), + r: (CallPath, Vec, Vec), + e: (CallPath, Vec, Vec), ) -> (Vec, Vec) { let mut warnings = vec![]; let mut errors = vec![]; diff --git a/sway-core/src/type_system/unify_check.rs b/sway-core/src/type_system/unify_check.rs index f3a0f8baf89..d0c59c86baa 100644 --- a/sway-core/src/type_system/unify_check.rs +++ b/sway-core/src/type_system/unify_check.rs @@ -178,12 +178,12 @@ impl<'a> UnifyCheck<'a> { ) if rvs.is_empty() => true, ( Enum { - name: l_name, + call_path: l_name, variant_types: l_variant_types, type_parameters: l_type_parameters, }, Enum { - name: r_name, + call_path: r_name, variant_types: r_variant_types, type_parameters: r_type_parameters, }, @@ -208,12 +208,12 @@ impl<'a> UnifyCheck<'a> { } ( Struct { - name: l_name, + call_path: l_name, fields: l_fields, type_parameters: l_type_parameters, }, Struct { - name: r_name, + call_path: r_name, fields: r_fields, type_parameters: r_type_parameters, }, diff --git a/sway-lsp/src/core/token.rs b/sway-lsp/src/core/token.rs index 80a0dd44df6..bbdd0b943cd 100644 --- a/sway-lsp/src/core/token.rs +++ b/sway-lsp/src/core/token.rs @@ -148,10 +148,10 @@ pub fn to_ident_key(ident: &Ident) -> (Ident, Span) { /// Use the [TypeId] to look up the associated [TypeInfo] and return the [Ident] if one is found. pub fn ident_of_type_id(type_engine: &TypeEngine, type_id: &TypeId) -> Option { match type_engine.get(*type_id) { - TypeInfo::UnknownGeneric { name, .. } - | TypeInfo::Enum { name, .. } - | TypeInfo::Struct { name, .. } - | TypeInfo::Custom { name, .. } => Some(name), + TypeInfo::UnknownGeneric { name, .. } | TypeInfo::Custom { name, .. } => Some(name), + TypeInfo::Enum { call_path, .. } | TypeInfo::Struct { call_path, .. } => { + Some(call_path.suffix) + } _ => None, } } diff --git a/sway-lsp/src/traverse/dependency.rs b/sway-lsp/src/traverse/dependency.rs index e332a4c50d5..4dd2a58ba11 100644 --- a/sway-lsp/src/traverse/dependency.rs +++ b/sway-lsp/src/traverse/dependency.rs @@ -52,7 +52,7 @@ impl<'a> Dependency<'a> { ty::TyDeclaration::VariableDeclaration(variable) => Ok(variable.name.clone()), ty::TyDeclaration::StructDeclaration(decl_id) => decl_engine .get_struct(decl_id.clone(), &declaration.span()) - .map(|decl| decl.name), + .map(|decl| decl.call_path.suffix), ty::TyDeclaration::TraitDeclaration(decl_id) => decl_engine .get_trait(decl_id.clone(), &declaration.span()) .map(|decl| decl.name), @@ -64,7 +64,7 @@ impl<'a> Dependency<'a> { .map(|decl| decl.name), ty::TyDeclaration::EnumDeclaration(decl_id) => decl_engine .get_enum(decl_id.clone(), &declaration.span()) - .map(|decl| decl.name), + .map(|decl| decl.call_path.suffix), _ => return, } { let ident = token::to_ident_key(&ident); diff --git a/sway-lsp/src/traverse/parsed_tree.rs b/sway-lsp/src/traverse/parsed_tree.rs index cc73e5d2b56..494deca6412 100644 --- a/sway-lsp/src/traverse/parsed_tree.rs +++ b/sway-lsp/src/traverse/parsed_tree.rs @@ -806,7 +806,7 @@ impl<'a> ParsedTree<'a> { } => { let token = Token::from_parsed(AstToken::Scrutinee(scrutinee.clone()), SymbolKind::Struct); - self.tokens.insert(to_ident_key(struct_name), token); + self.tokens.insert(to_ident_key(&struct_name.suffix), token); for field in fields { let token = Token::from_parsed( diff --git a/sway-lsp/src/traverse/typed_tree.rs b/sway-lsp/src/traverse/typed_tree.rs index 58c3e5ca83b..f55aa68bb96 100644 --- a/sway-lsp/src/traverse/typed_tree.rs +++ b/sway-lsp/src/traverse/typed_tree.rs @@ -120,11 +120,11 @@ impl<'a> TypedTree<'a> { { if let Some(mut token) = self .tokens - .try_get_mut(&to_ident_key(&struct_decl.name)) + .try_get_mut(&to_ident_key(&struct_decl.call_path.suffix)) .try_unwrap() { token.typed = Some(TypedAstToken::TypedDeclaration(declaration.clone())); - token.type_def = Some(TypeDefinition::Ident(struct_decl.name)); + token.type_def = Some(TypeDefinition::Ident(struct_decl.call_path.suffix)); } for field in &struct_decl.fields { @@ -148,11 +148,12 @@ impl<'a> TypedTree<'a> { if let Ok(enum_decl) = decl_engine.get_enum(decl_id.clone(), &decl_id.span()) { if let Some(mut token) = self .tokens - .try_get_mut(&to_ident_key(&enum_decl.name)) + .try_get_mut(&to_ident_key(&enum_decl.call_path.suffix)) .try_unwrap() { token.typed = Some(TypedAstToken::TypedDeclaration(declaration.clone())); - token.type_def = Some(TypeDefinition::Ident(enum_decl.name.clone())); + token.type_def = + Some(TypeDefinition::Ident(enum_decl.call_path.suffix.clone())); } for type_param in &enum_decl.type_parameters { @@ -504,7 +505,8 @@ impl<'a> TypedTree<'a> { .try_unwrap() { token.typed = Some(TypedAstToken::TypedExpression(expression.clone())); - token.type_def = Some(TypeDefinition::Ident(enum_decl.name.clone())); + token.type_def = + Some(TypeDefinition::Ident(enum_decl.call_path.suffix.clone())); } for type_arg in &type_binding.type_arguments { diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/Forc.lock new file mode 100644 index 00000000000..00a02b044a8 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/Forc.lock @@ -0,0 +1,8 @@ +[[package]] +name = 'core' +source = 'path+from-root-7683E8E5A9EFF30D' + +[[package]] +name = 'multiple_enums_with_the_same_name' +source = 'member' +dependencies = ['core'] diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/Forc.toml new file mode 100644 index 00000000000..ce7ef37644a --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/Forc.toml @@ -0,0 +1,9 @@ +[project] +authors = ["Fuel Labs "] +license = "Apache-2.0" +name = "multiple_enums_with_the_same_name" +entry = "main.sw" +implicit-std = false + +[dependencies] +core = { path = "../../../../../../sway-lib-core" } diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/json_abi_oracle.json b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/json_abi_oracle.json new file mode 100644 index 00000000000..07c6fb869a9 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/json_abi_oracle.json @@ -0,0 +1,16 @@ +[ + { + "attributes": null, + "inputs": [], + "name": "main", + "outputs": [ + { + "components": null, + "name": "", + "type": "u32", + "typeArguments": null + } + ], + "type": "function" + } +] \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/src/main.sw new file mode 100644 index 00000000000..c3e7053448a --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/src/main.sw @@ -0,0 +1,10 @@ +script; + +dep module0; +dep module1; + +fn main() { + let mut x = module0::MyEnum::A; + let y = module1::MyEnum::A; + x = y; +} diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/src/module0.sw b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/src/module0.sw new file mode 100644 index 00000000000..0753b4cf486 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/src/module0.sw @@ -0,0 +1,5 @@ +library module0; + +pub enum MyEnum { + A: () +} diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/src/module1.sw b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/src/module1.sw new file mode 100644 index 00000000000..5f4a80e0f79 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/src/module1.sw @@ -0,0 +1,5 @@ +library module1; + +pub enum MyEnum { + A: () +} \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/test.toml b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/test.toml new file mode 100644 index 00000000000..05cc5d38e5b --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_enums_with_the_same_name/test.toml @@ -0,0 +1,4 @@ +category = "fail" + +# check: $()x = y; +# nextln: $()Mismatched types. diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/Forc.lock new file mode 100644 index 00000000000..a2df315a0ab --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/Forc.lock @@ -0,0 +1,8 @@ +[[package]] +name = 'core' +source = 'path+from-root-1918441B04275B9A' + +[[package]] +name = 'multiple_structs_with_the_same_name' +source = 'member' +dependencies = ['core'] diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/Forc.toml new file mode 100644 index 00000000000..aa34123bf31 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/Forc.toml @@ -0,0 +1,9 @@ +[project] +authors = ["Fuel Labs "] +license = "Apache-2.0" +name = "multiple_structs_with_the_same_name" +entry = "main.sw" +implicit-std = false + +[dependencies] +core = { path = "../../../../../../sway-lib-core" } diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/json_abi_oracle.json b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/json_abi_oracle.json new file mode 100644 index 00000000000..07c6fb869a9 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/json_abi_oracle.json @@ -0,0 +1,16 @@ +[ + { + "attributes": null, + "inputs": [], + "name": "main", + "outputs": [ + { + "components": null, + "name": "", + "type": "u32", + "typeArguments": null + } + ], + "type": "function" + } +] \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/src/main.sw new file mode 100644 index 00000000000..c15615b55a6 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/src/main.sw @@ -0,0 +1,10 @@ +script; + +dep module0; +dep module1; + +fn main() { + let mut x = module0::Thing::new(); + let y = module1::Thing::new(); + x = y; +} diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/src/module0.sw b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/src/module0.sw new file mode 100644 index 00000000000..5b4b2c47e73 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/src/module0.sw @@ -0,0 +1,9 @@ +library module0; + +pub struct Thing {} + +impl Thing { + pub fn new() -> Self { + Thing {} + } +} diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/src/module1.sw b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/src/module1.sw new file mode 100644 index 00000000000..30a1299cb2b --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/src/module1.sw @@ -0,0 +1,9 @@ +library module1; + +pub struct Thing {} + +impl Thing { + pub fn new() -> Self { + Thing {} + } +} diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/test.toml b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/test.toml new file mode 100644 index 00000000000..05cc5d38e5b --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/multiple_structs_with_the_same_name/test.toml @@ -0,0 +1,4 @@ +category = "fail" + +# check: $()x = y; +# nextln: $()Mismatched types.