Hi. I’m trying to reduce the number of symbols visible from our object files during linkage by reducing the number of reachable trait methods (currently they are all marked as reachable). So, I need to find out what methods can be called from other crates. However, type inference stays in the way of my reasoning by uncovering some hidden methods to the outer world.
Consider the next several traits implemented for a private type S
and its derivatives:
(I use modules for this example, not crates, but there’s no difference in behavior here)
trait Self_ {
fn self_secret(&self);
}
trait Ref {
fn ref_secret(&self);
}
trait Ptr {
fn ptr_secret(&self);
}
trait Arr {
fn arr_secret(&self);
}
trait Arr0 {
fn arr0_secret(&self);
}
trait Arr1 {
fn arr1_secret(&self);
}
trait Fn {
fn fn_secret(&self);
}
trait TyParam {
fn ty_param_secret(&self);
}
mod m {
struct Priv;
impl ::Self_ for Priv { fn self_secret(&self) {} }
impl<'a> ::Ref for &'a Priv { fn ref_secret(&self) {} }
impl ::Ptr for *const Priv { fn ptr_secret(&self) {} }
impl ::Arr for [Priv] { fn arr_secret(&self) {} }
impl ::Arr0 for [Priv; 0] { fn arr0_secret(&self) {} }
impl ::Arr1 for [Priv; 1] { fn arr1_secret(&self) {} }
impl ::Fn for fn(Priv) { fn fn_secret(&self) {} }
impl ::TyParam for Option<Priv> { fn ty_param_secret(&self) {} }
}
fn main() {
// ???.self_secret();
// ???.ref_secret();
(0 as *const _).ptr_secret();
[].arr_secret();
[].arr0_secret();
// ???.arr1_secret();
// ???.fn_secret();
None.ty_param_secret();
}
As you can see, the methods of a trait can be called if the trait is implemented for pointers or empty arrays or unsized arrays of a private type. However, I haven’t found a way to call methods from impls for the private type itself or for references to it.
Can someone familiar with type inference guarantee that these methods can’t indeed be called from outside of module m
(without involving trait objects) or give counterexamples if that is not true?