Skip to content

Commit 4a1b69d

Browse files
committed
Auto merge of #69586 - petrochenkov:unmerge, r=Centril
ast: Unmerge structures for associated items and foreign items Follow-up to #69194. r? @Centril
2 parents b818ccc + 9c885d4 commit 4a1b69d

File tree

16 files changed

+183
-133
lines changed

16 files changed

+183
-133
lines changed

src/librustc_ast/ast.rs

+66-23
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use rustc_span::source_map::{respan, Spanned};
3838
use rustc_span::symbol::{kw, sym, Symbol};
3939
use rustc_span::{Span, DUMMY_SP};
4040

41+
use std::convert::TryFrom;
4142
use std::fmt;
4243
use 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.
26402632
pub 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)]
26502642
pub 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

26642654
impl 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>;

src/librustc_ast/attr/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,6 @@ macro_rules! derive_has_attrs {
721721
}
722722

723723
derive_has_attrs! {
724-
Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::Arm,
724+
Item, Expr, Local, ast::AssocItem, ast::ForeignItem, ast::StructField, ast::Arm,
725725
ast::Field, ast::FieldPat, ast::Variant, ast::Param, GenericParam
726726
}

src/librustc_ast/mut_visit.rs

+24-15
Original file line numberDiff line numberDiff line change
@@ -936,25 +936,12 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
936936
visitor: &mut T,
937937
) -> SmallVec<[P<AssocItem>; 1]> {
938938
let Item { id, ident, vis, attrs, kind, span, tokens: _ } = item.deref_mut();
939-
walk_nested_item(visitor, id, span, ident, vis, attrs, kind);
940-
smallvec![item]
941-
}
942-
943-
pub fn walk_nested_item(
944-
visitor: &mut impl MutVisitor,
945-
id: &mut NodeId,
946-
span: &mut Span,
947-
ident: &mut Ident,
948-
vis: &mut Visibility,
949-
attrs: &mut Vec<Attribute>,
950-
kind: &mut AssocItemKind,
951-
) {
952939
visitor.visit_id(id);
953940
visitor.visit_ident(ident);
954941
visitor.visit_vis(vis);
955942
visit_attrs(attrs, visitor);
956943
match kind {
957-
AssocItemKind::Const(_, ty, expr) | AssocItemKind::Static(ty, _, expr) => {
944+
AssocItemKind::Const(_, ty, expr) => {
958945
visitor.visit_ty(ty);
959946
visit_opt(expr, |expr| visitor.visit_expr(expr));
960947
}
@@ -971,6 +958,7 @@ pub fn walk_nested_item(
971958
AssocItemKind::Macro(mac) => visitor.visit_mac(mac),
972959
}
973960
visitor.visit_span(span);
961+
smallvec![item]
974962
}
975963

976964
pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
@@ -1036,7 +1024,28 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>(
10361024
visitor: &mut T,
10371025
) -> SmallVec<[P<ForeignItem>; 1]> {
10381026
let Item { ident, attrs, id, kind, vis, span, tokens: _ } = item.deref_mut();
1039-
walk_nested_item(visitor, id, span, ident, vis, attrs, kind);
1027+
visitor.visit_id(id);
1028+
visitor.visit_ident(ident);
1029+
visitor.visit_vis(vis);
1030+
visit_attrs(attrs, visitor);
1031+
match kind {
1032+
ForeignItemKind::Static(ty, _, expr) => {
1033+
visitor.visit_ty(ty);
1034+
visit_opt(expr, |expr| visitor.visit_expr(expr));
1035+
}
1036+
ForeignItemKind::Fn(_, sig, generics, body) => {
1037+
visitor.visit_generics(generics);
1038+
visit_fn_sig(sig, visitor);
1039+
visit_opt(body, |body| visitor.visit_block(body));
1040+
}
1041+
ForeignItemKind::TyAlias(_, generics, bounds, ty) => {
1042+
visitor.visit_generics(generics);
1043+
visit_bounds(bounds, visitor);
1044+
visit_opt(ty, |ty| visitor.visit_ty(ty));
1045+
}
1046+
ForeignItemKind::Macro(mac) => visitor.visit_mac(mac),
1047+
}
1048+
visitor.visit_span(span);
10401049
smallvec![item]
10411050
}
10421051

src/librustc_ast/visit.rs

+26-18
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,29 @@ pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
526526
}
527527

528528
pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignItem) {
529-
let Item { id, span, ident, vis, attrs, kind, tokens: _ } = item;
530-
walk_nested_item(visitor, *id, *span, *ident, vis, attrs, kind, FnCtxt::Foreign);
529+
let Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = *item;
530+
visitor.visit_vis(vis);
531+
visitor.visit_ident(ident);
532+
walk_list!(visitor, visit_attribute, attrs);
533+
match kind {
534+
ForeignItemKind::Static(ty, _, expr) => {
535+
visitor.visit_ty(ty);
536+
walk_list!(visitor, visit_expr, expr);
537+
}
538+
ForeignItemKind::Fn(_, sig, generics, body) => {
539+
visitor.visit_generics(generics);
540+
let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, body.as_deref());
541+
visitor.visit_fn(kind, span, id);
542+
}
543+
ForeignItemKind::TyAlias(_, generics, bounds, ty) => {
544+
visitor.visit_generics(generics);
545+
walk_list!(visitor, visit_param_bound, bounds);
546+
walk_list!(visitor, visit_ty, ty);
547+
}
548+
ForeignItemKind::Macro(mac) => {
549+
visitor.visit_mac(mac);
550+
}
551+
}
531552
}
532553

533554
pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) {
@@ -610,31 +631,18 @@ pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>, _span: Spa
610631
}
611632

612633
pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, ctxt: AssocCtxt) {
613-
let Item { id, span, ident, vis, attrs, kind, tokens: _ } = item;
614-
walk_nested_item(visitor, *id, *span, *ident, vis, attrs, kind, FnCtxt::Assoc(ctxt));
615-
}
616-
617-
fn walk_nested_item<'a, V: Visitor<'a>>(
618-
visitor: &mut V,
619-
id: NodeId,
620-
span: Span,
621-
ident: Ident,
622-
vis: &'a Visibility,
623-
attrs: &'a [Attribute],
624-
kind: &'a AssocItemKind,
625-
ctxt: FnCtxt,
626-
) {
634+
let Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = *item;
627635
visitor.visit_vis(vis);
628636
visitor.visit_ident(ident);
629637
walk_list!(visitor, visit_attribute, attrs);
630638
match kind {
631-
AssocItemKind::Const(_, ty, expr) | AssocItemKind::Static(ty, _, expr) => {
639+
AssocItemKind::Const(_, ty, expr) => {
632640
visitor.visit_ty(ty);
633641
walk_list!(visitor, visit_expr, expr);
634642
}
635643
AssocItemKind::Fn(_, sig, generics, body) => {
636644
visitor.visit_generics(generics);
637-
let kind = FnKind::Fn(ctxt, ident, sig, vis, body.as_deref());
645+
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, body.as_deref());
638646
visitor.visit_fn(kind, span, id);
639647
}
640648
AssocItemKind::TyAlias(_, generics, bounds, ty) => {

src/librustc_ast_lowering/item.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -675,11 +675,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
675675
let ty = self.lower_ty(t, ImplTraitContext::disallowed());
676676
hir::ForeignItemKind::Static(ty, m)
677677
}
678-
ForeignItemKind::Const(_, ref t, _) => {
679-
// For recovery purposes.
680-
let ty = self.lower_ty(t, ImplTraitContext::disallowed());
681-
hir::ForeignItemKind::Static(ty, Mutability::Not)
682-
}
683678
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
684679
ForeignItemKind::Macro(_) => panic!("macro shouldn't exist here"),
685680
},
@@ -757,8 +752,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
757752
let trait_item_def_id = self.resolver.definitions().local_def_id(i.id);
758753

759754
let (generics, kind) = match i.kind {
760-
AssocItemKind::Static(ref ty, _, ref default) // Let's pretend this is a `const`.
761-
| AssocItemKind::Const(_, ref ty, ref default) => {
755+
AssocItemKind::Const(_, ref ty, ref default) => {
762756
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
763757
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
764758
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body))
@@ -800,11 +794,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
800794

801795
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
802796
let (kind, has_default) = match &i.kind {
803-
AssocItemKind::Static(_, _, default) // Let's pretend this is a `const` for recovery.
804-
| AssocItemKind::Const(_, _, default) => {
805-
(hir::AssocItemKind::Const, default.is_some())
797+
AssocItemKind::Const(_, _, default) => (hir::AssocItemKind::Const, default.is_some()),
798+
AssocItemKind::TyAlias(_, _, _, default) => {
799+
(hir::AssocItemKind::Type, default.is_some())
806800
}
807-
AssocItemKind::TyAlias(_, _, _, default) => (hir::AssocItemKind::Type, default.is_some()),
808801
AssocItemKind::Fn(_, sig, _, default) => {
809802
(hir::AssocItemKind::Method { has_self: sig.decl.has_self() }, default.is_some())
810803
}
@@ -824,7 +817,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
824817
let impl_item_def_id = self.resolver.definitions().local_def_id(i.id);
825818

826819
let (generics, kind) = match &i.kind {
827-
AssocItemKind::Static(ty, _, expr) | AssocItemKind::Const(_, ty, expr) => {
820+
AssocItemKind::Const(_, ty, expr) => {
828821
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
829822
(
830823
hir::Generics::empty(),
@@ -892,8 +885,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
892885
vis: self.lower_visibility(&i.vis, Some(i.id)),
893886
defaultness: self.lower_defaultness(i.kind.defaultness(), true /* [1] */),
894887
kind: match &i.kind {
895-
AssocItemKind::Static(..) // Let's pretend this is a `const` for recovery.
896-
| AssocItemKind::Const(..) => hir::AssocItemKind::Const,
888+
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
897889
AssocItemKind::TyAlias(.., ty) => {
898890
match ty.as_deref().and_then(|ty| ty.kind.opaque_top_hack()) {
899891
None => hir::AssocItemKind::Type,

src/librustc_ast_passes/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
976976
ForeignItemKind::Static(_, _, body) => {
977977
self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
978978
}
979-
ForeignItemKind::Const(..) | ForeignItemKind::Macro(..) => {}
979+
ForeignItemKind::Macro(..) => {}
980980
}
981981

982982
visit::walk_foreign_item(self, fi)

src/librustc_ast_passes/feature_gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
400400
ast::ForeignItemKind::TyAlias(..) => {
401401
gate_feature_post!(&self, extern_types, i.span, "extern types are experimental");
402402
}
403-
ast::ForeignItemKind::Macro(..) | ast::ForeignItemKind::Const(..) => {}
403+
ast::ForeignItemKind::Macro(..) => {}
404404
}
405405

406406
visit::walk_foreign_item(self, i)

0 commit comments

Comments
 (0)