# Changing the \`impl Trait for .. { }\` syntax

**URL:** <https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891>\
**Category:** language design\
**Created:** [August 19, 2016, 6:53pm UTC](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891 "2016-08-19T18:53:44Z")\
**Posts on this page:** 14\
**Page:** 2

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [August 20, 2016, 9:53am UTC](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891/21 "2016-08-20T09:53:48Z")

</div>

> [@troplin](#):
>
> And I don't understand why being co-inductive precludes having supertraits and members. Why can't every impl (or trait if you like) be co-inductive?

Right now, if you have `trait Foo: Bar`, and you know that `T: Foo`, you get to assume that `T: Bar`. But if you have a coindutive trait, that would be unsound -- basically you can construct a self-supposed cycle to "prove" that `T: Foo` and then you just get to _assume_ that `T: Bar`. See [Cyclic traits allow arbitrary traits to be synthesized · Issue #29859 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/29859) for several examples exploring this. This is why supertraits don't make sense -- or at least would have to work quite differently than non-auto-traits.

As for having members, the reason is similar: the impl's definition of those members might well make use of this recursive reasoning, and it might never bottom out, but there is no easy place for us to check that except at monomorphization time. We might be able to allow members I guess under various restrictions, but again they would work quite differently than non-auto-traits.

> [@troplin](#):
>
> To put it differently, IMO the trait itself consists only of the operation/items and the semantic meaning that it implies.

I see that POV, but I still think that being an auto trait is really a property of the trait itself. If you read through the code, for example, you'll find we have a predicate "trait-is-auto-trait" (note: not "impl-is-auto-impl") and that we have to treat auto traits differently in lots of places. So basically adding this impl to a trait changes the trait itself in ways that no other impl does. It's not that we just have some special case where we say "when we are looking at a `..` impl, do things a bit differently than we would for other impls".

---

<div class="post-metadata">

**Author:** ![troplin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/troplin/32/1182_2.png) [@troplin](https://internals.rust-lang.org/u/troplin)\
**Post date:** [August 20, 2016, 10:07am UTC](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891/22 "2016-08-20T10:07:40Z")

</div>

> [@nikomatsakis](#):
>
> If you read through the code, for example, you'll find we have a predicate "trait-is-auto-trait" (note: not "impl-is-auto-impl") and that we have to treat auto traits differently in lots of places.

I'm not familiar with the compiler codebase but I think this is just a simplification in the compiler. As implemented, a trait can have either only inductive impls or only co-inductive impls. But I imagine that you also could have traits with both, inductive and co-inductive impls.

---

<div class="post-metadata">

**Author:** ![troplin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/troplin/32/1182_2.png) [@troplin](https://internals.rust-lang.org/u/troplin)\
**Post date:** [August 20, 2016, 11:22am UTC](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891/23 "2016-08-20T11:22:22Z")

</div>

> [@nikomatsakis](#):
>
> Right now, if you have trait Foo: Bar, and you know that T: Foo, you get to assume that T: Bar. But if you have a coindutive trait, that would be unsound -- basically you can construct a self-supposed cycle to "prove" that T: Foo and then you just get to assume that T: Bar.

I think co-induction should not extend to supertraits, e.g. in the example from [Cyclic traits allow arbitrary traits to be synthesized · Issue #29859 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/29859)

```rust
trait Magic: Copy {}
impl<T: Magic> Magic for T {}

fn copy<T: Magic>(x: T) -> (T, T) { (x, x) }

```

supertraits should be treated separately for matching as if they were written as:

```rust
trait Magic: Copy {}
impl<T: Magic+Copy> Magic for T {}

```

Which would be sound again.

---

<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:** [August 20, 2016, 11:30am UTC](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891/24 "2016-08-20T11:30:22Z")

</div>

> auto trait Foo {}

👍

> What do people think about: `auto trait Foo;` as an alternative syntax?

Auto traits are extremely rare, no need to introduce a syntactic inconsistency to save a couple of characters.

> pub trait Foo: Auto {}

The "auto" property is not related to trait inheritance at all, seems like going too far to avoid a contextual keyword.

> impl Auto for Send {}

Just rephrasing of `impl Send for .. {}`, still makes the impl special, not the trait.

> I think both a keyword on the trait decl and a supertrait are non-ideal because they would prevent Rust from ever having traits which are automatically implemented with a negative polarity.

`auto trait !Foo {}`

---

<div class="post-metadata">

**Author:** ![jethrogb](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jethrogb](https://internals.rust-lang.org/u/jethrogb)\
**Post date:** [August 23, 2016, 5:02am UTC](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891/25 "2016-08-23T05:02:34Z")

</div>

Just to be clear: this syntax change is supposed to only work for auto traits? Or also for other marker traits with no members? Think `Zeroable`, `Eq`, `Copy` etc.

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [August 23, 2016, 6:08pm UTC](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891/26 "2016-08-23T18:08:22Z")

</div>

> [@jethrogb](#):
>
> Just to be clear: this syntax change is supposed to only work for auto traits?

Which syntax change are you referring to?

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [August 23, 2016, 6:09pm UTC](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891/27 "2016-08-23T18:09:14Z")

</div>

> [@petrochenkov](#):
>
> Auto traits are extremely rare, no need to introduce a syntactic inconsistency to save a couple of characters.

You persuaded me. `auto trait Foo { }`. We could (orthogonally) discuss whether a `;` can be allowed (for all traits with no members, presumably).

---

<div class="post-metadata">

**Author:** ![sfackler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sfackler/32/9_2.png) [@sfackler](https://internals.rust-lang.org/u/sfackler)\
**Post date:** [August 23, 2016, 6:53pm UTC](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891/28 "2016-08-23T18:53:15Z")

</div>

I always initially read `impl Trait for .. {}` as implementing `Trait` for `RangeFull`, so I’d be on board with something like `auto trait`.

---

<div class="post-metadata">

**Author:** ![Uther](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/uther/32/6225_2.png) [@Uther](https://internals.rust-lang.org/u/Uther)\
**Post date:** [August 23, 2016, 10:27pm UTC](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891/29 "2016-08-23T22:27:45Z")

</div>

Since `{}` are mandatory for other empty blocs(`if`, `while`, `for`, …), it seems consistent to me to require them for empty traits too.

---

<div class="post-metadata">

**Author:** ![withoutboats](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/withoutboats/32/4560_2.png) [@withoutboats](https://internals.rust-lang.org/u/withoutboats)\
**Post date:** [August 23, 2016, 11:12pm UTC](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891/30 "2016-08-23T23:12:56Z")

</div>

They’re not required for empty structs, which is probably a better comparison. But this is orthogonal to the syntax for auto traits.

---

<div class="post-metadata">

**Author:** ![jethrogb](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jethrogb](https://internals.rust-lang.org/u/jethrogb)\
**Post date:** [August 24, 2016, 4:21am UTC](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891/31 "2016-08-24T04:21:35Z")

</div>

> [@nikomatsakis](#):
>
> Which syntax change are you referring to?

Yeah, not sure, from the topic intro it sounded like you wanted to get rid of `impl Trait for ... {}` but after re-reading it sounds more like you want to get rid of `trait Trait {}`? What's the impl-plan for auto traits then?

---

<div class="post-metadata">

**Author:** ![aturon](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aturon/32/3272_2.png) [@aturon](https://internals.rust-lang.org/u/aturon)\
**Post date:** [August 25, 2016, 5:16pm UTC](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891/32 "2016-08-25T17:16:28Z")

</div>

I agree entirely with @petrochenkov’s comment. I tend to find conservatism around (contextual) keywords very difficult to understand: whether or not we use a distinct keyword, the _concept_ exists in the language. And in general I think it’s better to have clear names and ways to state concepts, rather than trying to map a large number of concepts into a too-small keyword space.

In short: `auto trait Foo {}` please!

---

<div class="post-metadata">

**Author:** ![Halt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/halt/32/1457_2.png) [@Halt](https://internals.rust-lang.org/u/Halt)\
**Post date:** [July 3, 2017, 8:56am UTC](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891/33 "2017-07-03T08:56:26Z")

</div>

Don’t sure whether it’s still actual, but…

Personally I think, that new keyword is almost always a poor idea.

The way I see it, all syntax richness may be split into two parts: _declarations_ and _annotations_. Declarations work on the very same semantic level that programmer uses when thinking about the problem. For example, `fn`, `trait`, `mod` declare _things_, they all have very explicit and distinct meaning within the language.

On the other hand, annotations actually are meta information that affect how compiler _interprets_ the code. They’re not a part of the code itself. We may say, that annotations declare _ideas_, not things. Actual things arise later, when compiler (or human) interprets that ideas.

All of the following annotations are not the code, they are comments to the interpreter:

- `#[derive(Debug)]`
- `#[inline]`
- `#[cfg(test)]`
- `#[test]`

A rule of thumb is that after interpretation of the syntax, all annotations may be removed, and it would not cause any problem, because during interpretation phase we’ve already introduced all things, like auto-generated methods, provided trait impls for auto traits and removed unnecessary ones (in case of `test` or `cfg`).

Following this logic we may say, that auto trait is also an _idea_, not a _thing_. Therefore, it should be introduced on a lexical level as annotation:

```rust
#[auto_trait]
trait Foo {}

```

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [March 25, 2019, 8:26am UTC](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891/34 "2019-03-25T08:26:42Z")

</div>

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

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

[Previous page](https://internals.rust-lang.org/t/changing-the-impl-trait-for-syntax/3891.md?page=1)
