Skip to content

Commit 1718dfb

Browse files
fix(moonbit): eliminate warning and fix ownership (bytecodealliance#1058)
* fix(moonbit): eliminate warnings e.g. match / unreachable code / unused varaible * fix: ownership during exportation * fix(moonbit): update keywords * feat(moonbit): build project for testing
1 parent 5fe5336 commit 1718dfb

File tree

2 files changed

+51
-28
lines changed

2 files changed

+51
-28
lines changed

crates/moonbit/src/lib.rs

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,34 +111,34 @@ extern "wasm" fn copy_inline(dest : Int, src : Int, len : Int) =
111111
#|(func (param i32) (param i32) (param i32) local.get 0 local.get 1 local.get 2 memory.copy)
112112
113113
pub extern "wasm" fn str2ptr(str : String) -> Int =
114-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
114+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
115115
116116
pub extern "wasm" fn ptr2str(ptr : Int) -> String =
117117
#|(func (param i32) (result i32) local.get 0 i32.const 4 i32.sub i32.const 243 i32.store8 local.get 0 i32.const 8 i32.sub)
118118
119119
pub extern "wasm" fn bytes2ptr(bytes : Bytes) -> Int =
120-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
120+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
121121
122122
pub extern "wasm" fn ptr2bytes(ptr : Int) -> Bytes =
123123
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.sub)
124124
125125
pub extern "wasm" fn uint_array2ptr(array : FixedArray[UInt]) -> Int =
126-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
126+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
127127
128128
pub extern "wasm" fn uint64_array2ptr(array : FixedArray[UInt64]) -> Int =
129-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
129+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
130130
131131
pub extern "wasm" fn int_array2ptr(array : FixedArray[Int]) -> Int =
132-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
132+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
133133
134134
pub extern "wasm" fn int64_array2ptr(array : FixedArray[Int64]) -> Int =
135-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
135+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
136136
137137
pub extern "wasm" fn float_array2ptr(array : FixedArray[Float]) -> Int =
138-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
138+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
139139
140140
pub extern "wasm" fn double_array2ptr(array : FixedArray[Double]) -> Int =
141-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
141+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
142142
143143
pub extern "wasm" fn ptr2uint_array(ptr : Int) -> FixedArray[UInt] =
144144
#|(func (param i32) (result i32) local.get 0 i32.const 4 i32.sub i32.const 241 i32.store8 local.get 0 i32.const 8 i32.sub)
@@ -575,11 +575,17 @@ impl WorldGenerator for MoonBit {
575575
{ffi_qualifier}free(src_offset)
576576
dst
577577
}}
578-
579-
let return_area : Int = {ffi_qualifier}malloc({})
580-
",
581-
self.return_area_size,
578+
"
582579
);
580+
if self.return_area_size != 0 {
581+
uwriteln!(
582+
&mut body,
583+
"
584+
let return_area : Int = {ffi_qualifier}malloc({})
585+
",
586+
self.return_area_size,
587+
);
588+
}
583589
files.push(&format!("{EXPORT_DIR}/ffi.mbt"), indent(&body).as_bytes());
584590
self.export
585591
.insert("cabi_realloc".into(), "cabi_realloc".into());
@@ -797,7 +803,7 @@ impl InterfaceGenerator<'_> {
797803
.collect::<Vec<_>>()
798804
.join(", ");
799805

800-
let sig = self.sig_string(func);
806+
let sig = self.sig_string(func, false);
801807

802808
uwriteln!(
803809
self.ffi,
@@ -820,7 +826,7 @@ impl InterfaceGenerator<'_> {
820826
fn export(&mut self, interface_name: Option<&str>, func: &Function) {
821827
let sig = self.resolve.wasm_signature(AbiVariant::GuestExport, func);
822828

823-
let func_sig = self.sig_string(func);
829+
let func_sig = self.sig_string(func, true);
824830

825831
let export_name = func.core_export_name(interface_name);
826832

@@ -1039,7 +1045,7 @@ impl InterfaceGenerator<'_> {
10391045
}
10401046
}
10411047

1042-
fn sig_string(&mut self, func: &Function) -> String {
1048+
fn sig_string(&mut self, func: &Function, ignore_param: bool) -> String {
10431049
let name = match func.kind {
10441050
FunctionKind::Freestanding => func.name.to_moonbit_ident(),
10451051
FunctionKind::Constructor(_) => {
@@ -1074,7 +1080,11 @@ impl InterfaceGenerator<'_> {
10741080
.iter()
10751081
.map(|(name, ty)| {
10761082
let ty = self.type_name(ty, true);
1077-
let name = name.to_moonbit_ident();
1083+
let name = if ignore_param {
1084+
format!("_{}", name.to_moonbit_ident())
1085+
} else {
1086+
name.to_moonbit_ident()
1087+
};
10781088
format!("{name} : {ty}")
10791089
})
10801090
.collect::<Vec<_>>()
@@ -1202,7 +1212,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
12021212
&mut self.stub,
12031213
r#"
12041214
/// Destructor of the resource.
1205-
pub fn {name}::dtor(self : {name}) -> Unit {{
1215+
pub fn {name}::dtor(_self : {name}) -> Unit {{
12061216
abort("todo")
12071217
}}
12081218
"#
@@ -1570,8 +1580,10 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
15701580
.collect::<Vec<_>>()
15711581
.join(", ");
15721582

1573-
let payload = if self.gen.non_empty_type(ty.as_ref()).is_some() || is_result {
1583+
let payload = if self.gen.non_empty_type(ty.as_ref()).is_some() {
15741584
payload
1585+
} else if is_result {
1586+
format!("_{payload}")
15751587
} else {
15761588
String::new()
15771589
};
@@ -1601,7 +1613,6 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
16011613
r#"
16021614
match {op} {{
16031615
{cases}
1604-
_ => panic()
16051616
}}
16061617
"#
16071618
);
@@ -1611,7 +1622,6 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
16111622
r#"
16121623
let ({declarations}) = match {op} {{
16131624
{cases}
1614-
_ => panic()
16151625
}}
16161626
"#
16171627
);
@@ -2109,13 +2119,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {
21092119
Type::U8 => {
21102120
let result = self.locals.tmp("result");
21112121
let address = &operands[0];
2112-
let _length = &operands[1];
2122+
let length = &operands[1];
21132123

21142124
uwrite!(
21152125
self.src,
21162126
"
2117-
let {result} = {ffi_qualifier}ptr2bytes({address})
2118-
"
2127+
ignore({length})
2128+
let {result} = {ffi_qualifier}ptr2bytes({address})
2129+
"
21192130
);
21202131

21212132
results.push(result);
@@ -2132,13 +2143,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {
21322143

21332144
let result = self.locals.tmp("result");
21342145
let address = &operands[0];
2135-
let _length = &operands[1];
2146+
let length = &operands[1];
21362147

21372148
uwrite!(
21382149
self.src,
21392150
"
2140-
let {result} = {ffi_qualifier}ptr2{ty}_array({address})
2141-
"
2151+
ignore({length})
2152+
let {result} = {ffi_qualifier}ptr2{ty}_array({address})
2153+
"
21422154
);
21432155

21442156
results.push(result);
@@ -2159,11 +2171,12 @@ impl Bindgen for FunctionBindgen<'_, '_> {
21592171
Instruction::StringLift { .. } => {
21602172
let result = self.locals.tmp("result");
21612173
let address = &operands[0];
2162-
let _length = &operands[1];
2174+
let length = &operands[1];
21632175

21642176
uwrite!(
21652177
self.src,
21662178
"
2179+
ignore({length})
21672180
let {result} = {ffi_qualifier}ptr2str({address})
21682181
"
21692182
);
@@ -2502,6 +2515,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
25022515
"
25032516
match ({op}) {{
25042517
{cases}
2518+
_ => panic()
25052519
}}
25062520
"
25072521
);
@@ -2750,7 +2764,7 @@ impl ToMoonBitIdent for str {
27502764
"continue" | "for" | "match" | "if" | "pub" | "priv" | "readonly" | "break"
27512765
| "raise" | "try" | "except" | "catch" | "else" | "enum" | "struct" | "type"
27522766
| "trait" | "return" | "let" | "mut" | "while" | "loop" | "extern" | "with"
2753-
| "throw" | "init" | "main" | "test" | "in" => {
2767+
| "throw" | "init" | "main" | "test" | "in" | "guard" => {
27542768
format!("{self}_")
27552769
}
27562770
_ => self.to_snake_case(),

crates/moonbit/tests/codegen.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ test_helpers::codegen_tests!();
2929
fn verify(dir: &Path, _name: &str) {
3030
let mut cmd = Command::new("moon");
3131
cmd.arg("check")
32+
.arg("--target")
33+
.arg("wasm")
34+
.arg("--deny-warn")
35+
.arg("--source-dir")
36+
.arg(dir);
37+
38+
test_helpers::run_command(&mut cmd);
39+
let mut cmd = Command::new("moon");
40+
cmd.arg("build")
3241
.arg("--target")
3342
.arg("wasm")
3443
.arg("--source-dir")

0 commit comments

Comments
 (0)