Skip to content

Commit f8d2264

Browse files
committed
parse associated statics.
1 parent 1c2906e commit f8d2264

21 files changed

+243
-42
lines changed

src/librustc_ast_lowering/item.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
760760
let trait_item_def_id = self.resolver.definitions().local_def_id(i.id);
761761

762762
let (generics, kind) = match i.kind {
763-
AssocItemKind::Const(ref ty, ref default) => {
763+
AssocItemKind::Static(ref ty, _, ref default) // Let's pretend this is a `const`.
764+
| AssocItemKind::Const(ref ty, ref default) => {
764765
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
765766
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
766767
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body))
@@ -802,7 +803,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
802803

803804
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
804805
let (kind, has_default) = match &i.kind {
805-
AssocItemKind::Const(_, default) => (hir::AssocItemKind::Const, default.is_some()),
806+
AssocItemKind::Static(_, _, default) // Let's pretend this is a `const` for recovery.
807+
| AssocItemKind::Const(_, default) => {
808+
(hir::AssocItemKind::Const, default.is_some())
809+
}
806810
AssocItemKind::TyAlias(_, _, default) => (hir::AssocItemKind::Type, default.is_some()),
807811
AssocItemKind::Fn(sig, _, default) => {
808812
(hir::AssocItemKind::Method { has_self: sig.decl.has_self() }, default.is_some())
@@ -827,7 +831,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
827831
let impl_item_def_id = self.resolver.definitions().local_def_id(i.id);
828832

829833
let (generics, kind) = match i.kind {
830-
AssocItemKind::Const(ref ty, ref expr) => {
834+
AssocItemKind::Static(ref ty, _, ref expr) | AssocItemKind::Const(ref ty, ref expr) => {
831835
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
832836
(
833837
hir::Generics::empty(),
@@ -895,7 +899,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
895899
vis: self.lower_visibility(&i.vis, Some(i.id)),
896900
defaultness: self.lower_defaultness(i.defaultness, true /* [1] */),
897901
kind: match &i.kind {
898-
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
902+
AssocItemKind::Static(..) // Let's pretend this is a `const` for recovery.
903+
| AssocItemKind::Const(..) => hir::AssocItemKind::Const,
899904
AssocItemKind::TyAlias(_, _, ty) => {
900905
match ty.as_deref().and_then(|ty| ty.kind.opaque_top_hack()) {
901906
None => hir::AssocItemKind::Type,

src/librustc_ast_passes/ast_validation.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,8 +1250,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12501250
}
12511251
}
12521252

1253-
if let AssocItemKind::Const(..) = item.kind {
1254-
self.check_item_named(item.ident, "const");
1253+
match item.kind {
1254+
AssocItemKind::Const(..) => self.check_item_named(item.ident, "const"),
1255+
AssocItemKind::Static(..) => self
1256+
.err_handler()
1257+
.struct_span_err(item.span, "associated `static` items are not allowed")
1258+
.emit(),
1259+
_ => {}
12551260
}
12561261

12571262
self.with_in_trait_impl(false, |this| visit::walk_assoc_item(this, item, ctxt));

src/librustc_ast_pretty/pprust.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,9 @@ impl<'a> State<'a> {
14411441
self.print_outer_attributes(&item.attrs);
14421442
self.print_defaultness(item.defaultness);
14431443
match &item.kind {
1444+
ast::AssocItemKind::Static(ty, mutbl, expr) => {
1445+
self.print_item_const(item.ident, Some(*mutbl), ty, expr.as_deref(), &item.vis);
1446+
}
14441447
ast::AssocItemKind::Const(ty, expr) => {
14451448
self.print_item_const(item.ident, None, ty, expr.as_deref(), &item.vis);
14461449
}

src/librustc_parse/parser/item.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ impl<'a> Parser<'a> {
546546
1,
547547
&[
548548
kw::Impl,
549+
kw::Static,
549550
kw::Const,
550551
kw::Async,
551552
kw::Fn,
@@ -670,8 +671,14 @@ impl<'a> Parser<'a> {
670671
} else if self.check_fn_front_matter() {
671672
let (ident, sig, generics, body) = self.parse_fn(at_end, &mut attrs, req_name)?;
672673
(ident, AssocItemKind::Fn(sig, generics, body))
674+
} else if self.is_static_global() {
675+
self.bump(); // `static`
676+
let mutbl = self.parse_mutability();
677+
let (ident, ty, expr) = self.parse_item_const_common(Some(mutbl))?;
678+
(ident, AssocItemKind::Static(ty, mutbl, expr))
673679
} else if self.eat_keyword(kw::Const) {
674-
self.parse_assoc_const()?
680+
let (ident, ty, expr) = self.parse_item_const_common(None)?;
681+
(ident, AssocItemKind::Const(ty, expr))
675682
} else if self.isnt_macro_invocation() {
676683
return Err(self.missing_assoc_item_kind_err("associated", self.prev_span));
677684
} else if self.token.is_path_start() {
@@ -688,15 +695,6 @@ impl<'a> Parser<'a> {
688695
Ok(AssocItem { id, span, ident, attrs, vis, defaultness, kind, tokens: None })
689696
}
690697

691-
/// This parses the grammar:
692-
///
693-
/// AssocConst = "const" Ident ":" Ty "=" Expr ";"
694-
fn parse_assoc_const(&mut self) -> PResult<'a, (Ident, AssocItemKind)> {
695-
self.expect_keyword(kw::Const)?;
696-
let (ident, ty, expr) = self.parse_item_const_common(None)?;
697-
Ok((ident, AssocItemKind::Const(ty, expr)))
698-
}
699-
700698
/// Parses the following grammar:
701699
///
702700
/// AssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty]

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,8 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
12511251
// Add the item to the trait info.
12521252
let item_def_id = self.r.definitions.local_def_id(item.id);
12531253
let (res, ns) = match item.kind {
1254-
AssocItemKind::Const(..) => (Res::Def(DefKind::AssocConst, item_def_id), ValueNS),
1254+
AssocItemKind::Static(..) // Let's pretend it's a `const` for recovery.
1255+
| AssocItemKind::Const(..) => (Res::Def(DefKind::AssocConst, item_def_id), ValueNS),
12551256
AssocItemKind::Fn(ref sig, _, _) => {
12561257
if sig.decl.has_self() {
12571258
self.r.has_self.insert(item_def_id);

src/librustc_resolve/def_collector.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
228228
body.as_deref(),
229229
);
230230
}
231-
AssocItemKind::Fn(..) | AssocItemKind::Const(..) => DefPathData::ValueNs(i.ident.name),
231+
AssocItemKind::Fn(..) | AssocItemKind::Const(..) | AssocItemKind::Static(..) => {
232+
DefPathData::ValueNs(i.ident.name)
233+
}
232234
AssocItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name),
233235
AssocItemKind::Macro(..) => return self.visit_macro_invoc(i.id),
234236
};

src/librustc_resolve/late.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,8 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
836836
for item in trait_items {
837837
this.with_trait_items(trait_items, |this| {
838838
match &item.kind {
839-
AssocItemKind::Const(ty, default) => {
839+
AssocItemKind::Static(ty, _, default)
840+
| AssocItemKind::Const(ty, default) => {
840841
this.visit_ty(ty);
841842
// Only impose the restrictions of `ConstRibKind` for an
842843
// actual constant expression in a provided default.
@@ -1109,7 +1110,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
11091110
for item in impl_items {
11101111
use crate::ResolutionError::*;
11111112
match &item.kind {
1112-
AssocItemKind::Const(..) => {
1113+
AssocItemKind::Static(..) | AssocItemKind::Const(..) => {
11131114
debug!("resolve_implementation AssocItemKind::Const",);
11141115
// If this is a trait impl, ensure the const
11151116
// exists in trait

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,8 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
10041004
self.process_macro_use(trait_item.span);
10051005
let vis_span = trait_item.span.shrink_to_lo();
10061006
match trait_item.kind {
1007-
ast::AssocItemKind::Const(ref ty, ref expr) => {
1007+
ast::AssocItemKind::Static(ref ty, _, ref expr)
1008+
| ast::AssocItemKind::Const(ref ty, ref expr) => {
10081009
self.process_assoc_const(
10091010
trait_item.id,
10101011
trait_item.ident,
@@ -1074,7 +1075,8 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
10741075
fn process_impl_item(&mut self, impl_item: &'l ast::AssocItem, impl_id: DefId) {
10751076
self.process_macro_use(impl_item.span);
10761077
match impl_item.kind {
1077-
ast::AssocItemKind::Const(ref ty, ref expr) => {
1078+
ast::AssocItemKind::Static(ref ty, _, ref expr)
1079+
| ast::AssocItemKind::Const(ref ty, ref expr) => {
10781080
self.process_assoc_const(
10791081
impl_item.id,
10801082
impl_item.ident,

src/libsyntax/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,6 +2654,8 @@ pub enum AssocItemKind {
26542654
/// A constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`.
26552655
/// If `def` is parsed, then the constant is provided, and otherwise required.
26562656
Const(P<Ty>, Option<P<Expr>>),
2657+
/// A static item (`static FOO: u8`).
2658+
Static(P<Ty>, Mutability, Option<P<Expr>>),
26572659
/// A function.
26582660
Fn(FnSig, Generics, Option<P<Block>>),
26592661
/// A type.

src/libsyntax/mut_visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
954954
visitor.visit_vis(vis);
955955
visit_attrs(attrs, visitor);
956956
match kind {
957-
AssocItemKind::Const(ty, expr) => {
957+
AssocItemKind::Const(ty, expr) | AssocItemKind::Static(ty, _, expr) => {
958958
visitor.visit_ty(ty);
959959
visit_opt(expr, |expr| visitor.visit_expr(expr));
960960
}

0 commit comments

Comments
 (0)