-
Notifications
You must be signed in to change notification settings - Fork 13k
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 #53693 - scottmcm:marker-trait-attribute, r=nikomatsakis
Support an explicit annotation for marker traits From the tracking issue for rust-lang/rfcs#1268: > It seems obvious that we should make a `#[marker]` annotation. ~ #29864 (comment) This PR allows you to put `#[marker]` on a trait, at which point: - [x] The trait must not have any items ~~All of the trait's items must have defaults~~ - [x] Any impl of the trait must be empty (not override any items) - [x] But impls of the trait are allowed to overlap r? @nikomatsakis
- Loading branch information
Showing
26 changed files
with
540 additions
and
17 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/doc/unstable-book/src/language-features/marker-trait-attr.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,33 @@ | ||
# `marker_trait_attr` | ||
|
||
The tracking issue for this feature is: [#29864] | ||
|
||
[#29864]: https://github.com/rust-lang/rust/issues/29864 | ||
|
||
------------------------ | ||
|
||
Normally, Rust keeps you from adding trait implementations that could | ||
overlap with each other, as it would be ambiguous which to use. This | ||
feature, however, carves out an exception to that rule: a trait can | ||
opt-in to having overlapping implementations, at the cost that those | ||
implementations are not allowed to override anything (and thus the | ||
trait itself cannot have any associated items, as they're pointless | ||
when they'd need to do the same thing for every type anyway). | ||
|
||
```rust | ||
#![feature(marker_trait_attr)] | ||
|
||
use std::fmt::{Debug, Display}; | ||
|
||
#[marker] trait MyMarker {} | ||
|
||
impl<T: Debug> MyMarker for T {} | ||
impl<T: Display> MyMarker for T {} | ||
|
||
fn foo<T: MyMarker>(t: T) -> T { | ||
t | ||
} | ||
``` | ||
|
||
This is expected to replace the unstable `overlapping_marker_traits` | ||
feature, which applied to all empty traits (without needing an opt-in). |
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
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
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
35 changes: 35 additions & 0 deletions
35
src/test/run-pass/overlap-permitted-for-annotated-marker-traits.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,35 @@ | ||
// Copyright 2017 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. | ||
|
||
// Tests for RFC 1268: we allow overlapping impls of marker traits, | ||
// that is, traits with #[marker]. In this case, a type `T` is | ||
// `MyMarker` if it is either `Debug` or `Display`. | ||
|
||
#![feature(marker_trait_attr)] | ||
|
||
use std::fmt::{Debug, Display}; | ||
|
||
#[marker] trait MyMarker {} | ||
|
||
impl<T: Debug> MyMarker for T {} | ||
impl<T: Display> MyMarker for T {} | ||
|
||
fn foo<T: MyMarker>(t: T) -> T { | ||
t | ||
} | ||
|
||
fn main() { | ||
// Debug && Display: | ||
assert_eq!(1, foo(1)); | ||
assert_eq!(2.0, foo(2.0)); | ||
|
||
// Debug && !Display: | ||
assert_eq!(vec![1], foo(vec![1])); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/ui/feature-gates/feature-gate-marker_trait_attr.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,19 @@ | ||
// Copyright 2017 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. | ||
|
||
use std::fmt::{Debug, Display}; | ||
|
||
#[marker] trait ExplicitMarker {} | ||
//~^ ERROR marker traits is an experimental feature (see issue #29864) | ||
|
||
impl<T: Display> ExplicitMarker for T {} | ||
impl<T: Debug> ExplicitMarker for T {} | ||
|
||
fn main() {} |
11 changes: 11 additions & 0 deletions
11
src/test/ui/feature-gates/feature-gate-marker_trait_attr.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,11 @@ | ||
error[E0658]: marker traits is an experimental feature (see issue #29864) | ||
--> $DIR/feature-gate-marker_trait_attr.rs:13:1 | ||
| | ||
LL | #[marker] trait ExplicitMarker {} | ||
| ^^^^^^^^^ | ||
| | ||
= help: add #![feature(marker_trait_attr)] to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
Oops, something went wrong.