Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge std and core libraries #6729

Draft
wants to merge 33 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
387e4ed
move primitive_conversions
SwayStar123 Nov 18, 2024
cb63d64
more raw_ptr
SwayStar123 Nov 18, 2024
771081f
move codec
SwayStar123 Nov 18, 2024
129e18e
Merge branch 'master' into swaystar123/merge-core-std
SwayStar123 Nov 18, 2024
0d24485
remove uses of core::codec in std lib
SwayStar123 Nov 18, 2024
29c1b40
add codec to lib.sw
SwayStar123 Nov 18, 2024
6a14300
change order of libs in lib.sw
SwayStar123 Nov 18, 2024
f954429
move slice, ops, primitves
SwayStar123 Nov 18, 2024
24361d7
move never and str, add core imports to prelude
SwayStar123 Nov 18, 2024
109c11b
remove slice import - i tihnk unnecessary in ops
SwayStar123 Nov 18, 2024
33ace1b
add str to lib.sw
SwayStar123 Nov 18, 2024
d4d42dc
add r# to str
SwayStar123 Nov 18, 2024
295bcb4
remove original str impl
SwayStar123 Nov 18, 2024
39cb0ed
import AbiEncode where necessary
SwayStar123 Nov 18, 2024
e4d696c
add missing ;
SwayStar123 Nov 18, 2024
2cad4ba
remove log in import for flags.sw
SwayStar123 Nov 18, 2024
06b2172
remove assert and error from flags import
SwayStar123 Nov 18, 2024
f5b1f1e
move out tests from ops lib
SwayStar123 Nov 18, 2024
140e97c
remove primitive conversions dependency from codec
SwayStar123 Nov 18, 2024
928d527
fix typo
SwayStar123 Nov 18, 2024
7940ce2
fmt
SwayStar123 Nov 18, 2024
e809184
progress
SwayStar123 Jan 7, 2025
30f1c93
Merge branch 'master' into swaystar123/merge-core-std
SwayStar123 Jan 23, 2025
f62e3f1
remove todo stuff
SwayStar123 Jan 23, 2025
1bd6641
rid of all but 1 error
SwayStar123 Jan 27, 2025
4662ec9
Merge branch 'master' into swaystar123/merge-core-std
SwayStar123 Jan 27, 2025
06ad163
fmt
SwayStar123 Jan 27, 2025
05d7a2e
fix new errors
SwayStar123 Jan 27, 2025
9bb1d3e
fix all bugs
SwayStar123 Jan 27, 2025
e16357d
move storagekey
SwayStar123 Jan 29, 2025
e47e22a
move generate.sh
SwayStar123 Jan 29, 2025
28642b1
remove some references of core from compiler
SwayStar123 Feb 3, 2025
f45478d
Merge branch 'master' into swaystar123/merge-core-std
SwayStar123 Feb 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions forc-pkg/src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,14 +722,12 @@ impl PackageManifest {
/// Note: If only `core` is specified, we are unable to implicitly add `std` as we cannot
/// guarantee that the user's `core` is compatible with the implicit `std`.
fn implicitly_include_std_if_missing(&mut self) {
use sway_types::constants::{CORE, STD};
use sway_types::constants::STD;
// Don't include `std` if:
// - this *is* `core` or `std`.
// - either `core` or `std` packages are already specified.
// - this *is* `std`.
// - `std` package is already specified.
// - a dependency already exists with the name "std".
if self.project.name == CORE
|| self.project.name == STD
|| self.pkg_dep(CORE).is_some()
if self.project.name == STD
|| self.pkg_dep(STD).is_some()
|| self.dep(STD).is_some()
|| !self.project.implicit_std.unwrap_or(true)
Expand Down
50 changes: 0 additions & 50 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ use sway_core::{
use sway_core::{set_bytecode_configurables_offset, PrintAsm, PrintIr};
use sway_error::{error::CompileError, handler::Handler, warning::CompileWarning};
use sway_features::ExperimentalFeatures;
use sway_types::constants::{CORE, STD};
use sway_types::{Ident, Span, Spanned};
use sway_utils::{constants, time_expr, PerformanceData, PerformanceMetric};
use tracing::{debug, info};
Expand Down Expand Up @@ -1605,7 +1604,6 @@ pub fn dependency_namespace(
};

// Add direct dependencies.
let mut core_added = false;
for edge in graph.edges_directed(node, Direction::Outgoing) {
let dep_node = edge.target();
let dep_name = kebab_to_snake_case(&edge.weight().name);
Expand Down Expand Up @@ -1635,59 +1633,11 @@ pub fn dependency_namespace(
}
};
root_namespace.add_external(dep_name, dep_namespace);
let dep = &graph[dep_node];
if dep.name == CORE {
core_added = true;
}
}

// Add `core` if not already added.
if !core_added {
if let Some(core_node) = find_core_dep(graph, node) {
let core_namespace = &lib_namespace_map[&core_node];
root_namespace.add_external(CORE.to_string(), core_namespace.clone());
}
}

Ok(root_namespace)
}

/// Find the `core` dependency (whether direct or transitive) for the given node if it exists.
fn find_core_dep(graph: &Graph, node: NodeIx) -> Option<NodeIx> {
// If we are `core`, do nothing.
let pkg = &graph[node];
if pkg.name == CORE {
return None;
}

// If we have `core` as a direct dep, use it.
let mut maybe_std = None;
for edge in graph.edges_directed(node, Direction::Outgoing) {
let dep_node = edge.target();
let dep = &graph[dep_node];
match &dep.name[..] {
CORE => return Some(dep_node),
STD => maybe_std = Some(dep_node),
_ => {}
}
}

// If we have `std`, select `core` via `std`.
if let Some(std) = maybe_std {
return find_core_dep(graph, std);
}

// Otherwise, search from this node.
for dep_node in Dfs::new(graph, node).iter(graph) {
let dep = &graph[dep_node];
if dep.name == CORE {
return Some(dep_node);
}
}

None
}

/// Compiles the given package.
///
/// ## Program Types
Expand Down
2 changes: 1 addition & 1 deletion sway-ast/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ pub enum ReassignmentOpVariant {
}

impl ReassignmentOpVariant {
pub fn core_name(&self) -> &'static str {
pub fn std_name(&self) -> &'static str {
match self {
ReassignmentOpVariant::Equals => "eq",
ReassignmentOpVariant::AddEquals => "add",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl Instantiate {
lhs: ty::TyExpression,
rhs: ty::TyExpression,
) -> Result<ty::TyExpression, ErrorEmitted> {
ty::TyExpression::core_ops_eq(handler, ctx, vec![lhs, rhs], self.dummy_span())
ty::TyExpression::std_ops_eq(handler, ctx, vec![lhs, rhs], self.dummy_span())
}

/// Instantiates an expression equivalent to `<lhs> != <rhs>`.
Expand All @@ -176,7 +176,7 @@ impl Instantiate {
lhs: ty::TyExpression,
rhs: ty::TyExpression,
) -> Result<ty::TyExpression, ErrorEmitted> {
ty::TyExpression::core_ops_neq(handler, ctx, vec![lhs, rhs], self.dummy_span())
ty::TyExpression::std_ops_neq(handler, ctx, vec![lhs, rhs], self.dummy_span())
}

/// Instantiates an expression equivalent to `<lhs> == <rhs>`. The method expects that
Expand All @@ -187,8 +187,8 @@ impl Instantiate {
lhs: ty::TyExpression,
rhs: ty::TyExpression,
) -> ty::TyExpression {
ty::TyExpression::core_ops_eq(&Handler::default(), ctx, vec![lhs, rhs], self.dummy_span())
.expect("Instantiating `core::ops::eq` is expected to always work.")
ty::TyExpression::std_ops_eq(&Handler::default(), ctx, vec![lhs, rhs], self.dummy_span())
.expect("Instantiating `std::ops::eq` is expected to always work.")
}

/// Instantiates a [ty::TyExpressionVariant::TupleElemAccess] `<tuple_variable>.<index>`. The method expects that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use type_resolve::{resolve_call_path, VisibilityCheck};

#[allow(clippy::too_many_arguments)]
impl ty::TyExpression {
pub(crate) fn core_ops_eq(
pub(crate) fn std_ops_eq(
handler: &Handler,
ctx: TypeCheckContext,
arguments: Vec<ty::TyExpression>,
Expand All @@ -66,10 +66,10 @@ impl ty::TyExpression {
TypeInfo::Boolean,
span.source_id(),
));
Self::core_ops(handler, ctx, OpVariant::Equals, arguments, span)
Self::std_ops(handler, ctx, OpVariant::Equals, arguments, span)
}

pub(crate) fn core_ops_neq(
pub(crate) fn std_ops_neq(
handler: &Handler,
ctx: TypeCheckContext,
arguments: Vec<ty::TyExpression>,
Expand All @@ -81,10 +81,10 @@ impl ty::TyExpression {
TypeInfo::Boolean,
span.source_id(),
));
Self::core_ops(handler, ctx, OpVariant::NotEquals, arguments, span)
Self::std_ops(handler, ctx, OpVariant::NotEquals, arguments, span)
}

fn core_ops(
fn std_ops(
handler: &Handler,
mut ctx: TypeCheckContext,
op_variant: OpVariant,
Expand All @@ -95,7 +95,7 @@ impl ty::TyExpression {

let call_path = CallPath {
prefixes: vec![
Ident::new_with_override("core".into(), span.clone()),
Ident::new_with_override("std".into(), span.clone()),
Ident::new_with_override("ops".into(), span.clone()),
],
suffix: Op {
Expand Down Expand Up @@ -1242,11 +1242,12 @@ impl ty::TyExpression {
)
})?;

// The type of a storage access is `core::storage::StorageKey`. This is
// The type of a storage access is `std::storage::storage_key::StorageKey`. This is
// the path to it.
let storage_key_mod_path = vec![
Ident::new_with_override("core".into(), span.clone()),
Ident::new_with_override("std".into(), span.clone()),
Ident::new_with_override("storage".into(), span.clone()),
Ident::new_with_override("storage_key".into(), span.clone()),
];
let storage_key_ident = Ident::new_with_override("StorageKey".into(), span.clone());

Expand Down
39 changes: 9 additions & 30 deletions sway-core/src/semantic_analysis/namespace/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{module::Module, root::Root, ModulePath, ModulePathBuf};

use sway_error::handler::{ErrorEmitted, Handler};
use sway_types::{
constants::{CONTRACT_ID, CORE, PRELUDE, STD},
constants::{CONTRACT_ID, PRELUDE, STD},
span::Span,
};

Expand All @@ -29,21 +29,21 @@ impl Namespace {
/// Initialize the namespace
/// See also the factory functions in contract_helpers.rs
///
/// If `import_preludes_into_root` is true then core::prelude::* and std::prelude::* will be
/// imported into the root module, provided core and std are available in the external modules.
/// If `import_prelude_into_root` is true then and std::prelude::* will be
/// imported into the root module, provided std is available in the external modules.
pub fn new(
handler: &Handler,
engines: &Engines,
package_root: Root,
import_preludes_into_root: bool,
import_prelude_into_root: bool,
) -> Result<Self, ErrorEmitted> {
let package_name = package_root.current_package_name().clone();
let mut res = Self {
root: package_root,
current_mod_path: vec![package_name],
};

if import_preludes_into_root {
if import_prelude_into_root {
res.import_implicits(handler, engines)?;
}
Ok(res)
Expand Down Expand Up @@ -206,41 +206,20 @@ impl Namespace {
}
}

// Import core::prelude::*, std::prelude::* and ::CONTRACT_ID as appropriate into the current module
// Import std::prelude::* and ::CONTRACT_ID as appropriate into the current module
fn import_implicits(
&mut self,
handler: &Handler,
engines: &Engines,
) -> Result<(), ErrorEmitted> {
// Import preludes
let package_name = self.current_package_name().to_string();
let core_string = CORE.to_string();
let core_ident = Ident::new_no_span(core_string.clone());
let prelude_ident = Ident::new_no_span(PRELUDE.to_string());
if package_name == CORE {

if package_name == STD {
// Do nothing
} else if package_name == STD {
// Import core::prelude::*
assert!(self.root.exists_as_external(&core_string));
self.root.star_import(
handler,
engines,
&[core_ident, prelude_ident],
&self.current_mod_path,
Visibility::Private,
)?
} else {
// Import core::prelude::* and std::prelude::*
if self.root.exists_as_external(&core_string) {
self.root.star_import(
handler,
engines,
&[core_ident, prelude_ident.clone()],
&self.current_mod_path,
Visibility::Private,
)?;
}

// Import std::prelude::*
let std_string = STD.to_string();
// Only import std::prelude::* if std exists as a dependency
if self.root.exists_as_external(&std_string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2491,7 +2491,7 @@ fn expr_to_expression(
assignable.clone(),
)?;
let rhs = Box::new(op_call(
op_variant.core_name(),
op_variant.std_name(),
op_span,
span.clone(),
&vec![
Expand Down Expand Up @@ -2527,7 +2527,7 @@ fn op_call(
inner: MethodName::FromTrait {
call_path: CallPath {
prefixes: vec![
Ident::new_with_override("core".into(), op_span.clone()),
Ident::new_with_override("std".into(), op_span.clone()),
Ident::new_with_override("ops".into(), op_span.clone()),
],
suffix: Ident::new_with_override(name.into(), op_span.clone()),
Expand Down
Loading
Loading