# Idea: Argument-position \`impl Trait\` inference

**URL:** <https://internals.rust-lang.org/t/idea-argument-position-impl-trait-inference/8235>\
**Category:** Uncategorized\
**Created:** [August 17, 2018, 5:20pm UTC](https://internals.rust-lang.org/t/idea-argument-position-impl-trait-inference/8235 "2018-08-17T17:20:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [August 17, 2018, 5:20pm UTC](https://internals.rust-lang.org/t/idea-argument-position-impl-trait-inference/8235/1 "2018-08-17T17:20:18Z")

</div>

I’m a really big fan of `impl Trait`, especially in argument position. A lot of times there’s a type parameter for a function you really just don’t ever need anywhere else, and being able to avoid adding another parameter that is always inferred is great!

I propose adding some definition-time inference to `impl Trait` in argument positon. Consider the following code:

```rust
struct Complex<T: Num>(T, T);

fn consume<T: Num>(z: Complex<T>) { .. }

```

With `impl Trait` we can get rid of `T` and simplify this to

```rust
fn consume(z: Complex<impl Num>) { .. }

```

I propose allowing the following even simpler form (actual syntax TBD):

```rust
fn consume(z: Complex<impl>) { .. }

```

Using `impl` without a bound after it in argument type position will infer the bound to be the weakest possible. In this case, it looks at `Complex<?0>` and realizes that `?0: Num`, so it desugars to `impl Num`. Formally, `impl` in the type parameter or associated type position of a type constructor `T` desugars to `impl Bound`, whre `Bound` is the sum of all of there `where` clauses of `T` involving that type parameter or assoc type. In code,

```rust
struct K<.., T, ..> where T: B1, T: B2, .. { .. }
fn foo(_: K<.., impl, ..>) { .. }
// desug
fn foo(_: K<.., impl Sized + B1 + B2 + .., ..>) { .. }

```

In particular, `fn foo(_: impl)` desugars to `fn foo(_: impl Sized)`. If the parameter is `T: ?Sized`, we get

```rust
fn foo(_: &impl) { .. }
// desug
fn foo(_: &impl ?Sized) { .. }

```

as expected.

This is useful for avoiding repeating the same bounds all over the place for a particular `struct` which are easily understood from context. @Centril’s parameterized modules come to mind here:

```rust
mod<Db: Database + Serialize + 'static> { struct K<T> { .. } }
fn frob(k: K<i32, impl>) { .. }
// instead of
fn frob(k: K<i32, impl Database + Serialize + 'static>) { ..}

```

This doesn’t introduce any particularly wild inference; looking up what `impl` infers is as easy as looking up the type it’s used in.

The form `fn foo(_: impl)` is also useful for when you want to take ownership of _something_ but ignore its value.

In the precense of `const` generics, this becomes even more powerful, since `const` parameters usually involve a long `<..>` clause. For example,

```rust
const fn last<T, const n: usize>(xs: [T; n]) -> T { xs[n - 1] }
// becomes the following, recalling that [_; _]::len can be made `const`
const fn last<T>(xs: [T; impl]) -> T { xs[xs.len() - 1] }

```

While I think I am one of the more obnoxious “enforce readability with an iron fist” people, I think this is actually a readability _win_. Anywhere you can use `impl`, the implied bounds are a single code jump away! I could also imagine this is useful for by-example macros, which might not want to repeat a bunch of user-provided bounds all over the place.

I don’t feel like bikeshedding about syntax quite yet, but I’ll mention this anyway. An alternative syntax is `any`, to reflect that this represents the “most universal type” available in the given position. This, one might write

```rust
fn foo(z: Complex<any>) { .. }

```

I think it’s more illustrative of what I’m proposing, but I don’t think it’s worth arguing about yet.

---

<div class="post-metadata">

**Author:** ![glandium](https://avatars.discourse-cdn.com/v4/letter/g/3d9bf3/32.png) [@glandium](https://internals.rust-lang.org/u/glandium)\
**Post date:** [August 17, 2018, 10:41pm UTC](https://internals.rust-lang.org/t/idea-argument-position-impl-trait-inference/8235/2 "2018-08-17T22:41:51Z")

</div>

`Complex<impl _>` would feel more natural to me.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [August 18, 2018, 2:12am UTC](https://internals.rust-lang.org/t/idea-argument-position-impl-trait-inference/8235/3 "2018-08-18T02:12:34Z")

</div>

`let x: Complex<_> = ...` already means “let the compiler figure it out” in type position. To color the bikeshed, just using that as “minimally constrained” makes sense to me. (Then you can even use `<T: Debug + _>` to say minimally constrained plus debug?)

---

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [August 18, 2018, 3:55am UTC](https://internals.rust-lang.org/t/idea-argument-position-impl-trait-inference/8235/4 "2018-08-18T03:55:02Z")

</div>

I like this! Maybe this points to a more succinct solution: constraint inference. In particular, any place where a constraint can appear, you can write `_` to mean “the weakest constraint available”. For example,

```rust
fn foo<T: _>()

```

will infer `T: Sized` (boring). However, I could imagine that writing

```rust
fn foo<T: _>(z: Complex<T>)

```

would infer `T: Sized + Num`. As a corollary to this, we now get `impl _` pretty much everywhere it makes sense.

Now, I will point out a problem with this; one could interpret `T: _` to mean “infer the _necessary_ bounds for whatever I do in the function body” (which is way too powerful) since this is what `let x: _` does in a function body, and what `_` does in a turbofish.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [August 18, 2018, 6:12pm UTC](https://internals.rust-lang.org/t/idea-argument-position-impl-trait-inference/8235/5 "2018-08-18T18:12:00Z")

</div>

Rust explicitly avoids inferring types for `fn` items for the sake of clarity. In fact, it would be possible to infer all the types (including generics and trait bounds) for the wast majority of functions, thanks to the fact that Rust uses a modified Hindley-Milner type system, and some languages like Haskell do it. But Rust is acting strict around global functions on purpose: knowing the exact type annotations at what tend to be API boundaries makes it easier for the reader of the code to know what to expect in and out of the function. That certainty and ease would get lost as soon as trait bounds are inferred. (Which is, IMO, highly undesirable to say the least.)

---

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [August 18, 2018, 9:46pm UTC](https://internals.rust-lang.org/t/idea-argument-position-impl-trait-inference/8235/6 "2018-08-18T21:46:58Z")

</div>

I am not advocating for global type inference; in fact, I'm very opposed to it because of the reasons you describe. There are two kinds of constraint inference one can describe here:

1. Infer whatever is required by `where` clauses of types in the signature. This is a lot like C++ `auto`. This has a very minimal readability impact, since it's pretty rare for types to have `where` causes, and when they do, they're often either pretty obvious from what the type actually _is_, or incredibly prevalent throughout the codebase anyways (crates using `diesel` are an example of the latter). This is what I'm proposing.
2. Infer whatever is required by the type's uses in the function body. This is _incredibly_ powerful, and a readability footgun. I am very opposed to such inference, and, in an above post, why I'm slightly nervous about using `_` as a symbol (since in most places, `_` _is_ H-M inference).

To summarize in code, consider the following snippet:

```rust
fn dbg<T: _>(x: T) { println!("{:?}", x); }

```

With (1), this code fails to compile, since `T: Debug` cannot be proved from the inferred constraints; `T: _` only infers that `T: Sized`, since it can't look inside the function body. With (2), `T: _` infers `T: Sized + Debug`, so since `x` is passed into an internal `std::fmt` function that requires `T: Debug`.

> [@H2CO3](#):
>
> Rust explicitly avoids inferring types for `fn` items for the sake of clarity.

This is actually not _entirely_ true. While Rust does not infer types, it does infer another _kind_: lifetimes, via lifetime elision and in-band lifetime rules. Lifetimes behave very much _like_ types in the context of type parameters, though they aren't quite as complicated.

This sort of confusion is something I've encountered a few times proposing new kinds of inference. I think it's a matter of me not communicating which parts of the source an inference context is allowed to see; in this case, I only want to allow constraint inference to see the `where` clauses of the types the constrained type is part of.

---

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [August 18, 2018, 10:26pm UTC](https://internals.rust-lang.org/t/idea-argument-position-impl-trait-inference/8235/7 "2018-08-18T22:26:42Z")

</div>

I guest I have to in general **reject** this idea, oppose to what I usually do.

The main reason is that as a code reader I expect to predict what code have to be when I need to call a function, by looking at only its signature.

> [@mcy](#):
>
> ```rust
> fn consume(z: Complex<impl Num>) { .. }
> 
> ```

when look at the above, I know it will work if I pass something a `Num`. On the other hand what I can or cannot expect for

> [@mcy](#):
>
> ```rust
> fn consume(z: Complex<impl>) { .. }
> 
> ```

? I have no idea. Maybe I can only sent a `Drop`? or it have to be a `Debug`? If I make a decision that looks OK today, tomorrow you change the implementation my assumption may break.

So this kind of thing should at least NEVER be `pub`, I would say if it ever being useful its scope have to be inside a function body.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [August 18, 2018, 10:30pm UTC](https://internals.rust-lang.org/t/idea-argument-position-impl-trait-inference/8235/8 "2018-08-18T22:30:32Z")

</div>

To simplify:

If you see `fn consume(x: Complex<_>)`, then any `Complex` can be passed in. The only bounds that can apply are those that `Complex` implies on its own.

---

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [August 18, 2018, 10:30pm UTC](https://internals.rust-lang.org/t/idea-argument-position-impl-trait-inference/8235/9 "2018-08-18T22:30:42Z")

</div>

To call `consume`, you need to already own a `Complex<T>`; by mere virtue of this you have already satisfied the constraints, since we have `Complex<T> where T: Num` in the definition. `Complex<impl _>` would _never_ require traits you can’t learn from looking at the WC of `Complex<T>`. By the time you’re calling `consume`, you don’t care what `T` is.

---

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [August 18, 2018, 10:39pm UTC](https://internals.rust-lang.org/t/idea-argument-position-impl-trait-inference/8235/10 "2018-08-18T22:39:16Z")

</div>

> [@mcy](#):
>
> By the time you’re calling `consume` , you don’t care what `T` is.

In this simple case it seems to be true; but I don't know whether other harder cases. Anyways, I already saw some replication of trait bounds in structs and `impl` blocks. I think the other way round is to imply the trait bound that a certain struct do request. But the `Complex<_>` should at least being fully substituted by rust doc.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [August 18, 2018, 10:39pm UTC](https://internals.rust-lang.org/t/idea-argument-position-impl-trait-inference/8235/11 "2018-08-18T22:39:32Z")

</div>

Today, the general wisdom is to avoid bounding type parameters on structures. This is even if the type makes little to no sense without the bounds.

The reason for that is that functionality that doesn’t care about the type can avoid having to constrain the type. If you were able to use `impl _` (or whatever syntax) to elide those meaningless bounds (maybe you don’t even get access to them since you didn’t write them?) I could see this changing to support bounding struct type arguments to the sensible types.

Obviously existing structures can’t be changed, but new ones could be designed with this in mind.

---

<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:30am UTC](https://internals.rust-lang.org/t/idea-argument-position-impl-trait-inference/8235/12 "2019-03-25T08:30:44Z")

</div>

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

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