From f76a737bae12788ba6180c13ec5fcba9177d80d4 Mon Sep 17 00:00:00 2001 From: Seo Sanghyeon Date: Mon, 8 Aug 2016 21:35:15 +0900 Subject: Correct span for pub_restricted field --- src/libsyntax/parse/parser.rs | 19 +++++++------- .../compile-fail/pub-struct-field-span-26083.rs | 30 ---------------------- src/test/ui/span/pub-struct-field.rs | 22 ++++++++++++++++ src/test/ui/span/pub-struct-field.stderr | 19 ++++++++++++++ 4 files changed, 51 insertions(+), 39 deletions(-) delete mode 100644 src/test/compile-fail/pub-struct-field-span-26083.rs create mode 100644 src/test/ui/span/pub-struct-field.rs create mode 100644 src/test/ui/span/pub-struct-field.stderr (limited to 'src') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index c143e190c6f..72290a9d619 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3760,19 +3760,18 @@ impl<'a> Parser<'a> { } /// Parse a structure field - fn parse_name_and_ty(&mut self, pr: Visibility, - attrs: Vec ) -> PResult<'a, StructField> { - let lo = match pr { - Visibility::Inherited => self.span.lo, - _ => self.last_span.lo, - }; + fn parse_name_and_ty(&mut self, + lo: BytePos, + vis: Visibility, + attrs: Vec) + -> PResult<'a, StructField> { let name = self.parse_ident()?; self.expect(&token::Colon)?; let ty = self.parse_ty_sum()?; Ok(StructField { span: mk_sp(lo, self.last_span.hi), ident: Some(name), - vis: pr, + vis: vis, id: ast::DUMMY_NODE_ID, ty: ty, attrs: attrs, @@ -5092,10 +5091,11 @@ impl<'a> Parser<'a> { /// Parse a structure field declaration pub fn parse_single_struct_field(&mut self, + lo: BytePos, vis: Visibility, attrs: Vec ) -> 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 { token::Comma => { self.bump(); @@ -5116,8 +5116,9 @@ impl<'a> Parser<'a> { /// Parse an element of a struct definition fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> { let attrs = self.parse_outer_attributes()?; + let lo = self.span.lo; 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)`) diff --git a/src/test/compile-fail/pub-struct-field-span-26083.rs b/src/test/compile-fail/pub-struct-field-span-26083.rs deleted file mode 100644 index 0dc7e09f0e4..00000000000 --- a/src/test/compile-fail/pub-struct-field-span-26083.rs +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2015 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// Regression test for issue #26083 -// Test that span for public struct fields start at `pub` instead of the identifier - -struct Foo { - pub bar: u8, - - pub - //~^ error: field `bar` is already declared [E0124] - bar: u8, - - pub bar: - //~^ error: field `bar` is already declared [E0124] - u8, - - bar: - //~^ error: field `bar` is already declared [E0124] - u8, -} - -fn main() { } diff --git a/src/test/ui/span/pub-struct-field.rs b/src/test/ui/span/pub-struct-field.rs new file mode 100644 index 00000000000..9f8f871200c --- /dev/null +++ b/src/test/ui/span/pub-struct-field.rs @@ -0,0 +1,22 @@ +// Copyright 2016 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Regression test for issue #26083 and #35435 +// Test that span for public struct fields start at `pub` + +#![feature(pub_restricted)] + +struct Foo { + bar: u8, + pub bar: u8, + pub(crate) bar: u8, +} + +fn main() {} diff --git a/src/test/ui/span/pub-struct-field.stderr b/src/test/ui/span/pub-struct-field.stderr new file mode 100644 index 00000000000..2c002c34736 --- /dev/null +++ b/src/test/ui/span/pub-struct-field.stderr @@ -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 + -- cgit 1.4.1-3-g733a5