Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj committed Sep 6, 2023
1 parent 2e1f0e6 commit 94db2d9
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ library;
fn foo() {}

// Can import everything below because they are using the `pub` keyword
pub const ONE = "1";
pub const ONE = __to_str_array("1");

pub struct MyStruct {}

Expand Down
2 changes: 1 addition & 1 deletion docs/reference/src/code/language/variables/src/lib.sw
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn reassignment() {
// Set `foo` to take the value of `5` and the default `u64` type
let foo = 5;

// Reassign `foo` to be a `str[4]` with the value of `Fuel`
// Reassign `foo` to be a `str` with the value of `Fuel`
let foo = "Fuel";
// ANCHOR_END: reassignment
}
Expand Down
4 changes: 2 additions & 2 deletions examples/result/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ fn divide(numerator: u64, denominator: u64) -> Result<u64, MyContractError> {
}
}

fn main() -> Result<u64, str> {
fn main() -> Result<u64, str[4]> {
let result = divide(20, 2);
match result {
Ok(value) => Ok(value),
Err(MyContractError::DivisionByZero) => Err("Fail"),
Err(MyContractError::DivisionByZero) => Err(__to_str_array("Fail")),
}
}
13 changes: 8 additions & 5 deletions sway-core/src/language/ty/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,22 +251,25 @@ impl TyProgram {
// A script must not return a `raw_ptr` or any type aggregating a `raw_slice`.
// 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 is_invalid_type = |type_info: &TypeInfo| {
let is_invalid_main_argument = |type_info: &TypeInfo| {
matches!(type_info, TypeInfo::RawUntypedSlice | TypeInfo::StringSlice)
};
let is_invalid_main_return = |type_info: &TypeInfo| {
matches!(type_info, TypeInfo::StringSlice)
};

let main_func = mains.remove(0);

for p in main_func.parameters() {
let t = ty_engine.get(p.type_argument.type_id);

if is_invalid_type(&t) {
if is_invalid_main_argument(&t) {
handler.emit_err(CompileError::ThisTypeNotAllowedHere {
span: p.type_argument.span(),
});
}

if !t.extract_any(engines, &is_invalid_type).is_empty() {
if !t.extract_any(engines, &is_invalid_main_argument).is_empty() {
handler.emit_err(CompileError::ThisTypeNotAllowedHere {
span: p.type_argument.span(),
});
Expand All @@ -276,14 +279,14 @@ impl TyProgram {
// Check main return type is valid
let return_type = ty_engine.get(main_func.return_type.type_id);

if is_invalid_type(&return_type) {
if is_invalid_main_return(&return_type) {
handler.emit_err(CompileError::ThisTypeNotAllowedHere {
span: main_func.return_type.span(),
});
}

if !return_type
.extract_any(engines, &is_invalid_type)
.extract_any(engines, &is_invalid_main_return)
.is_empty()
{
handler.emit_err(CompileError::ThisTypeNotAllowedHere {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
category = "fail"

# check: fn main() -> HasRawSlice {
# nextln: $()Returning a type containing `raw_slice` from `main()` is not allowed. Consider converting it into a flat `raw_slice` first.
# nextln: $()This type is not allowed here

0 comments on commit 94db2d9

Please sign in to comment.