From daee1b4d5c6e1db8563c57ff5529b62d0a29fbde Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 24 Sep 2013 18:18:40 -0700 Subject: [PATCH] Ensure that skipped items aren't encoded If an item is skipped due to it being unreachable or for some optimization, then it shouldn't be encoded into the metadata (because it wasn't present in the first place). --- src/librustc/middle/trans/controlflow.rs | 11 +++++++++++ src/test/auxiliary/cfg_inner_static.rs | 17 +++++++++++++++++ src/test/run-pass/cfg_inner_static.rs | 18 ++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 src/test/auxiliary/cfg_inner_static.rs create mode 100644 src/test/run-pass/cfg_inner_static.rs diff --git a/src/librustc/middle/trans/controlflow.rs b/src/librustc/middle/trans/controlflow.rs index 2bf4e5c2bc6b0..bfa7beace8ced 100644 --- a/src/librustc/middle/trans/controlflow.rs +++ b/src/librustc/middle/trans/controlflow.rs @@ -25,6 +25,7 @@ use syntax::ast; use syntax::ast::Name; use syntax::ast_util; use syntax::codemap::Span; +use syntax::visit::Visitor; pub fn trans_block(bcx: @mut Block, b: &ast::Block, dest: expr::Dest) -> @mut Block { let _icx = push_ctxt("trans_block"); @@ -64,12 +65,22 @@ pub fn trans_if(bcx: @mut Block, // Drop branches that are known to be impossible if is_const(cond_val) && !is_undef(cond_val) { if const_to_uint(cond_val) == 1 { + match els { + Some(elexpr) => { + let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx }; + trans.visit_expr(elexpr, ()); + } + None => {} + } // if true { .. } [else { .. }] return do with_scope(bcx, thn.info(), "if_true_then") |bcx| { let bcx_out = trans_block(bcx, thn, dest); trans_block_cleanups(bcx_out, block_cleanups(bcx)) } } else { + let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx } ; + trans.visit_block(thn, ()); + match els { // if false { .. } else { .. } Some(elexpr) => { diff --git a/src/test/auxiliary/cfg_inner_static.rs b/src/test/auxiliary/cfg_inner_static.rs new file mode 100644 index 0000000000000..4331a1da2a26b --- /dev/null +++ b/src/test/auxiliary/cfg_inner_static.rs @@ -0,0 +1,17 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// this used to just ICE on compiling +pub fn foo() { + if cfg!(foo) { + static a: int = 3; + a + } else { 3 }; +} diff --git a/src/test/run-pass/cfg_inner_static.rs b/src/test/run-pass/cfg_inner_static.rs new file mode 100644 index 0000000000000..2d06a10a05f2a --- /dev/null +++ b/src/test/run-pass/cfg_inner_static.rs @@ -0,0 +1,18 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:cfg_inner_static.rs +// xfail-fast + +extern mod cfg_inner_static; + +fn main() { + cfg_inner_static::foo(); +}