# Limits of type inference smartness

**URL:** <https://internals.rust-lang.org/t/limits-of-type-inference-smartness/2919>\
**Category:** compiler\
**Created:** [November 19, 2015, 8:04pm UTC](https://internals.rust-lang.org/t/limits-of-type-inference-smartness/2919 "2015-11-19T20:04:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [November 19, 2015, 8:04pm UTC](https://internals.rust-lang.org/t/limits-of-type-inference-smartness/2919/1 "2015-11-19T20:04:43Z")

</div>

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)

```rust
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?

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [November 19, 2015, 8:55pm UTC](https://internals.rust-lang.org/t/limits-of-type-inference-smartness/2919/2 "2015-11-19T20:55:24Z")

</div>

The function case is like the others, just find a generic function with the same signature:

```rust
(drop as fn(_)).fn_secret();

```

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [November 19, 2015, 9:01pm UTC](https://internals.rust-lang.org/t/limits-of-type-inference-smartness/2919/3 "2015-11-19T21:01:13Z")

</div>

You can get the array case to _compile_ but it will of course panic at runtime:

```rust
fn anyarray<T>() -> [T; 1] { panic!() }

anyarray().arr1_secret();

```

But type inference does not work out if you try to expand it to this:

```rust
/// This does not help
fn anything<T>() -> T { panic!() }

```

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [November 19, 2015, 9:19pm UTC](https://internals.rust-lang.org/t/limits-of-type-inference-smartness/2919/4 "2015-11-19T21:19:12Z")

</div>

@bluss  
Excellent, thanks! Runtime panic doesn’t matter since the link time errors I want to prevent happen earlier.

Though, I mostly interested in impls for `Priv`, `&Priv` and `&mut Priv` (i.e. `self_secret`/`ref_secret`) since they have the largest practical impact. The trick with `panic!()` doesn’t work for them.

---

<div class="post-metadata">

**Author:** ![wthrowe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/wthrowe/32/1252_2.png) [@wthrowe](https://internals.rust-lang.org/u/wthrowe)\
**Post date:** [November 19, 2015, 9:49pm UTC](https://internals.rust-lang.org/t/limits-of-type-inference-smartness/2919/5 "2015-11-19T21:49:44Z")

</div>

Once you have an array of `Priv` it’s easy to get a `Priv` from it:

```rust
fn anyarray<T>() -> [T; 1] { panic!() }
let arr = anyarray();
arr.arr1_secret(); // Needed for type inferrence

(&arr[0]).ref_secret();
arr[0].self_secret();

```

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [November 19, 2015, 10:18pm UTC](https://internals.rust-lang.org/t/limits-of-type-inference-smartness/2919/6 "2015-11-19T22:18:39Z")

</div>

Damn. That’s seriously unfortunate.

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [November 19, 2015, 10:45pm UTC](https://internals.rust-lang.org/t/limits-of-type-inference-smartness/2919/7 "2015-11-19T22:45:47Z")

</div>

I think it would be reasonable to prohibit such super contrived cases through the privacy system to keep sanity for regular code. I’ll try to come up with something.

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [November 20, 2015, 12:46am UTC](https://internals.rust-lang.org/t/limits-of-type-inference-smartness/2919/8 "2015-11-20T00:46:25Z")

</div>

Yes, I don’t think any of the type inferences here are reasonable — the type should not be exposed at all.

---

<div class="post-metadata">

**Author:** ![arielb1](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arielb1/32/3044_2.png) [@arielb1](https://internals.rust-lang.org/u/arielb1)\
**Post date:** [November 20, 2015, 12:31pm UTC](https://internals.rust-lang.org/t/limits-of-type-inference-smartness/2919/9 "2015-11-20T12:31:41Z")

</div>

Maybe just reject selections that introduce cross-crate private structs? I would prefer to talk about this with @nikomatsakis though.

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [November 20, 2015, 3:46pm UTC](https://internals.rust-lang.org/t/limits-of-type-inference-smartness/2919/10 "2015-11-20T15:46:56Z")

</div>

I’m very ignorant of rustc internals, but `Priv` being private, the type or its trait impls shouldn’t necessarily need to be known or exposed at all in the crate metadata.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:25am UTC](https://internals.rust-lang.org/t/limits-of-type-inference-smartness/2919/11 "2019-03-25T08:25:29Z")

</div>

Closed on March 25, 2019, 8:25am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
