Skip to content

Commit 2d92aa5

Browse files
committed
Fuse associated constant parsing.
1 parent 92a372b commit 2d92aa5

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

src/librustc_parse/parser/item.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_errors::{PResult, Applicability, DiagnosticBuilder, StashKey};
77
use rustc_error_codes::*;
88
use syntax::ast::{self, DUMMY_NODE_ID, Ident, Attribute, AttrKind, AttrStyle, AnonConst, Item};
99
use syntax::ast::{ItemKind, ImplItem, ImplItemKind, TraitItem, TraitItemKind, UseTree, UseTreeKind};
10+
use syntax::ast::{AssocItemKind};
1011
use syntax::ast::{PathSegment, IsAuto, Constness, IsAsync, Unsafety, Defaultness, Extern, StrLit};
1112
use syntax::ast::{Visibility, VisibilityKind, Mutability, FnHeader, ForeignItem, ForeignItemKind};
1213
use syntax::ast::{Ty, TyKind, Generics, TraitRef, EnumDef, Variant, VariantData, StructField};
@@ -699,7 +700,7 @@ impl<'a> Parser<'a> {
699700
let (name, kind, generics) = if self.eat_keyword(kw::Type) {
700701
self.parse_impl_assoc_ty()?
701702
} else if self.is_const_item() {
702-
self.parse_impl_const()?
703+
self.parse_assoc_const()?
703704
} else if let Some(mac) = self.parse_assoc_macro_invoc("impl", Some(&vis), at_end)? {
704705
// FIXME: code copied from `parse_macro_use_or_failure` -- use abstraction!
705706
(Ident::invalid(), ast::ImplItemKind::Macro(mac), Generics::default())
@@ -749,22 +750,6 @@ impl<'a> Parser<'a> {
749750
!self.is_keyword_ahead(1, &[kw::Fn, kw::Unsafe])
750751
}
751752

752-
/// This parses the grammar:
753-
/// ImplItemConst = "const" Ident ":" Ty "=" Expr ";"
754-
fn parse_impl_const(&mut self) -> PResult<'a, (Ident, ImplItemKind, Generics)> {
755-
self.expect_keyword(kw::Const)?;
756-
let ident = self.parse_ident()?;
757-
self.expect(&token::Colon)?;
758-
let ty = self.parse_ty()?;
759-
let expr = if self.eat(&token::Eq) {
760-
Some(self.parse_expr()?)
761-
} else {
762-
None
763-
};
764-
self.expect_semi()?;
765-
Ok((ident, ImplItemKind::Const(ty, expr), Generics::default()))
766-
}
767-
768753
/// Parses the following grammar:
769754
///
770755
/// AssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty]
@@ -911,7 +896,7 @@ impl<'a> Parser<'a> {
911896
let (name, kind, generics) = if self.eat_keyword(kw::Type) {
912897
self.parse_trait_item_assoc_ty()?
913898
} else if self.is_const_item() {
914-
self.parse_trait_item_const()?
899+
self.parse_assoc_const()?
915900
} else if let Some(mac) = self.parse_assoc_macro_invoc("trait", None, &mut false)? {
916901
// trait item macro.
917902
(Ident::invalid(), TraitItemKind::Macro(mac), Generics::default())
@@ -932,7 +917,10 @@ impl<'a> Parser<'a> {
932917
})
933918
}
934919

935-
fn parse_trait_item_const(&mut self) -> PResult<'a, (Ident, TraitItemKind, Generics)> {
920+
/// This parses the grammar:
921+
///
922+
/// AssocConst = "const" Ident ":" Ty "=" Expr ";"
923+
fn parse_assoc_const(&mut self) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
936924
self.expect_keyword(kw::Const)?;
937925
let ident = self.parse_ident()?;
938926
self.expect(&token::Colon)?;
@@ -943,7 +931,7 @@ impl<'a> Parser<'a> {
943931
None
944932
};
945933
self.expect_semi()?;
946-
Ok((ident, TraitItemKind::Const(ty, expr), Generics::default()))
934+
Ok((ident, AssocItemKind::Const(ty, expr), Generics::default()))
947935
}
948936

949937
/// Parses the following grammar:

0 commit comments

Comments
 (0)