-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #53324 - alexreg:self_in_typedefs, r=eddyb
`Self` in type definitions (self_in_typedefs) This implements the [`self_in_typedefs` feature](https://github.com/rust-lang/rfcs/blob/master/text/2300-self-in-typedefs.md) ([tracking issue 49303](#49303)). r? @eddyb CC @Centril
- Loading branch information
Showing
7 changed files
with
153 additions
and
37 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/doc/unstable-book/src/language-features/self-in-typedefs.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# `self_in_typedefs` | ||
|
||
The tracking issue for this feature is: [#49303] | ||
|
||
[#49303]: https://github.com/rust-lang/rust/issues/49303 | ||
|
||
------------------------ | ||
|
||
The `self_in_typedefs` feature gate lets you use the special `Self` identifier | ||
in `struct`, `enum`, and `union` type definitions. | ||
|
||
A simple example is: | ||
|
||
```rust | ||
#![feature(self_in_typedefs)] | ||
|
||
enum List<T> | ||
where | ||
Self: PartialOrd<Self> // can write `Self` instead of `List<T>` | ||
{ | ||
Nil, | ||
Cons(T, Box<Self>) // likewise here | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(self_in_typedefs)] | ||
#![feature(untagged_unions)] | ||
|
||
#![allow(dead_code)] | ||
|
||
enum A<'a, T: 'a> | ||
where | ||
Self: Send, T: PartialEq<Self> | ||
{ | ||
Foo(&'a Self), | ||
Bar(T), | ||
} | ||
|
||
struct B<'a, T: 'a> | ||
where | ||
Self: Send, T: PartialEq<Self> | ||
{ | ||
foo: &'a Self, | ||
bar: T, | ||
} | ||
|
||
union C<'a, T: 'a> | ||
where | ||
Self: Send, T: PartialEq<Self> | ||
{ | ||
foo: &'a Self, | ||
bar: T, | ||
} | ||
|
||
fn main() {} |
18 changes: 18 additions & 0 deletions
18
src/test/ui/feature-gates/feature-gate-self-in-typedefs.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
enum StackList<'a, T: 'a> { | ||
Nil, | ||
Cons(T, &'a Self) | ||
//~^ ERROR cannot find type `Self` in this scope | ||
//~| `Self` is only available in traits and impls | ||
} | ||
|
||
fn main() {} |
9 changes: 9 additions & 0 deletions
9
src/test/ui/feature-gates/feature-gate-self-in-typedefs.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0411]: cannot find type `Self` in this scope | ||
--> $DIR/feature-gate-self-in-typedefs.rs:13:17 | ||
| | ||
LL | Cons(T, &'a Self) | ||
| ^^^^ `Self` is only available in traits and impls | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0411`. |