Skip to content

Commit 8476202

Browse files
committed
---
yaml --- r: 82271 b: refs/heads/master c: b0f6c29 h: refs/heads/master i: 82269: 47430f8 82267: 9fecb1e 82263: 9d210ee 82255: 8bcd89c 82239: 2e35ff4 v: v3
1 parent c638e8a commit 8476202

File tree

15 files changed

+226
-331
lines changed

15 files changed

+226
-331
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 34d123db4eb03c1b2378b6248ebea5f0f40f2a4f
2+
refs/heads/master: b0f6c29b4f10bafa59723714161a393a204f9c13
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 6c08cc2db4f98e9f07ae7d50338396c4123c2f0a
55
refs/heads/try: 70152ff55722878cde684ee6462c14c65f2c4729

trunk/src/libextra/base64.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ impl<'self> ToBase64 for &'self [u8] {
6464
*
6565
* ```rust
6666
* extern mod extra;
67-
* use extra::base64::{ToBase64, STANDARD};
67+
* use extra::base64::{ToBase64, standard};
6868
*
6969
* fn main () {
70-
* let str = [52,32].to_base64(STANDARD);
71-
* println!("base 64 output: {}", str);
70+
* let str = [52,32].to_base64(standard);
71+
* println!("{}", str);
7272
* }
7373
* ```
7474
*/
@@ -172,19 +172,16 @@ impl<'self> FromBase64 for &'self str {
172172
*
173173
* ```rust
174174
* extern mod extra;
175-
* use extra::base64::{ToBase64, FromBase64, STANDARD};
175+
* use extra::base64::{ToBase64, FromBase64, standard};
176176
* use std::str;
177177
*
178178
* fn main () {
179-
* let hello_str = bytes!("Hello, World").to_base64(STANDARD);
180-
* println!("base64 output: {}", hello_str);
181-
* let res = hello_str.from_base64();
182-
* if res.is_ok() {
183-
* let optBytes = str::from_utf8_opt(res.unwrap());
184-
* if optBytes.is_some() {
185-
* println!("decoded from base64: {}", optBytes.unwrap());
186-
* }
187-
* }
179+
* let hello_str = "Hello, World".to_base64(standard);
180+
* println!("{}", hello_str);
181+
* let bytes = hello_str.from_base64();
182+
* println!("{:?}", bytes);
183+
* let result_str = str::from_utf8(bytes);
184+
* println!("{}", result_str);
188185
* }
189186
* ```
190187
*/

trunk/src/librustc/driver/driver.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,10 @@ pub fn phase_3_run_analysis_passes(sess: Session,
263263
method_map, ty_cx));
264264

265265
let maps = (external_exports, last_private_map);
266-
time(time_passes, "privacy checking", maps, |(a, b)|
267-
middle::privacy::check_crate(ty_cx, &method_map, &exp_map2,
268-
a, b, crate));
266+
let exported_items =
267+
time(time_passes, "privacy checking", maps, |(a, b)|
268+
middle::privacy::check_crate(ty_cx, &method_map, &exp_map2,
269+
a, b, crate));
269270

270271
time(time_passes, "effect checking", (), |_|
271272
middle::effect::check_crate(ty_cx, method_map, crate));
@@ -300,7 +301,8 @@ pub fn phase_3_run_analysis_passes(sess: Session,
300301

301302
let reachable_map =
302303
time(time_passes, "reachability checking", (), |_|
303-
reachable::find_reachable(ty_cx, method_map, crate));
304+
reachable::find_reachable(ty_cx, method_map, exp_map2,
305+
&exported_items));
304306

305307
time(time_passes, "lint checking", (), |_|
306308
lint::check_crate(ty_cx, crate));

trunk/src/librustc/middle/effect.rs

Lines changed: 96 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@
1111
//! Enforces the Rust effect system. Currently there is just one effect,
1212
/// `unsafe`.
1313
14+
use middle::ty::{ty_bare_fn, ty_closure, ty_ptr};
1415
use middle::ty;
1516
use middle::typeck::method_map;
1617
use util::ppaux;
1718

19+
use syntax::ast::{UnDeref, ExprCall, ExprInlineAsm, ExprMethodCall};
20+
use syntax::ast::{ExprUnary, unsafe_fn, ExprPath};
1821
use syntax::ast;
1922
use syntax::codemap::Span;
23+
use syntax::visit::{fk_item_fn, fk_method};
2024
use syntax::visit;
21-
use syntax::visit::Visitor;
25+
use syntax::visit::{Visitor,fn_kind};
26+
use syntax::ast::{fn_decl,Block,NodeId,Expr};
2227

2328
#[deriving(Eq)]
2429
enum UnsafeContext {
@@ -27,26 +32,29 @@ enum UnsafeContext {
2732
UnsafeBlock(ast::NodeId),
2833
}
2934

35+
struct Context {
36+
/// The method map.
37+
method_map: method_map,
38+
/// Whether we're in an unsafe context.
39+
unsafe_context: UnsafeContext,
40+
}
41+
3042
fn type_is_unsafe_function(ty: ty::t) -> bool {
3143
match ty::get(ty).sty {
32-
ty::ty_bare_fn(ref f) => f.purity == ast::unsafe_fn,
33-
ty::ty_closure(ref f) => f.purity == ast::unsafe_fn,
44+
ty_bare_fn(ref f) => f.purity == unsafe_fn,
45+
ty_closure(ref f) => f.purity == unsafe_fn,
3446
_ => false,
3547
}
3648
}
3749

3850
struct EffectCheckVisitor {
3951
tcx: ty::ctxt,
40-
41-
/// The method map.
42-
method_map: method_map,
43-
/// Whether we're in an unsafe context.
44-
unsafe_context: UnsafeContext,
52+
context: @mut Context,
4553
}
4654

4755
impl EffectCheckVisitor {
4856
fn require_unsafe(&mut self, span: Span, description: &str) {
49-
match self.unsafe_context {
57+
match self.context.unsafe_context {
5058
SafeContext => {
5159
// Report an error.
5260
self.tcx.sess.span_err(span,
@@ -61,125 +69,113 @@ impl EffectCheckVisitor {
6169
UnsafeFn => {}
6270
}
6371
}
64-
65-
fn check_str_index(&mut self, e: @ast::Expr) {
66-
let base_type = match e.node {
67-
ast::ExprIndex(_, base, _) => ty::node_id_to_type(self.tcx, base.id),
68-
_ => return
69-
};
70-
debug2!("effect: checking index with base type {}",
71-
ppaux::ty_to_str(self.tcx, base_type));
72-
match ty::get(base_type).sty {
73-
ty::ty_estr(*) => {
74-
self.tcx.sess.span_err(e.span,
75-
"modification of string types is not allowed");
76-
}
77-
_ => {}
78-
}
79-
}
8072
}
8173

8274
impl Visitor<()> for EffectCheckVisitor {
83-
fn visit_fn(&mut self, fn_kind: &visit::fn_kind, fn_decl: &ast::fn_decl,
84-
block: &ast::Block, span: Span, node_id: ast::NodeId, _:()) {
85-
86-
let (is_item_fn, is_unsafe_fn) = match *fn_kind {
87-
visit::fk_item_fn(_, _, purity, _) =>
88-
(true, purity == ast::unsafe_fn),
89-
visit::fk_method(_, _, method) =>
90-
(true, method.purity == ast::unsafe_fn),
91-
_ => (false, false),
92-
};
93-
94-
let old_unsafe_context = self.unsafe_context;
95-
if is_unsafe_fn {
96-
self.unsafe_context = UnsafeFn
97-
} else if is_item_fn {
98-
self.unsafe_context = SafeContext
99-
}
75+
fn visit_fn(&mut self, fn_kind:&fn_kind, fn_decl:&fn_decl,
76+
block:&Block, span:Span, node_id:NodeId, _:()) {
77+
78+
let (is_item_fn, is_unsafe_fn) = match *fn_kind {
79+
fk_item_fn(_, _, purity, _) => (true, purity == unsafe_fn),
80+
fk_method(_, _, method) => (true, method.purity == unsafe_fn),
81+
_ => (false, false),
82+
};
83+
84+
let old_unsafe_context = self.context.unsafe_context;
85+
if is_unsafe_fn {
86+
self.context.unsafe_context = UnsafeFn
87+
} else if is_item_fn {
88+
self.context.unsafe_context = SafeContext
89+
}
10090

101-
visit::walk_fn(self, fn_kind, fn_decl, block, span, node_id, ());
91+
visit::walk_fn(self,
92+
fn_kind,
93+
fn_decl,
94+
block,
95+
span,
96+
node_id,
97+
());
10298

103-
self.unsafe_context = old_unsafe_context
99+
self.context.unsafe_context = old_unsafe_context
104100
}
105101

106-
fn visit_block(&mut self, block: &ast::Block, _:()) {
107-
let old_unsafe_context = self.unsafe_context;
108-
let is_unsafe = match block.rules {
109-
ast::UnsafeBlock(*) => true, ast::DefaultBlock => false
110-
};
111-
if is_unsafe && self.unsafe_context == SafeContext {
112-
self.unsafe_context = UnsafeBlock(block.id)
113-
}
102+
fn visit_block(&mut self, block:&Block, _:()) {
103+
104+
let old_unsafe_context = self.context.unsafe_context;
105+
let is_unsafe = match block.rules {
106+
ast::UnsafeBlock(*) => true, ast::DefaultBlock => false
107+
};
108+
if is_unsafe && self.context.unsafe_context == SafeContext {
109+
self.context.unsafe_context = UnsafeBlock(block.id)
110+
}
114111

115-
visit::walk_block(self, block, ());
112+
visit::walk_block(self, block, ());
116113

117-
self.unsafe_context = old_unsafe_context
114+
self.context.unsafe_context = old_unsafe_context
118115
}
119116

120-
fn visit_expr(&mut self, expr: @ast::Expr, _:()) {
121-
match expr.node {
122-
ast::ExprMethodCall(callee_id, _, _, _, _, _) => {
123-
let base_type = ty::node_id_to_type(self.tcx, callee_id);
124-
debug2!("effect: method call case, base type is {}",
125-
ppaux::ty_to_str(self.tcx, base_type));
126-
if type_is_unsafe_function(base_type) {
127-
self.require_unsafe(expr.span,
128-
"invocation of unsafe method")
117+
fn visit_expr(&mut self, expr:@Expr, _:()) {
118+
119+
match expr.node {
120+
ExprMethodCall(callee_id, _, _, _, _, _) => {
121+
let base_type = ty::node_id_to_type(self.tcx, callee_id);
122+
debug2!("effect: method call case, base type is {}",
123+
ppaux::ty_to_str(self.tcx, base_type));
124+
if type_is_unsafe_function(base_type) {
125+
self.require_unsafe(expr.span,
126+
"invocation of unsafe method")
127+
}
129128
}
130-
}
131-
ast::ExprCall(base, _, _) => {
132-
let base_type = ty::node_id_to_type(self.tcx, base.id);
133-
debug2!("effect: call case, base type is {}",
134-
ppaux::ty_to_str(self.tcx, base_type));
135-
if type_is_unsafe_function(base_type) {
136-
self.require_unsafe(expr.span, "call to unsafe function")
129+
ExprCall(base, _, _) => {
130+
let base_type = ty::node_id_to_type(self.tcx, base.id);
131+
debug2!("effect: call case, base type is {}",
132+
ppaux::ty_to_str(self.tcx, base_type));
133+
if type_is_unsafe_function(base_type) {
134+
self.require_unsafe(expr.span, "call to unsafe function")
135+
}
137136
}
138-
}
139-
ast::ExprUnary(_, ast::UnDeref, base) => {
140-
let base_type = ty::node_id_to_type(self.tcx, base.id);
141-
debug2!("effect: unary case, base type is {}",
142-
ppaux::ty_to_str(self.tcx, base_type));
143-
match ty::get(base_type).sty {
144-
ty::ty_ptr(_) => {
145-
self.require_unsafe(expr.span,
146-
"dereference of unsafe pointer")
137+
ExprUnary(_, UnDeref, base) => {
138+
let base_type = ty::node_id_to_type(self.tcx, base.id);
139+
debug2!("effect: unary case, base type is {}",
140+
ppaux::ty_to_str(self.tcx, base_type));
141+
match ty::get(base_type).sty {
142+
ty_ptr(_) => {
143+
self.require_unsafe(expr.span,
144+
"dereference of unsafe pointer")
145+
}
146+
_ => {}
147147
}
148-
_ => {}
149148
}
150-
}
151-
ast::ExprAssign(base, _) | ast::ExprAssignOp(_, _, base, _) => {
152-
self.check_str_index(base);
153-
}
154-
ast::ExprAddrOf(ast::MutMutable, base) => {
155-
self.check_str_index(base);
156-
}
157-
ast::ExprInlineAsm(*) => {
158-
self.require_unsafe(expr.span, "use of inline assembly")
159-
}
160-
ast::ExprPath(*) => {
161-
match ty::resolve_expr(self.tcx, expr) {
162-
ast::DefStatic(_, true) => {
163-
self.require_unsafe(expr.span, "use of mutable static")
149+
ExprInlineAsm(*) => {
150+
self.require_unsafe(expr.span, "use of inline assembly")
151+
}
152+
ExprPath(*) => {
153+
match ty::resolve_expr(self.tcx, expr) {
154+
ast::DefStatic(_, true) => {
155+
self.require_unsafe(expr.span, "use of mutable static")
156+
}
157+
_ => {}
164158
}
165-
_ => {}
166159
}
160+
_ => {}
167161
}
168-
_ => {}
169-
}
170162

171-
visit::walk_expr(self, expr, ());
163+
visit::walk_expr(self, expr, ());
172164
}
173165
}
174166

175167
pub fn check_crate(tcx: ty::ctxt,
176168
method_map: method_map,
177169
crate: &ast::Crate) {
178-
let mut visitor = EffectCheckVisitor {
179-
tcx: tcx,
170+
let context = @mut Context {
180171
method_map: method_map,
181172
unsafe_context: SafeContext,
182173
};
183174

175+
let mut visitor = EffectCheckVisitor {
176+
tcx: tcx,
177+
context: context,
178+
};
179+
184180
visit::walk_crate(&mut visitor, crate, ());
185181
}

trunk/src/librustc/middle/privacy.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ use syntax::visit::Visitor;
3131

3232
type Context<'self> = (&'self method_map, &'self resolve::ExportMap2);
3333

34+
// A set of all nodes in the ast which can be considered "publicly exported" in
35+
// the sense that they are accessible from anywhere in any hierarchy.
36+
pub type ExportedItems = HashSet<ast::NodeId>;
37+
3438
// This visitor is used to determine the parent of all nodes in question when it
3539
// comes to privacy. This is used to determine later on if a usage is actually
3640
// valid or not.
@@ -137,7 +141,7 @@ impl<'self> Visitor<()> for ParentVisitor<'self> {
137141
// This visitor is used to determine which items of the ast are embargoed,
138142
// otherwise known as not exported.
139143
struct EmbargoVisitor<'self> {
140-
exported_items: &'self mut HashSet<ast::NodeId>,
144+
exported_items: &'self mut ExportedItems,
141145
exp_map2: &'self resolve::ExportMap2,
142146
path_all_public: bool,
143147
}
@@ -239,7 +243,7 @@ struct PrivacyVisitor<'self> {
239243
curitem: ast::NodeId,
240244

241245
// Results of previous analyses necessary for privacy checking.
242-
exported_items: &'self HashSet<ast::NodeId>,
246+
exported_items: &'self ExportedItems,
243247
method_map: &'self method_map,
244248
parents: &'self HashMap<ast::NodeId, ast::NodeId>,
245249
external_exports: resolve::ExternalExports,
@@ -785,7 +789,7 @@ pub fn check_crate(tcx: ty::ctxt,
785789
exp_map2: &resolve::ExportMap2,
786790
external_exports: resolve::ExternalExports,
787791
last_private_map: resolve::LastPrivateMap,
788-
crate: &ast::Crate) {
792+
crate: &ast::Crate) -> ExportedItems {
789793
let mut parents = HashMap::new();
790794
let mut exported_items = HashSet::new();
791795

@@ -802,7 +806,7 @@ pub fn check_crate(tcx: ty::ctxt,
802806
{
803807
// Initialize the exported items with resolve's id for the "root crate"
804808
// to resolve references to `super` leading to the root and such.
805-
exported_items.insert(0);
809+
exported_items.insert(ast::CRATE_NODE_ID);
806810
let mut visitor = EmbargoVisitor {
807811
exported_items: &mut exported_items,
808812
exp_map2: exp_map2,
@@ -824,4 +828,5 @@ pub fn check_crate(tcx: ty::ctxt,
824828
};
825829
visit::walk_crate(&mut visitor, crate, ());
826830
}
831+
return exported_items;
827832
}

0 commit comments

Comments
 (0)