Skip to content

Commit

Permalink
Merge branch 'master' into kayagokalp/deploy-default-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaBatty authored Jul 15, 2024
2 parents e96b983 + 4adae25 commit bc59a7b
Show file tree
Hide file tree
Showing 337 changed files with 8,492 additions and 1,217 deletions.
7 changes: 6 additions & 1 deletion docs/book/spell-check-custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,9 @@ namespacing
unsafety
prioritizations
polymorphism
ContractId
ContractId
booleans
underflows
Codec
bool
str
1 change: 1 addition & 0 deletions docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [The Fuel Toolchain](./introduction/fuel_toolchain.md)
- [A Forc Project](./introduction/forc_project.md)
- [Standard Library](./introduction/standard_library.md)
- [Core Library](./introduction/core_library.md)
- [Sway Language Standards](./introduction/sway_standards.md)
- [Examples](./examples/index.md)
- [Counter](./examples/counter.md)
Expand Down
30 changes: 30 additions & 0 deletions docs/book/src/introduction/core_library.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Core Library

The Sway Core Library, like the name suggests contains core operators and logic for the primitive types of the Sway programming language. These traits and methods are an extension of the [primitive types](https://docs.fuel.network/docs/sway/basics/built_in_types/#primitive-types) `u8`, `u16`, `u32`, `u64`, `u256`, `str[]`, `str`, `bool` and , `b256` and can be used where appropriate.

The latest core library documentation can be found [here](https://fuellabs.github.io/sway/master/core/). If the latest version is not compatible please refer to the appropriate tagged release.

## Using the Core Library

Core library functionalities do not need to be explicitly imported and will work out of the box after creating any new Sway project with [`forc new`](../forc/commands/forc_new.md). The `use` keyword is simply not required.

Consider this example of using the modulo function for two like value types:

```sway
let val_1 = 10;
let val_2 = 2;
let result = val_1 % val_2;
```

## Core Library Prelude

The prelude contains a list of operations essential to all Sway programs. The latest version of the prelude can be found [here](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/prelude.sw).

- [`core::primitives::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/primitives.sw)
- [`core::primitive_conversions::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/primitive_conversions.sw)
- [`core::raw_ptr::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/raw_ptr.sw)
- [`core::raw_slice::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/raw_slice.sw)
- [`core::ops::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/ops.sw)
- [`core::storage::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/storage.sw)
- [`core::str::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/str.sw)
- [`core::codec::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/codec.sw)
1 change: 1 addition & 0 deletions docs/book/src/introduction/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ To get started with Forc and Sway smart contract development, install the Fuel t
- [The Fuel Toolchain](./fuel_toolchain.md)
- [A Forc Project](./forc_project.md)
- [Standard Library](./standard_library.md)
- [Core Library](./core_library.md)
- [Sway Language Standards](./sway_standards.md)
2 changes: 1 addition & 1 deletion docs/book/src/sway-program-types/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Every Sway file _must_ begin with a declaration of what type of program it is. A

Contracts are used primarily for protocols or systems that operate within a fixed set of rules. A good example would be a staking contract or a decentralized exchange (also called a DEX).

Scripts are used for complex on-chain interactions that won't persist. An example of this may be using a DEX and Lender to create a leveraged position (borrow, swap, re-collateralize, borrow) which is a complex transaction that would usually take multiple steps.
Scripts are used for complex on-chain interactions that won't persist. An example of this may be using a DEX and Lender to create a leveraged position (borrow, swap, re-collateralize) which is a complex transaction that would usually take multiple steps.

Libraries are for code that is reusable and useful for handling common situations. A good example of this would be a library to handle fixed-point math or big number math.

Expand Down
6 changes: 4 additions & 2 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1666,20 +1666,22 @@ pub fn dependency_namespace(
let mut root = namespace::Root::from(root_module);

if core_added {
let _ = root.star_import_with_reexports(
let _ = root.star_import(
&Handler::default(),
engines,
&[CORE, PRELUDE].map(|s| Ident::new_no_span(s.into())),
&[],
Visibility::Private,
);
}

if has_std_dep(graph, node) {
let _ = root.star_import_with_reexports(
let _ = root.star_import(
&Handler::default(),
engines,
&[STD, PRELUDE].map(|s| Ident::new_no_span(s.into())),
&[],
Visibility::Private,
);
}

Expand Down
12 changes: 6 additions & 6 deletions forc-plugins/forc-doc/src/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{
};
use sway_core::{
decl_engine::DeclEngine,
language::ty::{TyAstNodeContent, TyDecl, TyImplTrait, TyModule, TyProgram, TySubmodule},
language::ty::{TyAstNodeContent, TyDecl, TyImplSelfOrTrait, TyModule, TyProgram, TySubmodule},
Engines,
};
use sway_types::BaseIdent;
Expand All @@ -36,7 +36,7 @@ impl Documentation {
) -> Result<Documentation> {
// the first module prefix will always be the project name
let mut docs = Documentation::default();
let mut impl_traits: Vec<(TyImplTrait, ModuleInfo)> = Vec::new();
let mut impl_traits: Vec<(TyImplSelfOrTrait, ModuleInfo)> = Vec::new();
let module_info = ModuleInfo::from_ty_module(vec![project_name.to_owned()], None);
Documentation::from_ty_module(
engines.de(),
Expand Down Expand Up @@ -144,14 +144,14 @@ impl Documentation {
module_info: &ModuleInfo,
ty_module: &TyModule,
docs: &mut Documentation,
impl_traits: &mut Vec<(TyImplTrait, ModuleInfo)>,
impl_traits: &mut Vec<(TyImplSelfOrTrait, ModuleInfo)>,
document_private_items: bool,
) -> Result<()> {
for ast_node in &ty_module.all_nodes {
if let TyAstNodeContent::Declaration(ref decl) = ast_node.content {
if let TyDecl::ImplTrait(impl_trait) = decl {
if let TyDecl::ImplSelfOrTrait(impl_trait) = decl {
impl_traits.push((
(*decl_engine.get_impl_trait(&impl_trait.decl_id)).clone(),
(*decl_engine.get_impl_self_or_trait(&impl_trait.decl_id)).clone(),
module_info.clone(),
));
} else {
Expand All @@ -175,7 +175,7 @@ impl Documentation {
decl_engine: &DeclEngine,
typed_submodule: &TySubmodule,
docs: &mut Documentation,
impl_traits: &mut Vec<(TyImplTrait, ModuleInfo)>,
impl_traits: &mut Vec<(TyImplSelfOrTrait, ModuleInfo)>,
module_info: &ModuleInfo,
document_private_items: bool,
) -> Result<()> {
Expand Down
6 changes: 3 additions & 3 deletions forc-plugins/forc-doc/src/render/item/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use anyhow::Result;
use horrorshow::{box_html, Raw, RenderBox, Template};
use std::{collections::BTreeMap, fmt::Write};
use sway_core::language::ty::{
TyEnumVariant, TyImplTrait, TyStorageField, TyStructField, TyTraitFn, TyTraitItem,
TyEnumVariant, TyImplSelfOrTrait, TyStorageField, TyStructField, TyTraitFn, TyTraitItem,
};

/// The actual context of the item displayed by [ItemContext].
Expand Down Expand Up @@ -268,7 +268,7 @@ impl Renderable for Context {
#[derive(Debug, Clone)]
pub struct DocImplTrait {
pub impl_for_module: ModuleInfo,
pub impl_trait: TyImplTrait,
pub impl_trait: TyImplSelfOrTrait,
pub module_info_override: Option<Vec<String>>,
}

Expand Down Expand Up @@ -495,7 +495,7 @@ impl Renderable for ItemContext {
}
impl Renderable for DocImplTrait {
fn render(self, render_plan: RenderPlan) -> Result<Box<dyn RenderBox>> {
let TyImplTrait {
let TyImplSelfOrTrait {
trait_name,
items,
implementing_for,
Expand Down
2 changes: 1 addition & 1 deletion forc-plugins/forc-doc/src/render/title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl DocBlock for TyDecl {
TyDecl::TraitDecl(_) => "trait",
TyDecl::AbiDecl(_) => "abi",
TyDecl::StorageDecl(_) => "contract_storage",
TyDecl::ImplTrait(_) => "impl_trait",
TyDecl::ImplSelfOrTrait(_) => "impl_trait",
TyDecl::FunctionDecl(_) => "fn",
TyDecl::ConstantDecl(_) => "constant",
TyDecl::TypeAliasDecl(_) => "type_alias",
Expand Down
10 changes: 5 additions & 5 deletions forc-tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use ansi_term::Colour;
use std::str;
use std::{env, io};
use tracing::{Level, Metadata};
use tracing_subscriber::{
pub use tracing_subscriber::{
self,
filter::{EnvFilter, LevelFilter},
fmt::MakeWriter,
fmt::{format::FmtSpan, MakeWriter},
};

const ACTION_COLUMN_WIDTH: usize = 12;
Expand Down Expand Up @@ -83,8 +84,8 @@ const LOG_FILTER: &str = "RUST_LOG";

// This allows us to write ERROR and WARN level logs to stderr and everything else to stdout.
// https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/trait.MakeWriter.html
struct StdioTracingWriter {
writer_mode: TracingWriterMode,
pub struct StdioTracingWriter {
pub writer_mode: TracingWriterMode,
}

impl<'a> MakeWriter<'a> for StdioTracingWriter {
Expand Down Expand Up @@ -142,7 +143,6 @@ pub fn init_tracing_subscriber(options: TracingSubscriberOptions) {
Some(_) => EnvFilter::try_from_default_env().expect("Invalid `RUST_LOG` provided"),
None => EnvFilter::new("info"),
};

let level_filter = options
.log_level
.or_else(|| {
Expand Down
31 changes: 11 additions & 20 deletions sway-core/src/abi_generation/abi_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@ impl TypeId {
resolved_type_id: TypeId,
) -> String {
let type_engine = engines.te();
let self_abi_str = type_engine.get(*self).abi_str(ctx, engines);
if self.is_generic_parameter(engines, resolved_type_id) {
format!("generic {}", type_engine.get(*self).abi_str(ctx, engines))
format!("generic {}", self_abi_str)
} else {
match (
&*type_engine.get(*self),
&*type_engine.get(resolved_type_id),
) {
(TypeInfo::Custom { .. }, TypeInfo::Struct { .. }) => {
type_engine.get(resolved_type_id).abi_str(ctx, engines)
}
(TypeInfo::Custom { .. }, TypeInfo::Enum { .. }) => {
type_engine.get(resolved_type_id).abi_str(ctx, engines)
}
(TypeInfo::Custom { .. }, TypeInfo::Alias { .. }) => {
(TypeInfo::Custom { .. }, TypeInfo::Struct { .. })
| (TypeInfo::Custom { .. }, TypeInfo::Enum { .. })
| (TypeInfo::Custom { .. }, TypeInfo::Alias { .. }) => {
type_engine.get(resolved_type_id).abi_str(ctx, engines)
}
(TypeInfo::Tuple(fields), TypeInfo::Tuple(resolved_fields)) => {
Expand All @@ -57,7 +54,7 @@ impl TypeId {
format!("[{}; {}]", inner_type, count.val())
}
(TypeInfo::Custom { .. }, _) => {
format!("generic {}", type_engine.get(*self).abi_str(ctx, engines))
format!("generic {}", self_abi_str)
}
_ => type_engine.get(resolved_type_id).abi_str(ctx, engines),
}
Expand Down Expand Up @@ -185,19 +182,13 @@ fn call_path_display(ctx: &AbiStrContext, call_path: &CallPath) -> String {
return call_path.suffix.as_str().to_string();
}
let mut buf = String::new();
let root_name = ctx.program_name.as_deref();
for (index, prefix) in call_path.prefixes.iter().enumerate() {
let mut skip_prefix = false;
if index == 0 {
if let Some(root_name) = &ctx.program_name {
if prefix.as_str() == root_name.as_str() {
skip_prefix = true;
}
}
}
if !skip_prefix {
buf.push_str(prefix.as_str());
buf.push_str("::");
if index == 0 && Some(prefix.as_str()) == root_name {
continue;
}
buf.push_str(prefix.as_str());
buf.push_str("::");
}
buf.push_str(&call_path.suffix.to_string());

Expand Down
6 changes: 3 additions & 3 deletions sway-core/src/control_flow_analysis/analyze_return_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ fn connect_declaration<'eng: 'cfg, 'cfg>(
connect_typed_fn_decl(engines, &fn_decl, graph, entry_node)?;
Ok(leaves.to_vec())
}
ty::TyDecl::ImplTrait(ty::ImplTrait { decl_id, .. }) => {
let impl_trait = decl_engine.get_impl_trait(decl_id);
let ty::TyImplTrait {
ty::TyDecl::ImplSelfOrTrait(ty::ImplSelfOrTrait { decl_id, .. }) => {
let impl_trait = decl_engine.get_impl_self_or_trait(decl_id);
let ty::TyImplSelfOrTrait {
trait_name, items, ..
} = &*impl_trait;
let entry_node = graph.add_node(ControlFlowGraphNode::from_node(node));
Expand Down
26 changes: 15 additions & 11 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn is_entry_point(node: &TyAstNode, decl_engine: &DeclEngine, tree_type: &TreeTy
struct_decl.visibility == Visibility::Public
}
TyAstNode {
content: TyAstNodeContent::Declaration(TyDecl::ImplTrait { .. }),
content: TyAstNodeContent::Declaration(TyDecl::ImplSelfOrTrait { .. }),
..
} => true,
TyAstNode {
Expand Down Expand Up @@ -198,7 +198,10 @@ impl<'cfg> ControlFlowGraph<'cfg> {
ControlFlowGraphNode::ProgramNode {
node:
ty::TyAstNode {
content: ty::TyAstNodeContent::Declaration(ty::TyDecl::ImplTrait { .. }),
content:
ty::TyAstNodeContent::Declaration(ty::TyDecl::ImplSelfOrTrait {
..
}),
..
},
..
Expand Down Expand Up @@ -632,9 +635,9 @@ fn connect_declaration<'eng: 'cfg, 'cfg>(
connect_enum_declaration(&enum_decl, *enum_ref.id(), graph, entry_node);
Ok(leaves.to_vec())
}
ty::TyDecl::ImplTrait(ty::ImplTrait { decl_id, .. }) => {
let impl_trait_decl = decl_engine.get_impl_trait(decl_id);
let ty::TyImplTrait {
ty::TyDecl::ImplSelfOrTrait(ty::ImplSelfOrTrait { decl_id, .. }) => {
let impl_trait_decl = decl_engine.get_impl_self_or_trait(decl_id);
let ty::TyImplSelfOrTrait {
trait_name,
items,
trait_decl_ref,
Expand Down Expand Up @@ -1168,8 +1171,8 @@ fn get_trait_fn_node_index<'a>(
.namespace
.find_trait_method(&struct_decl.call_path.suffix.clone().into(), &fn_decl.name))
}
ty::TyDecl::ImplTrait(ty::ImplTrait { decl_id, .. }) => {
let impl_trait = decl_engine.get_impl_trait(decl_id);
ty::TyDecl::ImplSelfOrTrait(ty::ImplSelfOrTrait { decl_id, .. }) => {
let impl_trait = decl_engine.get_impl_self_or_trait(decl_id);
Ok(graph
.namespace
.find_trait_method(&impl_trait.trait_name, &fn_decl.name))
Expand Down Expand Up @@ -2351,12 +2354,13 @@ fn construct_dead_code_warning_from_node(
}
ty::TyAstNode {
content:
ty::TyAstNodeContent::Declaration(ty::TyDecl::ImplTrait(ty::ImplTrait {
decl_id, ..
ty::TyAstNodeContent::Declaration(ty::TyDecl::ImplSelfOrTrait(ty::ImplSelfOrTrait {
decl_id,
..
})),
span,
} => {
let ty::TyImplTrait { .. } = &*decl_engine.get_impl_trait(decl_id);
let ty::TyImplSelfOrTrait { .. } = &*decl_engine.get_impl_self_or_trait(decl_id);
CompileWarning {
span: span.clone(),
warning_content: Warning::DeadDeclaration,
Expand Down Expand Up @@ -2554,7 +2558,7 @@ fn allow_dead_code_ast_node(decl_engine: &DeclEngine, node: &ty::TyAstNode) -> b
ty::TyDecl::TypeAliasDecl(ty::TypeAliasDecl { decl_id, .. }) => {
allow_dead_code(decl_engine.get_type_alias(decl_id).attributes.clone())
}
ty::TyDecl::ImplTrait { .. } => false,
ty::TyDecl::ImplSelfOrTrait { .. } => false,
ty::TyDecl::AbiDecl { .. } => false,
ty::TyDecl::GenericTypeForFunctionScope { .. } => false,
ty::TyDecl::ErrorRecovery(..) => false,
Expand Down
Loading

0 comments on commit bc59a7b

Please sign in to comment.