Skip to content

Hint correct extern constant syntax #43720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
22 changes: 12 additions & 10 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5518,12 +5518,11 @@ impl<'a> Parser<'a> {
})
}

/// Parse a static item from a foreign module
/// Parse a static item from a foreign module.
/// Assumes that the `static` keyword is already parsed.
fn parse_item_foreign_static(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec<Attribute>)
-> PResult<'a, ForeignItem> {
self.expect_keyword(keywords::Static)?;
let mutbl = self.eat_keyword(keywords::Mut);

let ident = self.parse_ident()?;
self.expect(&token::Colon)?;
let ty = self.parse_ty()?;
Expand Down Expand Up @@ -5997,19 +5996,22 @@ impl<'a> Parser<'a> {
let lo = self.span;
let visibility = self.parse_visibility(false)?;

if self.check_keyword(keywords::Static) {
// FOREIGN STATIC ITEM
// FOREIGN STATIC ITEM
// Treat `const` as `static` for error recovery, but don't add it to expected tokens.
if self.check_keyword(keywords::Static) || self.token.is_keyword(keywords::Const) {
if self.token.is_keyword(keywords::Const) {
self.diagnostic()
.struct_span_err(self.span, "extern items cannot be `const`")
.span_label(self.span, "use `static` instead").emit();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be .span_suggestion(self.span, "instead try using", "static".to_owned()), which would allow IDEs and tools to automatically replace it.

}
self.bump(); // `static` or `const`
return Ok(Some(self.parse_item_foreign_static(visibility, lo, attrs)?));
}
// FOREIGN FUNCTION ITEM
if self.check_keyword(keywords::Fn) {
// FOREIGN FUNCTION ITEM
return Ok(Some(self.parse_item_foreign_fn(visibility, lo, attrs)?));
}

if self.check_keyword(keywords::Const) {
return Err(self.span_fatal(self.span, "extern items cannot be `const`"));
}

// FIXME #5668: this will occur for a macro invocation:
match self.parse_macro_use_or_failure(attrs, true, false, lo, visibility)? {
Some(item) => {
Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/extern-const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -Z continue-parse-after-error

extern "C" {
const C: u8; //~ ERROR extern items cannot be `const`
}

fn main() {
let x = C;
}
8 changes: 8 additions & 0 deletions src/test/ui/extern-const.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: extern items cannot be `const`
--> $DIR/extern-const.rs:14:5
|
14 | const C: u8; //~ ERROR extern items cannot be `const`
| ^^^^^ use `static` instead

error: aborting due to previous error