Skip to content

Commit 3b0d486

Browse files
committed
auto merge of #10427 : alexcrichton/rust/no-xray, r=brson
Just a few triage issues that I ran into.
2 parents f1a67bd + 5fdbcc4 commit 3b0d486

File tree

4 files changed

+59
-76
lines changed

4 files changed

+59
-76
lines changed

src/librustc/middle/check_loop.rs

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

11-
1211
use middle::ty;
1312

14-
use syntax::ast::*;
15-
use syntax::visit;
13+
use syntax::ast;
14+
use syntax::codemap::Span;
1615
use syntax::visit::Visitor;
16+
use syntax::visit;
1717

18-
#[deriving(Clone)]
19-
pub struct Context {
20-
in_loop: bool,
21-
can_ret: bool
18+
#[deriving(Clone, Eq)]
19+
enum Context {
20+
Normal, Loop, Closure
2221
}
2322

2423
struct CheckLoopVisitor {
2524
tcx: ty::ctxt,
2625
}
2726

28-
pub fn check_crate(tcx: ty::ctxt, crate: &Crate) {
29-
visit::walk_crate(&mut CheckLoopVisitor { tcx: tcx },
30-
crate,
31-
Context { in_loop: false, can_ret: true });
27+
pub fn check_crate(tcx: ty::ctxt, crate: &ast::Crate) {
28+
visit::walk_crate(&mut CheckLoopVisitor { tcx: tcx }, crate, Normal)
3229
}
3330

3431
impl Visitor<Context> for CheckLoopVisitor {
35-
fn visit_item(&mut self, i:@item, _cx:Context) {
36-
visit::walk_item(self, i, Context {
37-
in_loop: false,
38-
can_ret: true
39-
});
32+
fn visit_item(&mut self, i: @ast::item, _cx: Context) {
33+
visit::walk_item(self, i, Normal);
4034
}
4135

42-
fn visit_expr(&mut self, e:@Expr, cx:Context) {
43-
44-
match e.node {
45-
ExprWhile(e, ref b) => {
36+
fn visit_expr(&mut self, e: @ast::Expr, cx:Context) {
37+
match e.node {
38+
ast::ExprWhile(e, ref b) => {
4639
self.visit_expr(e, cx);
47-
self.visit_block(b, Context { in_loop: true,.. cx });
48-
}
49-
ExprLoop(ref b, _) => {
50-
self.visit_block(b, Context { in_loop: true,.. cx });
51-
}
52-
ExprFnBlock(_, ref b) | ExprProc(_, ref b) => {
53-
self.visit_block(b, Context { in_loop: false, can_ret: false });
54-
}
55-
ExprBreak(_) => {
56-
if !cx.in_loop {
57-
self.tcx.sess.span_err(e.span, "`break` outside of loop");
58-
}
59-
}
60-
ExprAgain(_) => {
61-
if !cx.in_loop {
62-
self.tcx.sess.span_err(e.span, "`loop` outside of loop");
63-
}
64-
}
65-
ExprRet(oe) => {
66-
if !cx.can_ret {
67-
self.tcx.sess.span_err(e.span, "`return` in block function");
40+
self.visit_block(b, Loop);
41+
}
42+
ast::ExprLoop(ref b, _) => {
43+
self.visit_block(b, Loop);
44+
}
45+
ast::ExprFnBlock(_, ref b) | ast::ExprProc(_, ref b) => {
46+
self.visit_block(b, Closure);
47+
}
48+
ast::ExprBreak(_) => self.require_loop("break", cx, e.span),
49+
ast::ExprAgain(_) => self.require_loop("continue", cx, e.span),
50+
ast::ExprRet(oe) => {
51+
if cx == Closure {
52+
self.tcx.sess.span_err(e.span, "`return` in a closure");
6853
}
6954
visit::walk_expr_opt(self, oe, cx);
70-
}
71-
_ => visit::walk_expr(self, e, cx)
7255
}
56+
_ => visit::walk_expr(self, e, cx)
57+
}
58+
}
59+
}
7360

61+
impl CheckLoopVisitor {
62+
fn require_loop(&self, name: &str, cx: Context, span: Span) {
63+
match cx {
64+
Loop => {}
65+
Closure => {
66+
self.tcx.sess.span_err(span, format!("`{}` inside of a closure",
67+
name));
68+
}
69+
Normal => {
70+
self.tcx.sess.span_err(span, format!("`{}` outside of loop",
71+
name));
72+
}
73+
}
7474
}
7575
}

src/librustc/middle/resolve.rs

-28
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use syntax::ast::*;
2525
use syntax::ast;
2626
use syntax::ast_util::{def_id_of_def, local_def, mtwt_resolve};
2727
use syntax::ast_util::{path_to_ident, walk_pat, trait_method_to_ty_method};
28-
use syntax::attr;
2928
use syntax::parse::token;
3029
use syntax::parse::token::{ident_interner, interner_get};
3130
use syntax::parse::token::special_idents;
@@ -254,19 +253,6 @@ enum MethodSort {
254253
Provided(NodeId)
255254
}
256255

257-
// The X-ray flag indicates that a context has the X-ray privilege, which
258-
// allows it to reference private names. Currently, this is used for the test
259-
// runner.
260-
//
261-
// FIXME #4947: The X-ray flag is kind of questionable in the first
262-
// place. It might be better to introduce an expr_xray_path instead.
263-
264-
#[deriving(Eq)]
265-
enum XrayFlag {
266-
NoXray, //< Private items cannot be accessed.
267-
Xray //< Private items can be accessed.
268-
}
269-
270256
enum UseLexicalScopeFlag {
271257
DontUseLexicalScope,
272258
UseLexicalScope
@@ -831,7 +817,6 @@ fn Resolver(session: Session,
831817
type_ribs: @mut ~[],
832818
label_ribs: @mut ~[],
833819

834-
xray_context: NoXray,
835820
current_trait_refs: None,
836821

837822
self_ident: special_idents::self_,
@@ -883,10 +868,6 @@ struct Resolver {
883868
// The current set of local scopes, for labels.
884869
label_ribs: @mut ~[@Rib],
885870

886-
// Whether the current context is an X-ray context. An X-ray context is
887-
// allowed to access private names of any module.
888-
xray_context: XrayFlag,
889-
890871
// The trait that the current context can refer to.
891872
current_trait_refs: Option<~[DefId]>,
892873

@@ -3545,13 +3526,6 @@ impl Resolver {
35453526
debug!("(resolving item) resolving {}",
35463527
self.session.str_of(item.ident));
35473528

3548-
// Items with the !resolve_unexported attribute are X-ray contexts.
3549-
// This is used to allow the test runner to run unexported tests.
3550-
let orig_xray_flag = self.xray_context;
3551-
if attr::contains_name(item.attrs, "!resolve_unexported") {
3552-
self.xray_context = Xray;
3553-
}
3554-
35553529
match item.node {
35563530

35573531
// enum item: resolve all the variants' discrs,
@@ -3715,8 +3689,6 @@ impl Resolver {
37153689
fail!("item macros unimplemented")
37163690
}
37173691
}
3718-
3719-
self.xray_context = orig_xray_flag;
37203692
}
37213693

37223694
fn with_type_parameter_rib(&mut self,

src/test/compile-fail/break-outside-loop.rs

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

11-
// error-pattern:`break` outside of loop
12-
1311
struct Foo {
1412
t: ~str
1513
}
1614

15+
fn cond() -> bool { true }
16+
17+
fn foo(_: ||) {}
18+
1719
fn main() {
18-
let pth = break;
20+
let pth = break; //~ ERROR: `break` outside of loop
21+
if cond() { continue } //~ ERROR: `continue` outside of loop
1922

20-
let rs: Foo = Foo{t: pth};
23+
while cond() {
24+
if cond() { break }
25+
if cond() { continue }
26+
do foo {
27+
if cond() { break } //~ ERROR: `break` inside of a closure
28+
if cond() { continue } //~ ERROR: `continue` inside of a closure
29+
}
30+
}
2131

32+
let rs: Foo = Foo{t: pth};
2233
}

src/test/compile-fail/return-in-block-function.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
fn main() {
1212
let _x = || {
13-
return //~ ERROR: `return` in block function
13+
return //~ ERROR: `return` in a closure
1414
};
1515
}

0 commit comments

Comments
 (0)