Skip to content

Commit 2278506

Browse files
committed
Auto merge of #45247 - leodasvacas:implement-auto-trait-syntax, r=nikomatsakis
[Syntax] Implement auto trait syntax Implements `auto trait Send {}` as a substitute for `trait Send {} impl Send for .. {}`. See the [internals thread](https://internals.rust-lang.org/t/pre-rfc-renaming-oibits-and-changing-their-declaration-syntax/3086) for motivation. Part of #13231. The first commit is just a rename moving from "default trait" to "auto trait". The rest is parser->AST->HIR work and making it the same as the current syntax for everything below HIR. It's under the `optin_builtin_traits` feature gate. When can we remove the old syntax? Do we need to wait for a new `stage0`? We also need to formally decide for the new form (even if the keyword is not settled yet). Observations: - If you `auto trait Auto {}` and then `impl Auto for .. {}` that's accepted even if it's redundant. - The new syntax is simpler internally which will allow for a net removal of code, for example well-formedness checks are effectively moved to the parser. - Rustfmt and clippy are broken, need to fix those. - Rustdoc just ignores it for now. ping @petrochenkov @nikomatsakis
2 parents 59d4845 + 5190abb commit 2278506

File tree

97 files changed

+494
-216
lines changed

Some content is hidden

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

97 files changed

+494
-216
lines changed

src/doc/unstable-book/src/language-features/optin-builtin-traits.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ Example:
2424
```rust
2525
#![feature(optin_builtin_traits)]
2626

27-
trait Valid {}
28-
29-
impl Valid for .. {}
27+
auto trait Valid {}
3028

3129
struct True;
3230
struct False;

src/libcore/marker.rs

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub unsafe trait Send {
4646
}
4747

4848
#[stable(feature = "rust1", since = "1.0.0")]
49+
#[allow(unknown_lints)]
50+
#[allow(auto_impl)]
4951
unsafe impl Send for .. { }
5052

5153
#[stable(feature = "rust1", since = "1.0.0")]
@@ -349,6 +351,8 @@ pub unsafe trait Sync {
349351
}
350352

351353
#[stable(feature = "rust1", since = "1.0.0")]
354+
#[allow(unknown_lints)]
355+
#[allow(auto_impl)]
352356
unsafe impl Sync for .. { }
353357

354358
#[stable(feature = "rust1", since = "1.0.0")]
@@ -562,6 +566,8 @@ mod impls {
562566
#[lang = "freeze"]
563567
unsafe trait Freeze {}
564568

569+
#[allow(unknown_lints)]
570+
#[allow(auto_impl)]
565571
unsafe impl Freeze for .. {}
566572

567573
impl<T: ?Sized> !Freeze for UnsafeCell<T> {}

src/librustc/dep_graph/dep_node.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ define_dep_nodes!( <'tcx>
498498
[] SuperPredicatesOfItem(DefId),
499499
[] TraitDefOfItem(DefId),
500500
[] AdtDefOfItem(DefId),
501-
[] IsDefaultImpl(DefId),
501+
[] IsAutoImpl(DefId),
502502
[] ImplTraitRef(DefId),
503503
[] ImplPolarity(DefId),
504504
[] ClosureKind(DefId),

src/librustc/hir/intravisit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
503503
// visit_enum_def() takes care of visiting the Item's NodeId
504504
visitor.visit_enum_def(enum_definition, type_parameters, item.id, item.span)
505505
}
506-
ItemDefaultImpl(_, ref trait_ref) => {
506+
ItemAutoImpl(_, ref trait_ref) => {
507507
visitor.visit_id(item.id);
508508
visitor.visit_trait_ref(trait_ref)
509509
}
@@ -520,7 +520,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
520520
visitor.visit_id(item.id);
521521
visitor.visit_variant_data(struct_definition, item.name, generics, item.id, item.span);
522522
}
523-
ItemTrait(_, ref generics, ref bounds, ref trait_item_refs) => {
523+
ItemTrait(.., ref generics, ref bounds, ref trait_item_refs) => {
524524
visitor.visit_id(item.id);
525525
visitor.visit_generics(generics);
526526
walk_list!(visitor, visit_ty_param_bound, bounds);

src/librustc/hir/lowering.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub struct LoweringContext<'a> {
9696
exported_macros: Vec<hir::MacroDef>,
9797

9898
trait_impls: BTreeMap<DefId, Vec<NodeId>>,
99-
trait_default_impl: BTreeMap<DefId, NodeId>,
99+
trait_auto_impl: BTreeMap<DefId, NodeId>,
100100

101101
is_generator: bool,
102102

@@ -146,7 +146,7 @@ pub fn lower_crate(sess: &Session,
146146
impl_items: BTreeMap::new(),
147147
bodies: BTreeMap::new(),
148148
trait_impls: BTreeMap::new(),
149-
trait_default_impl: BTreeMap::new(),
149+
trait_auto_impl: BTreeMap::new(),
150150
exported_macros: Vec::new(),
151151
catch_scopes: Vec::new(),
152152
loop_scopes: Vec::new(),
@@ -198,7 +198,7 @@ impl<'a> LoweringContext<'a> {
198198
ItemKind::Union(_, ref generics) |
199199
ItemKind::Enum(_, ref generics) |
200200
ItemKind::Ty(_, ref generics) |
201-
ItemKind::Trait(_, ref generics, ..) => {
201+
ItemKind::Trait(_, _, ref generics, ..) => {
202202
let def_id = self.lctx.resolver.definitions().local_def_id(item.id);
203203
let count = generics.lifetimes.len();
204204
self.lctx.type_def_lifetime_params.insert(def_id, count);
@@ -284,7 +284,7 @@ impl<'a> LoweringContext<'a> {
284284
bodies: self.bodies,
285285
body_ids,
286286
trait_impls: self.trait_impls,
287-
trait_default_impl: self.trait_default_impl,
287+
trait_auto_impl: self.trait_auto_impl,
288288
}
289289
}
290290

@@ -1479,14 +1479,14 @@ impl<'a> LoweringContext<'a> {
14791479
let vdata = self.lower_variant_data(vdata);
14801480
hir::ItemUnion(vdata, self.lower_generics(generics))
14811481
}
1482-
ItemKind::DefaultImpl(unsafety, ref trait_ref) => {
1482+
ItemKind::AutoImpl(unsafety, ref trait_ref) => {
14831483
let trait_ref = self.lower_trait_ref(trait_ref);
14841484

14851485
if let Def::Trait(def_id) = trait_ref.path.def {
1486-
self.trait_default_impl.insert(def_id, id);
1486+
self.trait_auto_impl.insert(def_id, id);
14871487
}
14881488

1489-
hir::ItemDefaultImpl(self.lower_unsafety(unsafety),
1489+
hir::ItemAutoImpl(self.lower_unsafety(unsafety),
14901490
trait_ref)
14911491
}
14921492
ItemKind::Impl(unsafety,
@@ -1515,10 +1515,11 @@ impl<'a> LoweringContext<'a> {
15151515
self.lower_ty(ty),
15161516
new_impl_items)
15171517
}
1518-
ItemKind::Trait(unsafety, ref generics, ref bounds, ref items) => {
1518+
ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref items) => {
15191519
let bounds = self.lower_bounds(bounds);
15201520
let items = items.iter().map(|item| self.lower_trait_item_ref(item)).collect();
1521-
hir::ItemTrait(self.lower_unsafety(unsafety),
1521+
hir::ItemTrait(self.lower_is_auto(is_auto),
1522+
self.lower_unsafety(unsafety),
15221523
self.lower_generics(generics),
15231524
bounds,
15241525
items)
@@ -1741,6 +1742,13 @@ impl<'a> LoweringContext<'a> {
17411742
}
17421743
}
17431744

1745+
fn lower_is_auto(&mut self, a: IsAuto) -> hir::IsAuto {
1746+
match a {
1747+
IsAuto::Yes => hir::IsAuto::Yes,
1748+
IsAuto::No => hir::IsAuto::No,
1749+
}
1750+
}
1751+
17441752
fn lower_unsafety(&mut self, u: Unsafety) -> hir::Unsafety {
17451753
match u {
17461754
Unsafety::Unsafe => hir::Unsafety::Unsafe,

src/librustc/hir/map/collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
7171
impl_items: _,
7272
bodies: _,
7373
trait_impls: _,
74-
trait_default_impl: _,
74+
trait_auto_impl: _,
7575
body_ids: _,
7676
} = *krate;
7777

src/librustc/hir/map/def_collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
104104
// Pick the def data. This need not be unique, but the more
105105
// information we encapsulate into
106106
let def_data = match i.node {
107-
ItemKind::DefaultImpl(..) | ItemKind::Impl(..) =>
107+
ItemKind::AutoImpl(..) | ItemKind::Impl(..) =>
108108
DefPathData::Impl,
109109
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) | ItemKind::Trait(..) |
110110
ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) | ItemKind::Ty(..) =>

src/librustc/hir/map/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -474,16 +474,16 @@ impl<'hir> Map<'hir> {
474474
self.forest.krate.trait_impls.get(&trait_did).map_or(&[], |xs| &xs[..])
475475
}
476476

477-
pub fn trait_default_impl(&self, trait_did: DefId) -> Option<NodeId> {
477+
pub fn trait_auto_impl(&self, trait_did: DefId) -> Option<NodeId> {
478478
self.dep_graph.read(DepNode::new_no_params(DepKind::AllLocalTraitImpls));
479479

480480
// NB: intentionally bypass `self.forest.krate()` so that we
481481
// do not trigger a read of the whole krate here
482-
self.forest.krate.trait_default_impl.get(&trait_did).cloned()
482+
self.forest.krate.trait_auto_impl.get(&trait_did).cloned()
483483
}
484484

485485
pub fn trait_is_auto(&self, trait_did: DefId) -> bool {
486-
self.trait_default_impl(trait_did).is_some()
486+
self.trait_auto_impl(trait_did).is_some()
487487
}
488488

489489
/// Get the attributes on the krate. This is preferable to
@@ -1140,7 +1140,7 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
11401140
ItemUnion(..) => "union",
11411141
ItemTrait(..) => "trait",
11421142
ItemImpl(..) => "impl",
1143-
ItemDefaultImpl(..) => "default impl",
1143+
ItemAutoImpl(..) => "default impl",
11441144
};
11451145
format!("{} {}{}", item_str, path_str(), id_str)
11461146
}

src/librustc/hir/mod.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ pub struct Crate {
499499
pub impl_items: BTreeMap<ImplItemId, ImplItem>,
500500
pub bodies: BTreeMap<BodyId, Body>,
501501
pub trait_impls: BTreeMap<DefId, Vec<NodeId>>,
502-
pub trait_default_impl: BTreeMap<DefId, NodeId>,
502+
pub trait_auto_impl: BTreeMap<DefId, NodeId>,
503503

504504
/// A list of the body ids written out in the order in which they
505505
/// appear in the crate. If you're going to process all the bodies
@@ -1500,6 +1500,13 @@ pub struct FnDecl {
15001500
pub has_implicit_self: bool,
15011501
}
15021502

1503+
/// Is the trait definition an auto trait?
1504+
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1505+
pub enum IsAuto {
1506+
Yes,
1507+
No
1508+
}
1509+
15031510
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
15041511
pub enum Unsafety {
15051512
Unsafe,
@@ -1811,12 +1818,12 @@ pub enum Item_ {
18111818
/// A union definition, e.g. `union Foo<A, B> {x: A, y: B}`
18121819
ItemUnion(VariantData, Generics),
18131820
/// Represents a Trait Declaration
1814-
ItemTrait(Unsafety, Generics, TyParamBounds, HirVec<TraitItemRef>),
1821+
ItemTrait(IsAuto, Unsafety, Generics, TyParamBounds, HirVec<TraitItemRef>),
18151822

1816-
// Default trait implementations
1823+
/// Auto trait implementations
18171824
///
18181825
/// `impl Trait for .. {}`
1819-
ItemDefaultImpl(Unsafety, TraitRef),
1826+
ItemAutoImpl(Unsafety, TraitRef),
18201827
/// An implementation, eg `impl<A> Trait for Foo { .. }`
18211828
ItemImpl(Unsafety,
18221829
ImplPolarity,
@@ -1844,7 +1851,7 @@ impl Item_ {
18441851
ItemUnion(..) => "union",
18451852
ItemTrait(..) => "trait",
18461853
ItemImpl(..) |
1847-
ItemDefaultImpl(..) => "item",
1854+
ItemAutoImpl(..) => "item",
18481855
}
18491856
}
18501857

@@ -1864,7 +1871,7 @@ impl Item_ {
18641871
ItemEnum(_, ref generics) |
18651872
ItemStruct(_, ref generics) |
18661873
ItemUnion(_, ref generics) |
1867-
ItemTrait(_, ref generics, _, _) |
1874+
ItemTrait(_, _, ref generics, _, _) |
18681875
ItemImpl(_, _, _, ref generics, _, _, _)=> generics,
18691876
_ => return None
18701877
})

src/librustc/hir/print.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ impl<'a> State<'a> {
660660
self.head(&visibility_qualified(&item.vis, "union"))?;
661661
self.print_struct(struct_def, generics, item.name, item.span, true)?;
662662
}
663-
hir::ItemDefaultImpl(unsafety, ref trait_ref) => {
663+
hir::ItemAutoImpl(unsafety, ref trait_ref) => {
664664
self.head("")?;
665665
self.print_visibility(&item.vis)?;
666666
self.print_unsafety(unsafety)?;
@@ -717,9 +717,10 @@ impl<'a> State<'a> {
717717
}
718718
self.bclose(item.span)?;
719719
}
720-
hir::ItemTrait(unsafety, ref generics, ref bounds, ref trait_items) => {
720+
hir::ItemTrait(is_auto, unsafety, ref generics, ref bounds, ref trait_items) => {
721721
self.head("")?;
722722
self.print_visibility(&item.vis)?;
723+
self.print_is_auto(is_auto)?;
723724
self.print_unsafety(unsafety)?;
724725
self.word_nbsp("trait")?;
725726
self.print_name(item.name)?;
@@ -2274,6 +2275,13 @@ impl<'a> State<'a> {
22742275
hir::Unsafety::Unsafe => self.word_nbsp("unsafe"),
22752276
}
22762277
}
2278+
2279+
pub fn print_is_auto(&mut self, s: hir::IsAuto) -> io::Result<()> {
2280+
match s {
2281+
hir::IsAuto::Yes => self.word_nbsp("auto"),
2282+
hir::IsAuto::No => Ok(()),
2283+
}
2284+
}
22772285
}
22782286

22792287
// Dup'ed from parse::classify, but adapted for the HIR.

src/librustc/ich/impls_hir.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for hir::Item {
898898
hir::ItemForeignMod(..) |
899899
hir::ItemGlobalAsm(..) |
900900
hir::ItemMod(..) |
901-
hir::ItemDefaultImpl(..) |
901+
hir::ItemAutoImpl(..) |
902902
hir::ItemTrait(..) |
903903
hir::ItemImpl(..) |
904904
hir::ItemTy(..) |
@@ -944,8 +944,8 @@ impl_stable_hash_for!(enum hir::Item_ {
944944
ItemEnum(enum_def, generics),
945945
ItemStruct(variant_data, generics),
946946
ItemUnion(variant_data, generics),
947-
ItemTrait(unsafety, generics, bounds, item_refs),
948-
ItemDefaultImpl(unsafety, trait_ref),
947+
ItemTrait(is_auto, unsafety, generics, bounds, item_refs),
948+
ItemAutoImpl(unsafety, trait_ref),
949949
ItemImpl(unsafety, impl_polarity, impl_defaultness, generics, trait_ref, ty, impl_item_refs)
950950
});
951951

@@ -1126,6 +1126,10 @@ impl_stable_hash_for!(enum hir::Mutability {
11261126
MutImmutable
11271127
});
11281128

1129+
impl_stable_hash_for!(enum hir::IsAuto {
1130+
Yes,
1131+
No
1132+
});
11291133

11301134
impl_stable_hash_for!(enum hir::Unsafety {
11311135
Unsafe,

src/librustc/ich/impls_ty.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -731,13 +731,13 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for ty::TraitDef {
731731
def_id: _,
732732
unsafety,
733733
paren_sugar,
734-
has_default_impl,
734+
has_auto_impl,
735735
def_path_hash,
736736
} = *self;
737737

738738
unsafety.hash_stable(hcx, hasher);
739739
paren_sugar.hash_stable(hcx, hasher);
740-
has_default_impl.hash_stable(hcx, hasher);
740+
has_auto_impl.hash_stable(hcx, hasher);
741741
def_path_hash.hash_stable(hcx, hasher);
742742
}
743743
}
@@ -856,7 +856,7 @@ for traits::Vtable<'gcx, N> where N: HashStable<StableHashingContext<'gcx>> {
856856

857857
match self {
858858
&VtableImpl(ref table_impl) => table_impl.hash_stable(hcx, hasher),
859-
&VtableDefaultImpl(ref table_def_impl) => table_def_impl.hash_stable(hcx, hasher),
859+
&VtableAutoImpl(ref table_def_impl) => table_def_impl.hash_stable(hcx, hasher),
860860
&VtableParam(ref table_param) => table_param.hash_stable(hcx, hasher),
861861
&VtableObject(ref table_obj) => table_obj.hash_stable(hcx, hasher),
862862
&VtableBuiltin(ref table_builtin) => table_builtin.hash_stable(hcx, hasher),
@@ -884,11 +884,11 @@ for traits::VtableImplData<'gcx, N> where N: HashStable<StableHashingContext<'gc
884884
}
885885

886886
impl<'gcx, N> HashStable<StableHashingContext<'gcx>>
887-
for traits::VtableDefaultImplData<N> where N: HashStable<StableHashingContext<'gcx>> {
887+
for traits::VtableAutoImplData<N> where N: HashStable<StableHashingContext<'gcx>> {
888888
fn hash_stable<W: StableHasherResult>(&self,
889889
hcx: &mut StableHashingContext<'gcx>,
890890
hasher: &mut StableHasher<W>) {
891-
let traits::VtableDefaultImplData {
891+
let traits::VtableAutoImplData {
892892
trait_def_id,
893893
ref nested,
894894
} = *self;

src/librustc/middle/dead.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
562562
hir::ItemStruct(..) |
563563
hir::ItemUnion(..) |
564564
hir::ItemTrait(..) |
565-
hir::ItemDefaultImpl(..) |
565+
hir::ItemAutoImpl(..) |
566566
hir::ItemImpl(..) => self.tcx.sess.codemap().def_span(item.span),
567567
_ => item.span,
568568
};

src/librustc/middle/reachable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
270270
hir::ItemMod(..) | hir::ItemForeignMod(..) |
271271
hir::ItemImpl(..) | hir::ItemTrait(..) |
272272
hir::ItemStruct(..) | hir::ItemEnum(..) |
273-
hir::ItemUnion(..) | hir::ItemDefaultImpl(..) |
273+
hir::ItemUnion(..) | hir::ItemAutoImpl(..) |
274274
hir::ItemGlobalAsm(..) => {}
275275
}
276276
}

0 commit comments

Comments
 (0)