Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc_ast_lowering/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
}
ForeignItemKind::Static(ref t, m) => {
ForeignItemKind::Static(ref t, m, _) => {
let ty = self.lower_ty(t, ImplTraitContext::disallowed());
hir::ForeignItemKind::Static(ty, m)
}
Expand Down
34 changes: 27 additions & 7 deletions src/librustc_ast_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,19 +464,22 @@ impl<'a> AstValidator<'a> {
}
}

fn check_foreign_ty_bodyless(&self, ident: Ident, body: Option<&Ty>) {
fn check_foreign_kind_bodyless(&self, ident: Ident, kind: &str, body: Option<Span>) {
let body = match body {
None => return,
Some(body) => body,
};
self.err_handler()
.struct_span_err(ident.span, "incorrect `type` inside `extern` block")
.struct_span_err(ident.span, &format!("incorrect `{}` inside `extern` block", kind))
.span_label(ident.span, "cannot have a body")
.span_label(body.span, "the invalid body")
.span_label(body, "the invalid body")
.span_label(
self.current_extern_span(),
"`extern` blocks define existing foreign types and types \
inside of them cannot have a body",
format!(
"`extern` blocks define existing foreign {0}s and {0}s \
inside of them cannot have a body",
kind
),
)
.note(MORE_EXTERN)
.emit();
Expand Down Expand Up @@ -579,6 +582,16 @@ impl<'a> AstValidator<'a> {
}
}
}

fn check_item_named(&self, ident: Ident, kind: &str) {
if ident.name != kw::Underscore {
return;
}
self.err_handler()
.struct_span_err(ident.span, &format!("`{}` items in this context need a name", kind))
.span_label(ident.span, format!("`_` is not a valid name for this `{}` item", kind))
.emit();
}
}

enum GenericPosition {
Expand Down Expand Up @@ -969,11 +982,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
self.check_foreign_fn_headerless(fi.ident, fi.span, sig.header);
}
ForeignItemKind::TyAlias(generics, bounds, body) => {
self.check_foreign_ty_bodyless(fi.ident, body.as_deref());
self.check_foreign_kind_bodyless(fi.ident, "type", body.as_ref().map(|b| b.span));
self.check_type_no_bounds(bounds, "`extern` blocks");
self.check_foreign_ty_genericless(generics);
}
ForeignItemKind::Static(..) | ForeignItemKind::Macro(..) => {}
ForeignItemKind::Static(_, _, body) => {
self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
}
ForeignItemKind::Macro(..) => {}
}

visit::walk_foreign_item(self, fi)
Expand Down Expand Up @@ -1234,6 +1250,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}
}

if let AssocItemKind::Const(..) = item.kind {
self.check_item_named(item.ident, "const");
}

self.with_in_trait_impl(false, |this| visit::walk_assoc_item(this, item, ctxt));
}
}
Expand Down
71 changes: 22 additions & 49 deletions src/librustc_ast_pretty/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,17 +1023,8 @@ impl<'a> State<'a> {
ast::ForeignItemKind::Fn(sig, gen, body) => {
self.print_fn_full(sig, item.ident, gen, &item.vis, body.as_deref(), &item.attrs);
}
ast::ForeignItemKind::Static(t, m) => {
self.head(visibility_qualified(&item.vis, "static"));
if *m == ast::Mutability::Mut {
self.word_space("mut");
}
self.print_ident(item.ident);
self.word_space(":");
self.print_type(t);
self.s.word(";");
self.end(); // end the head-ibox
self.end(); // end the outer cbox
ast::ForeignItemKind::Static(ty, mutbl, body) => {
self.print_item_const(item.ident, Some(*mutbl), ty, body.as_deref(), &item.vis);
}
ast::ForeignItemKind::TyAlias(generics, bounds, ty) => {
self.print_associated_type(item.ident, generics, bounds, ty.as_deref());
Expand All @@ -1047,24 +1038,31 @@ impl<'a> State<'a> {
}
}

fn print_associated_const(
fn print_item_const(
&mut self,
ident: ast::Ident,
mutbl: Option<ast::Mutability>,
ty: &ast::Ty,
default: Option<&ast::Expr>,
body: Option<&ast::Expr>,
vis: &ast::Visibility,
) {
self.s.word(visibility_qualified(vis, ""));
self.word_space("const");
let leading = match mutbl {
None => "const ",
Some(ast::Mutability::Not) => "static ",
Some(ast::Mutability::Mut) => "static mut ",
};
self.head(visibility_qualified(vis, leading));
self.print_ident(ident);
self.word_space(":");
self.print_type(ty);
if let Some(expr) = default {
self.s.space();
self.s.space();
self.end(); // end the head-ibox
if let Some(body) = body {
self.word_space("=");
self.print_expr(expr);
self.print_expr(body);
}
self.s.word(";")
self.s.word(";");
self.end(); // end the outer cbox
}

fn print_associated_type(
Expand Down Expand Up @@ -1114,36 +1112,11 @@ impl<'a> State<'a> {
self.end(); // end inner head-block
self.end(); // end outer head-block
}
ast::ItemKind::Static(ref ty, m, ref expr) => {
self.head(visibility_qualified(&item.vis, "static"));
if m == ast::Mutability::Mut {
self.word_space("mut");
}
self.print_ident(item.ident);
self.word_space(":");
self.print_type(ty);
self.s.space();
self.end(); // end the head-ibox
if let Some(expr) = expr {
self.word_space("=");
self.print_expr(expr);
}
self.s.word(";");
self.end(); // end the outer cbox
ast::ItemKind::Static(ref ty, mutbl, ref body) => {
self.print_item_const(item.ident, Some(mutbl), ty, body.as_deref(), &item.vis);
}
ast::ItemKind::Const(ref ty, ref expr) => {
self.head(visibility_qualified(&item.vis, "const"));
self.print_ident(item.ident);
self.word_space(":");
self.print_type(ty);
self.s.space();
self.end(); // end the head-ibox
if let Some(expr) = expr {
self.word_space("=");
self.print_expr(expr);
}
self.s.word(";");
self.end(); // end the outer cbox
ast::ItemKind::Const(ref ty, ref body) => {
self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis);
}
ast::ItemKind::Fn(ref sig, ref gen, ref body) => {
self.print_fn_full(sig, item.ident, gen, &item.vis, body.as_deref(), &item.attrs);
Expand Down Expand Up @@ -1469,7 +1442,7 @@ impl<'a> State<'a> {
self.print_defaultness(item.defaultness);
match &item.kind {
ast::AssocItemKind::Const(ty, expr) => {
self.print_associated_const(item.ident, ty, expr.as_deref(), &item.vis);
self.print_item_const(item.ident, None, ty, expr.as_deref(), &item.vis);
}
ast::AssocItemKind::Fn(sig, generics, body) => {
let body = body.as_deref();
Expand Down
37 changes: 21 additions & 16 deletions src/librustc_parse/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ impl<'a> Parser<'a> {
} else if self.check_fn_front_matter() {
let (ident, sig, generics, body) = self.parse_fn(at_end, &mut attrs, req_name)?;
(ident, AssocItemKind::Fn(sig, generics, body))
} else if self.check_keyword(kw::Const) {
} else if self.eat_keyword(kw::Const) {
self.parse_assoc_const()?
} else if self.isnt_macro_invocation() {
return Err(self.missing_assoc_item_kind_err("associated", self.prev_span));
Expand All @@ -693,11 +693,7 @@ impl<'a> Parser<'a> {
/// AssocConst = "const" Ident ":" Ty "=" Expr ";"
fn parse_assoc_const(&mut self) -> PResult<'a, (Ident, AssocItemKind)> {
self.expect_keyword(kw::Const)?;
let ident = self.parse_ident()?;
self.expect(&token::Colon)?;
let ty = self.parse_ty()?;
let expr = if self.eat(&token::Eq) { Some(self.parse_expr()?) } else { None };
self.expect_semi()?;
let (ident, ty, expr) = self.parse_item_const_common(None)?;
Ok((ident, AssocItemKind::Const(ty, expr)))
}

Expand Down Expand Up @@ -916,11 +912,8 @@ impl<'a> Parser<'a> {
/// Assumes that the `static` keyword is already parsed.
fn parse_item_foreign_static(&mut self) -> PResult<'a, (Ident, ForeignItemKind)> {
let mutbl = self.parse_mutability();
let ident = self.parse_ident()?;
self.expect(&token::Colon)?;
let ty = self.parse_ty()?;
self.expect_semi()?;
Ok((ident, ForeignItemKind::Static(ty, mutbl)))
let (ident, ty, expr) = self.parse_item_const_common(Some(mutbl))?;
Ok((ident, ForeignItemKind::Static(ty, mutbl, expr)))
}

/// Parses a type from a foreign module.
Expand Down Expand Up @@ -971,6 +964,22 @@ impl<'a> Parser<'a> {
///
/// When `m` is `"const"`, `$ident` may also be `"_"`.
fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> {
let (id, ty, expr) = self.parse_item_const_common(m)?;
let item = match m {
Some(m) => ItemKind::Static(ty, m, expr),
None => ItemKind::Const(ty, expr),
};
Ok((id, item))
}

/// Parse `["const" | ("static" "mut"?)] $ident ":" $ty (= $expr)?` with
/// `["const" | ("static" "mut"?)]` already parsed and stored in `m`.
///
/// When `m` is `"const"`, `$ident` may also be `"_"`.
fn parse_item_const_common(
&mut self,
m: Option<Mutability>,
) -> PResult<'a, (Ident, P<Ty>, Option<P<ast::Expr>>)> {
let id = if m.is_none() { self.parse_ident_or_underscore() } else { self.parse_ident() }?;

// Parse the type of a `const` or `static mut?` item.
Expand All @@ -983,11 +992,7 @@ impl<'a> Parser<'a> {

let expr = if self.eat(&token::Eq) { Some(self.parse_expr()?) } else { None };
self.expect_semi()?;
let item = match m {
Some(m) => ItemKind::Static(ty, m, expr),
None => ItemKind::Const(ty, expr),
};
Ok((id, item))
Ok((id, ty, expr))
}

/// We were supposed to parse `:` but the `:` was missing.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,7 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
self.visit_ty(&ret_ty);
}
}
ast::ForeignItemKind::Static(ref ty, _) => {
ast::ForeignItemKind::Static(ref ty, _, _) => {
if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) {
down_cast_data!(var_data, DefData, item.span);
self.dumper.dump_def(&access, var_data);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_save_analysis/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
attributes: lower_attributes(item.attrs.clone(), self),
}))
}
ast::ForeignItemKind::Static(ref ty, _) => {
ast::ForeignItemKind::Static(ref ty, _, _) => {
filter!(self.span_utils, item.ident.span);

let id = id_from_node_id(item.id, self);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_save_analysis/sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ impl Sig for ast::ForeignItem {

Ok(sig)
}
ast::ForeignItemKind::Static(ref ty, m) => {
ast::ForeignItemKind::Static(ref ty, m, _) => {
let mut text = "static ".to_owned();
if m == ast::Mutability::Mut {
text.push_str("mut ");
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2606,7 +2606,7 @@ pub type ForeignItem = Item<ForeignItemKind>;
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub enum ForeignItemKind {
/// A static item (`static FOO: u8`).
Static(P<Ty>, Mutability),
Static(P<Ty>, Mutability, Option<P<Expr>>),
/// A function.
Fn(FnSig, Generics, Option<P<Block>>),
/// A type.
Expand Down
5 changes: 4 additions & 1 deletion src/libsyntax/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,10 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>(
visitor.visit_generics(generics);
visit_opt(body, |body| visitor.visit_block(body));
}
ForeignItemKind::Static(t, _m) => visitor.visit_ty(t),
ForeignItemKind::Static(ty, _, body) => {
visitor.visit_ty(ty);
visit_opt(body, |body| visitor.visit_expr(body));
}
ForeignItemKind::TyAlias(generics, bounds, ty) => {
visitor.visit_generics(generics);
visit_bounds(bounds, visitor);
Expand Down
5 changes: 4 additions & 1 deletion src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,10 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI
let kind = FnKind::Fn(FnCtxt::Foreign, item.ident, sig, &item.vis, body.as_deref());
visitor.visit_fn(kind, item.span, item.id);
}
ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
ForeignItemKind::Static(ref typ, _, ref body) => {
visitor.visit_ty(typ);
walk_list!(visitor, visit_expr, body);
}
ForeignItemKind::TyAlias(ref generics, ref bounds, ref ty) => {
visitor.visit_generics(generics);
walk_list!(visitor, visit_param_bound, bounds);
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/parser/assoc-const-underscore-semantic-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Semantically, an associated constant cannot use `_` as a name.

fn main() {}

const _: () = {
pub trait A {
const _: () = (); //~ ERROR `const` items in this context need a name
}
impl A for () {
const _: () = (); //~ ERROR `const` items in this context need a name
//~^ ERROR const `_` is not a member of trait `A`
}
struct B;
impl B {
const _: () = (); //~ ERROR `const` items in this context need a name
}
};
27 changes: 27 additions & 0 deletions src/test/ui/parser/assoc-const-underscore-semantic-fail.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error: `const` items in this context need a name
--> $DIR/assoc-const-underscore-semantic-fail.rs:7:15
|
LL | const _: () = ();
| ^ `_` is not a valid name for this `const` item

error: `const` items in this context need a name
--> $DIR/assoc-const-underscore-semantic-fail.rs:10:15
|
LL | const _: () = ();
| ^ `_` is not a valid name for this `const` item

error: `const` items in this context need a name
--> $DIR/assoc-const-underscore-semantic-fail.rs:15:15
|
LL | const _: () = ();
| ^ `_` is not a valid name for this `const` item

error[E0438]: const `_` is not a member of trait `A`
--> $DIR/assoc-const-underscore-semantic-fail.rs:10:9
|
LL | const _: () = ();
| ^^^^^^^^^^^^^^^^^ not a member of trait `A`

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0438`.
18 changes: 18 additions & 0 deletions src/test/ui/parser/assoc-const-underscore-syntactic-pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// All constant items (associated or otherwise) may syntactically use `_` as a name.

// check-pass

fn main() {}

#[cfg(FALSE)]
const _: () = {
pub trait A {
const _: () = ();
}
impl A for () {
const _: () = ();
}
impl dyn A {
const _: () = ();
}
};
8 changes: 8 additions & 0 deletions src/test/ui/parser/foreign-static-semantic-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Syntactically, a foreign static may not have a body.

fn main() {}

extern {
static X: u8 = 0; //~ ERROR incorrect `static` inside `extern` block
static mut Y: u8 = 0; //~ ERROR incorrect `static` inside `extern` block
}
Loading