diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2020-02-29 18:29:44 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2020-03-01 13:47:48 +0300 |
| commit | 9c885d40fb2b216dee5ebd42c35929ad8b54bf4a (patch) | |
| tree | 19056d976b0b5e29bed6063a2939047f818595cd /src/librustc_parse/parser | |
| parent | 857e34c7a39f3bdab948888d36c2ee614fc73857 (diff) | |
| download | rust-9c885d40fb2b216dee5ebd42c35929ad8b54bf4a.tar.gz rust-9c885d40fb2b216dee5ebd42c35929ad8b54bf4a.zip | |
ast: Implement `TryFrom<ItemKind>` for associated and foreign items
Diffstat (limited to 'src/librustc_parse/parser')
| -rw-r--r-- | src/librustc_parse/parser/item.rs | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index cf07e452acb..08e74034e86 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -24,6 +24,7 @@ use rustc_span::source_map::{self, Span}; use rustc_span::symbol::{kw, sym, Symbol}; use log::debug; +use std::convert::TryFrom; use std::mem; pub(super) type ItemInfo = (Ident, ItemKind); @@ -647,16 +648,16 @@ impl<'a> Parser<'a> { /// Parses associated items. fn parse_assoc_item(&mut self, req_name: ReqName) -> PResult<'a, Option<Option<P<AssocItem>>>> { Ok(self.parse_item_(req_name)?.map(|Item { attrs, id, span, vis, ident, kind, tokens }| { - let kind = match kind { - ItemKind::Mac(a) => AssocItemKind::Macro(a), - ItemKind::Fn(a, b, c, d) => AssocItemKind::Fn(a, b, c, d), - ItemKind::TyAlias(a, b, c, d) => AssocItemKind::TyAlias(a, b, c, d), - ItemKind::Const(a, b, c) => AssocItemKind::Const(a, b, c), - ItemKind::Static(a, _, b) => { - self.struct_span_err(span, "associated `static` items are not allowed").emit(); - AssocItemKind::Const(Defaultness::Final, a, b) - } - _ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"), + let kind = match AssocItemKind::try_from(kind) { + Ok(kind) => kind, + Err(kind) => match kind { + ItemKind::Static(a, _, b) => { + self.struct_span_err(span, "associated `static` items are not allowed") + .emit(); + AssocItemKind::Const(Defaultness::Final, a, b) + } + _ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"), + }, }; Some(P(Item { attrs, id, span, vis, ident, kind, tokens })) })) @@ -833,16 +834,15 @@ impl<'a> Parser<'a> { /// Parses a foreign item (one in an `extern { ... }` block). pub fn parse_foreign_item(&mut self) -> PResult<'a, Option<Option<P<ForeignItem>>>> { Ok(self.parse_item_(|_| true)?.map(|Item { attrs, id, span, vis, ident, kind, tokens }| { - let kind = match kind { - ItemKind::Mac(a) => ForeignItemKind::Macro(a), - ItemKind::Fn(a, b, c, d) => ForeignItemKind::Fn(a, b, c, d), - ItemKind::TyAlias(a, b, c, d) => ForeignItemKind::TyAlias(a, b, c, d), - ItemKind::Static(a, b, c) => ForeignItemKind::Static(a, b, c), - ItemKind::Const(_, a, b) => { - self.error_on_foreign_const(span, ident); - ForeignItemKind::Static(a, Mutability::Not, b) - } - _ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"), + let kind = match ForeignItemKind::try_from(kind) { + Ok(kind) => kind, + Err(kind) => match kind { + ItemKind::Const(_, a, b) => { + self.error_on_foreign_const(span, ident); + ForeignItemKind::Static(a, Mutability::Not, b) + } + _ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"), + }, }; Some(P(Item { attrs, id, span, vis, ident, kind, tokens })) })) |
