@@ -38,6 +38,7 @@ use rustc_span::source_map::{respan, Spanned};
38
38
use rustc_span:: symbol:: { kw, sym, Symbol } ;
39
39
use rustc_span:: { Span , DUMMY_SP } ;
40
40
41
+ use std:: convert:: TryFrom ;
41
42
use std:: fmt;
42
43
use std:: iter;
43
44
@@ -2443,10 +2444,10 @@ impl Item {
2443
2444
}
2444
2445
}
2445
2446
2446
- impl < K : IntoItemKind > Item < K > {
2447
+ impl < K : Into < ItemKind > > Item < K > {
2447
2448
pub fn into_item ( self ) -> Item {
2448
2449
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 }
2450
2451
}
2451
2452
}
2452
2453
@@ -2626,20 +2627,11 @@ impl ItemKind {
2626
2627
}
2627
2628
}
2628
2629
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
-
2638
2630
/// Represents associated items.
2639
2631
/// These include items in `impl` and `trait` definitions.
2640
2632
pub type AssocItem = Item < AssocItemKind > ;
2641
2633
2642
- /// Represents non-free item kinds.
2634
+ /// Represents associated item kinds.
2643
2635
///
2644
2636
/// The term "provided" in the variants below refers to the item having a default
2645
2637
/// definition / body. Meanwhile, a "required" item lacks a definition / body.
@@ -2648,36 +2640,87 @@ pub type AssocItem = Item<AssocItemKind>;
2648
2640
/// means "provided" and conversely `None` means "required".
2649
2641
#[ derive( Clone , RustcEncodable , RustcDecodable , Debug ) ]
2650
2642
pub enum AssocItemKind {
2651
- /// A constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`.
2643
+ /// An associated constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`.
2652
2644
/// If `def` is parsed, then the constant is provided, and otherwise required.
2653
2645
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.
2657
2647
Fn ( Defaultness , FnSig , Generics , Option < P < Block > > ) ,
2658
- /// A type.
2648
+ /// An associated type.
2659
2649
TyAlias ( Defaultness , Generics , GenericBounds , Option < P < Ty > > ) ,
2660
- /// A macro expanding to items.
2650
+ /// A macro expanding to associated items.
2661
2651
Macro ( Mac ) ,
2662
2652
}
2663
2653
2664
2654
impl AssocItemKind {
2665
2655
pub fn defaultness ( & self ) -> Defaultness {
2666
2656
match * self {
2667
2657
Self :: Const ( def, ..) | Self :: Fn ( def, ..) | Self :: TyAlias ( def, ..) => def,
2668
- Self :: Macro ( ..) | Self :: Static ( .. ) => Defaultness :: Final ,
2658
+ Self :: Macro ( ..) => Defaultness :: Final ,
2669
2659
}
2670
2660
}
2671
2661
}
2672
2662
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 {
2676
2666
AssocItemKind :: Const ( a, b, c) => ItemKind :: Const ( a, b, c) ,
2677
- AssocItemKind :: Static ( a, b, c) => ItemKind :: Static ( a, b, c) ,
2678
2667
AssocItemKind :: Fn ( a, b, c, d) => ItemKind :: Fn ( a, b, c, d) ,
2679
2668
AssocItemKind :: TyAlias ( a, b, c, d) => ItemKind :: TyAlias ( a, b, c, d) ,
2680
2669
AssocItemKind :: Macro ( a) => ItemKind :: Mac ( a) ,
2681
2670
}
2682
2671
}
2683
2672
}
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