Skip to content

Commit 19a6fab

Browse files
committed
Implement the notion of a "generated unsafe block"
This way syntax extensions can generate unsafe blocks without worrying about them generating unnecessary unsafe warnings. Perhaps a special keyword could be added to be used in macros, but I don't think that's the best solution.
1 parent ba9fa89 commit 19a6fab

File tree

6 files changed

+11
-7
lines changed

6 files changed

+11
-7
lines changed

src/librustc/middle/effect.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ impl Visitor<()> for EffectCheckVisitor {
102102
fn visit_block(&mut self, block:&Block, _:()) {
103103

104104
let old_unsafe_context = self.context.unsafe_context;
105-
if block.rules == ast::UnsafeBlock &&
106-
self.context.unsafe_context == SafeContext {
105+
let is_unsafe = match block.rules {
106+
ast::UnsafeBlock(*) => true, ast::DefaultBlock => false
107+
};
108+
if is_unsafe && self.context.unsafe_context == SafeContext {
107109
self.context.unsafe_context = UnsafeBlock(block.id)
108110
}
109111

src/librustc/middle/lint.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,9 @@ impl Visitor<@mut Context> for UnusedUnsafeLintVisitor {
11311131
fn visit_expr(&mut self, e:@ast::Expr, cx:@mut Context) {
11321132

11331133
match e.node {
1134-
ast::ExprBlock(ref blk) if blk.rules == ast::UnsafeBlock => {
1134+
// Don't warn about generated blocks, that'll just pollute the
1135+
// output.
1136+
ast::ExprBlock(ref blk) if blk.rules == ast::UnsafeBlock(false) => {
11351137
if !cx.tcx.used_unsafe.contains(&blk.id) {
11361138
cx.span_lint(unused_unsafe, blk.span,
11371139
"unnecessary `unsafe` block");

src/librustc/middle/typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl PurityState {
200200

201201
purity => {
202202
let (purity, def) = match blk.rules {
203-
ast::UnsafeBlock => (ast::unsafe_fn, blk.id),
203+
ast::UnsafeBlock(*) => (ast::unsafe_fn, blk.id),
204204
ast::DefaultBlock => (purity, self.def),
205205
};
206206
PurityState{ def: def,

src/libsyntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ pub struct Field {
479479
#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
480480
pub enum BlockCheckMode {
481481
DefaultBlock,
482-
UnsafeBlock,
482+
UnsafeBlock(/* generated internally */ bool),
483483
}
484484

485485
#[deriving(Clone, Eq, Encodable, Decodable,IterBytes)]

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ impl Parser {
17921792
} else if self.eat_keyword(keywords::Match) {
17931793
return self.parse_match_expr();
17941794
} else if self.eat_keyword(keywords::Unsafe) {
1795-
return self.parse_block_expr(lo, UnsafeBlock);
1795+
return self.parse_block_expr(lo, UnsafeBlock(false));
17961796
} else if *self.token == token::LBRACKET {
17971797
self.bump();
17981798
let mutbl = self.parse_mutability();

src/libsyntax/print/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ pub fn print_possibly_embedded_block_(s: @ps,
951951
attrs: &[ast::Attribute],
952952
close_box: bool) {
953953
match blk.rules {
954-
ast::UnsafeBlock => word_space(s, "unsafe"),
954+
ast::UnsafeBlock(*) => word_space(s, "unsafe"),
955955
ast::DefaultBlock => ()
956956
}
957957
maybe_print_comment(s, blk.span.lo);

0 commit comments

Comments
 (0)