Skip to content

Commit 6e59f49

Browse files
authored
Rollup merge of #35491 - sanxiyn:pub-restricted-span, r=nikomatsakis
Correct span for pub_restricted field Fix #35435.
2 parents d1e286a + f76a737 commit 6e59f49

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

src/libsyntax/parse/parser.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -3788,19 +3788,18 @@ impl<'a> Parser<'a> {
37883788
}
37893789

37903790
/// Parse a structure field
3791-
fn parse_name_and_ty(&mut self, pr: Visibility,
3792-
attrs: Vec<Attribute> ) -> PResult<'a, StructField> {
3793-
let lo = match pr {
3794-
Visibility::Inherited => self.span.lo,
3795-
_ => self.last_span.lo,
3796-
};
3791+
fn parse_name_and_ty(&mut self,
3792+
lo: BytePos,
3793+
vis: Visibility,
3794+
attrs: Vec<Attribute>)
3795+
-> PResult<'a, StructField> {
37973796
let name = self.parse_ident()?;
37983797
self.expect(&token::Colon)?;
37993798
let ty = self.parse_ty_sum()?;
38003799
Ok(StructField {
38013800
span: mk_sp(lo, self.last_span.hi),
38023801
ident: Some(name),
3803-
vis: pr,
3802+
vis: vis,
38043803
id: ast::DUMMY_NODE_ID,
38053804
ty: ty,
38063805
attrs: attrs,
@@ -5120,10 +5119,11 @@ impl<'a> Parser<'a> {
51205119

51215120
/// Parse a structure field declaration
51225121
pub fn parse_single_struct_field(&mut self,
5122+
lo: BytePos,
51235123
vis: Visibility,
51245124
attrs: Vec<Attribute> )
51255125
-> PResult<'a, StructField> {
5126-
let a_var = self.parse_name_and_ty(vis, attrs)?;
5126+
let a_var = self.parse_name_and_ty(lo, vis, attrs)?;
51275127
match self.token {
51285128
token::Comma => {
51295129
self.bump();
@@ -5144,8 +5144,9 @@ impl<'a> Parser<'a> {
51445144
/// Parse an element of a struct definition
51455145
fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
51465146
let attrs = self.parse_outer_attributes()?;
5147+
let lo = self.span.lo;
51475148
let vis = self.parse_visibility(true)?;
5148-
self.parse_single_struct_field(vis, attrs)
5149+
self.parse_single_struct_field(lo, vis, attrs)
51495150
}
51505151

51515152
// If `allow_path` is false, just parse the `pub` in `pub(path)` (but still parse `pub(crate)`)
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -8,23 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// Regression test for issue #26083
12-
// Test that span for public struct fields start at `pub` instead of the identifier
11+
// Regression test for issue #26083 and #35435
12+
// Test that span for public struct fields start at `pub`
1313

14-
struct Foo {
15-
pub bar: u8,
14+
#![feature(pub_restricted)]
1615

17-
pub
18-
//~^ error: field `bar` is already declared [E0124]
16+
struct Foo {
1917
bar: u8,
20-
21-
pub bar:
22-
//~^ error: field `bar` is already declared [E0124]
23-
u8,
24-
25-
bar:
26-
//~^ error: field `bar` is already declared [E0124]
27-
u8,
18+
pub bar: u8,
19+
pub(crate) bar: u8,
2820
}
2921

30-
fn main() { }
22+
fn main() {}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0124]: field `bar` is already declared
2+
--> $DIR/pub-struct-field.rs:18:5
3+
|
4+
17 | bar: u8,
5+
| ------- `bar` first declared here
6+
18 | pub bar: u8,
7+
| ^^^^^^^^^^^ field already declared
8+
9+
error[E0124]: field `bar` is already declared
10+
--> $DIR/pub-struct-field.rs:19:5
11+
|
12+
17 | bar: u8,
13+
| ------- `bar` first declared here
14+
18 | pub bar: u8,
15+
19 | pub(crate) bar: u8,
16+
| ^^^^^^^^^^^^^^^^^^ field already declared
17+
18+
error: aborting due to 2 previous errors
19+

0 commit comments

Comments
 (0)