Skip to content

Commit

Permalink
Merge branch 'master' into IGI-111/fallback-only
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaBatty authored May 21, 2024
2 parents 876cfba + 10b7221 commit 142f897
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 28 deletions.
18 changes: 13 additions & 5 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1643,13 +1643,21 @@ fn connect_expression<'eng: 'cfg, 'cfg>(
for leaf in leaves {
graph.add_edge(*leaf, this_ix, "".into());
}
graph.add_edge(this_ix, field_ix, "".into());

if let Some(struct_node_ix) = graph
.namespace
.find_struct_decl(resolved_type_of_parent.suffix.as_str())
// autogenerated code should not increase usage of a struct field
if !engines
.se()
.is_span_in_autogenerated(&expression_span)
.unwrap_or(false)
{
graph.add_edge(this_ix, *struct_node_ix, "".into());
graph.add_edge(this_ix, field_ix, "".into());

if let Some(struct_node_ix) = graph
.namespace
.find_struct_decl(resolved_type_of_parent.suffix.as_str())
{
graph.add_edge(this_ix, *struct_node_ix, "".into());
}
}

Ok(vec![this_ix])
Expand Down
1 change: 0 additions & 1 deletion sway-core/src/type_system/unify/unifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ impl<'a> Unifier<'a> {
self.unify(handler, rtp.type_id, etp.type_id, span);
});
} else {
dbg!(rn == en, rvs.len() == evs.len(), rtps.len() == etps.len());
let internal = format!("[{received:?}] versus [{expected:?}]");
let (received, expected) = self.assign_args(received, expected);
handler.emit_err(
Expand Down
12 changes: 2 additions & 10 deletions sway-ir/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,16 +816,8 @@ impl<'a, 'eng> InstructionVerifier<'a, 'eng> {

fn verify_load(&self, src_val: &Value) -> Result<(), IrError> {
// Just confirm `src_val` is a pointer.
let r = self
.get_ptr_type(src_val, IrError::VerifyLoadFromNonPointer)
.map(|_| ());

if r.is_err() {
let meta = src_val.get_metadata(self.context).unwrap();
dbg!(&self.context.metadata[meta.0], &r);
}

r
self.get_ptr_type(src_val, IrError::VerifyLoadFromNonPointer)
.map(|_| ())
}

fn verify_log(&self, log_val: &Value, log_ty: &Type, log_id: &Value) -> Result<(), IrError> {
Expand Down
4 changes: 2 additions & 2 deletions sway-lib-std/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library;

pub mod constants;
pub mod error_signals;
pub mod logging;
pub mod revert;
Expand All @@ -16,17 +17,16 @@ pub mod bytes;
pub mod math;
pub mod flags;
pub mod u128;
pub mod b512;
pub mod primitive_conversions;
pub mod alias;
pub mod hash;
pub mod asset_id;
pub mod contract_id;
pub mod execution;
pub mod constants;
pub mod call_frames;
pub mod context;
pub mod external;
pub mod b512;
pub mod tx;
pub mod outputs;
pub mod address;
Expand Down
50 changes: 50 additions & 0 deletions sway-lib-std/src/primitive_conversions/b256.sw
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
library;

use ::bytes::Bytes;
use ::constants::ZERO_B256;
use ::convert::{From, TryFrom};
use ::option::Option::{self, *};
use ::u128::U128;
use ::b512::B512;

impl TryFrom<Bytes> for b256 {
fn try_from(b: Bytes) -> Option<Self> {
Expand All @@ -18,6 +20,41 @@ impl TryFrom<Bytes> for b256 {
}
}

impl TryFrom<B512> for b256 {
/// Attempts conversion from a `B512` to a `b256`.
///
/// # Additional Information
///
/// If the high bits of the `B512` are not zero, the conversion will fail.
///
/// # Arguments
///
/// * `val`: [B512] - The `B512` to be converted.
///
/// # Returns
///
/// * [Option<b256>] - The `b256` representation of the `B512` value.
///
/// # Examples
///
/// ```sway
/// use std::b512::B512;
///
/// fn foo() {
/// let b512_value = B512::new();
/// let b256_value = b256::try_from(b512_value).unwrap();
/// }
/// ```
fn try_from(val: B512) -> Option<Self> {
let bits = val.bits();
if bits[0] == ZERO_B256 {
Some(bits[1])
} else {
None
}
}
}

impl From<u256> for b256 {
/// Casts a `u256` to raw `b256` data.
///
Expand Down Expand Up @@ -152,3 +189,16 @@ fn test_b256_from_tuple() {
b256_value == 0x0000000000000001000000000000000200000000000000030000000000000004,
);
}

#[test]
fn test_b256_try_from_b512() {
use ::assert::assert;

let b512_value = B512::new();
let b256_value = b256::try_from(b512_value);
assert(b256_value.is_some());

let b512_value = B512::from((0x0000000000000000000000000000000000000000000000000000000000000001, ZERO_B256));
let b256_value = b256::try_from(b512_value);
assert(b256_value.is_none());
}
53 changes: 52 additions & 1 deletion sway-lib-std/src/primitive_conversions/u256.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
library;

use ::convert::From;
use ::constants::ZERO_B256;
use ::convert::{From, TryFrom};
use ::option::Option::{self, *};
use ::u128::U128;
use ::b512::B512;

impl TryFrom<B512> for u256 {
/// Attempts conversion from a `B512` to a `u256`.
///
/// # Additional Information
///
/// If the high bits of the `B512` are not zero, the conversion will fail.
///
/// # Arguments
///
/// * `val`: [B512] - The `B512` to be converted.
///
/// # Returns
///
/// * [Option<u256>] - The `u256` representation of the `B512` value.
///
/// # Examples
///
/// ```sway
/// use std::b512::B512;
///
/// fn foo() {
/// let b512_value = B512::new();
/// let u256_value = u256::try_from(b512_value).unwrap();
/// }
/// ```
fn try_from(val: B512) -> Option<Self> {
let bits = val.bits();
if bits[0] == ZERO_B256 {
Some(bits[1].as_u256())
} else {
None
}
}
}

/// Functions for casting between `u256` and other types.
impl From<u8> for u256 {
Expand Down Expand Up @@ -255,3 +293,16 @@ fn test_u256_from_tuple() {
u256_value == 0x0000000000000001000000000000000200000000000000030000000000000004_u256,
);
}

#[test]
fn test_u256_try_from_b512() {
use ::assert::assert;

let b512_value = B512::new();
let u256_value = u256::try_from(b512_value);
assert(u256_value.is_some());

let b512_value = B512::from((0x0000000000000000000000000000000000000000000000000000000000000001, ZERO_B256));
let u256_value = u256::try_from(b512_value);
assert(u256_value.is_none());
}
5 changes: 1 addition & 4 deletions sway-lib-std/src/storage/storage_map.sw
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ pub enum StorageMapError<V> {
}

/// A persistent key-value pair mapping struct.
pub struct StorageMap<K, V>
where
K: Hash,
{}
pub struct StorageMap<K, V> {}

impl<K, V> StorageKey<StorageMap<K, V>>
where
Expand Down
8 changes: 8 additions & 0 deletions sway-types/src/source_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ impl Clone for SourceEngine {
impl SourceEngine {
const AUTOGENERATED_PATH: &'static str = "<autogenerated>";

pub fn is_span_in_autogenerated(&self, span: &crate::Span) -> Option<bool> {
span.source_id().map(|s| self.is_source_id_autogenerated(s))
}

pub fn is_source_id_autogenerated(&self, source_id: &SourceId) -> bool {
self.get_path(source_id).starts_with("<autogenerated>")
}

/// This function retrieves an integer-based source ID for a provided path buffer.
/// If an ID already exists for the given path, the function will return that
/// existing ID. If not, a new ID will be created.
Expand Down
2 changes: 1 addition & 1 deletion test/src/e2e_vm_tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub(crate) async fn deploy_contract(file_name: &str, run_config: &RunConfig) ->
true => BuildProfile::RELEASE.to_string(),
false => BuildProfile::DEBUG.to_string(),
},
no_encoding_v1: !dbg!(run_config.experimental.new_encoding),
no_encoding_v1: !run_config.experimental.new_encoding,
..Default::default()
})
.await
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ registers.sw
flags.sw
math.sw
u128.sw
b512.sw
bytes_conversions.sw
bytes_conversions/b256.sw
bytes_conversions/u16.sw
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library;

pub mod constants;
pub mod error_signals;
pub mod logging;
pub mod revert;
Expand All @@ -16,6 +17,7 @@ pub mod registers;
pub mod flags;
pub mod math;
pub mod u128;
pub mod b512;
pub mod primitive_conversions;
pub mod array_conversions;
pub mod bytes_conversions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ script;
mod utils;
use utils::Foo;


struct Bar {
value: u64
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
category = "compile"
expected_warnings = 1

expected_warnings = 0
# check: $()struct Bar {
# nextln: $()value: u64
# nextln: $()This struct field is never accessed.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ expected_result_new_encoding = { action = "return_data", value = "00000000000020
validate_abi = true
expected_warnings = 26

#check: $()pub struct S
#nextln: $()This struct field is never accessed.
#nextln: $()pub enum Enum

#check: $()pub struct S
#nextln: $()This struct field is never accessed.
#nextln: $()pub enum Enum

#check: $()pub enum Enum
#nextln: $()This enum is never used.

Expand Down
2 changes: 0 additions & 2 deletions test/src/sdk-harness/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ members = [
"test_projects/asset_ops",
"test_projects/block",
"test_projects/call_frames",
# TODO Uncomment these when SDK supports encoding V1 for configurables
# https://github.com/FuelLabs/sway/issues/5727
"test_projects/configurables_in_contract",
"test_projects/configurables_in_script",
"test_projects/context",
Expand Down

0 comments on commit 142f897

Please sign in to comment.