Skip to content

Commit 5d3f9e0

Browse files
vaivaswathaElaela22soL
authored andcommitted
Skip returning a value when the type is unit (FuelLabs#7401)
FuelLabs#7381 (comment)
1 parent 1cc0a11 commit 5d3f9e0

File tree

34 files changed

+872
-840
lines changed

34 files changed

+872
-840
lines changed

forc-plugins/forc-client/tests/deploy.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ async fn test_simple_deploy() {
377377
node.kill().unwrap();
378378
let expected = vec![DeployedPackage::Contract(DeployedContract {
379379
id: ContractId::from_str(
380-
"2fa29eaad02a2b3e090307cede8dd70f73d81315e05b10ba2ded19c8fcdad0f6",
380+
"91c435d6cdb720db92097958163bc43dbc5c4acfbb3d98025039addb9e2da8bb",
381381
)
382382
.unwrap(),
383383
proxy: None,
@@ -421,7 +421,7 @@ async fn test_deploy_submit_only() {
421421
node.kill().unwrap();
422422
let expected = vec![DeployedPackage::Contract(DeployedContract {
423423
id: ContractId::from_str(
424-
"2fa29eaad02a2b3e090307cede8dd70f73d81315e05b10ba2ded19c8fcdad0f6",
424+
"91c435d6cdb720db92097958163bc43dbc5c4acfbb3d98025039addb9e2da8bb",
425425
)
426426
.unwrap(),
427427
proxy: None,
@@ -468,12 +468,12 @@ async fn test_deploy_fresh_proxy() {
468468
node.kill().unwrap();
469469
let impl_contract = DeployedPackage::Contract(DeployedContract {
470470
id: ContractId::from_str(
471-
"2fa29eaad02a2b3e090307cede8dd70f73d81315e05b10ba2ded19c8fcdad0f6",
471+
"91c435d6cdb720db92097958163bc43dbc5c4acfbb3d98025039addb9e2da8bb",
472472
)
473473
.unwrap(),
474474
proxy: Some(
475475
ContractId::from_str(
476-
"9aaebaece00250296a1459e6ab1413083b69061d7942360880457c5002d9040d",
476+
"212c0edca7ddbb762d93a2446083d94df2db059d1f41ad49807223e8249669e5",
477477
)
478478
.unwrap(),
479479
),

forc/tests/cli_integration.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ fn test_forc_test_raw_logs() -> Result<(), rexpect::error::Error> {
5151
// Assert that the output is correct
5252
process.exp_string(" test test_log_4")?;
5353
process.exp_string("raw logs:")?;
54-
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12416,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
54+
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12408,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
5555
process.exp_string(" test test_log_2")?;
5656
process.exp_string("raw logs:")?;
57-
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12416,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
57+
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12408,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
5858

5959
process.process.exit()?;
6060
Ok(())
@@ -77,12 +77,12 @@ fn test_forc_test_both_logs() -> Result<(), rexpect::error::Error> {
7777
process.exp_string("decoded log values:")?;
7878
process.exp_string("4, log rb: 1515152261580153489")?;
7979
process.exp_string("raw logs:")?;
80-
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12416,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
80+
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12408,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
8181
process.exp_string(" test test_log_2")?;
8282
process.exp_string("decoded log values:")?;
8383
process.exp_string("2, log rb: 1515152261580153489")?;
8484
process.exp_string("raw logs:")?;
85-
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12416,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
85+
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12408,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
8686
process.process.exit()?;
8787
Ok(())
8888
}

sway-core/src/asm_generation/fuel/functions.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,27 @@ impl FuelAsmBuilder<'_, '_> {
190190
owning_span: None,
191191
});
192192

193-
// Save the return value.
193+
// Save the return value, if it is not of type unit.
194194
let ret_reg = self.reg_seqr.next();
195-
self.cur_bytecode.push(Op {
196-
opcode: Either::Left(VirtualOp::MOVE(
197-
ret_reg.clone(),
198-
VirtualRegister::Constant(ConstantRegister::CallReturnValue),
199-
)),
200-
comment: "[call]: copy the return value".into(),
201-
owning_span: None,
202-
});
195+
if !function.get_return_type(self.context).is_unit(self.context) {
196+
self.cur_bytecode.push(Op {
197+
opcode: Either::Left(VirtualOp::MOVE(
198+
ret_reg.clone(),
199+
VirtualRegister::Constant(ConstantRegister::CallReturnValue),
200+
)),
201+
comment: "[call]: copy the return value".into(),
202+
owning_span: None,
203+
});
204+
} else {
205+
self.cur_bytecode.push(Op {
206+
opcode: Either::Left(VirtualOp::MOVE(
207+
ret_reg.clone(),
208+
VirtualRegister::Constant(ConstantRegister::Zero),
209+
)),
210+
comment: "[call]: return unit value".into(),
211+
owning_span: None,
212+
});
213+
}
203214
self.reg_map.insert(*instr_val, ret_reg);
204215

205216
Ok(())
@@ -210,15 +221,21 @@ impl FuelAsmBuilder<'_, '_> {
210221
instr_val: &Value,
211222
ret_val: &Value,
212223
) -> Result<(), CompileError> {
213-
// Move the result into the return value register.
224+
// Move the result (if there is one) into the return value register.
214225
let owning_span = self.md_mgr.val_to_span(self.context, *instr_val);
215-
let ret_reg = self.value_to_register(ret_val)?;
216-
self.cur_bytecode.push(Op::register_move(
217-
VirtualRegister::Constant(ConstantRegister::CallReturnValue),
218-
ret_reg,
219-
"set return value",
220-
owning_span,
221-
));
226+
227+
if !ret_val
228+
.get_type(self.context)
229+
.is_some_and(|t| t.is_unit(self.context))
230+
{
231+
let ret_reg = self.value_to_register(ret_val)?;
232+
self.cur_bytecode.push(Op::register_move(
233+
VirtualRegister::Constant(ConstantRegister::CallReturnValue),
234+
ret_reg,
235+
"set return value",
236+
owning_span,
237+
));
238+
}
222239

223240
// Jump to the end of the function.
224241
let end_label = self

test/src/e2e_vm_tests/test_programs/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ specified, as a boolean value.
5555

5656
Some tests are only compatible with some build targets. To indicate this the `supported_targets` field may be specified, as an array value.
5757

58+
## unsupported_profiles
59+
60+
Some tests can only work with some profiles (release / debug). By default, tests are tested with all profiles.
61+
In case of incompatibility, the `unsupported_profiles` field may be specified, as an array value.
62+
63+
## expected_warnings
64+
65+
Some tests can have valid warnings. To allow them, specify `expected_warnings`, which takes an integer arguments.
66+
If more than these many number of warnings are emitted, the test fails.
67+
5868
## FileCheck for 'fail' tests
5969

6070
The tests in the `fail` category _must_ employ verification using pattern matching via the [FileCheck](https://docs.rs/filecheck/latest/filecheck/)

test/src/e2e_vm_tests/test_programs/should_fail/language/const_generics/stdout.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ output:
2828
Building test/src/e2e_vm_tests/test_programs/should_fail/language/const_generics
2929
Compiling library std (sway-lib-std)
3030
Compiling script const_generics (test/src/e2e_vm_tests/test_programs/should_fail/language/const_generics)
31-
Finished debug [unoptimized + fuel] target(s) [120 B] in ???
31+
Finished debug [unoptimized + fuel] target(s) [112 B] in ???

0 commit comments

Comments
 (0)