Skip to content

Commit 9c885d4

Browse files
committedMar 1, 2020
ast: Implement TryFrom<ItemKind> for associated and foreign items
1 parent 857e34c commit 9c885d4

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed
 

‎src/librustc_ast/ast.rs

+29
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

@@ -2668,6 +2669,20 @@ impl From<AssocItemKind> for ItemKind {
26682669
}
26692670
}
26702671

2672+
impl TryFrom<ItemKind> for AssocItemKind {
2673+
type Error = ItemKind;
2674+
2675+
fn try_from(item_kind: ItemKind) -> Result<AssocItemKind, ItemKind> {
2676+
Ok(match item_kind {
2677+
ItemKind::Const(a, b, c) => AssocItemKind::Const(a, b, c),
2678+
ItemKind::Fn(a, b, c, d) => AssocItemKind::Fn(a, b, c, d),
2679+
ItemKind::TyAlias(a, b, c, d) => AssocItemKind::TyAlias(a, b, c, d),
2680+
ItemKind::Mac(a) => AssocItemKind::Macro(a),
2681+
_ => return Err(item_kind),
2682+
})
2683+
}
2684+
}
2685+
26712686
/// An item in `extern` block.
26722687
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
26732688
pub enum ForeignItemKind {
@@ -2692,4 +2707,18 @@ impl From<ForeignItemKind> for ItemKind {
26922707
}
26932708
}
26942709

2710+
impl TryFrom<ItemKind> for ForeignItemKind {
2711+
type Error = ItemKind;
2712+
2713+
fn try_from(item_kind: ItemKind) -> Result<ForeignItemKind, ItemKind> {
2714+
Ok(match item_kind {
2715+
ItemKind::Static(a, b, c) => ForeignItemKind::Static(a, b, c),
2716+
ItemKind::Fn(a, b, c, d) => ForeignItemKind::Fn(a, b, c, d),
2717+
ItemKind::TyAlias(a, b, c, d) => ForeignItemKind::TyAlias(a, b, c, d),
2718+
ItemKind::Mac(a) => ForeignItemKind::Macro(a),
2719+
_ => return Err(item_kind),
2720+
})
2721+
}
2722+
}
2723+
26952724
pub type ForeignItem = Item<ForeignItemKind>;

‎src/librustc_parse/parser/item.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_span::source_map::{self, Span};
2424
use rustc_span::symbol::{kw, sym, Symbol};
2525

2626
use log::debug;
27+
use std::convert::TryFrom;
2728
use std::mem;
2829

2930
pub(super) type ItemInfo = (Ident, ItemKind);
@@ -647,16 +648,16 @@ impl<'a> Parser<'a> {
647648
/// Parses associated items.
648649
fn parse_assoc_item(&mut self, req_name: ReqName) -> PResult<'a, Option<Option<P<AssocItem>>>> {
649650
Ok(self.parse_item_(req_name)?.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
650-
let kind = match kind {
651-
ItemKind::Mac(a) => AssocItemKind::Macro(a),
652-
ItemKind::Fn(a, b, c, d) => AssocItemKind::Fn(a, b, c, d),
653-
ItemKind::TyAlias(a, b, c, d) => AssocItemKind::TyAlias(a, b, c, d),
654-
ItemKind::Const(a, b, c) => AssocItemKind::Const(a, b, c),
655-
ItemKind::Static(a, _, b) => {
656-
self.struct_span_err(span, "associated `static` items are not allowed").emit();
657-
AssocItemKind::Const(Defaultness::Final, a, b)
658-
}
659-
_ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"),
651+
let kind = match AssocItemKind::try_from(kind) {
652+
Ok(kind) => kind,
653+
Err(kind) => match kind {
654+
ItemKind::Static(a, _, b) => {
655+
self.struct_span_err(span, "associated `static` items are not allowed")
656+
.emit();
657+
AssocItemKind::Const(Defaultness::Final, a, b)
658+
}
659+
_ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"),
660+
},
660661
};
661662
Some(P(Item { attrs, id, span, vis, ident, kind, tokens }))
662663
}))
@@ -833,16 +834,15 @@ impl<'a> Parser<'a> {
833834
/// Parses a foreign item (one in an `extern { ... }` block).
834835
pub fn parse_foreign_item(&mut self) -> PResult<'a, Option<Option<P<ForeignItem>>>> {
835836
Ok(self.parse_item_(|_| true)?.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
836-
let kind = match kind {
837-
ItemKind::Mac(a) => ForeignItemKind::Macro(a),
838-
ItemKind::Fn(a, b, c, d) => ForeignItemKind::Fn(a, b, c, d),
839-
ItemKind::TyAlias(a, b, c, d) => ForeignItemKind::TyAlias(a, b, c, d),
840-
ItemKind::Static(a, b, c) => ForeignItemKind::Static(a, b, c),
841-
ItemKind::Const(_, a, b) => {
842-
self.error_on_foreign_const(span, ident);
843-
ForeignItemKind::Static(a, Mutability::Not, b)
844-
}
845-
_ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
837+
let kind = match ForeignItemKind::try_from(kind) {
838+
Ok(kind) => kind,
839+
Err(kind) => match kind {
840+
ItemKind::Const(_, a, b) => {
841+
self.error_on_foreign_const(span, ident);
842+
ForeignItemKind::Static(a, Mutability::Not, b)
843+
}
844+
_ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
845+
},
846846
};
847847
Some(P(Item { attrs, id, span, vis, ident, kind, tokens }))
848848
}))

0 commit comments

Comments
 (0)
Please sign in to comment.