Skip to content

Commit

Permalink
Simplify the lifetimes around engines (FuelLabs#4629)
Browse files Browse the repository at this point in the history
## Description
Inspired by FuelLabs#4588

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
IGI-111 authored Jun 5, 2023
1 parent 6921e71 commit d5464e0
Show file tree
Hide file tree
Showing 129 changed files with 949 additions and 1,155 deletions.
28 changes: 12 additions & 16 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ use sway_core::{
parsed::{ParseProgram, TreeType},
ty, Visibility,
},
query_engine::QueryEngine,
semantic_analysis::namespace,
source_map::SourceMap,
transform::AttributeKind,
BuildTarget, CompileResult, Engines, FinalizedEntry, TypeEngine,
BuildTarget, CompileResult, Engines, FinalizedEntry,
};
use sway_error::{error::CompileError, warning::CompileWarning};
use sway_types::{Ident, Span, Spanned};
Expand Down Expand Up @@ -1583,7 +1582,7 @@ pub fn dependency_namespace(
compiled_contract_deps: &CompiledContractDeps,
graph: &Graph,
node: NodeIx,
engines: Engines<'_>,
engines: &Engines,
contract_id_value: Option<ContractIdConst>,
) -> Result<namespace::Module, vec1::Vec1<CompileError>> {
// TODO: Clean this up when config-time constants v1 are removed.
Expand Down Expand Up @@ -1719,7 +1718,7 @@ fn find_core_dep(graph: &Graph, node: NodeIx) -> Option<NodeIx> {
pub fn compile_ast(
pkg: &PackageDescriptor,
build_profile: &BuildProfile,
engines: Engines<'_>,
engines: &Engines,
namespace: namespace::Module,
package_name: &str,
metrics: &mut PerformanceData,
Expand Down Expand Up @@ -1763,7 +1762,7 @@ pub fn compile_ast(
pub fn compile(
pkg: &PackageDescriptor,
profile: &BuildProfile,
engines: Engines<'_>,
engines: &Engines,
namespace: namespace::Module,
source_map: &mut SourceMap,
) -> Result<CompiledPackage> {
Expand Down Expand Up @@ -1845,7 +1844,7 @@ pub fn compile(
let abi = time_expr!(
"generate JSON ABI program",
"generate_json_abi",
evm_json_abi::generate_json_abi_program(typed_program, &engines),
evm_json_abi::generate_json_abi_program(typed_program, engines),
Some(sway_build_config.clone()),
metrics
);
Expand Down Expand Up @@ -2241,10 +2240,7 @@ pub fn build(
.flat_map(|output_node| plan.node_deps(*output_node))
.collect();

let type_engine = TypeEngine::default();
let decl_engine = DeclEngine::default();
let query_engine = QueryEngine::default();
let engines = Engines::new(&type_engine, &decl_engine, &query_engine);
let engines = Engines::default();
let include_tests = profile.include_tests;

// This is the Contract ID of the current contract being compiled.
Expand Down Expand Up @@ -2306,7 +2302,7 @@ pub fn build(
&compiled_contract_deps,
plan.graph(),
node,
engines,
&engines,
None,
) {
Ok(o) => o,
Expand All @@ -2316,7 +2312,7 @@ pub fn build(
let compiled_without_tests = compile(
&descriptor,
&profile,
engines,
&engines,
dep_namespace,
&mut source_map,
)?;
Expand Down Expand Up @@ -2369,7 +2365,7 @@ pub fn build(
&compiled_contract_deps,
plan.graph(),
node,
engines,
&engines,
contract_id_value.clone(),
) {
Ok(o) => o,
Expand All @@ -2379,7 +2375,7 @@ pub fn build(
let mut compiled = compile(
&descriptor,
&profile,
engines,
&engines,
dep_namespace,
&mut source_map,
)?;
Expand Down Expand Up @@ -2560,7 +2556,7 @@ pub fn check(
build_target: BuildTarget,
terse_mode: bool,
include_tests: bool,
engines: Engines<'_>,
engines: &Engines,
) -> anyhow::Result<Vec<CompileResult<Programs>>> {
let mut lib_namespace_map = Default::default();
let mut source_map = SourceMap::new();
Expand Down Expand Up @@ -2642,7 +2638,7 @@ pub fn parse(
build_target: BuildTarget,
terse_mode: bool,
include_tests: bool,
engines: Engines<'_>,
engines: &Engines,
) -> anyhow::Result<CompileResult<(LexedProgram, ParseProgram)>> {
let profile = BuildProfile {
terse: terse_mode,
Expand Down
40 changes: 12 additions & 28 deletions forc-plugins/forc-doc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ use std::{
process::Command as Process,
{fs, path::PathBuf},
};
use sway_core::{
decl_engine::DeclEngine, language::ty::TyProgram, query_engine::QueryEngine, BuildTarget,
Engines, TypeEngine,
};
use sway_core::{language::ty::TyProgram, BuildTarget, Engines};

mod cli;
mod doc;
Expand All @@ -30,26 +27,19 @@ pub(crate) const ASSETS_DIR_NAME: &str = "static.files";
#[derive(Clone)]
struct RenderPlan<'e> {
document_private_items: bool,
type_engine: &'e TypeEngine,
decl_engine: &'e DeclEngine,
engines: &'e Engines,
}
impl<'e> RenderPlan<'e> {
fn new(
document_private_items: bool,
type_engine: &'e TypeEngine,
decl_engine: &'e DeclEngine,
) -> RenderPlan<'e> {
fn new(document_private_items: bool, engines: &'e Engines) -> RenderPlan<'e> {
Self {
document_private_items,
type_engine,
decl_engine,
engines,
}
}
}
struct ProgramInfo<'a> {
ty_program: TyProgram,
type_engine: &'a TypeEngine,
decl_engine: &'a DeclEngine,
engines: &'a Engines,
manifest: &'a ManifestFile,
pkg_manifest: &'a PackageManifestFile,
}
Expand Down Expand Up @@ -96,17 +86,14 @@ pub fn main() -> Result<()> {
build_instructions.offline,
)?;

let type_engine = TypeEngine::default();
let decl_engine = DeclEngine::default();
let query_engine = QueryEngine::default();
let engines = Engines::new(&type_engine, &decl_engine, &query_engine);
let engines = Engines::default();
let tests_enabled = build_instructions.document_private_items;
let mut compile_results = pkg::check(
&plan,
BuildTarget::default(),
build_instructions.silent,
tests_enabled,
engines,
&engines,
)?;

if !build_instructions.no_deps {
Expand All @@ -128,8 +115,7 @@ pub fn main() -> Result<()> {
};
let program_info = ProgramInfo {
ty_program,
type_engine: &type_engine,
decl_engine: &decl_engine,
engines: &engines,
manifest: &manifest_file,
pkg_manifest: pkg_manifest_file,
};
Expand All @@ -151,8 +137,7 @@ pub fn main() -> Result<()> {
};
let program_info = ProgramInfo {
ty_program,
type_engine: &type_engine,
decl_engine: &decl_engine,
engines: &engines,
manifest: &manifest,
pkg_manifest,
};
Expand Down Expand Up @@ -213,8 +198,7 @@ fn build_docs(
} = *build_instructions;
let ProgramInfo {
ty_program,
type_engine,
decl_engine,
engines,
manifest,
pkg_manifest,
} = program_info;
Expand All @@ -227,7 +211,7 @@ fn build_docs(
);

let raw_docs = Documentation::from_ty_program(
decl_engine,
engines.de(),
pkg_manifest.project_name(),
&ty_program,
document_private_items,
Expand All @@ -242,7 +226,7 @@ fn build_docs(
// render docs to HTML
let rendered_docs = RenderedDocumentation::from_raw_docs(
raw_docs,
RenderPlan::new(document_private_items, type_engine, decl_engine),
RenderPlan::new(document_private_items, engines),
root_attributes,
ty_program.kind,
forc_version,
Expand Down
8 changes: 4 additions & 4 deletions forc-plugins/forc-doc/src/render/item/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Renderable for Context {
for field in fields {
let struct_field_id = format!("structfield.{}", field.name.as_str());
let type_anchor = render_type_anchor(
render_plan.type_engine.get(field.type_argument.type_id),
render_plan.engines.te().get(field.type_argument.type_id),
&render_plan,
&self.module_info,
);
Expand Down Expand Up @@ -80,7 +80,7 @@ impl Renderable for Context {
for field in fields {
let storage_field_id = format!("storagefield.{}", field.name.as_str());
let type_anchor = render_type_anchor(
render_plan.type_engine.get(field.type_argument.type_id),
render_plan.engines.te().get(field.type_argument.type_id),
&render_plan,
&self.module_info,
);
Expand Down Expand Up @@ -108,7 +108,7 @@ impl Renderable for Context {
for variant in variants {
let enum_variant_id = format!("variant.{}", variant.name.as_str());
let type_anchor = render_type_anchor(
render_plan.type_engine.get(variant.type_argument.type_id),
render_plan.engines.te().get(variant.type_argument.type_id),
&render_plan,
&self.module_info,
);
Expand Down Expand Up @@ -449,7 +449,7 @@ impl Renderable for TyTraitItem {
TyTraitItem::Fn(item_fn) => item_fn,
TyTraitItem::Constant(_) => unimplemented!("Constant Trait items not yet implemented"),
};
let method = render_plan.decl_engine.get_function(item.id());
let method = render_plan.engines.de().get_function(item.id());
let attributes = method.attributes.to_html_string();

let mut fn_sig = format!("fn {}(", method.name.as_str());
Expand Down
8 changes: 4 additions & 4 deletions forc-plugins/forc-doc/src/render/item/type_anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) fn render_type_anchor(
match type_info {
TypeInfo::Array(ty_arg, len) => {
let inner = render_type_anchor(
render_plan.type_engine.get(ty_arg.type_id),
render_plan.engines.te().get(ty_arg.type_id),
render_plan,
current_module_info,
)?;
Expand All @@ -36,7 +36,7 @@ pub(crate) fn render_type_anchor(
let mut rendered_args: Vec<_> = Vec::new();
for ty_arg in ty_args {
rendered_args.push(render_type_anchor(
render_plan.type_engine.get(ty_arg.type_id),
render_plan.engines.te().get(ty_arg.type_id),
render_plan,
current_module_info,
)?)
Expand All @@ -50,7 +50,7 @@ pub(crate) fn render_type_anchor(
})
}
TypeInfo::Enum(decl_ref) => {
let enum_decl = render_plan.decl_engine.get_enum(&decl_ref);
let enum_decl = render_plan.engines.de().get_enum(&decl_ref);
if !render_plan.document_private_items && enum_decl.visibility.is_private() {
Ok(box_html! {
: decl_ref.name().clone().as_str();
Expand All @@ -67,7 +67,7 @@ pub(crate) fn render_type_anchor(
}
}
TypeInfo::Struct(decl_ref) => {
let struct_decl = render_plan.decl_engine.get_struct(&decl_ref);
let struct_decl = render_plan.engines.de().get_struct(&decl_ref);
if !render_plan.document_private_items && struct_decl.visibility.is_private() {
Ok(box_html! {
: decl_ref.name().clone().as_str();
Expand Down
11 changes: 3 additions & 8 deletions forc/src/cli/commands/check.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::ops::forc_check;
use clap::Parser;
use forc_util::{forc_result_bail, ForcResult};
use sway_core::{
decl_engine::DeclEngine, query_engine::QueryEngine, BuildTarget, Engines, TypeEngine,
};
use sway_core::{BuildTarget, Engines};

/// Check the current or target project and all of its dependencies for errors.
///
Expand Down Expand Up @@ -34,11 +32,8 @@ pub struct Command {
}

pub(crate) fn exec(command: Command) -> ForcResult<()> {
let type_engine = TypeEngine::default();
let decl_engine = DeclEngine::default();
let query_engine = QueryEngine::default();
let engines = Engines::new(&type_engine, &decl_engine, &query_engine);
let res = forc_check::check(command, engines)?;
let engines = Engines::default();
let res = forc_check::check(command, &engines)?;
if !res.is_ok() {
forc_result_bail!("unable to type check");
}
Expand Down
2 changes: 1 addition & 1 deletion forc/src/ops/forc_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use pkg::manifest::ManifestFile;
use std::path::PathBuf;
use sway_core::{language::ty, CompileResult, Engines};

pub fn check(command: CheckCommand, engines: Engines<'_>) -> Result<CompileResult<ty::TyProgram>> {
pub fn check(command: CheckCommand, engines: &Engines) -> Result<CompileResult<ty::TyProgram>> {
let CheckCommand {
build_target,
path,
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/abi_generation/evm_json_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
Engines, TypeArgument, TypeEngine, TypeId, TypeInfo,
};

pub fn generate_json_abi_program(program: &TyProgram, engines: &Engines<'_>) -> EvmAbiResult {
pub fn generate_json_abi_program(program: &TyProgram, engines: &Engines) -> EvmAbiResult {
let type_engine = engines.te();
let decl_engine = engines.de();
match &program.kind {
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/concurrent_slab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl ConcurrentSlab<TypeInfo> {
index: TypeId,
prev_value: &TypeInfo,
new_value: TypeInfo,
engines: Engines<'_>,
engines: &Engines,
) -> Option<TypeInfo> {
let index = index.index();
// The comparison below ends up calling functions in the slab, which
Expand Down
Loading

0 comments on commit d5464e0

Please sign in to comment.