Skip to content

Commit e41e82b

Browse files
authored
Do not initialise zero-sized types (#5741)
Fixes #5692
1 parent 7a12d06 commit e41e82b

File tree

6 files changed

+118
-0
lines changed

6 files changed

+118
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,10 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> {
851851
data_id,
852852
} in init_mut_vars
853853
{
854+
if var_size.in_bytes() == 0 {
855+
// Don't bother initializing zero-sized types.
856+
continue;
857+
}
854858
// Load our initialiser from the data section.
855859
self.cur_bytecode.push(Op {
856860
opcode: Either::Left(VirtualOp::LoadDataId(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[package]]
2+
name = "core"
3+
source = "path+from-root-3299F53BAA3C44EE"
4+
5+
[[package]]
6+
name = "mut_ref_empty_type"
7+
source = "member"
8+
dependencies = ["std"]
9+
10+
[[package]]
11+
name = "std"
12+
source = "path+from-root-3299F53BAA3C44EE"
13+
dependencies = ["core"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[project]
2+
authors = ["Fuel Labs <contact@fuel.sh>"]
3+
entry = "main.sw"
4+
license = "Apache-2.0"
5+
name = "mut_ref_empty_type"
6+
7+
[dependencies]
8+
std = { path = "../../../../../../../sway-lib-std" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"configurables": [],
3+
"functions": [
4+
{
5+
"attributes": null,
6+
"inputs": [],
7+
"name": "main",
8+
"output": {
9+
"name": "",
10+
"type": 0,
11+
"typeArguments": null
12+
}
13+
}
14+
],
15+
"loggedTypes": [],
16+
"messagesTypes": [],
17+
"types": [
18+
{
19+
"components": null,
20+
"type": "u64",
21+
"typeId": 0,
22+
"typeParameters": null
23+
}
24+
]
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
script;
2+
3+
pub trait New {
4+
fn new() -> Self;
5+
}
6+
7+
impl New for [u64;0] {
8+
fn new() -> Self {
9+
[]
10+
}
11+
}
12+
13+
impl Eq for [u64;0] {
14+
fn eq(self, other: Self) -> bool {
15+
true
16+
}
17+
}
18+
19+
struct EmptyStruct { }
20+
21+
impl New for EmptyStruct {
22+
fn new() -> Self {
23+
EmptyStruct { }
24+
}
25+
}
26+
27+
impl Eq for EmptyStruct {
28+
fn eq(self, other: Self) -> bool {
29+
true
30+
}
31+
}
32+
33+
#[inline(always)]
34+
fn reference_zero_sized_local_var_and_value<T>()
35+
where T: New + Eq
36+
{
37+
assert(__size_of::<T>() == 0);
38+
39+
let mut x1 = T::new();
40+
let x2 = T::new();
41+
42+
let x_ptr1 = asm(r: &x1) { r: raw_ptr };
43+
let res1 = x_ptr1.read::<T>();
44+
45+
let x_ptr2 = __addr_of(x2);
46+
let res2 = x_ptr2.read::<T>();
47+
assert(res1 == res2);
48+
}
49+
50+
#[inline(never)]
51+
fn reference_zero_sized_local_var_and_value_not_inlined<T>()
52+
where T: New + Eq
53+
{
54+
reference_zero_sized_local_var_and_value::<T>()
55+
}
56+
57+
fn main() -> u64 {
58+
reference_zero_sized_local_var_and_value_not_inlined::<EmptyStruct>();
59+
reference_zero_sized_local_var_and_value_not_inlined::<[u64;0]>();
60+
reference_zero_sized_local_var_and_value::<EmptyStruct>();
61+
reference_zero_sized_local_var_and_value::<[u64;0]>();
62+
63+
42
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
category = "run"
2+
expected_result = { action = "return", value = 42 }
3+
validate_abi = true
4+
expected_warnings = 2

0 commit comments

Comments
 (0)