Skip to content

Implement the notion of a "generated unsafe block" #9013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/librustc/middle/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ impl Visitor<()> for EffectCheckVisitor {
fn visit_block(&mut self, block:&Block, _:()) {

let old_unsafe_context = self.context.unsafe_context;
if block.rules == ast::UnsafeBlock &&
self.context.unsafe_context == SafeContext {
let is_unsafe = match block.rules {
ast::UnsafeBlock(*) => true, ast::DefaultBlock => false
};
if is_unsafe && self.context.unsafe_context == SafeContext {
self.context.unsafe_context = UnsafeBlock(block.id)
}

Expand Down
7 changes: 5 additions & 2 deletions src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,8 +1131,11 @@ impl Visitor<@mut Context> for UnusedUnsafeLintVisitor {
fn visit_expr(&mut self, e:@ast::Expr, cx:@mut Context) {

match e.node {
ast::ExprBlock(ref blk) if blk.rules == ast::UnsafeBlock => {
if !cx.tcx.used_unsafe.contains(&blk.id) {
// Don't warn about generated blocks, that'll just pollute the
// output.
ast::ExprBlock(ref blk) => {
if blk.rules == ast::UnsafeBlock(ast::UserProvided) &&
!cx.tcx.used_unsafe.contains(&blk.id) {
cx.span_lint(unused_unsafe, blk.span,
"unnecessary `unsafe` block");
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl PurityState {

purity => {
let (purity, def) = match blk.rules {
ast::UnsafeBlock => (ast::unsafe_fn, blk.id),
ast::UnsafeBlock(*) => (ast::unsafe_fn, blk.id),
ast::DefaultBlock => (purity, self.def),
};
PurityState{ def: def,
Expand Down
8 changes: 7 additions & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,13 @@ pub struct Field {
#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
pub enum BlockCheckMode {
DefaultBlock,
UnsafeBlock,
UnsafeBlock(UnsafeSource),
}

#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
pub enum UnsafeSource {
CompilerGenerated,
UserProvided,
}

#[deriving(Clone, Eq, Encodable, Decodable,IterBytes)]
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/ifmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ impl Context {
stmts: ~[],
expr: Some(result),
id: ast::DUMMY_NODE_ID,
rules: ast::UnsafeBlock,
rules: ast::UnsafeBlock(ast::CompilerGenerated),
span: self.fmtsp,
});

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1792,7 +1792,7 @@ impl Parser {
} else if self.eat_keyword(keywords::Match) {
return self.parse_match_expr();
} else if self.eat_keyword(keywords::Unsafe) {
return self.parse_block_expr(lo, UnsafeBlock);
return self.parse_block_expr(lo, UnsafeBlock(ast::UserProvided));
} else if *self.token == token::LBRACKET {
self.bump();
let mutbl = self.parse_mutability();
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ pub fn print_possibly_embedded_block_(s: @ps,
attrs: &[ast::Attribute],
close_box: bool) {
match blk.rules {
ast::UnsafeBlock => word_space(s, "unsafe"),
ast::UnsafeBlock(*) => word_space(s, "unsafe"),
ast::DefaultBlock => ()
}
maybe_print_comment(s, blk.span.lo);
Expand Down
9 changes: 9 additions & 0 deletions src/test/run-pass/ifmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

// xfail-fast: check-fast screws up repr paths

#[deny(warnings)];

use std::fmt;

struct A;
Expand Down Expand Up @@ -226,6 +228,13 @@ pub fn main() {
let a = ~3;
format!("{:?}", a);
format!("{:?}", a);

// make sure that format! doesn't cause spurious unused-unsafe warnings when
// it's inside of an outer unsafe block
unsafe {
let a: int = ::std::cast::transmute(3u);
format!("{}", a);
}
}

// Basic test to make sure that we can invoke the `write!` macro with an
Expand Down