Skip to content

Commit

Permalink
feat: enable raw pointer return from scripts (FuelLabs#4341)
Browse files Browse the repository at this point in the history
- This PR enables returning heap types from scripts.
- In FuelLabs/fuels-rs#848, support was added
for
returning vectors from smart contracts in the SDK, this safeguard can
now be
  removed so the same can happen with scripts.
  • Loading branch information
iqdecay authored Apr 12, 2023
1 parent dcb7917 commit c196d20
Show file tree
Hide file tree
Showing 14 changed files with 24 additions and 37 deletions.
12 changes: 0 additions & 12 deletions sway-core/src/language/ty/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,6 @@ impl TyProgram {
// Directly returning a `raw_slice` is allowed, which will be just mapped to a RETD.
// TODO: Allow returning nested `raw_slice`s when our spec supports encoding DSTs.
let main_func = mains.remove(0);
if !main_func
.return_type
.type_id
.extract_any_including_self(engines, &|type_info| {
matches!(type_info, TypeInfo::RawUntypedPtr)
})
.is_empty()
{
errors.push(CompileError::PointerReturnNotAllowedInMain {
span: main_func.return_type.span.clone(),
});
}
if !ty_engine
.get(main_func.return_type.type_id)
.extract_any(engines, &|type_info| {
Expand Down
3 changes: 0 additions & 3 deletions sway-error/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,6 @@ pub enum CompileError {
TypeNotAllowedInContractStorage { ty: String, span: Span },
#[error("ref mut parameter not allowed for main()")]
RefMutableNotAllowedInMain { param_name: Ident, span: Span },
#[error("Returning a `raw_ptr` from `main()` is not allowed.")]
PointerReturnNotAllowedInMain { span: Span },
#[error(
"Returning a type containing `raw_slice` from `main()` is not allowed. \
Consider converting it into a flat `raw_slice` first."
Expand Down Expand Up @@ -793,7 +791,6 @@ impl Spanned for CompileError {
ContractIdValueNotALiteral { span } => span.clone(),
TypeNotAllowedInContractStorage { span, .. } => span.clone(),
RefMutableNotAllowedInMain { span, .. } => span.clone(),
PointerReturnNotAllowedInMain { span } => span.clone(),
NestedSliceReturnNotAllowedInMain { span } => span.clone(),
InitializedRegisterReassignment { span, .. } => span.clone(),
DisallowedControlFlowInstruction { span, .. } => span.clone(),
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ license = "Apache-2.0"
name = "raw_ptr_ret"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
std = { path = "../../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
script;

use std::vec::Vec;

fn main() -> raw_ptr {
let mut a : Vec<u64> = Vec::new();
a.push(1234);
a.buf.ptr
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
category = "run"
expected_result = { action = "return", value = 67108854 }
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ license = "Apache-2.0"
name = "vec_ret"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
std = { path = "../../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
script;

fn main() -> Vec<u64> {
let mut vec = Vec::new();
vec.push(124);
vec.push(124);
vec.push(124);
vec
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
category = "run"
expected_result = { action = "return_data", value = "0000000003ffffc400000000000000040000000000000003" }

0 comments on commit c196d20

Please sign in to comment.