Skip to content

Commit b8ef9fd

Browse files
committed
auto merge of #13184 : alexcrichton/rust/priv-fields, r=brson
This is an implementation of a portion of [RFC #4](https://github.com/rust-lang/rfcs/blob/master/active/0004-private-fields.md). This PR makes named struct fields private by default (as opposed to inherited by default). The only real meaty change is the first commit to `rustc`, all other commits are just fallout of that change. Summary of changes made: * Named fields are private by default *everywhere* * The `priv` keyword is now default-deny on named fields (done in a "lint" pass in privacy) Changes yet to be done (before the RFC is closed) * Change tuple structs to have private fields by default * Remove `priv` enum variants * Make `priv` a reserved keyword
2 parents a7e057d + 37a3131 commit b8ef9fd

File tree

278 files changed

+2649
-2674
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

278 files changed

+2649
-2674
lines changed

src/compiletest/common.rs

+28-28
Original file line numberDiff line numberDiff line change
@@ -21,87 +21,87 @@ pub enum mode {
2121
#[deriving(Clone)]
2222
pub struct config {
2323
// The library paths required for running the compiler
24-
compile_lib_path: ~str,
24+
pub compile_lib_path: ~str,
2525

2626
// The library paths required for running compiled programs
27-
run_lib_path: ~str,
27+
pub run_lib_path: ~str,
2828

2929
// The rustc executable
30-
rustc_path: Path,
30+
pub rustc_path: Path,
3131

3232
// The clang executable
33-
clang_path: Option<Path>,
33+
pub clang_path: Option<Path>,
3434

3535
// The llvm binaries path
36-
llvm_bin_path: Option<Path>,
36+
pub llvm_bin_path: Option<Path>,
3737

3838
// The directory containing the tests to run
39-
src_base: Path,
39+
pub src_base: Path,
4040

4141
// The directory where programs should be built
42-
build_base: Path,
42+
pub build_base: Path,
4343

4444
// Directory for auxiliary libraries
45-
aux_base: Path,
45+
pub aux_base: Path,
4646

4747
// The name of the stage being built (stage1, etc)
48-
stage_id: ~str,
48+
pub stage_id: ~str,
4949

5050
// The test mode, compile-fail, run-fail, run-pass
51-
mode: mode,
51+
pub mode: mode,
5252

5353
// Run ignored tests
54-
run_ignored: bool,
54+
pub run_ignored: bool,
5555

5656
// Only run tests that match this filter
57-
filter: Option<~str>,
57+
pub filter: Option<~str>,
5858

5959
// Write out a parseable log of tests that were run
60-
logfile: Option<Path>,
60+
pub logfile: Option<Path>,
6161

6262
// Write out a json file containing any metrics of the run
63-
save_metrics: Option<Path>,
63+
pub save_metrics: Option<Path>,
6464

6565
// Write and ratchet a metrics file
66-
ratchet_metrics: Option<Path>,
66+
pub ratchet_metrics: Option<Path>,
6767

6868
// Percent change in metrics to consider noise
69-
ratchet_noise_percent: Option<f64>,
69+
pub ratchet_noise_percent: Option<f64>,
7070

71-
// "Shard" of the testsuite to run: this has the form of
71+
// "Shard" of the testsuite to pub run: this has the form of
7272
// two numbers (a,b), and causes only those tests with
7373
// positional order equal to a mod b to run.
74-
test_shard: Option<(uint,uint)>,
74+
pub test_shard: Option<(uint,uint)>,
7575

7676
// A command line to prefix program execution with,
7777
// for running under valgrind
78-
runtool: Option<~str>,
78+
pub runtool: Option<~str>,
7979

8080
// Flags to pass to the compiler when building for the host
81-
host_rustcflags: Option<~str>,
81+
pub host_rustcflags: Option<~str>,
8282

8383
// Flags to pass to the compiler when building for the target
84-
target_rustcflags: Option<~str>,
84+
pub target_rustcflags: Option<~str>,
8585

8686
// Run tests using the JIT
87-
jit: bool,
87+
pub jit: bool,
8888

8989
// Target system to be tested
90-
target: ~str,
90+
pub target: ~str,
9191

9292
// Host triple for the compiler being invoked
93-
host: ~str,
93+
pub host: ~str,
9494

9595
// Extra parameter to run adb on arm-linux-androideabi
96-
adb_path: ~str,
96+
pub adb_path: ~str,
9797

9898
// Extra parameter to run test sute on arm-linux-androideabi
99-
adb_test_dir: ~str,
99+
pub adb_test_dir: ~str,
100100

101101
// status whether android device available or not
102-
adb_device_status: bool,
102+
pub adb_device_status: bool,
103103

104104
// Explain what's going on
105-
verbose: bool
105+
pub verbose: bool
106106

107107
}

src/compiletest/errors.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
use std::io::{BufferedReader, File};
1212

13-
pub struct ExpectedError { line: uint, kind: ~str, msg: ~str }
13+
pub struct ExpectedError {
14+
pub line: uint,
15+
pub kind: ~str,
16+
pub msg: ~str,
17+
}
1418

1519
// Load any test directives embedded in the file
1620
pub fn load_errors(testfile: &Path) -> Vec<ExpectedError> {

src/compiletest/header.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,26 @@ use util;
1414

1515
pub struct TestProps {
1616
// Lines that should be expected, in order, on standard out
17-
error_patterns: Vec<~str> ,
17+
pub error_patterns: Vec<~str> ,
1818
// Extra flags to pass to the compiler
19-
compile_flags: Option<~str>,
19+
pub compile_flags: Option<~str>,
2020
// If present, the name of a file that this test should match when
2121
// pretty-printed
22-
pp_exact: Option<Path>,
22+
pub pp_exact: Option<Path>,
2323
// Modules from aux directory that should be compiled
24-
aux_builds: Vec<~str> ,
24+
pub aux_builds: Vec<~str> ,
2525
// Environment settings to use during execution
26-
exec_env: Vec<(~str,~str)> ,
26+
pub exec_env: Vec<(~str,~str)> ,
2727
// Commands to be given to the debugger, when testing debug info
28-
debugger_cmds: Vec<~str> ,
28+
pub debugger_cmds: Vec<~str> ,
2929
// Lines to check if they appear in the expected debugger output
30-
check_lines: Vec<~str> ,
30+
pub check_lines: Vec<~str> ,
3131
// Flag to force a crate to be built with the host architecture
32-
force_host: bool,
32+
pub force_host: bool,
3333
// Check stdout for error-pattern output as well as stderr
34-
check_stdout: bool,
34+
pub check_stdout: bool,
3535
// Don't force a --crate-type=dylib flag on the command line
36-
no_prefer_dynamic: bool,
36+
pub no_prefer_dynamic: bool,
3737
}
3838

3939
// Load any test directives embedded in the file

src/compiletest/procsrv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn target_env(lib_path: &str, prog: &str) -> Vec<(~str,~str)> {
5757
return env;
5858
}
5959

60-
pub struct Result {status: ProcessExit, out: ~str, err: ~str}
60+
pub struct Result {pub status: ProcessExit, pub out: ~str, pub err: ~str}
6161

6262
pub fn run(lib_path: &str,
6363
prog: &str,

src/doc/guide-unsafe.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ use std::ptr;
200200
// Unique<T> has the same semantics as ~T
201201
pub struct Unique<T> {
202202
// It contains a single raw, mutable pointer to the object in question.
203-
priv ptr: *mut T
203+
ptr: *mut T
204204
}
205205
206206
// Implement methods for creating and using the values in the box.

src/doc/rust.md

+6-9
Original file line numberDiff line numberDiff line change
@@ -1527,12 +1527,9 @@ of an item to see whether it should be allowed or not. This is where privacy
15271527
warnings are generated, or otherwise "you used a private item of another module
15281528
and weren't allowed to."
15291529

1530-
By default, everything in rust is *private*, with two exceptions. The first
1531-
exception is that struct fields are public by default (but the struct itself is
1532-
still private by default), and the remaining exception is that enum variants in
1533-
a `pub` enum are the default visibility of the enum container itself.. You are
1534-
allowed to alter this default visibility with the `pub` keyword (or `priv`
1535-
keyword for struct fields and enum variants). When an item is declared as `pub`,
1530+
By default, everything in rust is *private*, with one exception. Enum variants
1531+
in a `pub` enum are also public by default. You are allowed to alter this
1532+
default visibility with the `priv` keyword. When an item is declared as `pub`,
15361533
it can be thought of as being accessible to the outside world. For example:
15371534

15381535
~~~~
@@ -1542,7 +1539,7 @@ struct Foo;
15421539
15431540
// Declare a public struct with a private field
15441541
pub struct Bar {
1545-
priv field: int
1542+
field: int
15461543
}
15471544
15481545
// Declare a public enum with public and private variants
@@ -2354,7 +2351,7 @@ The following are examples of structure expressions:
23542351
~~~~
23552352
# struct Point { x: f64, y: f64 }
23562353
# struct TuplePoint(f64, f64);
2357-
# mod game { pub struct User<'a> { name: &'a str, age: uint, score: uint } }
2354+
# mod game { pub struct User<'a> { pub name: &'a str, pub age: uint, pub score: uint } }
23582355
# struct Cookie; fn some_fn<T>(t: T) {}
23592356
Point {x: 10.0, y: 20.0};
23602357
TuplePoint(10.0, 20.0);
@@ -3140,7 +3137,7 @@ The types `char` and `str` hold textual data.
31403137
A value of type `char` is a [Unicode scalar value](
31413138
http://www.unicode.org/glossary/#unicode_scalar_value)
31423139
(ie. a code point that is not a surrogate),
3143-
represented as a 32-bit unsigned word in the 0x0000 to 0xD7FF
3140+
represented as a 32-bit unsigned word in the 0x0000 to 0xD7FF
31443141
or 0xE000 to 0x10FFFF range.
31453142
A `[char]` vector is effectively an UCS-4 / UTF-32 string.
31463143

src/doc/tutorial.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -2657,8 +2657,8 @@ Rust doesn't support encapsulation: both struct fields and methods can
26572657
be private. But this encapsulation is at the module level, not the
26582658
struct level.
26592659

2660-
For convenience, fields are _public_ by default, and can be made _private_ with
2661-
the `priv` keyword:
2660+
Fields are _private_ by default, and can be made _public_ with
2661+
the `pub` keyword:
26622662

26632663
~~~
26642664
mod farm {
@@ -2667,8 +2667,8 @@ mod farm {
26672667
# impl Human { pub fn rest(&self) { } }
26682668
# pub fn make_me_a_farm() -> Farm { Farm { chickens: ~[], farmer: Human(0) } }
26692669
pub struct Farm {
2670-
priv chickens: ~[Chicken],
2671-
farmer: Human
2670+
chickens: ~[Chicken],
2671+
pub farmer: Human
26722672
}
26732673
26742674
impl Farm {

src/libarena/lib.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#![allow(missing_doc)]
2626
#![feature(managed_boxes)]
2727

28+
#![allow(visible_private_types)] // NOTE: remove after a stage0 snap
29+
2830
extern crate collections;
2931

3032
use std::cast::{transmute, transmute_mut, transmute_mut_region};
@@ -83,9 +85,9 @@ pub struct Arena {
8385
// The head is separated out from the list as a unbenchmarked
8486
// microoptimization, to avoid needing to case on the list to
8587
// access the head.
86-
priv head: Chunk,
87-
priv copy_head: Chunk,
88-
priv chunks: RefCell<Vec<Chunk>>,
88+
head: Chunk,
89+
copy_head: Chunk,
90+
chunks: RefCell<Vec<Chunk>>,
8991
}
9092

9193
impl Arena {
@@ -333,14 +335,14 @@ fn test_arena_destructors_fail() {
333335
/// run again for these objects.
334336
pub struct TypedArena<T> {
335337
/// A pointer to the next object to be allocated.
336-
priv ptr: *T,
338+
ptr: *T,
337339

338340
/// A pointer to the end of the allocated area. When this pointer is
339341
/// reached, a new chunk is allocated.
340-
priv end: *T,
342+
end: *T,
341343

342344
/// A pointer to the first arena segment.
343-
priv first: Option<~TypedArenaChunk<T>>,
345+
first: Option<~TypedArenaChunk<T>>,
344346
}
345347

346348
struct TypedArenaChunk<T> {

src/libcollections/bitv.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ enum Op {Union, Intersect, Assign, Difference}
227227
#[deriving(Clone)]
228228
pub struct Bitv {
229229
/// Internal representation of the bit vector (small or large)
230-
priv rep: BitvVariant,
230+
rep: BitvVariant,
231231
/// The number of valid bits in the internal representation
232-
priv nbits: uint
232+
nbits: uint
233233
}
234234

235235
fn die() -> ! {
@@ -587,9 +587,9 @@ fn iterate_bits(base: uint, bits: uint, f: |uint| -> bool) -> bool {
587587

588588
/// An iterator for `Bitv`.
589589
pub struct Bits<'a> {
590-
priv bitv: &'a Bitv,
591-
priv next_idx: uint,
592-
priv end_idx: uint,
590+
bitv: &'a Bitv,
591+
next_idx: uint,
592+
end_idx: uint,
593593
}
594594

595595
impl<'a> Iterator<bool> for Bits<'a> {
@@ -648,12 +648,12 @@ impl<'a> RandomAccessIterator<bool> for Bits<'a> {
648648
/// as a `uint`.
649649
#[deriving(Clone)]
650650
pub struct BitvSet {
651-
priv size: uint,
651+
size: uint,
652652

653653
// In theory this is a `Bitv` instead of always a `BigBitv`, but knowing that
654654
// there's an array of storage makes our lives a whole lot easier when
655655
// performing union/intersection/etc operations
656-
priv bitv: BigBitv
656+
bitv: BigBitv
657657
}
658658

659659
impl BitvSet {
@@ -912,8 +912,8 @@ impl BitvSet {
912912
}
913913

914914
pub struct BitPositions<'a> {
915-
priv set: &'a BitvSet,
916-
priv next_idx: uint
915+
set: &'a BitvSet,
916+
next_idx: uint
917917
}
918918

919919
impl<'a> Iterator<uint> for BitPositions<'a> {

src/libcollections/btree.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ use std::fmt::Show;
2323

2424
#[allow(missing_doc)]
2525
pub struct BTree<K, V> {
26-
priv root: Node<K, V>,
27-
priv len: uint,
28-
priv lower_bound: uint,
29-
priv upper_bound: uint
26+
root: Node<K, V>,
27+
len: uint,
28+
lower_bound: uint,
29+
upper_bound: uint
3030
}
3131

3232
impl<K: TotalOrd, V> BTree<K, V> {

0 commit comments

Comments
 (0)