Skip to content

Commit 4141a40

Browse files
committed
Auto merge of #53542 - alexreg:impl-trait-in-bindings, r=cramertj
`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
2 parents 31789a6 + 16cf404 commit 4141a40

25 files changed

+746
-370
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# `impl_trait_in_bindings`
2+
3+
The tracking issue for this feature is: [#34511]
4+
5+
[#34511]: https://github.com/rust-lang/rust/issues/34511
6+
7+
------------------------
8+
9+
The `impl_trait_in_bindings` feature gate lets you use `impl Trait` syntax in
10+
`let`, `static`, and `const` bindings.
11+
12+
A simple example is:
13+
14+
```rust
15+
#![feature(impl_trait_in_bindings)]
16+
17+
use std::fmt::Debug;
18+
19+
fn main() {
20+
let a: impl Debug + Clone = 42;
21+
let b = a.clone();
22+
println!("{:?}", b); // prints `42`
23+
}
24+
```
25+
26+
Note however that because the types of `a` and `b` are opaque in the above
27+
example, calling inherent methods or methods outside of the specified traits
28+
(e.g., `a.abs()` or `b.abs()`) is not allowed, and yields an error.

src/librustc/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,7 @@ specified exit code, use `std::process::exit`.
17391739

17401740
E0562: r##"
17411741
Abstract return types (written `impl Trait` for some trait `Trait`) are only
1742-
allowed as function return types.
1742+
allowed as function and inherent impl return types.
17431743
17441744
Erroneous code example:
17451745

src/librustc/hir/def_id.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ pub struct DefIndex(u32);
131131
/// thanks to `NodeCollector::new`.
132132
pub const CRATE_DEF_INDEX: DefIndex = DefIndex(0);
133133

134-
135134
impl fmt::Debug for DefIndex {
136135
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
137136
write!(f,
@@ -216,7 +215,7 @@ impl DefIndexAddressSpace {
216215
}
217216
}
218217

219-
/// A DefId identifies a particular *definition*, by combining a crate
218+
/// A `DefId` identifies a particular *definition*, by combining a crate
220219
/// index and a def index.
221220
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
222221
pub struct DefId {

0 commit comments

Comments
 (0)