Skip to content

Commit a965bee

Browse files
petrochenkovkornelski
authored andcommitted
Better diagnostics and recovery for const in extern blocks
1 parent ff0513c commit a965bee

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

src/libsyntax/parse/parser.rs

+12-12
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,21 +5996,22 @@ 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_label(self.span, "use `static` instead").emit();
6006+
}
6007+
self.bump(); // `static` or `const`
60026008
return Ok(Some(self.parse_item_foreign_static(visibility, lo, attrs)?));
60036009
}
6010+
// FOREIGN FUNCTION ITEM
60046011
if self.check_keyword(keywords::Fn) {
6005-
// FOREIGN FUNCTION ITEM
60066012
return Ok(Some(self.parse_item_foreign_fn(visibility, lo, attrs)?));
60076013
}
60086014

6009-
if self.check_keyword(keywords::Const) {
6010-
let mut err = self.span_fatal(self.span, "extern items cannot be `const`");
6011-
err.help("use `static` instead");
6012-
return Err(err);
6013-
}
6014-
60156015
// FIXME #5668: this will occur for a macro invocation:
60166016
match self.parse_macro_use_or_failure(attrs, true, false, lo, visibility)? {
60176017
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+
| ^^^^^ use `static` instead
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)