@@ -38,6 +38,7 @@ use rustc_span::source_map::{respan, Spanned};
3838use rustc_span:: symbol:: { kw, sym, Symbol } ;
3939use rustc_span:: { Span , DUMMY_SP } ;
4040
41+ use std:: convert:: TryFrom ;
4142use std:: fmt;
4243use std:: iter;
4344
@@ -2443,10 +2444,10 @@ impl Item {
24432444 }
24442445}
24452446
2446- impl < K : IntoItemKind > Item < K > {
2447+ impl < K : Into < ItemKind > > Item < K > {
24472448 pub fn into_item ( self ) -> Item {
24482449 let Item { attrs, id, span, vis, ident, kind, tokens } = self ;
2449- Item { attrs, id, span, vis, ident, kind : kind. into_item_kind ( ) , tokens }
2450+ Item { attrs, id, span, vis, ident, kind : kind. into ( ) , tokens }
24502451 }
24512452}
24522453
@@ -2626,20 +2627,11 @@ impl ItemKind {
26262627 }
26272628}
26282629
2629- pub trait IntoItemKind {
2630- fn into_item_kind ( self ) -> ItemKind ;
2631- }
2632-
2633- // FIXME(Centril): These definitions should be unmerged;
2634- // see https://github.com/rust-lang/rust/pull/69194#discussion_r379899975
2635- pub type ForeignItem = Item < AssocItemKind > ;
2636- pub type ForeignItemKind = AssocItemKind ;
2637-
26382630/// Represents associated items.
26392631/// These include items in `impl` and `trait` definitions.
26402632pub type AssocItem = Item < AssocItemKind > ;
26412633
2642- /// Represents non-free item kinds.
2634+ /// Represents associated item kinds.
26432635///
26442636/// The term "provided" in the variants below refers to the item having a default
26452637/// definition / body. Meanwhile, a "required" item lacks a definition / body.
@@ -2648,36 +2640,87 @@ pub type AssocItem = Item<AssocItemKind>;
26482640/// means "provided" and conversely `None` means "required".
26492641#[ derive( Clone , RustcEncodable , RustcDecodable , Debug ) ]
26502642pub enum AssocItemKind {
2651- /// A constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`.
2643+ /// An associated constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`.
26522644 /// If `def` is parsed, then the constant is provided, and otherwise required.
26532645 Const ( Defaultness , P < Ty > , Option < P < Expr > > ) ,
2654- /// A static item (`static FOO: u8`).
2655- Static ( P < Ty > , Mutability , Option < P < Expr > > ) ,
2656- /// A function.
2646+ /// An associated function.
26572647 Fn ( Defaultness , FnSig , Generics , Option < P < Block > > ) ,
2658- /// A type.
2648+ /// An associated type.
26592649 TyAlias ( Defaultness , Generics , GenericBounds , Option < P < Ty > > ) ,
2660- /// A macro expanding to items.
2650+ /// A macro expanding to associated items.
26612651 Macro ( Mac ) ,
26622652}
26632653
26642654impl AssocItemKind {
26652655 pub fn defaultness ( & self ) -> Defaultness {
26662656 match * self {
26672657 Self :: Const ( def, ..) | Self :: Fn ( def, ..) | Self :: TyAlias ( def, ..) => def,
2668- Self :: Macro ( ..) | Self :: Static ( .. ) => Defaultness :: Final ,
2658+ Self :: Macro ( ..) => Defaultness :: Final ,
26692659 }
26702660 }
26712661}
26722662
2673- impl IntoItemKind for AssocItemKind {
2674- fn into_item_kind ( self ) -> ItemKind {
2675- match self {
2663+ impl From < AssocItemKind > for ItemKind {
2664+ fn from ( assoc_item_kind : AssocItemKind ) -> ItemKind {
2665+ match assoc_item_kind {
26762666 AssocItemKind :: Const ( a, b, c) => ItemKind :: Const ( a, b, c) ,
2677- AssocItemKind :: Static ( a, b, c) => ItemKind :: Static ( a, b, c) ,
26782667 AssocItemKind :: Fn ( a, b, c, d) => ItemKind :: Fn ( a, b, c, d) ,
26792668 AssocItemKind :: TyAlias ( a, b, c, d) => ItemKind :: TyAlias ( a, b, c, d) ,
26802669 AssocItemKind :: Macro ( a) => ItemKind :: Mac ( a) ,
26812670 }
26822671 }
26832672}
2673+
2674+ impl TryFrom < ItemKind > for AssocItemKind {
2675+ type Error = ItemKind ;
2676+
2677+ fn try_from ( item_kind : ItemKind ) -> Result < AssocItemKind , ItemKind > {
2678+ Ok ( match item_kind {
2679+ ItemKind :: Const ( a, b, c) => AssocItemKind :: Const ( a, b, c) ,
2680+ ItemKind :: Fn ( a, b, c, d) => AssocItemKind :: Fn ( a, b, c, d) ,
2681+ ItemKind :: TyAlias ( a, b, c, d) => AssocItemKind :: TyAlias ( a, b, c, d) ,
2682+ ItemKind :: Mac ( a) => AssocItemKind :: Macro ( a) ,
2683+ _ => return Err ( item_kind) ,
2684+ } )
2685+ }
2686+ }
2687+
2688+ /// An item in `extern` block.
2689+ #[ derive( Clone , RustcEncodable , RustcDecodable , Debug ) ]
2690+ pub enum ForeignItemKind {
2691+ /// A foreign static item (`static FOO: u8`).
2692+ Static ( P < Ty > , Mutability , Option < P < Expr > > ) ,
2693+ /// A foreign function.
2694+ Fn ( Defaultness , FnSig , Generics , Option < P < Block > > ) ,
2695+ /// A foreign type.
2696+ TyAlias ( Defaultness , Generics , GenericBounds , Option < P < Ty > > ) ,
2697+ /// A macro expanding to foreign items.
2698+ Macro ( Mac ) ,
2699+ }
2700+
2701+ impl From < ForeignItemKind > for ItemKind {
2702+ fn from ( foreign_item_kind : ForeignItemKind ) -> ItemKind {
2703+ match foreign_item_kind {
2704+ ForeignItemKind :: Static ( a, b, c) => ItemKind :: Static ( a, b, c) ,
2705+ ForeignItemKind :: Fn ( a, b, c, d) => ItemKind :: Fn ( a, b, c, d) ,
2706+ ForeignItemKind :: TyAlias ( a, b, c, d) => ItemKind :: TyAlias ( a, b, c, d) ,
2707+ ForeignItemKind :: Macro ( a) => ItemKind :: Mac ( a) ,
2708+ }
2709+ }
2710+ }
2711+
2712+ impl TryFrom < ItemKind > for ForeignItemKind {
2713+ type Error = ItemKind ;
2714+
2715+ fn try_from ( item_kind : ItemKind ) -> Result < ForeignItemKind , ItemKind > {
2716+ Ok ( match item_kind {
2717+ ItemKind :: Static ( a, b, c) => ForeignItemKind :: Static ( a, b, c) ,
2718+ ItemKind :: Fn ( a, b, c, d) => ForeignItemKind :: Fn ( a, b, c, d) ,
2719+ ItemKind :: TyAlias ( a, b, c, d) => ForeignItemKind :: TyAlias ( a, b, c, d) ,
2720+ ItemKind :: Mac ( a) => ForeignItemKind :: Macro ( a) ,
2721+ _ => return Err ( item_kind) ,
2722+ } )
2723+ }
2724+ }
2725+
2726+ pub type ForeignItem = Item < ForeignItemKind > ;
0 commit comments