From 99cda0b258199125d9bafec8fa54e1b539c9754c Mon Sep 17 00:00:00 2001 From: whataloadofwhat Date: Sun, 12 Oct 2014 14:29:00 +0100 Subject: [PATCH] Stop ICE when using foreign statics in statics Only change is to stop an ICE when a foreign static are referenced in consts and statics. See: #16538, #16479, #14227, #13325 --- src/librustc/middle/check_static_recursion.rs | 16 +++++++++++++--- .../run-pass/static-reference-foreign-static.rs | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 src/test/run-pass/static-reference-foreign-static.rs diff --git a/src/librustc/middle/check_static_recursion.rs b/src/librustc/middle/check_static_recursion.rs index 1f76d9dba2635..3c04a0cb13d80 100644 --- a/src/librustc/middle/check_static_recursion.rs +++ b/src/librustc/middle/check_static_recursion.rs @@ -96,10 +96,20 @@ impl<'a, 'ast, 'v> Visitor<'v> for CheckItemRecursionVisitor<'a, 'ast> { match e.node { ast::ExprPath(..) => { match self.def_map.borrow().find(&e.id) { - Some(&DefStatic(def_id, _)) | - Some(&DefConst(def_id)) if + Some(&DefStatic(def_id, _)) | Some(&DefConst(def_id)) if ast_util::is_local(def_id) => { - self.visit_item(&*self.ast_map.expect_item(def_id.node)); + match self.ast_map.find(def_id.node) { + Some(ast_map::NodeItem(item)) => { + self.visit_item(item); + } + Some(ast_map::NodeForeignItem(item)) => { + //External statics don't have values so it + //won't be recursive. + self.visit_foreign_item(item); + } + _ => fail!("expected item or foreign item, got {}", + self.ast_map.node_to_string(def_id.node)) + } } _ => () } diff --git a/src/test/run-pass/static-reference-foreign-static.rs b/src/test/run-pass/static-reference-foreign-static.rs new file mode 100644 index 0000000000000..92429e0a2165f --- /dev/null +++ b/src/test/run-pass/static-reference-foreign-static.rs @@ -0,0 +1,17 @@ +// Copyright 2014 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. + +extern { + static x: i32; +} + +static X: &'static i32 = &x; + +fn main() { }