Skip to content

Commit b617960

Browse files
committed
Auto merge of #43720 - pornel:staticconst, r=petrochenkov
Hint correct extern constant syntax Error message for `extern "C" { const …}` is terse, and the right syntax is hard to guess given unfortunate difference between meaning of `static` in C and Rust. I've added a hint for the right syntax.
2 parents 2400ebf + cabc9be commit b617960

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

src/libsyntax/parse/parser.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -5518,12 +5518,11 @@ impl<'a> Parser<'a> {
55185518
})
55195519
}
55205520

5521-
/// Parse a static item from a foreign module
5521+
/// Parse a static item from a foreign module.
5522+
/// Assumes that the `static` keyword is already parsed.
55225523
fn parse_item_foreign_static(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec<Attribute>)
55235524
-> PResult<'a, ForeignItem> {
5524-
self.expect_keyword(keywords::Static)?;
55255525
let mutbl = self.eat_keyword(keywords::Mut);
5526-
55275526
let ident = self.parse_ident()?;
55285527
self.expect(&token::Colon)?;
55295528
let ty = self.parse_ty()?;
@@ -5997,19 +5996,23 @@ impl<'a> Parser<'a> {
59975996
let lo = self.span;
59985997
let visibility = self.parse_visibility(false)?;
59995998

6000-
if self.check_keyword(keywords::Static) {
6001-
// FOREIGN STATIC ITEM
5999+
// FOREIGN STATIC ITEM
6000+
// Treat `const` as `static` for error recovery, but don't add it to expected tokens.
6001+
if self.check_keyword(keywords::Static) || self.token.is_keyword(keywords::Const) {
6002+
if self.token.is_keyword(keywords::Const) {
6003+
self.diagnostic()
6004+
.struct_span_err(self.span, "extern items cannot be `const`")
6005+
.span_suggestion(self.span, "instead try using", "static".to_owned())
6006+
.emit();
6007+
}
6008+
self.bump(); // `static` or `const`
60026009
return Ok(Some(self.parse_item_foreign_static(visibility, lo, attrs)?));
60036010
}
6011+
// FOREIGN FUNCTION ITEM
60046012
if self.check_keyword(keywords::Fn) {
6005-
// FOREIGN FUNCTION ITEM
60066013
return Ok(Some(self.parse_item_foreign_fn(visibility, lo, attrs)?));
60076014
}
60086015

6009-
if self.check_keyword(keywords::Const) {
6010-
return Err(self.span_fatal(self.span, "extern items cannot be `const`"));
6011-
}
6012-
60136016
// FIXME #5668: this will occur for a macro invocation:
60146017
match self.parse_macro_use_or_failure(attrs, true, false, lo, visibility)? {
60156018
Some(item) => {

src/test/ui/extern-const.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z continue-parse-after-error
12+
13+
extern "C" {
14+
const C: u8; //~ ERROR extern items cannot be `const`
15+
}
16+
17+
fn main() {
18+
let x = C;
19+
}

src/test/ui/extern-const.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: extern items cannot be `const`
2+
--> $DIR/extern-const.rs:14:5
3+
|
4+
14 | const C: u8; //~ ERROR extern items cannot be `const`
5+
| ^^^^^ help: instead try using: `static`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)