From 19a6fabad8e43a81617182eba24fb80e33da8e52 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 28 Aug 2013 23:47:26 -0700 Subject: [PATCH 1/2] 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. --- src/librustc/middle/effect.rs | 6 ++++-- src/librustc/middle/lint.rs | 4 +++- src/librustc/middle/typeck/check/mod.rs | 2 +- src/libsyntax/ast.rs | 2 +- src/libsyntax/parse/parser.rs | 2 +- src/libsyntax/print/pprust.rs | 2 +- 6 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/librustc/middle/effect.rs b/src/librustc/middle/effect.rs index 160b03132e06..9c02f544fbae 100644 --- a/src/librustc/middle/effect.rs +++ b/src/librustc/middle/effect.rs @@ -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) } diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs index 6f4d94e2a647..a4b64f5a1d84 100644 --- a/src/librustc/middle/lint.rs +++ b/src/librustc/middle/lint.rs @@ -1131,7 +1131,9 @@ 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 => { + // Don't warn about generated blocks, that'll just pollute the + // output. + ast::ExprBlock(ref blk) if blk.rules == ast::UnsafeBlock(false) => { if !cx.tcx.used_unsafe.contains(&blk.id) { cx.span_lint(unused_unsafe, blk.span, "unnecessary `unsafe` block"); diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index b6e0fd93fa9b..0c0326e9317e 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -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, diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 8d557125d370..63a6e9365507 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -479,7 +479,7 @@ pub struct Field { #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)] pub enum BlockCheckMode { DefaultBlock, - UnsafeBlock, + UnsafeBlock(/* generated internally */ bool), } #[deriving(Clone, Eq, Encodable, Decodable,IterBytes)] diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 4adc34d75a73..b0bcc91f9627 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -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(false)); } else if *self.token == token::LBRACKET { self.bump(); let mutbl = self.parse_mutability(); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 55f052d769dc..9b9b157c9d85 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -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); From 11e9c48353a6fcbfd036e5ee58b1d4b5f572d7eb Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 5 Sep 2013 20:50:10 -0700 Subject: [PATCH 2/2] Flag unsafe blocks from format! as compiler-generated --- src/librustc/middle/lint.rs | 5 +++-- src/libsyntax/ast.rs | 8 +++++++- src/libsyntax/ext/ifmt.rs | 2 +- src/libsyntax/parse/parser.rs | 2 +- src/test/run-pass/ifmt.rs | 9 +++++++++ 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs index a4b64f5a1d84..da181ff2eb6b 100644 --- a/src/librustc/middle/lint.rs +++ b/src/librustc/middle/lint.rs @@ -1133,8 +1133,9 @@ impl Visitor<@mut Context> for UnusedUnsafeLintVisitor { match e.node { // Don't warn about generated blocks, that'll just pollute the // output. - ast::ExprBlock(ref blk) if blk.rules == ast::UnsafeBlock(false) => { - if !cx.tcx.used_unsafe.contains(&blk.id) { + 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"); } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 63a6e9365507..ef2e557b6ea6 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -479,7 +479,13 @@ pub struct Field { #[deriving(Clone, Eq, Encodable, Decodable, IterBytes)] pub enum BlockCheckMode { DefaultBlock, - UnsafeBlock(/* generated internally */ bool), + UnsafeBlock(UnsafeSource), +} + +#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)] +pub enum UnsafeSource { + CompilerGenerated, + UserProvided, } #[deriving(Clone, Eq, Encodable, Decodable,IterBytes)] diff --git a/src/libsyntax/ext/ifmt.rs b/src/libsyntax/ext/ifmt.rs index b7722ffc2971..486069db4f0c 100644 --- a/src/libsyntax/ext/ifmt.rs +++ b/src/libsyntax/ext/ifmt.rs @@ -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, }); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index b0bcc91f9627..b5772a9eede2 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -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(false)); + return self.parse_block_expr(lo, UnsafeBlock(ast::UserProvided)); } else if *self.token == token::LBRACKET { self.bump(); let mutbl = self.parse_mutability(); diff --git a/src/test/run-pass/ifmt.rs b/src/test/run-pass/ifmt.rs index 3f0c7e070416..ab66bfc10111 100644 --- a/src/test/run-pass/ifmt.rs +++ b/src/test/run-pass/ifmt.rs @@ -10,6 +10,8 @@ // xfail-fast: check-fast screws up repr paths +#[deny(warnings)]; + use std::fmt; struct A; @@ -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