Skip to content

Commit a3f0f51

Browse files
committedNov 3, 2018
Auto merge of #55101 - alexreg:trait-aliases, r=nikomatsakis
Implement trait aliases (RFC 1733) Extends groundwork done in #45047, and fully implements rust-lang/rfcs#1733. CC @durka @nikomatsakis
·
1.90.01.32.0
2 parents 3d28ee3 + 4171685 commit a3f0f51

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+631
-332
lines changed
 
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# `trait_alias`
2+
3+
The tracking issue for this feature is: [#41517]
4+
5+
[#41417]: https://github.com/rust-lang/rust/issues/41517
6+
7+
------------------------
8+
9+
The `trait_alias` feature adds support for trait aliases. These allow aliases
10+
to be created for one or more traits (currently just a single regular trait plus
11+
any number of auto-traits), and used wherever traits would normally be used as
12+
either bounds or trait objects.
13+
14+
```rust
15+
#![feature(trait_alias)]
16+
17+
trait Foo = std::fmt::Debug + Send;
18+
trait Bar = Foo + Sync;
19+
20+
// Use trait alias as bound on type parameter.
21+
fn foo<T: Foo>(v: &T) {
22+
println!("{:?}", v);
23+
}
24+
25+
pub fn main() {
26+
foo(&1);
27+
28+
// Use trait alias for trait objects.
29+
let a: &Bar = &123;
30+
println!("{:?}", a);
31+
let b = Box::new(456) as Box<dyn Foo>;
32+
println!("{:?}", b);
33+
}
34+
```

‎src/librustc/hir/lowering.rs‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4875,23 +4875,24 @@ impl<'a> LoweringContext<'a> {
48754875
let node = match qpath {
48764876
hir::QPath::Resolved(None, path) => {
48774877
// Turn trait object paths into `TyKind::TraitObject` instead.
4878-
if let Def::Trait(_) = path.def {
4879-
let principal = hir::PolyTraitRef {
4880-
bound_generic_params: hir::HirVec::new(),
4881-
trait_ref: hir::TraitRef {
4882-
path: path.and_then(|path| path),
4883-
ref_id: id.node_id,
4884-
hir_ref_id: id.hir_id,
4885-
},
4886-
span,
4887-
};
4878+
match path.def {
4879+
Def::Trait(_) | Def::TraitAlias(_) => {
4880+
let principal = hir::PolyTraitRef {
4881+
bound_generic_params: hir::HirVec::new(),
4882+
trait_ref: hir::TraitRef {
4883+
path: path.and_then(|path| path),
4884+
ref_id: id.node_id,
4885+
hir_ref_id: id.hir_id,
4886+
},
4887+
span,
4888+
};
48884889

4889-
// The original ID is taken by the `PolyTraitRef`,
4890-
// so the `Ty` itself needs a different one.
4891-
id = self.next_id();
4892-
hir::TyKind::TraitObject(hir_vec![principal], self.elided_dyn_bound(span))
4893-
} else {
4894-
hir::TyKind::Path(hir::QPath::Resolved(None, path))
4890+
// The original ID is taken by the `PolyTraitRef`,
4891+
// so the `Ty` itself needs a different one.
4892+
id = self.next_id();
4893+
hir::TyKind::TraitObject(hir_vec![principal], self.elided_dyn_bound(span))
4894+
}
4895+
_ => hir::TyKind::Path(hir::QPath::Resolved(None, path)),
48954896
}
48964897
}
48974898
_ => hir::TyKind::Path(qpath),

0 commit comments

Comments
 (0)
Please sign in to comment.