Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse pub(restricted) visibilities on tuple struct fields #33161

Merged
merged 2 commits into from
Apr 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5218,8 +5218,25 @@ impl<'a> Parser<'a> {
|p| {
let attrs = p.parse_outer_attributes()?;
let lo = p.span.lo;
let vis = p.parse_visibility(false)?;
let ty = p.parse_ty_sum()?;
let mut vis = p.parse_visibility(false)?;
let ty_is_interpolated =
p.token.is_interpolated() || p.look_ahead(1, |t| t.is_interpolated());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any tests covering this interpolated case? I expected to see one where we substitute in a $t:ty.

It is a bit of a shame that we have no $p:path fragment specificier, though I guess rust-lang/rfcs#1575 would address the use case.

let mut ty = p.parse_ty_sum()?;

// Handle `pub(path) type`, in which `vis` will be `pub` and `ty` will be `(path)`.
if vis == Visibility::Public && !ty_is_interpolated &&
p.token != token::Comma && p.token != token::CloseDelim(token::Paren) {
ty = if let TyKind::Paren(ref path_ty) = ty.node {
if let TyKind::Path(None, ref path) = path_ty.node {
vis = Visibility::Restricted { path: P(path.clone()), id: path_ty.id };
Some(p.parse_ty_sum()?)
} else {
None
}
} else {
None
}.unwrap_or(ty);
}
Ok(StructField {
span: mk_sp(lo, p.span.hi),
vis: vis,
Expand Down Expand Up @@ -5263,15 +5280,29 @@ impl<'a> Parser<'a> {
self.parse_single_struct_field(vis, attrs)
}

fn parse_visibility(&mut self, allow_restricted: bool) -> PResult<'a, Visibility> {
// If `allow_path` is false, just parse the `pub` in `pub(path)` (but still parse `pub(crate)`)
fn parse_visibility(&mut self, allow_path: bool) -> PResult<'a, Visibility> {
let pub_crate = |this: &mut Self| {
let span = this.last_span;
this.expect(&token::CloseDelim(token::Paren))?;
Ok(Visibility::Crate(span))
};

if !self.eat_keyword(keywords::Pub) {
Ok(Visibility::Inherited)
} else if !allow_restricted || !self.eat(&token::OpenDelim(token::Paren)) {
} else if !allow_path {
// Look ahead to avoid eating the `(` in `pub(path)` while still parsing `pub(crate)`
if self.token == token::OpenDelim(token::Paren) &&
self.look_ahead(1, |t| t.is_keyword(keywords::Crate)) {
self.bump(); self.bump();
pub_crate(self)
} else {
Ok(Visibility::Public)
}
} else if !self.eat(&token::OpenDelim(token::Paren)) {
Ok(Visibility::Public)
} else if self.eat_keyword(keywords::Crate) {
let span = self.last_span;
self.expect(&token::CloseDelim(token::Paren))?;
Ok(Visibility::Crate(span))
pub_crate(self)
} else {
let path = self.parse_path(PathStyle::Mod)?;
self.expect(&token::CloseDelim(token::Paren))?;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted, type_macros)]

mod foo {
type T = ();
struct S1(pub(foo) (), pub(T), pub(crate) (), pub(((), T)));
struct S2(pub((foo)) ()); //~ ERROR expected one of `+` or `,`, found `(`
//~| ERROR expected one of `+`, `;`, or `where`, found `(`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted, type_macros)]

macro_rules! define_struct {
($t:ty) => {
struct S1(pub $t);
struct S2(pub (foo) ());
struct S3(pub $t ()); //~ ERROR expected one of `+` or `,`, found `(`
//~| ERROR expected one of `+`, `;`, or `where`, found `(`
}
}

mod foo {
define_struct! { (foo) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted, type_macros)]

macro_rules! define_struct {
($t:ty) => {
struct S1(pub($t));
struct S2(pub (foo) ());
struct S3(pub($t) ()); //~ ERROR expected one of `+` or `,`, found `(`
//~| ERROR expected one of `+`, `;`, or `where`, found `(`
}
}

mod foo {
define_struct! { foo }
}
4 changes: 4 additions & 0 deletions src/test/compile-fail/privacy/restricted/ty-params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ macro_rules! m {
struct S<T>(T);
m!{ S<u8> } //~ ERROR type or lifetime parameters in visibility path

mod foo {
struct S(pub(foo<T>) ()); //~ ERROR type or lifetime parameters in visibility path
}

fn main() {}