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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 17 additions & 9 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 13 additions & 6 deletions
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

Lines changed: 10 additions & 2 deletions
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.

0 commit comments

Comments
 (0)