Rollup merge of #35491 - sanxiyn:pub-restricted-span, r=nikomatsakis

Correct span for pub_restricted field

Fix #35435.
This commit is contained in:
Eduard-Mihai Burtescu
2016-08-14 20:29:48 +03:00
committed by GitHub
3 changed files with 37 additions and 25 deletions

View File

@@ -3788,19 +3788,18 @@ impl<'a> Parser<'a> {
} }
/// Parse a structure field /// Parse a structure field
fn parse_name_and_ty(&mut self, pr: Visibility, fn parse_name_and_ty(&mut self,
attrs: Vec<Attribute> ) -> PResult<'a, StructField> { lo: BytePos,
let lo = match pr { vis: Visibility,
Visibility::Inherited => self.span.lo, attrs: Vec<Attribute>)
_ => self.last_span.lo, -> PResult<'a, StructField> {
};
let name = self.parse_ident()?; let name = self.parse_ident()?;
self.expect(&token::Colon)?; self.expect(&token::Colon)?;
let ty = self.parse_ty_sum()?; let ty = self.parse_ty_sum()?;
Ok(StructField { Ok(StructField {
span: mk_sp(lo, self.last_span.hi), span: mk_sp(lo, self.last_span.hi),
ident: Some(name), ident: Some(name),
vis: pr, vis: vis,
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
ty: ty, ty: ty,
attrs: attrs, attrs: attrs,
@@ -5120,10 +5119,11 @@ impl<'a> Parser<'a> {
/// Parse a structure field declaration /// Parse a structure field declaration
pub fn parse_single_struct_field(&mut self, pub fn parse_single_struct_field(&mut self,
lo: BytePos,
vis: Visibility, vis: Visibility,
attrs: Vec<Attribute> ) attrs: Vec<Attribute> )
-> PResult<'a, StructField> { -> PResult<'a, StructField> {
let a_var = self.parse_name_and_ty(vis, attrs)?; let a_var = self.parse_name_and_ty(lo, vis, attrs)?;
match self.token { match self.token {
token::Comma => { token::Comma => {
self.bump(); self.bump();
@@ -5144,8 +5144,9 @@ impl<'a> Parser<'a> {
/// Parse an element of a struct definition /// Parse an element of a struct definition
fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> { fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
let attrs = self.parse_outer_attributes()?; let attrs = self.parse_outer_attributes()?;
let lo = self.span.lo;
let vis = self.parse_visibility(true)?; let vis = self.parse_visibility(true)?;
self.parse_single_struct_field(vis, attrs) self.parse_single_struct_field(lo, vis, attrs)
} }
// If `allow_path` is false, just parse the `pub` in `pub(path)` (but still parse `pub(crate)`) // If `allow_path` is false, just parse the `pub` in `pub(path)` (but still parse `pub(crate)`)

View File

@@ -1,4 +1,4 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at // file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT. // http://rust-lang.org/COPYRIGHT.
// //
@@ -8,23 +8,15 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
// Regression test for issue #26083 // Regression test for issue #26083 and #35435
// Test that span for public struct fields start at `pub` instead of the identifier // Test that span for public struct fields start at `pub`
#![feature(pub_restricted)]
struct Foo { struct Foo {
pub bar: u8,
pub
//~^ error: field `bar` is already declared [E0124]
bar: u8, bar: u8,
pub bar: u8,
pub bar: pub(crate) bar: u8,
//~^ error: field `bar` is already declared [E0124]
u8,
bar:
//~^ error: field `bar` is already declared [E0124]
u8,
} }
fn main() { } fn main() {}

View File

@@ -0,0 +1,19 @@
error[E0124]: field `bar` is already declared
--> $DIR/pub-struct-field.rs:18:5
|
17 | bar: u8,
| ------- `bar` first declared here
18 | pub bar: u8,
| ^^^^^^^^^^^ field already declared
error[E0124]: field `bar` is already declared
--> $DIR/pub-struct-field.rs:19:5
|
17 | bar: u8,
| ------- `bar` first declared here
18 | pub bar: u8,
19 | pub(crate) bar: u8,
| ^^^^^^^^^^^^^^^^^^ field already declared
error: aborting due to 2 previous errors