diff --git a/sway-core/src/control_flow_analysis/analyze_return_paths.rs b/sway-core/src/control_flow_analysis/analyze_return_paths.rs index 44e68cb792a..85c2f0c3878 100644 --- a/sway-core/src/control_flow_analysis/analyze_return_paths.rs +++ b/sway-core/src/control_flow_analysis/analyze_return_paths.rs @@ -194,12 +194,12 @@ fn connect_node( fn connect_declaration( node: &TyAstNode, - decl: &TyDeclaration, + decl: &ty::TyDeclaration, graph: &mut ControlFlowGraph, span: Span, leaves: &[NodeIndex], ) -> Result, CompileError> { - use TyDeclaration::*; + use ty::TyDeclaration::*; match decl { TraitDeclaration(_) | AbiDeclaration(_) 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 1a93f34b116..c0128df2075 100644 --- a/sway-core/src/control_flow_analysis/dead_code_analysis.rs +++ b/sway-core/src/control_flow_analysis/dead_code_analysis.rs @@ -4,7 +4,7 @@ use crate::{ language::{parsed::TreeType, ty, CallPath, Visibility}, semantic_analysis::{ ast_node::{ - TyAbiDeclaration, TyCodeBlock, TyConstantDeclaration, TyDeclaration, TyEnumDeclaration, + TyAbiDeclaration, TyCodeBlock, TyConstantDeclaration, TyEnumDeclaration, TyFunctionDeclaration, TyStructDeclaration, TyStructExpressionField, TyTraitDeclaration, TyVariableDeclaration, VariableMutability, }, @@ -129,7 +129,7 @@ impl ControlFlowGraph { ControlFlowGraphNode::ProgramNode(TyAstNode { span, content: - TyAstNodeContent::Declaration(TyDeclaration::FunctionDeclaration( + TyAstNodeContent::Declaration(ty::TyDeclaration::FunctionDeclaration( decl_id, )), .. @@ -152,7 +152,7 @@ impl ControlFlowGraph { ControlFlowGraphNode::OrganizationalDominator(_) => false, ControlFlowGraphNode::ProgramNode(TyAstNode { content: - TyAstNodeContent::Declaration(TyDeclaration::FunctionDeclaration( + TyAstNodeContent::Declaration(ty::TyDeclaration::FunctionDeclaration( decl_id, )), .. @@ -162,26 +162,31 @@ impl ControlFlowGraph { } ControlFlowGraphNode::ProgramNode(TyAstNode { content: - TyAstNodeContent::Declaration(TyDeclaration::TraitDeclaration(decl_id)), + TyAstNodeContent::Declaration(ty::TyDeclaration::TraitDeclaration( + decl_id, + )), .. }) => de_get_trait(decl_id.clone(), &decl_id.span())? .visibility .is_public(), ControlFlowGraphNode::ProgramNode(TyAstNode { content: - TyAstNodeContent::Declaration(TyDeclaration::StructDeclaration(decl_id)), + TyAstNodeContent::Declaration(ty::TyDeclaration::StructDeclaration( + decl_id, + )), .. }) => { let struct_decl = de_get_struct(decl_id.clone(), &decl_id.span())?; struct_decl.visibility == Visibility::Public } ControlFlowGraphNode::ProgramNode(TyAstNode { - content: TyAstNodeContent::Declaration(TyDeclaration::ImplTrait { .. }), + content: + TyAstNodeContent::Declaration(ty::TyDeclaration::ImplTrait { .. }), .. }) => true, ControlFlowGraphNode::ProgramNode(TyAstNode { content: - TyAstNodeContent::Declaration(TyDeclaration::ConstantDeclaration( + TyAstNodeContent::Declaration(ty::TyDeclaration::ConstantDeclaration( decl_id, )), .. @@ -278,7 +283,7 @@ fn connect_node( } fn connect_declaration( - decl: &TyDeclaration, + decl: &ty::TyDeclaration, graph: &mut ControlFlowGraph, entry_node: NodeIndex, span: Span, @@ -286,7 +291,7 @@ fn connect_declaration( tree_type: &TreeType, leaves: &[NodeIndex], ) -> Result, CompileError> { - use TyDeclaration::*; + use ty::TyDeclaration::*; match decl { VariableDeclaration(var_decl) => { let TyVariableDeclaration { @@ -1246,7 +1251,7 @@ fn construct_dead_code_warning_from_node(node: &TyAstNode) -> Option CompileWarning { @@ -1254,14 +1259,14 @@ fn construct_dead_code_warning_from_node(node: &TyAstNode) -> Option CompileWarning { span: span.clone(), warning_content: Warning::DeadStructDeclaration, }, TyAstNode { - content: TyAstNodeContent::Declaration(TyDeclaration::TraitDeclaration(decl_id)), + content: TyAstNodeContent::Declaration(ty::TyDeclaration::TraitDeclaration(decl_id)), .. } => { let span = match de_get_trait(decl_id.clone(), &decl_id.span()) { @@ -1274,7 +1279,7 @@ fn construct_dead_code_warning_from_node(node: &TyAstNode) -> Option match de_get_impl_trait(decl_id.clone(), span) { Ok(TyImplTrait { methods, .. }) if methods.is_empty() => return None, @@ -1284,13 +1289,13 @@ fn construct_dead_code_warning_from_node(node: &TyAstNode) -> Option return None, // We handle storage fields individually. There is no need to emit any warnings for the // storage declaration itself. TyAstNode { - content: TyAstNodeContent::Declaration(TyDeclaration::StorageDeclaration { .. }), + content: TyAstNodeContent::Declaration(ty::TyDeclaration::StorageDeclaration { .. }), .. } => return None, TyAstNode { diff --git a/sway-core/src/declaration_engine/mod.rs b/sway-core/src/declaration_engine/mod.rs index 649f17d68cb..8cf5ea4d032 100644 --- a/sway-core/src/declaration_engine/mod.rs +++ b/sway-core/src/declaration_engine/mod.rs @@ -18,3 +18,4 @@ pub(crate) mod declaration_id; pub(crate) mod declaration_wrapper; pub use declaration_engine::*; +pub(crate) use declaration_id::*; diff --git a/sway-core/src/ir_generation/compile.rs b/sway-core/src/ir_generation/compile.rs index a43aa8badef..7dd77d3970b 100644 --- a/sway-core/src/ir_generation/compile.rs +++ b/sway-core/src/ir_generation/compile.rs @@ -1,6 +1,6 @@ use crate::{ declaration_engine::declaration_engine::de_get_constant, - language::Visibility, + language::{ty, Visibility}, metadata::MetadataManager, semantic_analysis::{ast_node::*, namespace}, type_system::look_up_type_id, @@ -20,7 +20,7 @@ pub(super) fn compile_script( context: &mut Context, main_function: TyFunctionDeclaration, namespace: &namespace::Module, - declarations: Vec, + declarations: Vec, ) -> Result { let module = Module::new(context, Kind::Script); let mut md_mgr = MetadataManager::default(); @@ -36,7 +36,7 @@ pub(super) fn compile_contract( context: &mut Context, abi_entries: Vec, namespace: &namespace::Module, - declarations: Vec, + declarations: Vec, ) -> Result { let module = Module::new(context, Kind::Contract); let mut md_mgr = MetadataManager::default(); @@ -90,11 +90,11 @@ fn compile_declarations( md_mgr: &mut MetadataManager, module: Module, namespace: &namespace::Module, - declarations: Vec, + declarations: Vec, ) -> Result<(), CompileError> { for declaration in declarations { match declaration { - TyDeclaration::ConstantDeclaration(ref decl_id) => { + ty::TyDeclaration::ConstantDeclaration(ref decl_id) => { let decl = de_get_constant(decl_id.clone(), &declaration.span())?; compile_const_decl( &mut LookupEnv { @@ -108,14 +108,14 @@ fn compile_declarations( )?; } - TyDeclaration::FunctionDeclaration(_decl) => { + ty::TyDeclaration::FunctionDeclaration(_decl) => { // We no longer compile functions other than `main()` until we can improve the name // resolution. Currently there isn't enough information in the AST to fully // distinguish similarly named functions and especially trait methods. // //compile_function(context, module, decl).map(|_| ())? } - TyDeclaration::ImplTrait(_) => { + ty::TyDeclaration::ImplTrait(_) => { // And for the same reason we don't need to compile impls at all. // // compile_impl( @@ -126,14 +126,14 @@ fn compile_declarations( //)?, } - TyDeclaration::StructDeclaration(_) - | TyDeclaration::EnumDeclaration(_) - | TyDeclaration::TraitDeclaration(_) - | TyDeclaration::VariableDeclaration(_) - | TyDeclaration::AbiDeclaration(_) - | TyDeclaration::GenericTypeForFunctionScope { .. } - | TyDeclaration::StorageDeclaration(_) - | TyDeclaration::ErrorRecovery => (), + ty::TyDeclaration::StructDeclaration(_) + | ty::TyDeclaration::EnumDeclaration(_) + | ty::TyDeclaration::TraitDeclaration(_) + | ty::TyDeclaration::VariableDeclaration(_) + | ty::TyDeclaration::AbiDeclaration(_) + | ty::TyDeclaration::GenericTypeForFunctionScope { .. } + | ty::TyDeclaration::StorageDeclaration(_) + | ty::TyDeclaration::ErrorRecovery => (), } } Ok(()) diff --git a/sway-core/src/ir_generation/const_eval.rs b/sway-core/src/ir_generation/const_eval.rs index 17acaf032b8..ce0a7f9c062 100644 --- a/sway-core/src/ir_generation/const_eval.rs +++ b/sway-core/src/ir_generation/const_eval.rs @@ -4,7 +4,7 @@ use crate::{ metadata::MetadataManager, semantic_analysis::{ declaration::ProjectionKind, namespace, TyAstNode, TyAstNodeContent, TyConstantDeclaration, - TyDeclaration, TyStructExpressionField, + TyStructExpressionField, }, }; @@ -43,7 +43,7 @@ pub(crate) fn compile_const_decl( // See if we it's a global const and whether we can compile it *now*. let decl = module_ns.check_symbol(name)?; let decl_name_value = match decl { - TyDeclaration::ConstantDeclaration(decl_id) => { + ty::TyDeclaration::ConstantDeclaration(decl_id) => { let TyConstantDeclaration { name, value, .. } = de_get_constant(decl_id.clone(), &name.span())?; Some((name, value)) diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index 36854237d48..983300bb1e3 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -114,32 +114,38 @@ impl FnCompiler { let span_md_idx = md_mgr.span_to_md(context, &ast_node.span); match ast_node.content { TyAstNodeContent::Declaration(td) => match td { - TyDeclaration::VariableDeclaration(tvd) => { + ty::TyDeclaration::VariableDeclaration(tvd) => { self.compile_var_decl(context, md_mgr, *tvd, span_md_idx) } - TyDeclaration::ConstantDeclaration(decl_id) => { + ty::TyDeclaration::ConstantDeclaration(decl_id) => { let tcd = declaration_engine::de_get_constant(decl_id, &ast_node.span)?; self.compile_const_decl(context, md_mgr, tcd, span_md_idx)?; Ok(None) } - TyDeclaration::FunctionDeclaration(_) => Err(CompileError::UnexpectedDeclaration { - decl_type: "function", - span: ast_node.span, - }), - TyDeclaration::TraitDeclaration(_) => Err(CompileError::UnexpectedDeclaration { - decl_type: "trait", - span: ast_node.span, - }), - TyDeclaration::StructDeclaration(_) => Err(CompileError::UnexpectedDeclaration { - decl_type: "struct", - span: ast_node.span, - }), - TyDeclaration::EnumDeclaration(decl_id) => { + ty::TyDeclaration::FunctionDeclaration(_) => { + Err(CompileError::UnexpectedDeclaration { + decl_type: "function", + span: ast_node.span, + }) + } + ty::TyDeclaration::TraitDeclaration(_) => { + Err(CompileError::UnexpectedDeclaration { + decl_type: "trait", + span: ast_node.span, + }) + } + ty::TyDeclaration::StructDeclaration(_) => { + Err(CompileError::UnexpectedDeclaration { + decl_type: "struct", + span: ast_node.span, + }) + } + ty::TyDeclaration::EnumDeclaration(decl_id) => { let ted = declaration_engine::de_get_enum(decl_id, &ast_node.span)?; create_enum_aggregate(context, ted.variants).map(|_| ())?; Ok(None) } - TyDeclaration::ImplTrait(_) => { + ty::TyDeclaration::ImplTrait(_) => { // XXX What if we ignore the trait implementation??? Potentially since // we currently inline everything and below we 'recreate' the functions // lazily as they are called, nothing needs to be done here. BUT! @@ -147,24 +153,28 @@ impl FnCompiler { // compile and then call these properly. Ok(None) } - TyDeclaration::AbiDeclaration(_) => Err(CompileError::UnexpectedDeclaration { + ty::TyDeclaration::AbiDeclaration(_) => Err(CompileError::UnexpectedDeclaration { decl_type: "abi", span: ast_node.span, }), - TyDeclaration::GenericTypeForFunctionScope { .. } => { + ty::TyDeclaration::GenericTypeForFunctionScope { .. } => { Err(CompileError::UnexpectedDeclaration { decl_type: "abi", span: ast_node.span, }) } - TyDeclaration::ErrorRecovery { .. } => Err(CompileError::UnexpectedDeclaration { - decl_type: "error recovery", - span: ast_node.span, - }), - TyDeclaration::StorageDeclaration(_) => Err(CompileError::UnexpectedDeclaration { - decl_type: "storage", - span: ast_node.span, - }), + ty::TyDeclaration::ErrorRecovery { .. } => { + Err(CompileError::UnexpectedDeclaration { + decl_type: "error recovery", + span: ast_node.span, + }) + } + ty::TyDeclaration::StorageDeclaration(_) => { + Err(CompileError::UnexpectedDeclaration { + decl_type: "storage", + span: ast_node.span, + }) + } }, TyAstNodeContent::Expression(te) => { // An expression with an ignored return value... I assume. diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs new file mode 100644 index 00000000000..c5583b6c336 --- /dev/null +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -0,0 +1,230 @@ +use std::fmt; + +use sway_types::{Ident, Span, Spanned}; + +use crate::{ + declaration_engine::*, + error::*, + semantic_analysis::{ + TyConstantDeclaration, TyEnumDeclaration, TyStructDeclaration, TyTraitDeclaration, + TyVariableDeclaration, VariableMutability, + }, + type_system::*, + TyFunctionDeclaration, +}; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum TyDeclaration { + VariableDeclaration(Box), + ConstantDeclaration(DeclarationId), + FunctionDeclaration(DeclarationId), + TraitDeclaration(DeclarationId), + StructDeclaration(DeclarationId), + EnumDeclaration(DeclarationId), + ImplTrait(DeclarationId), + AbiDeclaration(DeclarationId), + // If type parameters are defined for a function, they are put in the namespace just for + // the body of that function. + GenericTypeForFunctionScope { name: Ident, type_id: TypeId }, + ErrorRecovery, + StorageDeclaration(DeclarationId), +} + +impl CopyTypes for TyDeclaration { + /// The entry point to monomorphizing typed declarations. Instantiates all new type ids, + /// assuming `self` has already been copied. + fn copy_types(&mut self, type_mapping: &TypeMapping) { + use TyDeclaration::*; + match self { + VariableDeclaration(ref mut var_decl) => var_decl.copy_types(type_mapping), + FunctionDeclaration(ref mut fn_decl) => fn_decl.copy_types(type_mapping), + TraitDeclaration(ref mut trait_decl) => trait_decl.copy_types(type_mapping), + StructDeclaration(ref mut decl_id) => decl_id.copy_types(type_mapping), + EnumDeclaration(ref mut enum_decl) => enum_decl.copy_types(type_mapping), + ImplTrait(impl_trait) => impl_trait.copy_types(type_mapping), + // generics in an ABI is unsupported by design + AbiDeclaration(..) + | ConstantDeclaration(_) + | StorageDeclaration(..) + | GenericTypeForFunctionScope { .. } + | ErrorRecovery => (), + } + } +} + +impl Spanned for TyDeclaration { + fn span(&self) -> Span { + use TyDeclaration::*; + match self { + VariableDeclaration(decl) => decl.name.span(), + ConstantDeclaration(decl_id) => decl_id.span(), + FunctionDeclaration(decl_id) => decl_id.span(), + TraitDeclaration(decl_id) => decl_id.span(), + StructDeclaration(decl_id) => decl_id.span(), + EnumDeclaration(decl_id) => decl_id.span(), + AbiDeclaration(decl_id) => decl_id.span(), + ImplTrait(decl_id) => decl_id.span(), + StorageDeclaration(decl) => decl.span(), + ErrorRecovery | GenericTypeForFunctionScope { .. } => { + unreachable!("No span exists for these ast node types") + } + } + } +} + +impl fmt::Display for TyDeclaration { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{} declaration ({})", + self.friendly_name(), + match self { + TyDeclaration::VariableDeclaration(decl) => { + let TyVariableDeclaration { + mutability, + name, + type_ascription, + body, + .. + } = &**decl; + let mut builder = String::new(); + match mutability { + VariableMutability::Mutable => builder.push_str("mut"), + VariableMutability::RefMutable => builder.push_str("ref mut"), + VariableMutability::Immutable => {} + VariableMutability::ExportedConst => builder.push_str("pub const"), + } + builder.push_str(name.as_str()); + builder.push_str(": "); + builder.push_str( + &crate::type_system::look_up_type_id(*type_ascription).to_string(), + ); + builder.push_str(" = "); + builder.push_str(&body.to_string()); + builder + } + TyDeclaration::FunctionDeclaration(decl_id) => { + match de_get_function(decl_id.clone(), &decl_id.span()) { + Ok(TyFunctionDeclaration { name, .. }) => name.as_str().into(), + Err(_) => "unknown function".into(), + } + } + TyDeclaration::TraitDeclaration(decl_id) => { + match de_get_trait(decl_id.clone(), &decl_id.span()) { + Ok(TyTraitDeclaration { name, .. }) => name.as_str().into(), + Err(_) => "unknown trait".into(), + } + } + TyDeclaration::StructDeclaration(decl_id) => { + match de_get_struct(decl_id.clone(), &decl_id.span()) { + Ok(TyStructDeclaration { name, .. }) => name.as_str().into(), + Err(_) => "unknown struct".into(), + } + } + TyDeclaration::EnumDeclaration(decl_id) => { + match de_get_enum(decl_id.clone(), &decl_id.span()) { + Ok(TyEnumDeclaration { name, .. }) => name.as_str().into(), + Err(_) => "unknown enum".into(), + } + } + _ => String::new(), + } + ) + } +} + +impl CollectTypesMetadata for TyDeclaration { + // this is only run on entry nodes, which must have all well-formed types + fn collect_types_metadata(&self) -> CompileResult> { + use TyDeclaration::*; + let mut warnings = vec![]; + let mut errors = vec![]; + let metadata = match self { + VariableDeclaration(decl) => { + let mut body = check!( + decl.body.collect_types_metadata(), + return err(warnings, errors), + warnings, + errors + ); + body.append(&mut check!( + decl.type_ascription.collect_types_metadata(), + return err(warnings, errors), + warnings, + errors + )); + body + } + FunctionDeclaration(decl_id) => match de_get_function(decl_id.clone(), &decl_id.span()) + { + Ok(decl) => { + let mut body = vec![]; + for content in decl.body.contents.iter() { + body.append(&mut check!( + content.collect_types_metadata(), + return err(warnings, errors), + warnings, + errors + )); + } + body.append(&mut check!( + decl.return_type.collect_types_metadata(), + return err(warnings, errors), + warnings, + errors + )); + for type_param in decl.type_parameters.iter() { + body.append(&mut check!( + type_param.type_id.collect_types_metadata(), + return err(warnings, errors), + warnings, + errors + )); + } + for param in decl.parameters.iter() { + body.append(&mut check!( + param.type_id.collect_types_metadata(), + return err(warnings, errors), + warnings, + errors + )); + } + body + } + Err(e) => { + errors.push(e); + return err(warnings, errors); + } + }, + ConstantDeclaration(decl_id) => { + match de_get_constant(decl_id.clone(), &decl_id.span()) { + Ok(TyConstantDeclaration { value, .. }) => { + check!( + value.collect_types_metadata(), + return err(warnings, errors), + warnings, + errors + ) + } + Err(e) => { + errors.push(e); + return err(warnings, errors); + } + } + } + ErrorRecovery + | StorageDeclaration(_) + | TraitDeclaration(_) + | StructDeclaration(_) + | EnumDeclaration(_) + | ImplTrait { .. } + | AbiDeclaration(_) + | GenericTypeForFunctionScope { .. } => vec![], + }; + if errors.is_empty() { + ok(metadata, warnings, errors) + } else { + err(warnings, errors) + } + } +} diff --git a/sway-core/src/language/ty/declaration/mod.rs b/sway-core/src/language/ty/declaration/mod.rs new file mode 100644 index 00000000000..3c7091c939e --- /dev/null +++ b/sway-core/src/language/ty/declaration/mod.rs @@ -0,0 +1,4 @@ +#[allow(clippy::module_inception)] +mod declaration; + +pub use declaration::*; diff --git a/sway-core/src/language/ty/mod.rs b/sway-core/src/language/ty/mod.rs index 012f5e3cf1f..f79619b2521 100644 --- a/sway-core/src/language/ty/mod.rs +++ b/sway-core/src/language/ty/mod.rs @@ -1,3 +1,5 @@ +mod declaration; mod expression; +pub use declaration::*; pub use expression::*; diff --git a/sway-core/src/lib.rs b/sway-core/src/lib.rs index d94900ce2cc..371e40e985d 100644 --- a/sway-core/src/lib.rs +++ b/sway-core/src/lib.rs @@ -30,7 +30,7 @@ use sway_ir::{Context, Function, Instruction, Kind, Module, Value}; pub use semantic_analysis::{ namespace::{self, Namespace}, - TyDeclaration, TyFunctionDeclaration, TyModule, TyProgram, TyProgramKind, + TyFunctionDeclaration, TyModule, TyProgram, TyProgramKind, }; pub mod types; diff --git a/sway-core/src/semantic_analysis.rs b/sway-core/src/semantic_analysis.rs index 5fd013d9e52..8100bc7ed9f 100644 --- a/sway-core/src/semantic_analysis.rs +++ b/sway-core/src/semantic_analysis.rs @@ -6,8 +6,9 @@ mod node_dependencies; mod program; mod storage_only_types; mod type_check_context; + pub(crate) use ast_node::*; -pub use ast_node::{TyConstantDeclaration, TyDeclaration, TyFunctionDeclaration}; +pub use ast_node::{TyConstantDeclaration, TyFunctionDeclaration}; pub use module::{TyModule, TySubmodule}; pub use namespace::Namespace; pub use program::{TyProgram, TyProgramKind}; diff --git a/sway-core/src/semantic_analysis/ast_node/declaration.rs b/sway-core/src/semantic_analysis/ast_node/declaration.rs index 641f84811b0..a4d86d8e756 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration.rs @@ -17,7 +17,7 @@ pub use storage::*; pub use variable::*; use crate::{ - declaration_engine::{declaration_engine::*, declaration_id::DeclarationId}, + declaration_engine::declaration_engine::*, error::*, language::{ty, *}, semantic_analysis::*, @@ -25,233 +25,17 @@ use crate::{ AttributesMap, }; use derivative::Derivative; -use std::{borrow::Cow, fmt}; +use std::borrow::Cow; use sway_error::error::CompileError; use sway_types::{Ident, Span, Spanned}; -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum TyDeclaration { - VariableDeclaration(Box), - ConstantDeclaration(DeclarationId), - FunctionDeclaration(DeclarationId), - TraitDeclaration(DeclarationId), - StructDeclaration(DeclarationId), - EnumDeclaration(DeclarationId), - ImplTrait(DeclarationId), - AbiDeclaration(DeclarationId), - // If type parameters are defined for a function, they are put in the namespace just for - // the body of that function. - GenericTypeForFunctionScope { name: Ident, type_id: TypeId }, - ErrorRecovery, - StorageDeclaration(DeclarationId), -} - -impl CopyTypes for TyDeclaration { - /// The entry point to monomorphizing typed declarations. Instantiates all new type ids, - /// assuming `self` has already been copied. - fn copy_types(&mut self, type_mapping: &TypeMapping) { - use TyDeclaration::*; - match self { - VariableDeclaration(ref mut var_decl) => var_decl.copy_types(type_mapping), - FunctionDeclaration(ref mut fn_decl) => fn_decl.copy_types(type_mapping), - TraitDeclaration(ref mut trait_decl) => trait_decl.copy_types(type_mapping), - StructDeclaration(ref mut decl_id) => decl_id.copy_types(type_mapping), - EnumDeclaration(ref mut enum_decl) => enum_decl.copy_types(type_mapping), - ImplTrait(impl_trait) => impl_trait.copy_types(type_mapping), - // generics in an ABI is unsupported by design - AbiDeclaration(..) - | ConstantDeclaration(_) - | StorageDeclaration(..) - | GenericTypeForFunctionScope { .. } - | ErrorRecovery => (), - } - } -} - -impl Spanned for TyDeclaration { - fn span(&self) -> Span { - use TyDeclaration::*; - match self { - VariableDeclaration(decl) => decl.name.span(), - ConstantDeclaration(decl_id) => decl_id.span(), - FunctionDeclaration(decl_id) => decl_id.span(), - TraitDeclaration(decl_id) => decl_id.span(), - StructDeclaration(decl_id) => decl_id.span(), - EnumDeclaration(decl_id) => decl_id.span(), - AbiDeclaration(decl_id) => decl_id.span(), - ImplTrait(decl_id) => decl_id.span(), - StorageDeclaration(decl) => decl.span(), - ErrorRecovery | GenericTypeForFunctionScope { .. } => { - unreachable!("No span exists for these ast node types") - } - } - } -} - -impl fmt::Display for TyDeclaration { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{} declaration ({})", - self.friendly_name(), - match self { - TyDeclaration::VariableDeclaration(decl) => { - let TyVariableDeclaration { - mutability, - name, - type_ascription, - body, - .. - } = &**decl; - let mut builder = String::new(); - match mutability { - VariableMutability::Mutable => builder.push_str("mut"), - VariableMutability::RefMutable => builder.push_str("ref mut"), - VariableMutability::Immutable => {} - VariableMutability::ExportedConst => builder.push_str("pub const"), - } - builder.push_str(name.as_str()); - builder.push_str(": "); - builder.push_str( - &crate::type_system::look_up_type_id(*type_ascription).to_string(), - ); - builder.push_str(" = "); - builder.push_str(&body.to_string()); - builder - } - TyDeclaration::FunctionDeclaration(decl_id) => { - match de_get_function(decl_id.clone(), &decl_id.span()) { - Ok(TyFunctionDeclaration { name, .. }) => name.as_str().into(), - Err(_) => "unknown function".into(), - } - } - TyDeclaration::TraitDeclaration(decl_id) => { - match de_get_trait(decl_id.clone(), &decl_id.span()) { - Ok(TyTraitDeclaration { name, .. }) => name.as_str().into(), - Err(_) => "unknown trait".into(), - } - } - TyDeclaration::StructDeclaration(decl_id) => { - match de_get_struct(decl_id.clone(), &decl_id.span()) { - Ok(TyStructDeclaration { name, .. }) => name.as_str().into(), - Err(_) => "unknown struct".into(), - } - } - TyDeclaration::EnumDeclaration(decl_id) => { - match de_get_enum(decl_id.clone(), &decl_id.span()) { - Ok(TyEnumDeclaration { name, .. }) => name.as_str().into(), - Err(_) => "unknown enum".into(), - } - } - _ => String::new(), - } - ) - } -} - -impl CollectTypesMetadata for TyDeclaration { - // this is only run on entry nodes, which must have all well-formed types - fn collect_types_metadata(&self) -> CompileResult> { - use TyDeclaration::*; - let mut warnings = vec![]; - let mut errors = vec![]; - let metadata = match self { - VariableDeclaration(decl) => { - let mut body = check!( - decl.body.collect_types_metadata(), - return err(warnings, errors), - warnings, - errors - ); - body.append(&mut check!( - decl.type_ascription.collect_types_metadata(), - return err(warnings, errors), - warnings, - errors - )); - body - } - FunctionDeclaration(decl_id) => match de_get_function(decl_id.clone(), &decl_id.span()) - { - Ok(decl) => { - let mut body = vec![]; - for content in decl.body.contents.iter() { - body.append(&mut check!( - content.collect_types_metadata(), - return err(warnings, errors), - warnings, - errors - )); - } - body.append(&mut check!( - decl.return_type.collect_types_metadata(), - return err(warnings, errors), - warnings, - errors - )); - for type_param in decl.type_parameters.iter() { - body.append(&mut check!( - type_param.type_id.collect_types_metadata(), - return err(warnings, errors), - warnings, - errors - )); - } - for param in decl.parameters.iter() { - body.append(&mut check!( - param.type_id.collect_types_metadata(), - return err(warnings, errors), - warnings, - errors - )); - } - body - } - Err(e) => { - errors.push(e); - return err(warnings, errors); - } - }, - ConstantDeclaration(decl_id) => { - match de_get_constant(decl_id.clone(), &decl_id.span()) { - Ok(TyConstantDeclaration { value, .. }) => { - check!( - value.collect_types_metadata(), - return err(warnings, errors), - warnings, - errors - ) - } - Err(e) => { - errors.push(e); - return err(warnings, errors); - } - } - } - ErrorRecovery - | StorageDeclaration(_) - | TraitDeclaration(_) - | StructDeclaration(_) - | EnumDeclaration(_) - | ImplTrait { .. } - | AbiDeclaration(_) - | GenericTypeForFunctionScope { .. } => vec![], - }; - if errors.is_empty() { - ok(metadata, warnings, errors) - } else { - err(warnings, errors) - } - } -} - -impl TyDeclaration { +impl ty::TyDeclaration { /// Retrieves the declaration as an enum declaration. /// /// Returns an error if `self` is not a [TyEnumDeclaration]. pub(crate) fn expect_enum(&self, access_span: &Span) -> CompileResult { match self { - TyDeclaration::EnumDeclaration(decl_id) => { + ty::TyDeclaration::EnumDeclaration(decl_id) => { CompileResult::from(de_get_enum(decl_id.clone(), access_span)) } decl => err( @@ -271,7 +55,7 @@ impl TyDeclaration { let mut warnings = vec![]; let mut errors = vec![]; match self { - TyDeclaration::StructDeclaration(decl_id) => { + ty::TyDeclaration::StructDeclaration(decl_id) => { let decl = check!( CompileResult::from(de_get_struct(decl_id.clone(), access_span)), return err(warnings, errors), @@ -300,7 +84,7 @@ impl TyDeclaration { let mut warnings = vec![]; let mut errors = vec![]; match self { - TyDeclaration::FunctionDeclaration(decl_id) => { + ty::TyDeclaration::FunctionDeclaration(decl_id) => { let decl = check!( CompileResult::from(de_get_function(decl_id.clone(), access_span)), return err(warnings, errors), @@ -326,7 +110,7 @@ impl TyDeclaration { let warnings = vec![]; let mut errors = vec![]; match self { - TyDeclaration::VariableDeclaration(decl) => ok(decl, warnings, errors), + ty::TyDeclaration::VariableDeclaration(decl) => ok(decl, warnings, errors), decl => { errors.push(CompileError::DeclIsNotAVariable { actually: decl.friendly_name().to_string(), @@ -342,7 +126,7 @@ impl TyDeclaration { /// Returns an error if `self` is not a [TyAbiDeclaration]. pub(crate) fn expect_abi(&self, access_span: &Span) -> CompileResult { match self { - TyDeclaration::AbiDeclaration(decl_id) => { + ty::TyDeclaration::AbiDeclaration(decl_id) => { CompileResult::from(de_get_abi(decl_id.clone(), access_span)) } decl => err( @@ -357,7 +141,7 @@ impl TyDeclaration { /// friendly name string used for error reporting. pub fn friendly_name(&self) -> &'static str { - use TyDeclaration::*; + use ty::TyDeclaration::*; match self { VariableDeclaration(_) => "variable", ConstantDeclaration(_) => "constant", @@ -377,15 +161,15 @@ impl TyDeclaration { let mut warnings = vec![]; let mut errors = vec![]; let type_id = match self { - TyDeclaration::VariableDeclaration(decl) => decl.body.return_type, - TyDeclaration::FunctionDeclaration { .. } => { + ty::TyDeclaration::VariableDeclaration(decl) => decl.body.return_type, + ty::TyDeclaration::FunctionDeclaration { .. } => { errors.push(CompileError::Unimplemented( "Function pointers have not yet been implemented.", self.span(), )); return err(warnings, errors); } - TyDeclaration::StructDeclaration(decl_id) => { + ty::TyDeclaration::StructDeclaration(decl_id) => { let decl = check!( CompileResult::from(de_get_struct(decl_id.clone(), &self.span())), return err(warnings, errors), @@ -394,7 +178,7 @@ impl TyDeclaration { ); decl.create_type_id() } - TyDeclaration::EnumDeclaration(decl_id) => { + ty::TyDeclaration::EnumDeclaration(decl_id) => { let decl = check!( CompileResult::from(de_get_enum(decl_id.clone(), access_span)), return err(warnings, errors), @@ -403,7 +187,7 @@ impl TyDeclaration { ); decl.create_type_id() } - TyDeclaration::StorageDeclaration(decl_id) => { + ty::TyDeclaration::StorageDeclaration(decl_id) => { let storage_decl = check!( CompileResult::from(de_get_storage(decl_id.clone(), &self.span())), return err(warnings, errors), @@ -414,7 +198,7 @@ impl TyDeclaration { fields: storage_decl.fields_as_typed_struct_fields(), }) } - TyDeclaration::GenericTypeForFunctionScope { type_id, .. } => *type_id, + ty::TyDeclaration::GenericTypeForFunctionScope { type_id, .. } => *type_id, decl => { errors.push(CompileError::NotAType { span: decl.span(), @@ -428,7 +212,7 @@ impl TyDeclaration { } pub(crate) fn visibility(&self) -> CompileResult { - use TyDeclaration::*; + use ty::TyDeclaration::*; let mut warnings = vec![]; let mut errors = vec![]; let visibility = match self { diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/function.rs b/sway-core/src/semantic_analysis/ast_node/declaration/function.rs index db2f41f86f1..1e6ed0431e7 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/function.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/function.rs @@ -39,7 +39,7 @@ impl From<&TyFunctionDeclaration> for TyAstNode { fn from(o: &TyFunctionDeclaration) -> Self { let span = o.span.clone(); TyAstNode { - content: TyAstNodeContent::Declaration(TyDeclaration::FunctionDeclaration( + content: TyAstNodeContent::Declaration(ty::TyDeclaration::FunctionDeclaration( de_insert_function(o.clone()), )), span, diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/function/function_parameter.rs b/sway-core/src/semantic_analysis/ast_node/declaration/function/function_parameter.rs index 91a6c22d03e..8eaf69aea66 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/function/function_parameter.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/function/function_parameter.rs @@ -6,7 +6,7 @@ use crate::{ VariableMutability, }, type_system::*, - CompileResult, Ident, Namespace, TyDeclaration, + CompileResult, Ident, Namespace, }; use sway_error::error::CompileError; @@ -189,7 +189,7 @@ impl TyFunctionParameter { fn insert_into_namespace(ctx: TypeCheckContext, typed_parameter: &TyFunctionParameter) { ctx.namespace.insert_symbol( typed_parameter.name.clone(), - TyDeclaration::VariableDeclaration(Box::new(TyVariableDeclaration { + ty::TyDeclaration::VariableDeclaration(Box::new(TyVariableDeclaration { name: typed_parameter.name.clone(), body: ty::TyExpression { expression: ty::TyExpressionVariant::FunctionParameter, diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs b/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs index ea6f6ed8f51..88c12fdf922 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs @@ -14,7 +14,7 @@ use crate::{ insert_type, look_up_type_id, set_type_as_storage_only, to_typeinfo, unify_with_self, CopyTypes, TypeId, TypeMapping, TypeParameter, }, - CompileResult, TyDeclaration, TyFunctionDeclaration, TypeInfo, + CompileResult, TyFunctionDeclaration, TypeInfo, }; use super::TyTraitFn; @@ -103,7 +103,7 @@ impl TyImplTrait { .ok(&mut warnings, &mut errors) .cloned() { - Some(TyDeclaration::TraitDeclaration(decl_id)) => { + Some(ty::TyDeclaration::TraitDeclaration(decl_id)) => { let tr = check!( CompileResult::from(de_get_trait(decl_id, &trait_name.span())), return err(warnings, errors), @@ -143,7 +143,7 @@ impl TyImplTrait { ); (impl_trait, implementing_for_type_id) } - Some(TyDeclaration::AbiDeclaration(decl_id)) => { + Some(ty::TyDeclaration::AbiDeclaration(decl_id)) => { // if you are comparing this with the `impl_trait` branch above, note that // there are no type arguments here because we don't support generic types // in contract ABIs yet (or ever?) due to the complexity of communicating @@ -328,29 +328,29 @@ impl TyImplTrait { } fn decl_contains_get_storage_index( - decl: &TyDeclaration, + decl: &ty::TyDeclaration, access_span: &Span, ) -> Result { match decl { - TyDeclaration::VariableDeclaration(decl) => { + ty::TyDeclaration::VariableDeclaration(decl) => { expr_contains_get_storage_index(&decl.body, access_span) } - TyDeclaration::ConstantDeclaration(decl_id) => { + ty::TyDeclaration::ConstantDeclaration(decl_id) => { let TyConstantDeclaration { value: expr, .. } = de_get_constant(decl_id.clone(), access_span)?; expr_contains_get_storage_index(&expr, access_span) } // We're already inside a type's impl. So we can't have these // nested functions etc. We just ignore them. - TyDeclaration::FunctionDeclaration(_) - | TyDeclaration::TraitDeclaration(_) - | TyDeclaration::StructDeclaration(_) - | TyDeclaration::EnumDeclaration(_) - | TyDeclaration::ImplTrait(_) - | TyDeclaration::AbiDeclaration(_) - | TyDeclaration::GenericTypeForFunctionScope { .. } - | TyDeclaration::ErrorRecovery - | TyDeclaration::StorageDeclaration(_) => Ok(false), + ty::TyDeclaration::FunctionDeclaration(_) + | ty::TyDeclaration::TraitDeclaration(_) + | ty::TyDeclaration::StructDeclaration(_) + | ty::TyDeclaration::EnumDeclaration(_) + | ty::TyDeclaration::ImplTrait(_) + | ty::TyDeclaration::AbiDeclaration(_) + | ty::TyDeclaration::GenericTypeForFunctionScope { .. } + | ty::TyDeclaration::ErrorRecovery + | ty::TyDeclaration::StorageDeclaration(_) => Ok(false), } } diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs b/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs index 3de3dcd7a98..83349d7911f 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs @@ -1,17 +1,20 @@ use derivative::Derivative; -use sway_error::warning::{CompileWarning, Warning}; +use sway_error::{ + error::CompileError, + warning::{CompileWarning, Warning}, +}; use sway_types::{style::is_upper_camel_case, Ident, Spanned}; use crate::{ - declaration_engine::declaration_engine::de_get_trait, - error::{err, ok}, - language::{parsed::*, CallPath, Visibility}, + declaration_engine::*, + error::*, + language::{parsed::*, ty, CallPath, Visibility}, semantic_analysis::{ ast_node::{type_check_interface_surface, type_check_trait_methods}, Mode, TyCodeBlock, TypeCheckContext, }, - type_system::{insert_type, CopyTypes, TypeMapping}, - CompileError, CompileResult, Namespace, TyDeclaration, TyFunctionDeclaration, TypeInfo, + type_system::*, + Namespace, TyFunctionDeclaration, }; use super::{EnforceTypeArguments, TyFunctionParameter, TyTraitFn}; @@ -124,7 +127,7 @@ fn handle_supertraits( .ok(&mut warnings, &mut errors) .cloned() { - Some(TyDeclaration::TraitDeclaration(decl_id)) => { + Some(ty::TyDeclaration::TraitDeclaration(decl_id)) => { let TyTraitDeclaration { ref interface_surface, ref methods, @@ -169,9 +172,11 @@ fn handle_supertraits( errors ); } - Some(TyDeclaration::AbiDeclaration(_)) => errors.push(CompileError::AbiAsSupertrait { - span: supertrait.name.span().clone(), - }), + Some(ty::TyDeclaration::AbiDeclaration(_)) => { + errors.push(CompileError::AbiAsSupertrait { + span: supertrait.name.span().clone(), + }) + } _ => errors.push(CompileError::TraitNotFound { name: supertrait.name.to_string(), span: supertrait.name.span(), diff --git a/sway-core/src/semantic_analysis/ast_node/expression/match_expression/typed/typed_match_branch.rs b/sway-core/src/semantic_analysis/ast_node/expression/match_expression/typed/typed_match_branch.rs index c247d5c116b..aec02ac67de 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/match_expression/typed/typed_match_branch.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/match_expression/typed/typed_match_branch.rs @@ -10,7 +10,7 @@ use crate::{ }, type_system::insert_type, types::DeterministicallyAborts, - CompileResult, TyDeclaration, TypeInfo, + CompileResult, TypeInfo, }; use super::matcher::{matcher, MatchReqMap}; @@ -64,13 +64,14 @@ impl TyMatchBranch { for (left_decl, right_decl) in match_decl_map.into_iter() { let type_ascription = right_decl.return_type; let span = left_decl.span().clone(); - let var_decl = TyDeclaration::VariableDeclaration(Box::new(TyVariableDeclaration { - name: left_decl.clone(), - body: right_decl, - mutability: VariableMutability::Immutable, - type_ascription, - type_ascription_span: None, - })); + let var_decl = + ty::TyDeclaration::VariableDeclaration(Box::new(TyVariableDeclaration { + name: left_decl.clone(), + body: right_decl, + mutability: VariableMutability::Immutable, + type_ascription, + type_ascription_span: None, + })); ctx.namespace.insert_symbol(left_decl, var_decl.clone()); code_block_contents.push(TyAstNode { content: TyAstNodeContent::Declaration(var_decl), 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 e66918566f4..de64f867056 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 @@ -3,10 +3,10 @@ use sway_types::{Ident, Span, Spanned}; use crate::{ declaration_engine::de_get_constant, error::{err, ok}, - language::{parsed::*, CallPath, Literal}, + language::{parsed::*, ty, CallPath, Literal}, semantic_analysis::{TyEnumVariant, TypeCheckContext}, type_system::{insert_type, CreateTypeId, EnforceTypeArguments, TypeArgument, TypeId}, - CompileError, CompileResult, TyDeclaration, TypeInfo, + CompileError, CompileResult, TypeInfo, }; #[derive(Debug, Clone)] @@ -86,7 +86,7 @@ fn type_check_variable( let typed_scrutinee = match ctx.namespace.resolve_symbol(&name).value { // If this variable is a constant, then we turn it into a [TyScrutinee::Constant]. - Some(TyDeclaration::ConstantDeclaration(decl_id)) => { + Some(ty::TyDeclaration::ConstantDeclaration(decl_id)) => { let constant_decl = check!( CompileResult::from(de_get_constant(decl_id.clone(), &span)), return err(warnings, errors), 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 0d2b959d005..72a23721450 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 @@ -459,7 +459,7 @@ impl ty::TyExpression { let mut warnings = vec![]; let mut errors = vec![]; let exp = match namespace.resolve_symbol(&name).value { - Some(TyDeclaration::VariableDeclaration(decl)) => { + Some(ty::TyDeclaration::VariableDeclaration(decl)) => { let TyVariableDeclaration { name: decl_name, body, @@ -477,7 +477,7 @@ impl ty::TyExpression { span, } } - Some(TyDeclaration::ConstantDeclaration(decl_id)) => { + Some(ty::TyDeclaration::ConstantDeclaration(decl_id)) => { let TyConstantDeclaration { name: decl_name, value, @@ -501,7 +501,7 @@ impl ty::TyExpression { span, } } - Some(TyDeclaration::AbiDeclaration(decl_id)) => { + Some(ty::TyDeclaration::AbiDeclaration(decl_id)) => { let decl = check!( CompileResult::from(de_get_abi(decl_id.clone(), &span)), return err(warnings, errors), @@ -1225,7 +1225,7 @@ impl ty::TyExpression { errors ); let abi = match abi { - TyDeclaration::AbiDeclaration(decl_id) => { + ty::TyDeclaration::AbiDeclaration(decl_id) => { check!( CompileResult::from(de_get_abi(decl_id, &span)), return err(warnings, errors), @@ -1233,7 +1233,7 @@ impl ty::TyExpression { errors ) } - TyDeclaration::VariableDeclaration(ref decl) => { + ty::TyDeclaration::VariableDeclaration(ref decl) => { let TyVariableDeclaration { body: expr, .. } = &**decl; let ret_ty = look_up_type_id(expr.return_type); let abi_name = match ret_ty { diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs index 79185833967..2eac582427f 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs @@ -8,7 +8,7 @@ use crate::{ ContractCallParams, IsConstant, TyFunctionParameter, TyStorageField, TypeCheckContext, }, type_system::*, - TyDeclaration, TyFunctionDeclaration, + TyFunctionDeclaration, }; use std::collections::{HashMap, VecDeque}; use sway_error::error::CompileError; @@ -193,7 +193,7 @@ pub(crate) fn type_check_method_application( ); let is_decl_mutable = match unknown_decl { - TyDeclaration::ConstantDeclaration(_) => false, + ty::TyDeclaration::ConstantDeclaration(_) => false, _ => { let variable_decl = check!( unknown_decl.expect_variable().cloned(), diff --git a/sway-core/src/semantic_analysis/ast_node/mod.rs b/sway-core/src/semantic_analysis/ast_node/mod.rs index 92343a55064..9900f9dd4d2 100644 --- a/sway-core/src/semantic_analysis/ast_node/mod.rs +++ b/sway-core/src/semantic_analysis/ast_node/mod.rs @@ -42,7 +42,7 @@ pub(crate) enum IsConstant { #[derive(Clone, Debug, PartialEq, Eq)] pub enum TyAstNodeContent { - Declaration(TyDeclaration), + Declaration(ty::TyDeclaration), Expression(ty::TyExpression), ImplicitReturnExpression(ty::TyExpression), // a no-op node used for something that just issues a side effect, like an import statement. @@ -139,7 +139,8 @@ impl TyAstNode { match &self { TyAstNode { span, - content: TyAstNodeContent::Declaration(TyDeclaration::FunctionDeclaration(decl_id)), + content: + TyAstNodeContent::Declaration(ty::TyDeclaration::FunctionDeclaration(decl_id)), .. } => { let TyFunctionDeclaration { name, .. } = check!( @@ -164,7 +165,7 @@ impl TyAstNode { match &self.content { TyAstNodeContent::ImplicitReturnExpression(ref exp) => exp.gather_return_statements(), // assignments and reassignments can happen during control flow and can abort - TyAstNodeContent::Declaration(TyDeclaration::VariableDeclaration(decl)) => { + TyAstNodeContent::Declaration(ty::TyDeclaration::VariableDeclaration(decl)) => { decl.body.gather_return_statements() } TyAstNodeContent::Expression(exp) => exp.gather_return_statements(), @@ -261,7 +262,7 @@ impl TyAstNode { warnings, errors ); - let typed_var_decl = TyDeclaration::VariableDeclaration(Box::new( + let typed_var_decl = ty::TyDeclaration::VariableDeclaration(Box::new( TyVariableDeclaration { name: name.clone(), body, @@ -304,7 +305,7 @@ impl TyAstNode { visibility, }; let typed_const_decl = - TyDeclaration::ConstantDeclaration(de_insert_constant(decl)); + ty::TyDeclaration::ConstantDeclaration(de_insert_constant(decl)); ctx.namespace.insert_symbol(name, typed_const_decl.clone()); typed_const_decl } @@ -316,7 +317,8 @@ impl TyAstNode { errors ); let name = enum_decl.name.clone(); - let decl = TyDeclaration::EnumDeclaration(de_insert_enum(enum_decl)); + let decl = + ty::TyDeclaration::EnumDeclaration(de_insert_enum(enum_decl)); check!( ctx.namespace.insert_symbol(name, decl.clone()), return err(warnings, errors), @@ -336,7 +338,7 @@ impl TyAstNode { let name = fn_decl.name.clone(); let decl = - TyDeclaration::FunctionDeclaration(de_insert_function(fn_decl)); + ty::TyDeclaration::FunctionDeclaration(de_insert_function(fn_decl)); ctx.namespace.insert_symbol(name, decl.clone()); decl } @@ -349,7 +351,7 @@ impl TyAstNode { ); let name = trait_decl.name.clone(); let decl_id = de_insert_trait(trait_decl); - let decl = TyDeclaration::TraitDeclaration(decl_id); + let decl = ty::TyDeclaration::TraitDeclaration(decl_id); ctx.namespace.insert_symbol(name, decl.clone()); decl } @@ -365,7 +367,7 @@ impl TyAstNode { implementing_for_type_id, impl_trait.methods.clone(), ); - TyDeclaration::ImplTrait(de_insert_impl_trait(impl_trait)) + ty::TyDeclaration::ImplTrait(de_insert_impl_trait(impl_trait)) } Declaration::ImplSelf(impl_self) => { let impl_trait = check!( @@ -379,7 +381,7 @@ impl TyAstNode { impl_trait.implementing_for_type_id, impl_trait.methods.clone(), ); - TyDeclaration::ImplTrait(de_insert_impl_trait(impl_trait)) + ty::TyDeclaration::ImplTrait(de_insert_impl_trait(impl_trait)) } Declaration::StructDeclaration(decl) => { let decl = check!( @@ -390,7 +392,7 @@ impl TyAstNode { ); let name = decl.name.clone(); let decl_id = de_insert_struct(decl); - let decl = TyDeclaration::StructDeclaration(decl_id); + let decl = ty::TyDeclaration::StructDeclaration(decl_id); // insert the struct decl into namespace check!( ctx.namespace.insert_symbol(name, decl.clone()), @@ -408,7 +410,7 @@ impl TyAstNode { errors ); let name = abi_decl.name.clone(); - let decl = TyDeclaration::AbiDeclaration(de_insert_abi(abi_decl)); + let decl = ty::TyDeclaration::AbiDeclaration(de_insert_abi(abi_decl)); ctx.namespace.insert_symbol(name, decl.clone()); decl } @@ -468,7 +470,7 @@ impl TyAstNode { warnings, errors ); - TyDeclaration::StorageDeclaration(decl_id) + ty::TyDeclaration::StorageDeclaration(decl_id) } }) } diff --git a/sway-core/src/semantic_analysis/module.rs b/sway-core/src/semantic_analysis/module.rs index 11263f92df4..1beb3869751 100644 --- a/sway-core/src/semantic_analysis/module.rs +++ b/sway-core/src/semantic_analysis/module.rs @@ -1,5 +1,8 @@ use crate::{ - declaration_engine::declaration_engine::*, error::*, language::parsed::*, semantic_analysis::*, + declaration_engine::declaration_engine::*, + error::*, + language::{parsed::*, ty}, + semantic_analysis::*, type_system::*, }; @@ -104,7 +107,8 @@ impl TySubmodule { fn check_supertraits(typed_tree_nodes: &[TyAstNode], namespace: &Namespace) -> Vec { let mut errors = vec![]; for node in typed_tree_nodes { - if let TyAstNodeContent::Declaration(TyDeclaration::ImplTrait(decl_id)) = &node.content { + if let TyAstNodeContent::Declaration(ty::TyDeclaration::ImplTrait(decl_id)) = &node.content + { let TyImplTrait { trait_name, span, @@ -118,7 +122,7 @@ fn check_supertraits(typed_tree_nodes: &[TyAstNode], namespace: &Namespace) -> V } }; if let CompileResult { - value: Some(TyDeclaration::TraitDeclaration(decl_id)), + value: Some(ty::TyDeclaration::TraitDeclaration(decl_id)), .. } = namespace.resolve_call_path(&trait_name) { @@ -131,8 +135,9 @@ fn check_supertraits(typed_tree_nodes: &[TyAstNode], namespace: &Namespace) -> V }; for supertrait in &tr.supertraits { if !typed_tree_nodes.iter().any(|search_node| { - if let TyAstNodeContent::Declaration(TyDeclaration::ImplTrait(decl_id)) = - &search_node.content + if let TyAstNodeContent::Declaration(ty::TyDeclaration::ImplTrait( + decl_id, + )) = &search_node.content { let TyImplTrait { trait_name: search_node_trait_name, @@ -147,11 +152,11 @@ fn check_supertraits(typed_tree_nodes: &[TyAstNode], namespace: &Namespace) -> V }; if let ( CompileResult { - value: Some(TyDeclaration::TraitDeclaration(decl_id1)), + value: Some(ty::TyDeclaration::TraitDeclaration(decl_id1)), .. }, CompileResult { - value: Some(TyDeclaration::TraitDeclaration(decl_id2)), + value: Some(ty::TyDeclaration::TraitDeclaration(decl_id2)), .. }, ) = ( diff --git a/sway-core/src/semantic_analysis/namespace/items.rs b/sway-core/src/semantic_analysis/namespace/items.rs index 6834ea98330..6b0023a4284 100644 --- a/sway-core/src/semantic_analysis/namespace/items.rs +++ b/sway-core/src/semantic_analysis/namespace/items.rs @@ -24,7 +24,7 @@ pub(crate) enum GlobImport { No, } -pub(super) type SymbolMap = im::OrdMap; +pub(super) type SymbolMap = im::OrdMap; pub(super) type UseSynonyms = im::HashMap, GlobImport)>; pub(super) type UseAliases = im::HashMap; @@ -98,17 +98,22 @@ impl Items { self.symbols().keys() } - pub(crate) fn insert_symbol(&mut self, name: Ident, item: TyDeclaration) -> CompileResult<()> { + pub(crate) fn insert_symbol( + &mut self, + name: Ident, + item: ty::TyDeclaration, + ) -> CompileResult<()> { let mut warnings = vec![]; let mut errors = vec![]; // purposefully do not preemptively return errors so that the // new definition allows later usages to compile if self.symbols.get(&name).is_some() { match item { - TyDeclaration::EnumDeclaration { .. } | TyDeclaration::StructDeclaration { .. } => { + ty::TyDeclaration::EnumDeclaration { .. } + | ty::TyDeclaration::StructDeclaration { .. } => { errors.push(CompileError::ShadowsOtherSymbol { name: name.clone() }); } - TyDeclaration::GenericTypeForFunctionScope { .. } => { + ty::TyDeclaration::GenericTypeForFunctionScope { .. } => { errors.push(CompileError::GenericShadowsGeneric { name: name.clone() }); } _ => { @@ -123,7 +128,7 @@ impl Items { ok((), warnings, errors) } - pub(crate) fn check_symbol(&self, name: &Ident) -> Result<&TyDeclaration, CompileError> { + pub(crate) fn check_symbol(&self, name: &Ident) -> Result<&ty::TyDeclaration, CompileError> { self.symbols .get(name) .ok_or_else(|| CompileError::SymbolNotFound { name: name.clone() }) diff --git a/sway-core/src/semantic_analysis/namespace/module.rs b/sway-core/src/semantic_analysis/namespace/module.rs index b06c50ea20a..fd63c9e77e4 100644 --- a/sway-core/src/semantic_analysis/namespace/module.rs +++ b/sway-core/src/semantic_analysis/namespace/module.rs @@ -1,12 +1,12 @@ use crate::{ error::*, - language::{parsed::*, Visibility}, + language::{parsed::*, ty, Visibility}, semantic_analysis::{ ast_node::{TyAstNode, TyAstNodeContent, TyVariableDeclaration}, declaration::VariableMutability, TypeCheckContext, }, - CompileResult, Ident, Namespace, TyDeclaration, + Ident, Namespace, }; use super::{ @@ -342,7 +342,7 @@ impl Module { errors.push(CompileError::ImportPrivateSymbol { name: item.clone() }); } // if this is a const, insert it into the local namespace directly - if let TyDeclaration::VariableDeclaration(ref var_decl) = decl { + if let ty::TyDeclaration::VariableDeclaration(ref var_decl) = decl { let TyVariableDeclaration { mutability, name, .. } = &**var_decl; diff --git a/sway-core/src/semantic_analysis/namespace/namespace.rs b/sway-core/src/semantic_analysis/namespace/namespace.rs index ae5a3c0ea28..02f3c92dfc7 100644 --- a/sway-core/src/semantic_analysis/namespace/namespace.rs +++ b/sway-core/src/semantic_analysis/namespace/namespace.rs @@ -1,7 +1,7 @@ use crate::{ language::{ty, CallPath}, type_system::*, - CompileResult, Ident, TyDeclaration, TyFunctionDeclaration, + CompileResult, Ident, TyFunctionDeclaration, }; use super::{module::Module, root::Root, submodule_namespace::SubmoduleNamespace, Path, PathBuf}; @@ -86,12 +86,15 @@ impl Namespace { } /// Short-hand for calling [Root::resolve_symbol] on `root` with the `mod_path`. - pub(crate) fn resolve_symbol(&self, symbol: &Ident) -> CompileResult<&TyDeclaration> { + pub(crate) fn resolve_symbol(&self, symbol: &Ident) -> CompileResult<&ty::TyDeclaration> { self.root.resolve_symbol(&self.mod_path, symbol) } /// Short-hand for calling [Root::resolve_call_path] on `root` with the `mod_path`. - pub(crate) fn resolve_call_path(&self, call_path: &CallPath) -> CompileResult<&TyDeclaration> { + pub(crate) fn resolve_call_path( + &self, + call_path: &CallPath, + ) -> CompileResult<&ty::TyDeclaration> { self.root.resolve_call_path(&self.mod_path, call_path) } diff --git a/sway-core/src/semantic_analysis/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index c147a99fda5..0b78ac17f39 100644 --- a/sway-core/src/semantic_analysis/namespace/root.rs +++ b/sway-core/src/semantic_analysis/namespace/root.rs @@ -2,7 +2,7 @@ use crate::{ error::*, language::{ty, CallPath}, type_system::*, - CompileResult, Ident, TyDeclaration, TyFunctionDeclaration, TypeInfo, + CompileResult, Ident, TyFunctionDeclaration, TypeInfo, }; use super::{module::Module, namespace::Namespace, Path}; @@ -34,7 +34,7 @@ impl Root { &self, mod_path: &Path, call_path: &CallPath, - ) -> CompileResult<&TyDeclaration> { + ) -> CompileResult<&ty::TyDeclaration> { let symbol_path: Vec<_> = mod_path .iter() .chain(&call_path.prefixes) @@ -52,7 +52,7 @@ impl Root { &self, mod_path: &Path, symbol: &Ident, - ) -> CompileResult<&TyDeclaration> { + ) -> CompileResult<&ty::TyDeclaration> { self.check_submodule(mod_path).flat_map(|module| { let true_symbol = self[mod_path] .use_aliases diff --git a/sway-core/src/semantic_analysis/program.rs b/sway-core/src/semantic_analysis/program.rs index 48c2d828fe3..fd2c635a59a 100644 --- a/sway-core/src/semantic_analysis/program.rs +++ b/sway-core/src/semantic_analysis/program.rs @@ -1,6 +1,6 @@ use super::{ - storage_only_types, TyAstNode, TyAstNodeContent, TyDeclaration, TyFunctionDeclaration, - TyImplTrait, TyStorageDeclaration, + storage_only_types, TyAstNode, TyAstNodeContent, TyFunctionDeclaration, TyImplTrait, + TyStorageDeclaration, }; use crate::{ declaration_engine::declaration_engine::{de_get_function, de_get_impl_trait, de_get_storage}, @@ -84,12 +84,12 @@ impl TyProgram { } let mut mains = Vec::new(); - let mut declarations = Vec::::new(); + let mut declarations = Vec::::new(); let mut abi_entries = Vec::new(); let mut fn_declarations = std::collections::HashSet::new(); for node in &root.all_nodes { match &node.content { - TyAstNodeContent::Declaration(TyDeclaration::FunctionDeclaration(decl_id)) => { + TyAstNodeContent::Declaration(ty::TyDeclaration::FunctionDeclaration(decl_id)) => { let func = check!( CompileResult::from(de_get_function(decl_id.clone(), &node.span)), return err(warnings, errors), @@ -106,11 +106,11 @@ impl TyProgram { .push(CompileError::MultipleDefinitionsOfFunction { name: func.name }); } - declarations.push(TyDeclaration::FunctionDeclaration(decl_id.clone())); + declarations.push(ty::TyDeclaration::FunctionDeclaration(decl_id.clone())); } // ABI entries are all functions declared in impl_traits on the contract type // itself. - TyAstNodeContent::Declaration(TyDeclaration::ImplTrait(decl_id)) => { + TyAstNodeContent::Declaration(ty::TyDeclaration::ImplTrait(decl_id)) => { let TyImplTrait { methods, implementing_for_type_id, @@ -155,9 +155,9 @@ impl TyProgram { // `storage` declarations are not allowed in non-contracts let storage_decl = declarations .iter() - .find(|decl| matches!(decl, TyDeclaration::StorageDeclaration(_))); + .find(|decl| matches!(decl, ty::TyDeclaration::StorageDeclaration(_))); - if let Some(TyDeclaration::StorageDeclaration(decl_id)) = storage_decl { + if let Some(ty::TyDeclaration::StorageDeclaration(decl_id)) = storage_decl { let TyStorageDeclaration { span, .. } = check!( CompileResult::from(de_get_storage(decl_id.clone(), &decl_id.span())), return err(warnings, errors), @@ -334,11 +334,11 @@ impl TyProgram { TyProgramKind::Contract { declarations, .. } => { let storage_decl = declarations .iter() - .find(|decl| matches!(decl, TyDeclaration::StorageDeclaration(_))); + .find(|decl| matches!(decl, ty::TyDeclaration::StorageDeclaration(_))); // Expecting at most a single storage declaration match storage_decl { - Some(TyDeclaration::StorageDeclaration(decl_id)) => { + Some(ty::TyDeclaration::StorageDeclaration(decl_id)) => { let decl = check!( CompileResult::from(de_get_storage(decl_id.clone(), &decl_id.span())), return err(warnings, errors), @@ -463,18 +463,18 @@ impl TyProgram { pub enum TyProgramKind { Contract { abi_entries: Vec, - declarations: Vec, + declarations: Vec, }, Library { name: Ident, }, Predicate { main_function: TyFunctionDeclaration, - declarations: Vec, + declarations: Vec, }, Script { main_function: TyFunctionDeclaration, - declarations: Vec, + declarations: Vec, }, } @@ -491,14 +491,14 @@ impl TyProgramKind { } fn disallow_impure_functions( - declarations: &[TyDeclaration], + declarations: &[ty::TyDeclaration], mains: &[TyFunctionDeclaration], ) -> Vec { let mut errs: Vec = vec![]; let fn_decls = declarations .iter() .filter_map(|decl| match decl { - TyDeclaration::FunctionDeclaration(decl_id) => { + ty::TyDeclaration::FunctionDeclaration(decl_id) => { match de_get_function(decl_id.clone(), &decl.span()) { Ok(fn_decl) => Some(fn_decl), Err(err) => { diff --git a/sway-core/src/semantic_analysis/storage_only_types.rs b/sway-core/src/semantic_analysis/storage_only_types.rs index 8bad8732dba..fea2d8a4bb5 100644 --- a/sway-core/src/semantic_analysis/storage_only_types.rs +++ b/sway-core/src/semantic_analysis/storage_only_types.rs @@ -7,7 +7,7 @@ use crate::{ error::*, language::ty, semantic_analysis::{ - TyAstNodeContent, TyCodeBlock, TyConstantDeclaration, TyDeclaration, TyEnumDeclaration, + TyAstNodeContent, TyCodeBlock, TyConstantDeclaration, TyEnumDeclaration, TyFunctionDeclaration, TyImplTrait, TyIntrinsicFunctionKind, TyReassignment, TyStorageDeclaration, TyStructDeclaration, }, @@ -170,11 +170,11 @@ fn check_type(ty: TypeId, span: Span, ignore_self: bool) -> CompileResult<()> { ok((), warnings, errors) } -fn decl_validate(decl: &TyDeclaration) -> CompileResult<()> { +fn decl_validate(decl: &ty::TyDeclaration) -> CompileResult<()> { let mut warnings: Vec = vec![]; let mut errors: Vec = vec![]; match decl { - TyDeclaration::VariableDeclaration(decl) => { + ty::TyDeclaration::VariableDeclaration(decl) => { check!( check_type(decl.body.return_type, decl.name.span(), false), (), @@ -183,7 +183,7 @@ fn decl_validate(decl: &TyDeclaration) -> CompileResult<()> { ); check!(expr_validate(&decl.body), (), warnings, errors) } - TyDeclaration::ConstantDeclaration(decl_id) => { + ty::TyDeclaration::ConstantDeclaration(decl_id) => { let TyConstantDeclaration { value: expr, name, .. } = check!( @@ -200,7 +200,7 @@ fn decl_validate(decl: &TyDeclaration) -> CompileResult<()> { ); check!(expr_validate(&expr), (), warnings, errors) } - TyDeclaration::FunctionDeclaration(decl_id) => { + ty::TyDeclaration::FunctionDeclaration(decl_id) => { let TyFunctionDeclaration { body, parameters, .. } = check!( @@ -224,10 +224,10 @@ fn decl_validate(decl: &TyDeclaration) -> CompileResult<()> { ); } } - TyDeclaration::AbiDeclaration(_) | TyDeclaration::TraitDeclaration(_) => { + ty::TyDeclaration::AbiDeclaration(_) | ty::TyDeclaration::TraitDeclaration(_) => { // These methods are not typed. They are however handled from ImplTrait. } - TyDeclaration::ImplTrait(decl_id) => { + ty::TyDeclaration::ImplTrait(decl_id) => { let TyImplTrait { methods, .. } = check!( CompileResult::from(de_get_impl_trait(decl_id.clone(), &decl_id.span())), return err(warnings, errors), @@ -243,7 +243,7 @@ fn decl_validate(decl: &TyDeclaration) -> CompileResult<()> { ) } } - TyDeclaration::StructDeclaration(decl_id) => { + ty::TyDeclaration::StructDeclaration(decl_id) => { let TyStructDeclaration { fields, .. } = check!( CompileResult::from(de_get_struct(decl_id.clone(), &decl_id.span())), return err(warnings, errors), @@ -259,7 +259,7 @@ fn decl_validate(decl: &TyDeclaration) -> CompileResult<()> { ); } } - TyDeclaration::EnumDeclaration(decl_id) => { + ty::TyDeclaration::EnumDeclaration(decl_id) => { let TyEnumDeclaration { variants, .. } = check!( CompileResult::from(de_get_enum(decl_id.clone(), &decl.span())), return err(warnings, errors), @@ -275,7 +275,7 @@ fn decl_validate(decl: &TyDeclaration) -> CompileResult<()> { ); } } - TyDeclaration::StorageDeclaration(decl_id) => { + ty::TyDeclaration::StorageDeclaration(decl_id) => { let TyStorageDeclaration { fields, .. } = check!( CompileResult::from(de_get_storage(decl_id.clone(), &decl.span())), return err(warnings, errors), @@ -291,7 +291,8 @@ fn decl_validate(decl: &TyDeclaration) -> CompileResult<()> { ); } } - TyDeclaration::GenericTypeForFunctionScope { .. } | TyDeclaration::ErrorRecovery => {} + ty::TyDeclaration::GenericTypeForFunctionScope { .. } + | ty::TyDeclaration::ErrorRecovery => {} } ok((), warnings, errors) } diff --git a/sway-core/src/type_system/type_binding.rs b/sway-core/src/type_system/type_binding.rs index c6237695bd0..bae0068fde4 100644 --- a/sway-core/src/type_system/type_binding.rs +++ b/sway-core/src/type_system/type_binding.rs @@ -2,11 +2,11 @@ use sway_types::{Span, Spanned}; use crate::{ declaration_engine::declaration_engine::*, - error::{err, ok}, - language::CallPath, + error::*, + language::{ty, CallPath}, semantic_analysis::TypeCheckContext, type_system::{insert_type, EnforceTypeArguments}, - CompileResult, TyDeclaration, TypeInfo, + TypeInfo, }; use super::{ReplaceSelfType, TypeArgument, TypeId}; @@ -131,7 +131,7 @@ impl TypeBinding { pub(crate) fn type_check_with_ident( &mut self, ctx: &TypeCheckContext, - ) -> CompileResult { + ) -> CompileResult { let mut warnings = vec![]; let mut errors = vec![]; @@ -150,7 +150,7 @@ impl TypeBinding { // monomorphize the declaration, if needed let new_decl = match unknown_decl { - TyDeclaration::FunctionDeclaration(original_id) => { + ty::TyDeclaration::FunctionDeclaration(original_id) => { // get the copy from the declaration engine let mut new_copy = check!( CompileResult::from(de_get_function(original_id.clone(), &self.span())), @@ -178,9 +178,9 @@ impl TypeBinding { // add the new copy as a monomorphized copy of the original id de_add_monomorphized_copy(original_id, new_id.clone()); - TyDeclaration::FunctionDeclaration(new_id) + ty::TyDeclaration::FunctionDeclaration(new_id) } - TyDeclaration::EnumDeclaration(original_id) => { + ty::TyDeclaration::EnumDeclaration(original_id) => { // get the copy from the declaration engine let mut new_copy = check!( CompileResult::from(de_get_enum(original_id.clone(), &self.span())), @@ -208,9 +208,9 @@ impl TypeBinding { // add the new copy as a monomorphized copy of the original id de_add_monomorphized_copy(original_id, new_id.clone()); - TyDeclaration::EnumDeclaration(new_id) + ty::TyDeclaration::EnumDeclaration(new_id) } - TyDeclaration::StructDeclaration(original_id) => { + ty::TyDeclaration::StructDeclaration(original_id) => { // get the copy from the declaration engine let mut new_copy = check!( CompileResult::from(de_get_struct(original_id.clone(), &self.span())), @@ -238,7 +238,7 @@ impl TypeBinding { // add the new copy as a monomorphized copy of the original id de_add_monomorphized_copy(original_id, new_id.clone()); - TyDeclaration::StructDeclaration(new_id) + ty::TyDeclaration::StructDeclaration(new_id) } _ => unknown_decl, }; diff --git a/sway-core/src/type_system/type_engine.rs b/sway-core/src/type_system/type_engine.rs index 837e479254c..793cde71a82 100644 --- a/sway-core/src/type_system/type_engine.rs +++ b/sway-core/src/type_system/type_engine.rs @@ -1,10 +1,11 @@ -use super::*; -use crate::concurrent_slab::ConcurrentSlab; -use crate::declaration_engine::{ - de_add_monomorphized_enum_copy, de_add_monomorphized_struct_copy, de_get_enum, de_get_struct, +use crate::{ + concurrent_slab::ConcurrentSlab, + declaration_engine::*, + language::ty, + namespace::{Path, Root}, + type_system::*, }; -use crate::namespace::{Path, Root}; -use crate::TyDeclaration; + use lazy_static::lazy_static; use sway_error::error::CompileError; use sway_error::type_error::TypeError; @@ -531,7 +532,7 @@ impl TypeEngine { .ok(&mut warnings, &mut errors) .cloned() { - Some(TyDeclaration::StructDeclaration(original_id)) => { + Some(ty::TyDeclaration::StructDeclaration(original_id)) => { // get the copy from the declaration engine let mut new_copy = check!( CompileResult::from(de_get_struct(original_id.clone(), &name.span())), @@ -564,7 +565,7 @@ impl TypeEngine { // return the id type_id } - Some(TyDeclaration::EnumDeclaration(original_id)) => { + Some(ty::TyDeclaration::EnumDeclaration(original_id)) => { // get the copy from the declaration engine let mut new_copy = check!( CompileResult::from(de_get_enum(original_id.clone(), &name.span())), @@ -597,7 +598,7 @@ impl TypeEngine { // return the id type_id } - Some(TyDeclaration::GenericTypeForFunctionScope { type_id, .. }) => type_id, + Some(ty::TyDeclaration::GenericTypeForFunctionScope { type_id, .. }) => type_id, _ => { errors.push(CompileError::UnknownTypeName { name: name.to_string(), diff --git a/sway-core/src/type_system/type_parameter.rs b/sway-core/src/type_system/type_parameter.rs index 54f45d80f4a..21e21614a78 100644 --- a/sway-core/src/type_system/type_parameter.rs +++ b/sway-core/src/type_system/type_parameter.rs @@ -1,4 +1,4 @@ -use crate::{error::*, semantic_analysis::*, type_system::*}; +use crate::{error::*, language::ty, semantic_analysis::*, type_system::*}; use sway_error::error::CompileError; use sway_types::{ident::Ident, span::Span, JsonTypeDeclaration, Spanned}; @@ -79,7 +79,7 @@ impl TypeParameter { let type_id = insert_type(TypeInfo::UnknownGeneric { name: type_parameter.name_ident.clone(), }); - let type_parameter_decl = TyDeclaration::GenericTypeForFunctionScope { + let type_parameter_decl = ty::TyDeclaration::GenericTypeForFunctionScope { name: type_parameter.name_ident.clone(), type_id, }; diff --git a/sway-lsp/src/capabilities/hover.rs b/sway-lsp/src/capabilities/hover.rs index e4c1f371196..5f3eaab4f47 100644 --- a/sway-lsp/src/capabilities/hover.rs +++ b/sway-lsp/src/capabilities/hover.rs @@ -11,8 +11,7 @@ use crate::{ }; use sway_core::{ declaration_engine, - language::{parsed::Declaration, Visibility}, - semantic_analysis::ast_node::TyDeclaration, + language::{parsed::Declaration, ty, Visibility}, }; use sway_types::{Ident, Spanned}; use tower_lsp::lsp_types::{Hover, HoverContents, HoverParams, MarkupContent, MarkupKind}; @@ -59,26 +58,26 @@ fn hover_format(token: &Token, ident: &Ident) -> Hover { let value = match &token.typed { Some(typed_token) => match typed_token { TypedAstToken::TypedDeclaration(decl) => match decl { - TyDeclaration::VariableDeclaration(var_decl) => { + ty::TyDeclaration::VariableDeclaration(var_decl) => { let type_name = format!("{}", var_decl.type_ascription); format_variable_hover(var_decl.mutability.is_mutable(), type_name) } - TyDeclaration::FunctionDeclaration(func) => extract_fn_signature(&func.span()), - TyDeclaration::StructDeclaration(decl_id) => { + ty::TyDeclaration::FunctionDeclaration(func) => extract_fn_signature(&func.span()), + ty::TyDeclaration::StructDeclaration(decl_id) => { declaration_engine::de_get_struct(decl_id.clone(), &decl.span()) .map(|struct_decl| { format_visibility_hover(struct_decl.visibility, decl.friendly_name()) }) .unwrap_or(token_name) } - TyDeclaration::TraitDeclaration(ref decl_id) => { + ty::TyDeclaration::TraitDeclaration(ref decl_id) => { declaration_engine::de_get_trait(decl_id.clone(), &decl.span()) .map(|trait_decl| { format_visibility_hover(trait_decl.visibility, decl.friendly_name()) }) .unwrap_or(token_name) } - TyDeclaration::EnumDeclaration(decl_id) => { + ty::TyDeclaration::EnumDeclaration(decl_id) => { declaration_engine::de_get_enum(decl_id.clone(), &decl.span()) .map(|enum_decl| { format_visibility_hover(enum_decl.visibility, decl.friendly_name()) diff --git a/sway-lsp/src/core/token.rs b/sway-lsp/src/core/token.rs index a1a76769730..d972d97d191 100644 --- a/sway-lsp/src/core/token.rs +++ b/sway-lsp/src/core/token.rs @@ -9,8 +9,8 @@ use sway_core::{ ty, }, semantic_analysis::ast_node::{ - TyDeclaration, TyEnumVariant, TyFunctionDeclaration, TyFunctionParameter, TyReassignment, - TyStorageField, TyStorageReassignDescriptor, TyStructField, TyTraitFn, + TyEnumVariant, TyFunctionDeclaration, TyFunctionParameter, TyReassignment, TyStorageField, + TyStorageReassignDescriptor, TyStructField, TyTraitFn, }, type_system::TypeId, }; @@ -60,7 +60,7 @@ pub enum AstToken { #[derive(Debug, Clone)] pub enum TypedAstToken { - TypedDeclaration(TyDeclaration), + TypedDeclaration(ty::TyDeclaration), TypedExpression(ty::TyExpression), TypedFunctionDeclaration(TyFunctionDeclaration), TypedFunctionParameter(TyFunctionParameter), diff --git a/sway-lsp/src/core/traverse_typed_tree.rs b/sway-lsp/src/core/traverse_typed_tree.rs index 109b8f0796d..6afb7645c61 100644 --- a/sway-lsp/src/core/traverse_typed_tree.rs +++ b/sway-lsp/src/core/traverse_typed_tree.rs @@ -11,7 +11,7 @@ use sway_core::{ code_block::TyCodeBlock, expression::TyIntrinsicFunctionKind, ProjectionKind, TyFunctionDeclaration, TyFunctionParameter, TyImplTrait, TyTraitFn, - {TyAstNode, TyAstNodeContent, TyDeclaration}, + {TyAstNode, TyAstNodeContent}, }, }; use sway_types::{ident::Ident, Spanned}; @@ -27,9 +27,9 @@ pub fn traverse_node(node: &TyAstNode, tokens: &TokenMap) { }; } -fn handle_declaration(declaration: &TyDeclaration, tokens: &TokenMap) { +fn handle_declaration(declaration: &ty::TyDeclaration, tokens: &TokenMap) { match declaration { - TyDeclaration::VariableDeclaration(variable) => { + ty::TyDeclaration::VariableDeclaration(variable) => { if let Some(mut token) = tokens.get_mut(&to_ident_key(&variable.name)) { token.typed = Some(TypedAstToken::TypedDeclaration(declaration.clone())); } @@ -44,7 +44,7 @@ fn handle_declaration(declaration: &TyDeclaration, tokens: &TokenMap) { handle_expression(&variable.body, tokens); } - TyDeclaration::ConstantDeclaration(decl_id) => { + ty::TyDeclaration::ConstantDeclaration(decl_id) => { if let Ok(const_decl) = declaration_engine::de_get_constant(decl_id.clone(), &decl_id.span()) { @@ -54,14 +54,14 @@ fn handle_declaration(declaration: &TyDeclaration, tokens: &TokenMap) { handle_expression(&const_decl.value, tokens); } } - TyDeclaration::FunctionDeclaration(decl_id) => { + ty::TyDeclaration::FunctionDeclaration(decl_id) => { if let Ok(func_decl) = declaration_engine::de_get_function(decl_id.clone(), &decl_id.span()) { collect_typed_fn_decl(&func_decl, tokens); } } - TyDeclaration::TraitDeclaration(decl_id) => { + ty::TyDeclaration::TraitDeclaration(decl_id) => { if let Ok(trait_decl) = declaration_engine::de_get_trait(decl_id.clone(), &decl_id.span()) { @@ -74,7 +74,7 @@ fn handle_declaration(declaration: &TyDeclaration, tokens: &TokenMap) { } } } - TyDeclaration::StructDeclaration(decl_id) => { + ty::TyDeclaration::StructDeclaration(decl_id) => { if let Ok(struct_decl) = declaration_engine::de_get_struct(decl_id.clone(), &declaration.span()) { @@ -104,7 +104,7 @@ fn handle_declaration(declaration: &TyDeclaration, tokens: &TokenMap) { } } } - TyDeclaration::EnumDeclaration(decl_id) => { + ty::TyDeclaration::EnumDeclaration(decl_id) => { if let Ok(enum_decl) = declaration_engine::de_get_enum(decl_id.clone(), &decl_id.span()) { if let Some(mut token) = tokens.get_mut(&to_ident_key(&enum_decl.name)) { @@ -133,7 +133,7 @@ fn handle_declaration(declaration: &TyDeclaration, tokens: &TokenMap) { } } } - TyDeclaration::ImplTrait(decl_id) => { + ty::TyDeclaration::ImplTrait(decl_id) => { if let Ok(TyImplTrait { trait_name, methods, @@ -165,7 +165,7 @@ fn handle_declaration(declaration: &TyDeclaration, tokens: &TokenMap) { } } } - TyDeclaration::AbiDeclaration(decl_id) => { + ty::TyDeclaration::AbiDeclaration(decl_id) => { if let Ok(abi_decl) = declaration_engine::de_get_abi(decl_id.clone(), &decl_id.span()) { if let Some(mut token) = tokens.get_mut(&to_ident_key(&abi_decl.name)) { token.typed = Some(TypedAstToken::TypedDeclaration(declaration.clone())); @@ -176,13 +176,13 @@ fn handle_declaration(declaration: &TyDeclaration, tokens: &TokenMap) { } } } - TyDeclaration::GenericTypeForFunctionScope { name, .. } => { + ty::TyDeclaration::GenericTypeForFunctionScope { name, .. } => { if let Some(mut token) = tokens.get_mut(&to_ident_key(name)) { token.typed = Some(TypedAstToken::TypedDeclaration(declaration.clone())); } } - TyDeclaration::ErrorRecovery => {} - TyDeclaration::StorageDeclaration(decl_id) => { + ty::TyDeclaration::ErrorRecovery => {} + ty::TyDeclaration::StorageDeclaration(decl_id) => { if let Ok(storage_decl) = declaration_engine::de_get_storage(decl_id.clone(), &decl_id.span()) { diff --git a/sway-lsp/src/utils/token.rs b/sway-lsp/src/utils/token.rs index 13966b2f4d2..1e76d82df31 100644 --- a/sway-lsp/src/utils/token.rs +++ b/sway-lsp/src/utils/token.rs @@ -1,7 +1,10 @@ use crate::core::token::{AstToken, SymbolKind, Token, TokenMap, TypedAstToken}; -use sway_core::declaration_engine; -use sway_core::semantic_analysis::ast_node::{declaration::TyStructDeclaration, TyDeclaration}; -use sway_core::type_system::{TypeId, TypeInfo}; +use sway_core::{ + declaration_engine, + language::ty, + semantic_analysis::ast_node::declaration::TyStructDeclaration, + type_system::{TypeId, TypeInfo}, +}; use sway_types::{ident::Ident, span::Span, Spanned}; pub fn is_initial_declaration(token_type: &Token) -> bool { @@ -39,7 +42,10 @@ pub(crate) fn to_ident_key(ident: &Ident) -> (Ident, Span) { } /// Uses the TypeId to find the associated TypedDeclaration in the TokenMap. -pub(crate) fn declaration_of_type_id(type_id: &TypeId, tokens: &TokenMap) -> Option { +pub(crate) fn declaration_of_type_id( + type_id: &TypeId, + tokens: &TokenMap, +) -> Option { ident_of_type_id(type_id) .and_then(|decl_ident| tokens.try_get(&to_ident_key(&decl_ident)).try_unwrap()) .map(|item| item.value().clone()) @@ -57,7 +63,7 @@ pub(crate) fn struct_declaration_of_type_id( tokens: &TokenMap, ) -> Option { declaration_of_type_id(type_id, tokens).and_then(|decl| match decl { - TyDeclaration::StructDeclaration(ref decl_id) => { + ty::TyDeclaration::StructDeclaration(ref decl_id) => { declaration_engine::de_get_struct(decl_id.clone(), &decl_id.span()).ok() } _ => None, @@ -96,8 +102,8 @@ pub(crate) fn type_id(token_type: &Token) -> Option { match &token_type.typed { Some(typed_ast_token) => match typed_ast_token { TypedAstToken::TypedDeclaration(dec) => match dec { - TyDeclaration::VariableDeclaration(var_decl) => Some(var_decl.type_ascription), - TyDeclaration::ConstantDeclaration(decl_id) => { + ty::TyDeclaration::VariableDeclaration(var_decl) => Some(var_decl.type_ascription), + ty::TyDeclaration::ConstantDeclaration(decl_id) => { declaration_engine::de_get_constant(decl_id.clone(), &decl_id.span()) .ok() .map(|const_decl| const_decl.value.return_type)