Skip to content

Commit 35884fe

Browse files
committed
parse extern consts
1 parent f8d2264 commit 35884fe

File tree

19 files changed

+98
-44
lines changed

19 files changed

+98
-44
lines changed

src/librustc_ast_lowering/item.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
683683
let ty = self.lower_ty(t, ImplTraitContext::disallowed());
684684
hir::ForeignItemKind::Static(ty, m)
685685
}
686+
ForeignItemKind::Const(ref t, _) => {
687+
// For recovery purposes.
688+
let ty = self.lower_ty(t, ImplTraitContext::disallowed());
689+
hir::ForeignItemKind::Static(ty, Mutability::Not)
690+
}
686691
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
687692
ForeignItemKind::Macro(_) => panic!("macro shouldn't exist here"),
688693
},

src/librustc_ast_passes/ast_validation.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,20 @@ impl<'a> AstValidator<'a> {
533533
}
534534
}
535535

536+
fn error_foreign_const(&self, ident: Ident, span: Span) {
537+
self.err_handler()
538+
.struct_span_err(ident.span, "extern items cannot be `const`")
539+
.span_suggestion(
540+
span.with_hi(ident.span.lo()),
541+
"try using a static value",
542+
"static ".to_string(),
543+
Applicability::MachineApplicable,
544+
)
545+
.span_label(self.current_extern_span(), "in this `extern` block")
546+
.note(MORE_EXTERN)
547+
.emit();
548+
}
549+
536550
/// Reject C-varadic type unless the function is foreign,
537551
/// or free and `unsafe extern "C"` semantically.
538552
fn check_c_varadic_type(&self, fk: FnKind<'a>) {
@@ -989,6 +1003,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
9891003
ForeignItemKind::Static(_, _, body) => {
9901004
self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
9911005
}
1006+
ForeignItemKind::Const(..) => {
1007+
self.error_foreign_const(fi.ident, fi.span);
1008+
}
9921009
ForeignItemKind::Macro(..) => {}
9931010
}
9941011

src/librustc_ast_passes/feature_gate.rs

Lines changed: 1 addition & 1 deletion
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(..) => {}
403+
ast::ForeignItemKind::Macro(..) | ast::ForeignItemKind::Const(..) => {}
404404
}
405405

406406
visit::walk_foreign_item(self, i)

src/librustc_ast_pretty/pprust.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,9 @@ impl<'a> State<'a> {
10231023
ast::ForeignItemKind::Fn(sig, gen, body) => {
10241024
self.print_fn_full(sig, item.ident, gen, &item.vis, body.as_deref(), &item.attrs);
10251025
}
1026+
ast::ForeignItemKind::Const(ty, body) => {
1027+
self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis);
1028+
}
10261029
ast::ForeignItemKind::Static(ty, mutbl, body) => {
10271030
self.print_item_const(item.ident, Some(*mutbl), ty, body.as_deref(), &item.vis);
10281031
}

src/librustc_parse/parser/item.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -880,19 +880,12 @@ impl<'a> Parser<'a> {
880880
} else if self.is_static_global() {
881881
// FOREIGN STATIC ITEM
882882
self.bump(); // `static`
883-
self.parse_item_foreign_static()?
884-
} else if self.token.is_keyword(kw::Const) {
885-
// Treat `const` as `static` for error recovery, but don't add it to expected tokens.
886-
self.bump(); // `const`
887-
self.struct_span_err(self.prev_span, "extern items cannot be `const`")
888-
.span_suggestion(
889-
self.prev_span,
890-
"try using a static value",
891-
"static".to_owned(),
892-
Applicability::MachineApplicable,
893-
)
894-
.emit();
895-
self.parse_item_foreign_static()?
883+
let mutbl = self.parse_mutability();
884+
let (ident, ty, expr) = self.parse_item_const_common(Some(mutbl))?;
885+
(ident, ForeignItemKind::Static(ty, mutbl, expr))
886+
} else if self.eat_keyword(kw::Const) {
887+
let (ident, ty, expr) = self.parse_item_const_common(None)?;
888+
(ident, ForeignItemKind::Const(ty, expr))
896889
} else if self.isnt_macro_invocation() {
897890
return Err(self.missing_assoc_item_kind_err("extern", self.prev_span));
898891
} else if self.token.is_path_start() {
@@ -906,14 +899,6 @@ impl<'a> Parser<'a> {
906899
Ok(P(self.mk_item(lo, ident, kind, vis, attrs)))
907900
}
908901

909-
/// Parses a static item from a foreign module.
910-
/// Assumes that the `static` keyword is already parsed.
911-
fn parse_item_foreign_static(&mut self) -> PResult<'a, (Ident, ForeignItemKind)> {
912-
let mutbl = self.parse_mutability();
913-
let (ident, ty, expr) = self.parse_item_const_common(Some(mutbl))?;
914-
Ok((ident, ForeignItemKind::Static(ty, mutbl, expr)))
915-
}
916-
917902
/// Parses a type from a foreign module.
918903
fn parse_item_foreign_type(&mut self) -> PResult<'a, (Ident, ForeignItemKind)> {
919904
let (ident, kind) = self.parse_assoc_ty()?;

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
826826
ForeignItemKind::Fn(..) => {
827827
(Res::Def(DefKind::Fn, self.r.definitions.local_def_id(item.id)), ValueNS)
828828
}
829-
ForeignItemKind::Static(..) => {
829+
ForeignItemKind::Static(..) | ForeignItemKind::Const(..) => {
830830
(Res::Def(DefKind::Static, self.r.definitions.local_def_id(item.id)), ValueNS)
831831
}
832832
ForeignItemKind::TyAlias(..) => {

src/librustc_resolve/late.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ impl<'a, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
443443
visit::walk_foreign_item(this, foreign_item);
444444
});
445445
}
446-
ForeignItemKind::Static(..) => {
446+
ForeignItemKind::Const(..) | ForeignItemKind::Static(..) => {
447447
self.with_item_rib(HasGenericParams::No, |this| {
448448
visit::walk_foreign_item(this, foreign_item);
449449
});

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,7 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
15341534
self.visit_ty(&ret_ty);
15351535
}
15361536
}
1537-
ast::ForeignItemKind::Static(ref ty, _, _) => {
1537+
ast::ForeignItemKind::Const(ref ty, _) | ast::ForeignItemKind::Static(ref ty, _, _) => {
15381538
if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) {
15391539
down_cast_data!(var_data, DefData, item.span);
15401540
self.dumper.dump_def(&access, var_data);

src/librustc_save_analysis/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
151151
attributes: lower_attributes(item.attrs.clone(), self),
152152
}))
153153
}
154-
ast::ForeignItemKind::Static(ref ty, _, _) => {
154+
ast::ForeignItemKind::Const(ref ty, _) | ast::ForeignItemKind::Static(ref ty, _, _) => {
155155
filter!(self.span_utils, item.ident.span);
156156

157157
let id = id_from_node_id(item.id, self);

src/librustc_save_analysis/sig.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,7 @@ impl Sig for ast::ForeignItem {
792792

793793
Ok(Signature { text: text, defs: defs, refs: vec![] })
794794
}
795+
ast::ForeignItemKind::Const(..) => Err("foreign const"),
795796
ast::ForeignItemKind::Macro(..) => Err("macro"),
796797
}
797798
}

0 commit comments

Comments
 (0)