Repository: BrynCooke/buildstructor (language: rust)

Issue:
Visibility of builders should match the visibility of the build method.
Otherwise the compile will complain of leakage of types.


Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 68a235b..df84884 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
+## 0.1.12 - unreleased
+[#39](https://github.com/BrynCooke/buildstructor/issues/39)
+Visibility of builder now matches the visibility of each constructor.
+
 ## 0.1.11 - 2022-05-06
 [#39](https://github.com/BrynCooke/buildstructor/issues/39)
 Visibility of builder now matches the visibility of each constructor.
diff --git a/src/buildstructor/codegen.rs b/src/buildstructor/codegen.rs
index d33bc85..84276a4 100644
--- a/src/buildstructor/codegen.rs
+++ b/src/buildstructor/codegen.rs
@@ -65,7 +65,7 @@ pub fn codegen(ir: Ir) -> Result<TokenStream> {
         mod #module_name {
             use super::*;
 
-            pub fn new #builder_init_generics() -> #builder_name<#builder_state_type_initial, #(#target_generics_raw), *>
+            #builder_vis fn new #builder_init_generics() -> #builder_name<#builder_state_type_initial, #(#target_generics_raw), *>
             {
                 #builder_name {
                     fields: (#(#builder_state_initial ,) *),
@@ -108,7 +108,7 @@ pub fn codegen(ir: Ir) -> Result<TokenStream> {
             }
 
 
-            pub struct #builder_name #all_ty_generics {
+            #builder_vis struct #builder_name #all_ty_generics {
                 fields: __P,
                 _phantom: (#(core::marker::PhantomData<#target_generics_raw>,) *)
             }
@@ -129,6 +129,8 @@ pub fn builder_methods(
     builder_where_clause: Option<&WhereClause>,
 ) -> Result<Vec<TokenStream>> {
     let builder_generics = Generics::combine(vec![&ir.generics, &ir.method_generics]);
+    let builder_vis = &ir.builder_vis;
+
     Ok(ir.builder_fields
         .iter()
         .enumerate()
@@ -185,14 +187,14 @@ pub fn builder_methods(
                     }
                     quote! {
                         impl #builder_type_generics #builder_name #before {
-                            pub fn #method_name #into_generics(self, #field_name: #field_collection_type) -> #builder_name #after #builder_where_clause {
+                            #builder_vis fn #method_name #into_generics(self, #field_name: #field_collection_type) -> #builder_name #after #builder_where_clause {
                                 let #field_name = Some(#field_name #into_call);
                                 #builder_name {
                                     fields: #new_state,
                                     _phantom: core::default::Default::default()
                                 }
                             }
-                            pub fn #and_method_name #into_generics(self, #field_name: Option<#field_collection_type>) -> #builder_name #after #builder_where_clause {
+                            #builder_vis fn #and_method_name #into_generics(self, #field_name: Option<#field_collection_type>) -> #builder_name #after #builder_where_clause {
                                 let #field_name = #field_name.map(|v|v #into_call);
                                 #builder_name {
                                     fields: #new_state,
@@ -220,12 +222,12 @@ pub fn builder_methods(
                     quote! {
                         impl #builder_type_generics #builder_name #before {
 
-                            pub fn #plural (mut self, #field_name: #ty) -> #builder_name #before #builder_where_clause {
+                            #builder_vis fn #plural (mut self, #field_name: #ty) -> #builder_name #before #builder_where_clause {
                                 self.fields.#index.lazy.get_or_insert_with(||core::default::Default::default()).extend(#field_name.into_iter());
                                 self
                             }
 
-                            pub fn #singular #into_generics(mut self, value: #field_collection_type) -> #builder_name #before #builder_where_clause{
+                            #builder_vis fn #singular #into_generics(mut self, value: #field_collection_type) -> #builder_name #before #builder_where_clause{
                                 self.fields.#index.lazy.get_or_insert_with(||core::default::Default::default()).insert(value #into_call);
                                 self
                             }
@@ -252,12 +254,12 @@ pub fn builder_methods(
                     quote! {
                         impl #builder_type_generics #builder_name #before {
 
-                            pub fn #plural (mut self, #field_name: #ty) -> #builder_name #before #builder_where_clause{
+                            #builder_vis fn #plural (mut self, #field_name: #ty) -> #builder_name #before #builder_where_clause{
                                 self.fields.#index.lazy.get_or_insert_with(||core::default::Default::default()).extend(#field_name.into_iter());
                                 self
                             }
 
-                            pub fn #singular #into_generics(mut self, value: #field_collection_type) -> #builder_name #before #builder_where_clause{
+                            #builder_vis fn #singular #into_generics(mut self, value: #field_collection_type) -> #builder_name #before #builder_where_clause{
                                 self.fields.#index.lazy.get_or_insert_with(||core::default::Default::default()).push(value #into_call);
                                 self
                             }
@@ -305,12 +307,12 @@ pub fn builder_methods(
                     quote! {
                         impl #builder_type_generics #builder_name #before {
 
-                            pub fn #plural (mut self, #field_name: #ty) -> #builder_name #before #builder_where_clause{
+                            #builder_vis fn #plural (mut self, #field_name: #ty) -> #builder_name #before #builder_where_clause{
                                 self.fields.#index.lazy.get_or_insert_with(||core::default::Default::default()).extend(#field_name.into_iter());
                                 self
                             }
 
-                            pub fn #singular #into_generics_final (mut self, key: #field_key_type, value: #field_value_type) -> #builder_name #before {
+                            #builder_vis fn #singular #into_generics_final (mut self, key: #field_key_type, value: #field_value_type) -> #builder_name #before {
                                 self.fields.#index.lazy.get_or_insert_with(||core::default::Default::default()).insert(key #field_key_into_call, value #field_value_into_call);
                                 self
                             }
@@ -332,7 +334,7 @@ pub fn builder_methods(
                     }
                     quote! {
                         impl #builder_type_generics #builder_name #before {
-                            pub fn #method_name #into_generics(self, #field_name: #ty) -> #builder_name #after {
+                            #builder_vis fn #method_name #into_generics(self, #field_name: #ty) -> #builder_name #after {
                                 let #field_name = #field_name #into_call;
                                 #builder_name {
                                     fields: #new_state,


Test patch:
diff --git a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__async_test.snap b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__async_test.snap
index abb90bc..8d74205 100644
--- a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__async_test.snap
+++ b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__async_test.snap
@@ -11,7 +11,7 @@ impl Foo {
 }
 mod __foo_new_builder {
     use super::*;
-    pub fn new() -> __FooBuilder<(__foo_new_builder::__Required<usize>,)> {
+    pub(super) fn new() -> __FooBuilder<(__foo_new_builder::__Required<usize>,)> {
         __FooBuilder {
             fields: (__required(),),
             _phantom: core::default::Default::default(),
@@ -44,12 +44,12 @@ mod __foo_new_builder {
             }
         }
     }
-    pub struct __FooBuilder<__P> {
+    pub(super) struct __FooBuilder<__P> {
         fields: __P,
         _phantom: (),
     }
     impl __FooBuilder<(__Required<usize>,)> {
-        pub fn simple(self, simple: usize) -> __FooBuilder<(__Set<usize>,)> {
+        pub(super) fn simple(self, simple: usize) -> __FooBuilder<(__Set<usize>,)> {
             let simple = simple;
             __FooBuilder {
                 fields: (__set(simple),),
diff --git a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_generics_test.snap b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_generics_test.snap
index e8cf185..4854d06 100644
--- a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_generics_test.snap
+++ b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_generics_test.snap
@@ -13,7 +13,7 @@ impl Foo {
 }
 mod __foo_new_builder {
     use super::*;
-    pub fn new<K: Into<String> + Eq + Hash, V: Into<String>>() -> __FooBuilder<
+    pub(super) fn new<K: Into<String> + Eq + Hash, V: Into<String>>() -> __FooBuilder<
             (__foo_new_builder::__Optional<HashMap<K, V>>,),
             K,
             V,
@@ -50,7 +50,7 @@ mod __foo_new_builder {
             }
         }
     }
-    pub struct __FooBuilder<__P, K, V> {
+    pub(super) struct __FooBuilder<__P, K, V> {
         fields: __P,
         _phantom: (core::marker::PhantomData<K>, core::marker::PhantomData<V>),
     }
@@ -58,7 +58,7 @@ mod __foo_new_builder {
         K: Into<String> + Eq + Hash,
         V: Into<String>,
     > __FooBuilder<(__Optional<HashMap<K, V>>,), K, V> {
-        pub fn param(
+        pub(super) fn param(
             mut self,
             param: HashMap<K, V>,
         ) -> __FooBuilder<(__Optional<HashMap<K, V>>,), K, V> {
@@ -69,7 +69,7 @@ mod __foo_new_builder {
                 .extend(param.into_iter());
             self
         }
-        pub fn param_entry(
+        pub(super) fn param_entry(
             mut self,
             key: K,
             value: V,
diff --git a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_generics_test2.snap b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_generics_test2.snap
index 9a680aa..d03e9f6 100644
--- a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_generics_test2.snap
+++ b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_generics_test2.snap
@@ -16,7 +16,7 @@ impl Collections {
 }
 mod __collections_new_builder {
     use super::*;
-    pub fn new<K: Into<String> + Eq + Hash, V: Into<String>>() -> __CollectionsBuilder<
+    pub(super) fn new<K: Into<String> + Eq + Hash, V: Into<String>>() -> __CollectionsBuilder<
             (
                 __collections_new_builder::__Optional<HashMap<K, V>>,
                 __collections_new_builder::__Optional<HashSet<K>>,
@@ -56,7 +56,7 @@ mod __collections_new_builder {
             }
         }
     }
-    pub struct __CollectionsBuilder<__P, K, V> {
+    pub(super) struct __CollectionsBuilder<__P, K, V> {
         fields: __P,
         _phantom: (core::marker::PhantomData<K>, core::marker::PhantomData<V>),
     }
@@ -65,7 +65,7 @@ mod __collections_new_builder {
         K: Into<String> + Eq + Hash,
         V: Into<String>,
     > __CollectionsBuilder<(__Optional<HashMap<K, V>>, __1), K, V> {
-        pub fn map(
+        pub(super) fn map(
             mut self,
             map: HashMap<K, V>,
         ) -> __CollectionsBuilder<(__Optional<HashMap<K, V>>, __1), K, V> {
@@ -76,7 +76,7 @@ mod __collections_new_builder {
                 .extend(map.into_iter());
             self
         }
-        pub fn map_entry(
+        pub(super) fn map_entry(
             mut self,
             key: K,
             value: V,
@@ -94,7 +94,7 @@ mod __collections_new_builder {
         K: Into<String> + Eq + Hash,
         V: Into<String>,
     > __CollectionsBuilder<(__0, __Optional<HashSet<K>>), K, V> {
-        pub fn set(
+        pub(super) fn set(
             mut self,
             set: HashSet<K>,
         ) -> __CollectionsBuilder<(__0, __Optional<HashSet<K>>), K, V> {
@@ -105,7 +105,7 @@ mod __collections_new_builder {
                 .extend(set.into_iter());
             self
         }
-        pub fn set_entry(
+        pub(super) fn set_entry(
             mut self,
             value: K,
         ) -> __CollectionsBuilder<(__0, __Optional<HashSet<K>>), K, V> {
diff --git a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_option_test.snap b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_option_test.snap
index ec2b7b1..4952557 100644
--- a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_option_test.snap
+++ b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_option_test.snap
@@ -11,7 +11,7 @@ impl Foo {
 }
 mod __foo_new_builder {
     use super::*;
-    pub fn new() -> __FooBuilder<
+    pub(super) fn new() -> __FooBuilder<
             (__foo_new_builder::__Optional<HashMap<Option<String>, Option<String>>>,),
         > {
         __FooBuilder {
@@ -46,12 +46,12 @@ mod __foo_new_builder {
             }
         }
     }
-    pub struct __FooBuilder<__P> {
+    pub(super) struct __FooBuilder<__P> {
         fields: __P,
         _phantom: (),
     }
     impl __FooBuilder<(__Optional<HashMap<Option<String>, Option<String>>>,)> {
-        pub fn param(
+        pub(super) fn param(
             mut self,
             param: HashMap<Option<String>, Option<String>>,
         ) -> __FooBuilder<(__Optional<HashMap<Option<String>, Option<String>>>,)> {
@@ -62,7 +62,7 @@ mod __foo_new_builder {
                 .extend(param.into_iter());
             self
         }
-        pub fn param_entry(
+        pub(super) fn param_entry(
             mut self,
             key: Option<String>,
             value: Option<String>,
diff --git a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_test.snap b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_test.snap
index d65423e..517d6ed 100644
--- a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_test.snap
+++ b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__collection_test.snap
@@ -18,7 +18,7 @@ impl Foo {
 }
 mod __foo_new_builder {
     use super::*;
-    pub fn new() -> __FooBuilder<
+    pub(super) fn new() -> __FooBuilder<
             (
                 __foo_new_builder::__Required<usize>,
                 __foo_new_builder::__Optional<HashSet<String>>,
@@ -67,7 +67,7 @@ mod __foo_new_builder {
             }
         }
     }
-    pub struct __FooBuilder<__P> {
+    pub(super) struct __FooBuilder<__P> {
         fields: __P,
         _phantom: (),
     }
@@ -78,7 +78,7 @@ mod __foo_new_builder {
         __4,
         __5,
     > __FooBuilder<(__Required<usize>, __1, __2, __3, __4, __5)> {
-        pub fn simple(
+        pub(super) fn simple(
             self,
             simple: usize,
         ) -> __FooBuilder<(__Set<usize>, __1, __2, __3, __4, __5)> {
@@ -103,7 +103,7 @@ mod __foo_new_builder {
         __4,
         __5,
     > __FooBuilder<(__0, __Optional<HashSet<String>>, __2, __3, __4, __5)> {
-        pub fn set(
+        pub(super) fn set(
             mut self,
             set: HashSet<String>,
         ) -> __FooBuilder<(__0, __Optional<HashSet<String>>, __2, __3, __4, __5)> {
@@ -114,7 +114,7 @@ mod __foo_new_builder {
                 .extend(set.into_iter());
             self
         }
-        pub fn set_entry<__T: Into<String>>(
+        pub(super) fn set_entry<__T: Into<String>>(
             mut self,
             value: __T,
         ) -> __FooBuilder<(__0, __Optional<HashSet<String>>, __2, __3, __4, __5)> {
@@ -133,7 +133,7 @@ mod __foo_new_builder {
         __4,
         __5,
     > __FooBuilder<(__0, __1, __Optional<HashMap<String, String>>, __3, __4, __5)> {
-        pub fn map(
+        pub(super) fn map(
             mut self,
             map: HashMap<String, String>,
         ) -> __FooBuilder<
@@ -146,7 +146,7 @@ mod __foo_new_builder {
                 .extend(map.into_iter());
             self
         }
-        pub fn map_entry<__K: Into<String>, __V: Into<String>>(
+        pub(super) fn map_entry<__K: Into<String>, __V: Into<String>>(
             mut self,
             key: __K,
             value: __V,
@@ -168,7 +168,7 @@ mod __foo_new_builder {
         __4,
         __5,
     > __FooBuilder<(__0, __1, __2, __Optional<Vec<String>>, __4, __5)> {
-        pub fn vec(
+        pub(super) fn vec(
             mut self,
             vec: Vec<String>,
         ) -> __FooBuilder<(__0, __1, __2, __Optional<Vec<String>>, __4, __5)> {
@@ -179,7 +179,7 @@ mod __foo_new_builder {
                 .extend(vec.into_iter());
             self
         }
-        pub fn vec_entry<__T: Into<String>>(
+        pub(super) fn vec_entry<__T: Into<String>>(
             mut self,
             value: __T,
         ) -> __FooBuilder<(__0, __1, __2, __Optional<Vec<String>>, __4, __5)> {
@@ -198,7 +198,7 @@ mod __foo_new_builder {
         __3,
         __5,
     > __FooBuilder<(__0, __1, __2, __3, __Optional<BTreeMap<String, String>>, __5)> {
-        pub fn btmap(
+        pub(super) fn btmap(
             mut self,
             btmap: BTreeMap<String, String>,
         ) -> __FooBuilder<
@@ -211,7 +211,7 @@ mod __foo_new_builder {
                 .extend(btmap.into_iter());
             self
         }
-        pub fn btmap_entry<__K: Into<String>, __V: Into<String>>(
+        pub(super) fn btmap_entry<__K: Into<String>, __V: Into<String>>(
             mut self,
             key: __K,
             value: __V,
@@ -233,7 +233,7 @@ mod __foo_new_builder {
         __3,
         __4,
     > __FooBuilder<(__0, __1, __2, __3, __4, __Optional<BTreeSet<String>>)> {
-        pub fn btset(
+        pub(super) fn btset(
             mut self,
             btset: BTreeSet<String>,
         ) -> __FooBuilder<(__0, __1, __2, __3, __4, __Optional<BTreeSet<String>>)> {
@@ -244,7 +244,7 @@ mod __foo_new_builder {
                 .extend(btset.into_iter());
             self
         }
-        pub fn btset_entry<__T: Into<String>>(
+        pub(super) fn btset_entry<__T: Into<String>>(
             mut self,
             value: __T,
         ) -> __FooBuilder<(__0, __1, __2, __3, __4, __Optional<BTreeSet<String>>)> {
diff --git a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__fallible_test.snap b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__fallible_test.snap
index 8126415..2efcfe2 100644
--- a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__fallible_test.snap
+++ b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__fallible_test.snap
@@ -11,7 +11,7 @@ impl Foo {
 }
 mod __foo_new_builder {
     use super::*;
-    pub fn new() -> __FooBuilder<(__foo_new_builder::__Required<usize>,)> {
+    pub(super) fn new() -> __FooBuilder<(__foo_new_builder::__Required<usize>,)> {
         __FooBuilder {
             fields: (__required(),),
             _phantom: core::default::Default::default(),
@@ -44,12 +44,12 @@ mod __foo_new_builder {
             }
         }
     }
-    pub struct __FooBuilder<__P> {
+    pub(super) struct __FooBuilder<__P> {
         fields: __P,
         _phantom: (),
     }
     impl __FooBuilder<(__Required<usize>,)> {
-        pub fn simple(self, simple: usize) -> __FooBuilder<(__Set<usize>,)> {
+        pub(super) fn simple(self, simple: usize) -> __FooBuilder<(__Set<usize>,)> {
             let simple = simple;
             __FooBuilder {
                 fields: (__set(simple),),
diff --git a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__generic_test.snap b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__generic_test.snap
index 1932589..c357a07 100644
--- a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__generic_test.snap
+++ b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__generic_test.snap
@@ -12,7 +12,7 @@ impl<T> Foo<T> {
 }
 mod __foo_new_builder {
     use super::*;
-    pub fn new<T>() -> __FooBuilder<(__foo_new_builder::__Required<T>,), T> {
+    pub(super) fn new<T>() -> __FooBuilder<(__foo_new_builder::__Required<T>,), T> {
         __FooBuilder {
             fields: (__required(),),
             _phantom: core::default::Default::default(),
@@ -45,12 +45,12 @@ mod __foo_new_builder {
             }
         }
     }
-    pub struct __FooBuilder<__P, T> {
+    pub(super) struct __FooBuilder<__P, T> {
         fields: __P,
         _phantom: (core::marker::PhantomData<T>,),
     }
     impl<T> __FooBuilder<(__Required<T>,), T> {
-        pub fn simple(self, simple: T) -> __FooBuilder<(__Set<T>,), T> {
+        pub(super) fn simple(self, simple: T) -> __FooBuilder<(__Set<T>,), T> {
             let simple = simple;
             __FooBuilder {
                 fields: (__set(simple),),
diff --git a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__into_test.snap b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__into_test.snap
index 5675232..8d38d76 100644
--- a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__into_test.snap
+++ b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__into_test.snap
@@ -12,7 +12,7 @@ impl Foo {
 }
 mod __foo_new_builder {
     use super::*;
-    pub fn new<T: Into<String>>() -> __FooBuilder<
+    pub(super) fn new<T: Into<String>>() -> __FooBuilder<
             (__foo_new_builder::__Required<T>,),
             T,
         > {
@@ -48,12 +48,12 @@ mod __foo_new_builder {
             }
         }
     }
-    pub struct __FooBuilder<__P, T> {
+    pub(super) struct __FooBuilder<__P, T> {
         fields: __P,
         _phantom: (core::marker::PhantomData<T>,),
     }
     impl<T: Into<String>> __FooBuilder<(__Required<T>,), T> {
-        pub fn simple(self, simple: T) -> __FooBuilder<(__Set<T>,), T> {
+        pub(super) fn simple(self, simple: T) -> __FooBuilder<(__Set<T>,), T> {
             let simple = simple;
             __FooBuilder {
                 fields: (__set(simple),),
diff --git a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__into_where_test.snap b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__into_where_test.snap
index 5254b4e..9bbcd02 100644
--- a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__into_where_test.snap
+++ b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__into_where_test.snap
@@ -12,7 +12,7 @@ impl Foo {
 }
 mod __foo_new_builder {
     use super::*;
-    pub fn new<T>() -> __FooBuilder<(__foo_new_builder::__Required<T>,), T> {
+    pub(super) fn new<T>() -> __FooBuilder<(__foo_new_builder::__Required<T>,), T> {
         __FooBuilder {
             fields: (__required(),),
             _phantom: core::default::Default::default(),
@@ -45,12 +45,12 @@ mod __foo_new_builder {
             }
         }
     }
-    pub struct __FooBuilder<__P, T> {
+    pub(super) struct __FooBuilder<__P, T> {
         fields: __P,
         _phantom: (core::marker::PhantomData<T>,),
     }
     impl<T> __FooBuilder<(__Required<T>,), T> {
-        pub fn simple(self, simple: T) -> __FooBuilder<(__Set<T>,), T> {
+        pub(super) fn simple(self, simple: T) -> __FooBuilder<(__Set<T>,), T> {
             let simple = simple;
             __FooBuilder {
                 fields: (__set(simple),),
diff --git a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__multi_field_test.snap b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__multi_field_test.snap
index d11e253..e6f3f89 100644
--- a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__multi_field_test.snap
+++ b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__multi_field_test.snap
@@ -11,7 +11,7 @@ impl Foo {
 }
 mod __foo_new_builder {
     use super::*;
-    pub fn new() -> __FooBuilder<
+    pub(super) fn new() -> __FooBuilder<
             (__foo_new_builder::__Required<usize>, __foo_new_builder::__Required<usize>),
         > {
         __FooBuilder {
@@ -46,12 +46,12 @@ mod __foo_new_builder {
             }
         }
     }
-    pub struct __FooBuilder<__P> {
+    pub(super) struct __FooBuilder<__P> {
         fields: __P,
         _phantom: (),
     }
     impl<__1> __FooBuilder<(__Required<usize>, __1)> {
-        pub fn simple(self, simple: usize) -> __FooBuilder<(__Set<usize>, __1)> {
+        pub(super) fn simple(self, simple: usize) -> __FooBuilder<(__Set<usize>, __1)> {
             let simple = simple;
             __FooBuilder {
                 fields: (__set(simple), self.fields.1),
@@ -60,7 +60,10 @@ mod __foo_new_builder {
         }
     }
     impl<__0> __FooBuilder<(__0, __Required<usize>)> {
-        pub fn simple2(self, simple2: usize) -> __FooBuilder<(__0, __Set<usize>)> {
+        pub(super) fn simple2(
+            self,
+            simple2: usize,
+        ) -> __FooBuilder<(__0, __Set<usize>)> {
             let simple2 = simple2;
             __FooBuilder {
                 fields: (self.fields.0, __set(simple2)),
diff --git a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__option_test.snap b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__option_test.snap
index 85cf574..7e60cf5 100644
--- a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__option_test.snap
+++ b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__option_test.snap
@@ -11,7 +11,9 @@ impl Foo {
 }
 mod __foo_new_builder {
     use super::*;
-    pub fn new() -> __FooBuilder<(__foo_new_builder::__Optional<Option<usize>>,)> {
+    pub(super) fn new() -> __FooBuilder<
+            (__foo_new_builder::__Optional<Option<usize>>,),
+        > {
         __FooBuilder {
             fields: (__optional(),),
             _phantom: core::default::Default::default(),
@@ -44,19 +46,22 @@ mod __foo_new_builder {
             }
         }
     }
-    pub struct __FooBuilder<__P> {
+    pub(super) struct __FooBuilder<__P> {
         fields: __P,
         _phantom: (),
     }
     impl __FooBuilder<(__Optional<Option<usize>>,)> {
-        pub fn option(self, option: usize) -> __FooBuilder<(__Set<Option<usize>>,)> {
+        pub(super) fn option(
+            self,
+            option: usize,
+        ) -> __FooBuilder<(__Set<Option<usize>>,)> {
             let option = Some(option);
             __FooBuilder {
                 fields: (__set(option),),
                 _phantom: core::default::Default::default(),
             }
         }
-        pub fn and_option(
+        pub(super) fn and_option(
             self,
             option: Option<usize>,
         ) -> __FooBuilder<(__Set<Option<usize>>,)> {
diff --git a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__returns_self_test.snap b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__returns_self_test.snap
index c1e86f1..3d734fa 100644
--- a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__returns_self_test.snap
+++ b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__returns_self_test.snap
@@ -11,7 +11,7 @@ impl Foo {
 }
 mod __foo_new_builder {
     use super::*;
-    pub fn new() -> __FooBuilder<(__foo_new_builder::__Required<usize>,)> {
+    pub(super) fn new() -> __FooBuilder<(__foo_new_builder::__Required<usize>,)> {
         __FooBuilder {
             fields: (__required(),),
             _phantom: core::default::Default::default(),
@@ -44,12 +44,12 @@ mod __foo_new_builder {
             }
         }
     }
-    pub struct __FooBuilder<__P> {
+    pub(super) struct __FooBuilder<__P> {
         fields: __P,
         _phantom: (),
     }
     impl __FooBuilder<(__Required<usize>,)> {
-        pub fn simple(self, simple: usize) -> __FooBuilder<(__Set<usize>,)> {
+        pub(super) fn simple(self, simple: usize) -> __FooBuilder<(__Set<usize>,)> {
             let simple = simple;
             __FooBuilder {
                 fields: (__set(simple),),
diff --git a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__single_field_test.snap b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__single_field_test.snap
index c1e86f1..3d734fa 100644
--- a/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__single_field_test.snap
+++ b/src/buildstructor/snapshots/buildstructor__buildstructor__codegen__tests__single_field_test.snap
@@ -11,7 +11,7 @@ impl Foo {
 }
 mod __foo_new_builder {
     use super::*;
-    pub fn new() -> __FooBuilder<(__foo_new_builder::__Required<usize>,)> {
+    pub(super) fn new() -> __FooBuilder<(__foo_new_builder::__Required<usize>,)> {
         __FooBuilder {
             fields: (__required(),),
             _phantom: core::default::Default::default(),
@@ -44,12 +44,12 @@ mod __foo_new_builder {
             }
         }
     }
-    pub struct __FooBuilder<__P> {
+    pub(super) struct __FooBuilder<__P> {
         fields: __P,
         _phantom: (),
     }
     impl __FooBuilder<(__Required<usize>,)> {
-        pub fn simple(self, simple: usize) -> __FooBuilder<(__Set<usize>,)> {
+        pub(super) fn simple(self, simple: usize) -> __FooBuilder<(__Set<usize>,)> {
             let simple = simple;
             __FooBuilder {
                 fields: (__set(simple),),
diff --git a/tests/buildstructor/pass/crate_visibility.rs b/tests/buildstructor/pass/crate_visibility.rs
index 8befaef..87a918a 100644
--- a/tests/buildstructor/pass/crate_visibility.rs
+++ b/tests/buildstructor/pass/crate_visibility.rs
@@ -1,11 +1,15 @@
 mod sub1 {
     pub(crate) struct Foo {
-        simple: usize,
+        simple: Bar1,
+    }
+
+    pub(crate) struct Bar1 {
+        pub(crate) simple: usize,
     }
 
     #[buildstructor::builder]
     impl Foo {
-        pub(crate) fn new(simple: usize) -> Self {
+        pub(crate) fn new(simple: Bar1) -> Self {
             Self { simple }
         }
     }
@@ -13,12 +17,16 @@ mod sub1 {
 
 mod sub2 {
     pub struct Foo {
-        simple: usize,
+        simple: Bar2,
+    }
+
+    pub struct Bar2 {
+        pub(crate) simple: usize,
     }
 
     #[buildstructor::builder]
     impl Foo {
-        pub fn new(simple: usize) -> Self {
+        pub fn new(simple: Bar2) -> Self {
             Self { simple }
         }
     }
@@ -26,23 +34,31 @@ mod sub2 {
 
 mod sub3 {
     struct Foo {
-        simple: usize,
+        simple: Bar3,
+    }
+
+    pub struct Bar3 {
+        pub simple: usize,
     }
 
     #[buildstructor::builder]
     impl Foo {
-        fn new(simple: usize) -> Self {
+        fn new(simple: Bar3) -> Self {
             Self { simple }
         }
     }
 
     pub fn foo() {
-        let _ = Foo::builder().simple(3).build();
+        let _ = Foo::builder().simple(Bar3 { simple: 3 }).build();
     }
 }
 
 fn main() {
-    let _ = sub1::Foo::builder().simple(3).build();
-    let _ = sub2::Foo::builder().simple(3).build();
+    let _ = sub1::Foo::builder()
+        .simple(sub1::Bar1 { simple: 1 })
+        .build();
+    let _ = sub2::Foo::builder()
+        .simple(sub2::Bar2 { simple: 2 })
+        .build();
     sub3::foo();
 }