Skip to content

Commit

Permalink
Auto merge of #53542 - alexreg:impl-trait-in-bindings, r=cramertj
Browse files Browse the repository at this point in the history
`impl trait` in bindings (feature: impl-trait-existential-types)

This PR enables `impl Trait` syntax (opaque types) to be used in bindings, e.g.

* `let foo: impl Clone = 1;`
* `static foo: impl Clone = 2;`
* `const foo: impl Clone = 3;`

This is part of [RFC 2071](https://github.com/rust-lang/rfcs/blob/master/text/2071-impl-trait-existential-types.md) ([tracking issue](#34511)), but exists behind the separate feature gate `impl_trait_in_bindings`.

CC @cramertj @oli-obk @eddyb @Centril @varkor
  • Loading branch information
bors committed Sep 25, 2018
2 parents 31789a6 + 16cf404 commit 4141a40
Show file tree
Hide file tree
Showing 25 changed files with 746 additions and 370 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# `impl_trait_in_bindings`

The tracking issue for this feature is: [#34511]

[#34511]: https://github.com/rust-lang/rust/issues/34511

------------------------

The `impl_trait_in_bindings` feature gate lets you use `impl Trait` syntax in
`let`, `static`, and `const` bindings.

A simple example is:

```rust
#![feature(impl_trait_in_bindings)]

use std::fmt::Debug;

fn main() {
let a: impl Debug + Clone = 42;
let b = a.clone();
println!("{:?}", b); // prints `42`
}
```

Note however that because the types of `a` and `b` are opaque in the above
example, calling inherent methods or methods outside of the specified traits
(e.g., `a.abs()` or `b.abs()`) is not allowed, and yields an error.
2 changes: 1 addition & 1 deletion src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,7 @@ specified exit code, use `std::process::exit`.

E0562: r##"
Abstract return types (written `impl Trait` for some trait `Trait`) are only
allowed as function return types.
allowed as function and inherent impl return types.
Erroneous code example:
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/hir/def_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ pub struct DefIndex(u32);
/// thanks to `NodeCollector::new`.
pub const CRATE_DEF_INDEX: DefIndex = DefIndex(0);


impl fmt::Debug for DefIndex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
Expand Down Expand Up @@ -216,7 +215,7 @@ impl DefIndexAddressSpace {
}
}

/// A DefId identifies a particular *definition*, by combining a crate
/// A `DefId` identifies a particular *definition*, by combining a crate
/// index and a def index.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
pub struct DefId {
Expand Down
Loading

0 comments on commit 4141a40

Please sign in to comment.