Skip to content

Commit 41f388e

Browse files
authored
fix: 'core' module conflict in Bindgen::emit (#568)
* fix: 'core' module conflict in Bindgen::emit This commit is an attempt to avoid module name conflicts where a user library may have a module defined as follows: ```rust mod core; wit_bindgen::generate!("component"); ``` This would currently clash with the 'core' module used in the `Bindgen::emit` function and result in a compilation error: ```console 31 | wit_bindgen::generate!("component"); |^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ could not find `mem` in `core` ``` Signed-off-by: Daniel Bevenius <[email protected]> * squash! fix: 'core' module conflict in Bindgen::emit This commit adds additional external crate prefixes (::) to 'core' modules used in code generated from macros. It also adds a "test" in that an empty module named 'core' has been added to catch any future additions or modifications. Signed-off-by: Daniel Bevenius <[email protected]> --------- Signed-off-by: Daniel Bevenius <[email protected]>
1 parent 8bd0fb3 commit 41f388e

3 files changed

Lines changed: 31 additions & 23 deletions

File tree

crates/rust-lib/src/lib.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,12 @@ pub trait RustGenerator<'a> {
578578

579579
self.push_str("impl");
580580
self.print_generics(lt);
581-
self.push_str(" core::fmt::Debug for ");
581+
self.push_str(" ::core::fmt::Debug for ");
582582
self.push_str(&name);
583583
self.print_generics(lt);
584584
self.push_str(" {\n");
585585
self.push_str(
586-
"fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n",
586+
"fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {\n",
587587
);
588588
self.push_str(&format!("f.debug_struct(\"{}\")", name));
589589
for field in record.fields.iter() {
@@ -600,12 +600,12 @@ pub trait RustGenerator<'a> {
600600
if info.error {
601601
self.push_str("impl");
602602
self.print_generics(lt);
603-
self.push_str(" core::fmt::Display for ");
603+
self.push_str(" ::core::fmt::Display for ");
604604
self.push_str(&name);
605605
self.print_generics(lt);
606606
self.push_str(" {\n");
607607
self.push_str(
608-
"fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n",
608+
"fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {\n",
609609
);
610610
self.push_str("write!(f, \"{:?}\", self)\n");
611611
self.push_str("}\n");
@@ -748,12 +748,12 @@ pub trait RustGenerator<'a> {
748748
if info.error {
749749
self.push_str("impl");
750750
self.print_generics(lt);
751-
self.push_str(" core::fmt::Display for ");
751+
self.push_str(" ::core::fmt::Display for ");
752752
self.push_str(&name);
753753
self.print_generics(lt);
754754
self.push_str(" {\n");
755755
self.push_str(
756-
"fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n",
756+
"fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {\n",
757757
);
758758
self.push_str("write!(f, \"{:?}\", self)");
759759
self.push_str("}\n");
@@ -786,11 +786,13 @@ pub trait RustGenerator<'a> {
786786
let lt = self.lifetime_for(&info, mode);
787787
self.push_str("impl");
788788
self.print_generics(lt);
789-
self.push_str(" core::fmt::Debug for ");
789+
self.push_str(" ::core::fmt::Debug for ");
790790
self.push_str(name);
791791
self.print_generics(lt);
792792
self.push_str(" {\n");
793-
self.push_str("fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n");
793+
self.push_str(
794+
"fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {\n",
795+
);
794796
self.push_str("match self {\n");
795797
for (case_name, payload) in cases {
796798
self.push_str(name);
@@ -909,10 +911,10 @@ pub trait RustGenerator<'a> {
909911

910912
self.push_str("}\n");
911913

912-
self.push_str("impl core::fmt::Debug for ");
914+
self.push_str("impl ::core::fmt::Debug for ");
913915
self.push_str(&name);
914916
self.push_str(
915-
"{\nfn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n",
917+
"{\nfn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {\n",
916918
);
917919
self.push_str("f.debug_struct(\"");
918920
self.push_str(&name);
@@ -924,10 +926,10 @@ pub trait RustGenerator<'a> {
924926
self.push_str("}\n");
925927
self.push_str("}\n");
926928

927-
self.push_str("impl core::fmt::Display for ");
929+
self.push_str("impl ::core::fmt::Display for ");
928930
self.push_str(&name);
929931
self.push_str(
930-
"{\nfn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n",
932+
"{\nfn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {\n",
931933
);
932934
self.push_str("write!(f, \"{} (error {})\", self.name(), *self as i32)");
933935
self.push_str("}\n");

crates/rust/src/lib.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ impl InterfaceGenerator<'_> {
434434
"
435435
#[repr(align({import_return_pointer_area_align}))]
436436
struct RetArea([u8; {import_return_pointer_area_size}]);
437-
let mut ret_area = core::mem::MaybeUninit::<RetArea>::uninit();
437+
let mut ret_area = ::core::mem::MaybeUninit::<RetArea>::uninit();
438438
",
439439
);
440440
}
@@ -996,9 +996,9 @@ impl Bindgen for FunctionBindgen<'_, '_> {
996996
results.push(format!(
997997
"{{
998998
#[cfg(not(debug_assertions))]
999-
{{ core::char::from_u32_unchecked({} as u32) }}
999+
{{ ::core::char::from_u32_unchecked({} as u32) }}
10001000
#[cfg(debug_assertions)]
1001-
{{ core::char::from_u32({} as u32).unwrap() }}
1001+
{{ ::core::char::from_u32({} as u32).unwrap() }}
10021002
}}",
10031003
operands[0], operands[0]
10041004
));
@@ -1015,7 +1015,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
10151015
results.push(format!(
10161016
"{{
10171017
#[cfg(not(debug_assertions))]
1018-
{{ core::mem::transmute::<u8, bool>({} as u8) }}
1018+
{{ ::core::mem::transmute::<u8, bool>({} as u8) }}
10191019
#[cfg(debug_assertions)]
10201020
{{
10211021
match {} {{
@@ -1109,7 +1109,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
11091109
// defined the type so we can transmute directly into it.
11101110
result.push_str("#[cfg(not(debug_assertions))]");
11111111
result.push_str("{");
1112-
result.push_str("core::mem::transmute::<_, ");
1112+
result.push_str("::core::mem::transmute::<_, ");
11131113
result.push_str(&name.to_upper_camel_case());
11141114
result.push_str(">(");
11151115
result.push_str(op0);
@@ -1228,7 +1228,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
12281228
0 => None,
12291229
1 => Some({some}),
12301230
#[cfg(not(debug_assertions))]
1231-
_ => core::hint::unreachable_unchecked(),
1231+
_ => ::core::hint::unreachable_unchecked(),
12321232
#[cfg(debug_assertions)]
12331233
_ => panic!(\"invalid enum discriminant\"),
12341234
}}"
@@ -1263,7 +1263,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
12631263
0 => Ok({ok}),
12641264
1 => Err({err}),
12651265
#[cfg(not(debug_assertions))]
1266-
_ => core::hint::unreachable_unchecked(),
1266+
_ => ::core::hint::unreachable_unchecked(),
12671267
#[cfg(debug_assertions)]
12681268
_ => panic!(\"invalid enum discriminant\"),
12691269
}}"
@@ -1304,7 +1304,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
13041304
// defined the type so we can transmute directly into it.
13051305
result.push_str("#[cfg(not(debug_assertions))]");
13061306
result.push_str("{");
1307-
result.push_str("core::mem::transmute::<_, ");
1307+
result.push_str("::core::mem::transmute::<_, ");
13081308
result.push_str(&self.gen.type_path(*ty, true));
13091309
result.push_str(">(");
13101310
result.push_str(&operands[0]);
@@ -1331,7 +1331,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
13311331
self.push_str(&format!("let {} = {}.as_ptr() as i32;\n", ptr, val));
13321332
self.push_str(&format!("let {} = {}.len() as i32;\n", len, val));
13331333
if realloc.is_some() {
1334-
self.push_str(&format!("core::mem::forget({});\n", val));
1334+
self.push_str(&format!("::core::mem::forget({});\n", val));
13351335
}
13361336
results.push(ptr);
13371337
results.push(len);
@@ -1362,7 +1362,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
13621362
self.push_str(&format!("let {} = {}.as_ptr() as i32;\n", ptr, val));
13631363
self.push_str(&format!("let {} = {}.len() as i32;\n", len, val));
13641364
if realloc.is_some() {
1365-
self.push_str(&format!("core::mem::forget({});\n", val));
1365+
self.push_str(&format!("::core::mem::forget({});\n", val));
13661366
}
13671367
results.push(ptr);
13681368
results.push(len);
@@ -1420,7 +1420,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
14201420
self.push_str(&format!(
14211421
"if ptr.is_null()\n{{\nalloc::handle_alloc_error({layout});\n}}\nptr\n}}",
14221422
));
1423-
self.push_str(&format!("else {{\ncore::ptr::null_mut()\n}};\n",));
1423+
self.push_str(&format!("else {{\n::core::ptr::null_mut()\n}};\n",));
14241424
self.push_str(&format!("for (i, e) in {vec}.into_iter().enumerate() {{\n",));
14251425
self.push_str(&format!(
14261426
"let base = {result} as i32 + (i as i32) * {size};\n",

crates/rust/tests/codegen.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ mod codegen_tests {
66
mod $id {
77
wit_bindgen::generate!($name in $test);
88

9+
// This empty module named 'core' is here to catch module path
10+
// conflicts with 'core' modules used in code generated by the
11+
// wit_bindgen::generate macro.
12+
// Ref: https://github.com/bytecodealliance/wit-bindgen/pull/568
13+
mod core {}
14+
915
#[test]
1016
fn works() {}
1117

0 commit comments

Comments
 (0)