Skip to content

Commit da400d2

Browse files
committed
Update the compiler itself to use #[rustc_auto_trait]
1 parent 54706c0 commit da400d2

File tree

14 files changed

+82
-40
lines changed

14 files changed

+82
-40
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2993,7 +2993,7 @@ pub enum ItemKind {
29932993
Union(VariantData, Generics),
29942994
/// A trait declaration (`trait`).
29952995
///
2996-
/// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`.
2996+
/// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }`.
29972997
Trait(Box<Trait>),
29982998
/// Trait alias
29992999
///

compiler/rustc_data_structures/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
1010
#![feature(array_windows)]
1111
#![feature(associated_type_bounds)]
12-
#![feature(auto_traits)]
12+
#![cfg_attr(bootstrap, feature(auto_traits))]
1313
#![feature(cell_leak)]
1414
#![feature(core_intrinsics)]
1515
#![feature(extend_one)]

compiler/rustc_data_structures/src/marker.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
11
cfg_if!(
22
if #[cfg(not(parallel_compiler))] {
3+
#[cfg(bootstrap)]
34
pub auto trait DynSend {}
5+
6+
#[cfg(not(bootstrap))]
7+
#[rustc_auto_trait]
8+
pub trait DynSend {}
9+
10+
#[cfg(bootstrap)]
411
pub auto trait DynSync {}
512

13+
#[cfg(not(bootstrap))]
14+
#[rustc_auto_trait]
15+
pub trait DynSync {}
16+
617
impl<T> DynSend for T {}
718
impl<T> DynSync for T {}
819
} else {
20+
#[cfg(bootstrap)]
21+
#[rustc_on_unimplemented(
22+
message = "`{Self}` doesn't implement `DynSend`. \
23+
Add it to `rustc_data_structures::marker` or use `IntoDynSyncSend` if it's already `Send`"
24+
)]
25+
pub unsafe auto trait DynSend {}
26+
27+
#[cfg(not(bootstrap))]
928
#[rustc_on_unimplemented(
1029
message = "`{Self}` doesn't implement `DynSend`. \
1130
Add it to `rustc_data_structures::marker` or use `IntoDynSyncSend` if it's already `Send`"
1231
)]
32+
#[rustc_auto_trait]
1333
// This is an auto trait for types which can be sent across threads if `sync::is_dyn_thread_safe()`
1434
// is true. These types can be wrapped in a `FromDyn` to get a `Send` type. Wrapping a
1535
// `Send` type in `IntoDynSyncSend` will create a `DynSend` type.
16-
pub unsafe auto trait DynSend {}
36+
pub unsafe trait DynSend {}
37+
38+
#[cfg(bootstrap)]
39+
#[rustc_on_unimplemented(
40+
message = "`{Self}` doesn't implement `DynSync`. \
41+
Add it to `rustc_data_structures::marker` or use `IntoDynSyncSend` if it's already `Sync`"
42+
)]
43+
pub unsafe auto trait DynSync {}
1744

45+
#[cfg(not(bootstrap))]
1846
#[rustc_on_unimplemented(
1947
message = "`{Self}` doesn't implement `DynSync`. \
2048
Add it to `rustc_data_structures::marker` or use `IntoDynSyncSend` if it's already `Sync`"
2149
)]
50+
#[rustc_auto_trait]
2251
// This is an auto trait for types which can be shared across threads if `sync::is_dyn_thread_safe()`
2352
// is true. These types can be wrapped in a `FromDyn` to get a `Sync` type. Wrapping a
2453
// `Sync` type in `IntoDynSyncSend` will create a `DynSync` type.
25-
pub unsafe auto trait DynSync {}
54+
pub unsafe trait DynSync {}
2655

2756
// Same with `Sync` and `Send`.
2857
unsafe impl<T: DynSync + ?Sized> DynSend for &T {}

compiler/rustc_data_structures/src/sync.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,17 @@ cfg_if! {
114114
use std::ops::Add;
115115
use std::cell::Cell;
116116

117+
#[cfg(bootstrap)]
117118
pub unsafe auto trait Send {}
119+
#[cfg(not(bootstrap))]
120+
#[rustc_auto_trait]
121+
pub unsafe trait Send {}
122+
123+
#[cfg(bootstrap)]
118124
pub unsafe auto trait Sync {}
125+
#[cfg(not(bootstrap))]
126+
#[rustc_auto_trait]
127+
pub unsafe trait Sync {}
119128

120129
unsafe impl<T> Send for T {}
121130
unsafe impl<T> Sync for T {}

compiler/rustc_error_codes/src/error_codes/E0198.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ unsafe.
1515

1616
This will compile:
1717

18-
```ignore (ignore auto_trait future compatibility warning)
19-
#![feature(auto_traits)]
18+
```
19+
#![feature(negative_impls)]
2020
2121
struct Foo;
2222
23-
auto trait Enterprise {}
23+
trait Enterprise {}
2424
2525
impl !Enterprise for Foo { }
2626
```
27-
28-
Please note that negative impls are only allowed for auto traits.

compiler/rustc_error_codes/src/error_codes/E0321.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
A cross-crate opt-out trait was implemented on something which wasn't a struct
1+
A cross-crate auto trait was implemented on something which wasn't a struct
22
or enum type.
33

44
Erroneous code example:
55

66
```compile_fail,E0321
7-
#![feature(auto_traits)]
7+
#![feature(negative_impls)]
88
99
struct Foo;
1010
@@ -16,6 +16,6 @@ unsafe impl Send for &'static Foo {}
1616
// `&'static Foo`
1717
```
1818

19-
Only structs and enums are permitted to impl Send, Sync, and other opt-out
19+
Only structs and enums are permitted to impl Send, Sync, and other auto
2020
trait, and the struct or enum must be local to the current crate. So, for
2121
example, `unsafe impl Send for Rc<Foo>` is not allowed.

compiler/rustc_error_codes/src/error_codes/E0380.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ An auto trait was declared with a method or an associated item.
33
Erroneous code example:
44

55
```compile_fail,E0380
6-
unsafe auto trait Trait {
6+
#[rustc_auto_trait]
7+
unsafe trait Trait {
78
type Output; // error!
89
}
910
```

compiler/rustc_error_codes/src/error_codes/E0567.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ Generics have been used on an auto trait.
33
Erroneous code example:
44

55
```compile_fail,E0567
6-
#![feature(auto_traits)]
6+
#![feature(rustc_attrs)]
77
8-
auto trait Generic<T> {} // error!
8+
#[rustc_auto_trait]
9+
trait Generic<T> {} // error!
910
# fn main() {}
1011
```
1112

@@ -16,8 +17,9 @@ parameters.
1617
To fix this issue, just remove the generics:
1718

1819
```
19-
#![feature(auto_traits)]
20+
#![feature(rustc_attrs)]
2021
21-
auto trait Generic {} // ok!
22+
#[rustc_auto_trait]
23+
trait Generic {} // ok!
2224
# fn main() {}
2325
```

compiler/rustc_error_codes/src/error_codes/E0568.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ A super trait has been added to an auto trait.
33
Erroneous code example:
44

55
```compile_fail,E0568
6-
#![feature(auto_traits)]
6+
#![feature(rustc_attrs)]
77
8-
auto trait Bound : Copy {} // error!
8+
#[rustc_auto_trait]
9+
trait Bound : Copy {} // error!
910
1011
fn main() {}
1112
```
@@ -18,9 +19,10 @@ all the existing types could implement `Bound` because very few of them have the
1819
To fix this issue, just remove the super trait:
1920

2021
```
21-
#![feature(auto_traits)]
22+
#![feature(rustc_attrs)]
2223
23-
auto trait Bound {} // ok!
24+
#[rustc_auto_trait]
25+
trait Bound {} // ok!
2426
2527
fn main() {}
2628
```

compiler/rustc_error_codes/src/error_codes/E0785.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ An inherent `impl` was written on a dyn auto trait.
33
Erroneous code example:
44

55
```compile_fail,E0785
6-
#![feature(auto_traits)]
6+
#![feature(rustc_attrs)]
77
8-
auto trait AutoTrait {}
8+
#[rustc_auto_trait]
9+
trait AutoTrait {}
910
1011
impl dyn AutoTrait {}
1112
```
@@ -20,11 +21,12 @@ trait, it cannot be implemented at all.
2021
Working example:
2122

2223
```
23-
#![feature(auto_traits)]
24+
#![feature(rustc_attrs)]
2425
2526
trait PrincipalTrait {}
2627
27-
auto trait AutoTrait {}
28+
#[rustc_auto_trait]
29+
trait AutoTrait {}
2830
2931
impl dyn PrincipalTrait + AutoTrait + Send {}
3032
```

compiler/rustc_hir_analysis/src/coherence/orphan.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ fn do_orphan_check_impl<'tcx>(
7171
//
7272
// ```
7373
// // Crate A
74-
// auto trait Foo { }
74+
// #[rustc_auto_trait]
75+
// trait Foo { }
7576
// fn two_foos<A:Foo,B:Foo>(..) {
7677
// one_foo::<(A,B)>(..)
7778
// }
@@ -127,7 +128,8 @@ fn do_orphan_check_impl<'tcx>(
127128
// This is necessary in order for autotrait bounds on methods of trait
128129
// objects to be sound.
129130
//
130-
// auto trait AutoTrait {}
131+
// #[rustc_auto_trait]
132+
// trait AutoTrait {}
131133
//
132134
// trait ObjectSafeTrait {
133135
// fn f(&self) where Self: AutoTrait;

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,17 @@ impl<'tcx> ObligationCause<'tcx> {
160160
self.code = f(std::mem::take(&mut self.code)).into();
161161
}
162162

163+
/// Creates a cause for obligations that are derived from `obligation` by a recursive search.
164+
///
165+
/// E.g., for a builtin bound, or eventually a `#[rustc_auto_trait] trait Foo`.
166+
/// If `obligation` is itself a derived obligation, this is just a clone, but
167+
/// otherwise we create a "derived obligation" cause so as to keep track of the
168+
/// original root obligation for error reporting.
163169
pub fn derived_cause(
164170
mut self,
165171
parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
166172
variant: impl FnOnce(DerivedObligationCause<'tcx>) -> ObligationCauseCode<'tcx>,
167173
) -> ObligationCause<'tcx> {
168-
/*!
169-
* Creates a cause for obligations that are derived from
170-
* `obligation` by a recursive search (e.g., for a builtin
171-
* bound, or eventually a `auto trait Foo`). If `obligation`
172-
* is itself a derived obligation, this is just a clone, but
173-
* otherwise we create a "derived obligation" cause so as to
174-
* keep track of the original root obligation for error
175-
* reporting.
176-
*/
177-
178174
// NOTE(flaper87): As of now, it keeps track of the whole error
179175
// chain. Ideally, we should have a way to configure this either
180176
// by using -Z verbose or just a CLI argument.

compiler/rustc_trait_selection/src/traits/object_safety.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,8 @@ fn virtual_call_violation_for_method<'tcx>(
561561

562562
// dyn Trait is okay:
563563
//
564-
// auto trait AutoTrait {}
564+
// #[rustc_auto_trait]
565+
// trait AutoTrait {}
565566
//
566567
// trait Trait {
567568
// fn f(&self) where Self: AutoTrait;
@@ -580,7 +581,7 @@ fn virtual_call_violation_for_method<'tcx>(
580581
&& tcx.trait_is_auto(pred_trait_ref.def_id)
581582
{
582583
// Consider bounds like `Self: Bound<Self>`. Auto traits are not
583-
// allowed to have generic parameters so `auto trait Bound<T> {}`
584+
// allowed to have generic parameters so `#[rustc_auto_trait] trait Bound<T> {}`
584585
// would already have reported an error at the definition of the
585586
// auto trait.
586587
if pred_trait_ref.args.len() != 1 {

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
354354
Ok(fully_flattened)
355355
}
356356

357-
/// This handles the case where an `auto trait Foo` impl is being used.
357+
/// This handles the case where an auto trait `Foo` impl is being used.
358358
/// The idea is that the impl applies to `X : Foo` if the following conditions are met:
359359
///
360360
/// 1. For each constituent type `Y` in `X`, `Y : Foo` holds

0 commit comments

Comments
 (0)