Skip to content

Commit fbc3642

Browse files
committedOct 24, 2017
Auto merge of #45401 - zackmdavis:crate_shorthand_visibility_modifier, r=nikomatsakis
`crate` shorthand visibility modifier cc #45388. r? @nikomatsakis
2 parents a789fa0 + 214b0f2 commit fbc3642

File tree

14 files changed

+97
-10
lines changed

14 files changed

+97
-10
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# `crate_visibility_modifier`
2+
3+
The tracking issue for this feature is: [#45388]
4+
5+
[#45388]: https://github.com/rust-lang/rust/issues/45388
6+
7+
-----
8+
9+
The `crate_visibility_modifier` feature allows the `crate` keyword to be used
10+
as a visibility modifier synonymous to `pub(crate)`, indicating that a type
11+
(function, _&c._) is to be visible to the entire enclosing crate, but not to
12+
other crates.
13+
14+
```rust
15+
#![feature(crate_visibility_modifier)]
16+
17+
crate struct Foo {
18+
bar: usize,
19+
}
20+
```

‎src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2667,7 +2667,7 @@ impl<'a> LoweringContext<'a> {
26672667
-> hir::Visibility {
26682668
match *v {
26692669
Visibility::Public => hir::Public,
2670-
Visibility::Crate(_) => hir::Visibility::Crate,
2670+
Visibility::Crate(..) => hir::Visibility::Crate,
26712671
Visibility::Restricted { ref path, id } => {
26722672
hir::Visibility::Restricted {
26732673
path: P(self.lower_path(id, path, ParamMode::Explicit, true)),

‎src/libsyntax/ast.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1788,10 +1788,19 @@ impl PolyTraitRef {
17881788
}
17891789
}
17901790

1791+
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1792+
pub enum CrateSugar {
1793+
/// Source is `pub(crate)`
1794+
PubCrate,
1795+
1796+
/// Source is (just) `crate`
1797+
JustCrate,
1798+
}
1799+
17911800
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
17921801
pub enum Visibility {
17931802
Public,
1794-
Crate(Span),
1803+
Crate(Span, CrateSugar),
17951804
Restricted { path: P<Path>, id: NodeId },
17961805
Inherited,
17971806
}

‎src/libsyntax/feature_gate.rs

+11
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ declare_features! (
401401

402402
// Trait object syntax with `dyn` prefix
403403
(active, dyn_trait, "1.22.0", Some(44662)),
404+
405+
// `crate` as visibility modifier, synonymous to `pub(crate)`
406+
(active, crate_visibility_modifier, "1.23.0", Some(45388)),
404407
);
405408

406409
declare_features! (
@@ -1582,6 +1585,14 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
15821585
visit::walk_impl_item(self, ii);
15831586
}
15841587

1588+
fn visit_vis(&mut self, vis: &'a ast::Visibility) {
1589+
if let ast::Visibility::Crate(span, ast::CrateSugar::JustCrate) = *vis {
1590+
gate_feature_post!(&self, crate_visibility_modifier, span,
1591+
"`crate` visibility modifier is experimental");
1592+
}
1593+
visit::walk_vis(self, vis);
1594+
}
1595+
15851596
fn visit_generics(&mut self, g: &'a ast::Generics) {
15861597
for t in &g.ty_params {
15871598
if !t.attrs.is_empty() {

‎src/libsyntax/parse/parser.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use ast::SelfKind;
3636
use ast::{TraitItem, TraitRef, TraitObjectSyntax};
3737
use ast::{Ty, TyKind, TypeBinding, TyParam, TyParamBounds};
3838
use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
39-
use ast::{Visibility, WhereClause};
39+
use ast::{Visibility, WhereClause, CrateSugar};
4040
use ast::{BinOpKind, UnOp};
4141
use ast::{RangeEnd, RangeSyntax};
4242
use {ast, attr};
@@ -5327,6 +5327,10 @@ impl<'a> Parser<'a> {
53275327
pub fn parse_visibility(&mut self, can_take_tuple: bool) -> PResult<'a, Visibility> {
53285328
maybe_whole!(self, NtVis, |x| x);
53295329

5330+
if self.eat_keyword(keywords::Crate) {
5331+
return Ok(Visibility::Crate(self.prev_span, CrateSugar::JustCrate));
5332+
}
5333+
53305334
if !self.eat_keyword(keywords::Pub) {
53315335
return Ok(Visibility::Inherited)
53325336
}
@@ -5340,7 +5344,7 @@ impl<'a> Parser<'a> {
53405344
// `pub(crate)`
53415345
self.bump(); // `(`
53425346
self.bump(); // `crate`
5343-
let vis = Visibility::Crate(self.prev_span);
5347+
let vis = Visibility::Crate(self.prev_span, CrateSugar::PubCrate);
53445348
self.expect(&token::CloseDelim(token::Paren))?; // `)`
53455349
return Ok(vis)
53465350
} else if self.look_ahead(1, |t| t.is_keyword(keywords::In)) {

‎src/libsyntax/print/pprust.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,10 @@ impl<'a> State<'a> {
14401440
pub fn print_visibility(&mut self, vis: &ast::Visibility) -> io::Result<()> {
14411441
match *vis {
14421442
ast::Visibility::Public => self.word_nbsp("pub"),
1443-
ast::Visibility::Crate(_) => self.word_nbsp("pub(crate)"),
1443+
ast::Visibility::Crate(_, sugar) => match sugar {
1444+
ast::CrateSugar::PubCrate => self.word_nbsp("pub(crate)"),
1445+
ast::CrateSugar::JustCrate => self.word_nbsp("crate")
1446+
}
14441447
ast::Visibility::Restricted { ref path, .. } => {
14451448
let path = to_string(|s| s.print_path(path, false, 0, true));
14461449
if path == "self" || path == "super" {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
crate struct Bender { //~ ERROR `crate` visibility modifier is experimental
12+
earth: bool,
13+
fire: bool,
14+
air: bool,
15+
water: bool,
16+
}
17+
18+
fn main() {}

‎src/test/compile-fail/privacy/restricted/auxiliary/pub_restricted.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(crate_visibility_modifier)]
12+
1113
pub(crate) struct Crate;
14+
1215
#[derive(Default)]
1316
pub struct Universe {
1417
pub x: i32,
15-
pub(crate) y: i32
18+
pub(crate) y: i32,
19+
crate z: i32,
1620
}
1721

1822
impl Universe {
1923
pub fn f(&self) {}
2024
pub(crate) fn g(&self) {}
25+
crate fn h(&self) {}
2126
}

‎src/test/compile-fail/privacy/restricted/private-in-public.rs

+3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(crate_visibility_modifier)]
12+
1113
mod foo {
1214
struct Priv;
1315
mod bar {
1416
use foo::Priv;
1517
pub(super) fn f(_: Priv) {}
1618
pub(crate) fn g(_: Priv) {} //~ ERROR E0446
19+
crate fn h(_: Priv) {} //~ ERROR E0446
1720
}
1821
}
1922

‎src/test/compile-fail/privacy/restricted/test.rs

+2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ fn main() {
5050
let u = Universe::default();
5151
let _ = u.x;
5252
let _ = u.y; //~ ERROR private
53+
let _ = u.z; //~ ERROR private
5354
u.f();
5455
u.g(); //~ ERROR private
56+
u.h(); //~ ERROR private
5557
}
5658

5759
mod pathological {

‎src/test/parse-fail/issue-20711-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ impl Foo {
1616
fn foo() {}
1717

1818
#[stable(feature = "rust1", since = "1.0.0")]
19-
} //~ ERROR expected one of `const`, `default`, `extern`, `fn`, `pub`, `type`, or `unsafe`
19+
} //~ ERROR expected one of `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, or `unsafe`
2020

2121
fn main() {}

‎src/test/parse-fail/issue-20711.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ struct Foo;
1414

1515
impl Foo {
1616
#[stable(feature = "rust1", since = "1.0.0")]
17-
} //~ ERROR expected one of `const`, `default`, `extern`, `fn`, `pub`, `type`, or `unsafe`
17+
} //~ ERROR expected one of `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, or `unsafe`
1818

1919
fn main() {}

‎src/test/parse-fail/removed-syntax-static-fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ struct S;
1515
impl S {
1616
static fn f() {}
1717
}
18-
//~^^ ERROR expected one of `const`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`
18+
//~^^ ERROR expected one of `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`

‎src/test/run-pass/macro-pub-matcher.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
#![allow(dead_code, unused_imports)]
12-
#![feature(macro_vis_matcher)]
12+
#![feature(macro_vis_matcher, crate_visibility_modifier)]
1313

1414
/**
1515
Ensure that `:vis` matches can be captured in existing positions, and passed
@@ -64,6 +64,18 @@ mod with_pub_restricted {
6464
vis_passthru! { pub(crate) use A as I; }
6565
}
6666

67+
mod with_crate {
68+
vis_passthru! { crate const A: i32 = 0; }
69+
vis_passthru! { crate enum B {} }
70+
vis_passthru! { crate extern "C" fn c() {} }
71+
vis_passthru! { crate mod d {} }
72+
vis_passthru! { crate static E: i32 = 0; }
73+
vis_passthru! { crate struct F; }
74+
vis_passthru! { crate trait G {} }
75+
vis_passthru! { crate type H = i32; }
76+
vis_passthru! { crate use A as I; }
77+
}
78+
6779
mod garden {
6880
mod with_pub_restricted_path {
6981
vis_passthru! { pub(in garden) const A: i32 = 0; }

0 commit comments

Comments
 (0)
Please sign in to comment.