2014-09-14 17:21:25 -07:00
|
|
|
// 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 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-07-02 14:07:42 -07:00
|
|
|
// This compiler pass detects constants that refer to themselves
|
2014-09-14 17:21:25 -07:00
|
|
|
// recursively.
|
|
|
|
|
2016-01-29 15:04:07 -05:00
|
|
|
use rustc::dep_graph::DepNode;
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir::map as ast_map;
|
2016-07-21 07:01:14 +05:30
|
|
|
use rustc::session::{CompileResult, Session};
|
2016-11-25 13:21:19 +02:00
|
|
|
use rustc::hir::def::{Def, CtorKind};
|
2016-09-10 00:11:31 +03:00
|
|
|
use rustc::util::nodemap::{NodeMap, NodeSet};
|
2014-09-14 17:21:25 -07:00
|
|
|
|
2016-07-21 07:01:14 +05:30
|
|
|
use syntax::ast;
|
2015-09-04 16:37:22 -07:00
|
|
|
use syntax::feature_gate::{GateIssue, emit_feature_err};
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2016-11-28 14:00:26 -05:00
|
|
|
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir;
|
2014-09-14 17:21:25 -07:00
|
|
|
|
|
|
|
struct CheckCrateVisitor<'a, 'ast: 'a> {
|
|
|
|
sess: &'a Session,
|
2015-06-22 20:55:57 -06:00
|
|
|
ast_map: &'a ast_map::Map<'ast>,
|
2015-07-08 20:51:47 -06:00
|
|
|
// `discriminant_map` is a cache that associates the `NodeId`s of local
|
|
|
|
// variant definitions with the discriminant expression that applies to
|
|
|
|
// each one. If the variant uses the default values (starting from `0`),
|
|
|
|
// then `None` is stored.
|
2016-09-10 00:11:31 +03:00
|
|
|
discriminant_map: NodeMap<Option<&'ast hir::Expr>>,
|
|
|
|
detected_recursive_ids: NodeSet,
|
2014-09-14 17:21:25 -07:00
|
|
|
}
|
|
|
|
|
2015-06-22 20:55:57 -06:00
|
|
|
impl<'a, 'ast: 'a> Visitor<'ast> for CheckCrateVisitor<'a, 'ast> {
|
2016-11-28 14:00:26 -05:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'ast> {
|
|
|
|
NestedVisitorMap::None
|
|
|
|
}
|
2016-11-24 20:15:11 +01:00
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
fn visit_item(&mut self, it: &'ast hir::Item) {
|
2015-03-15 19:35:25 -06:00
|
|
|
match it.node {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemStatic(..) |
|
|
|
|
hir::ItemConst(..) => {
|
2016-07-21 07:01:14 +05:30
|
|
|
let mut recursion_visitor = CheckItemRecursionVisitor::new(self, &it.span);
|
2015-03-15 19:35:25 -06:00
|
|
|
recursion_visitor.visit_item(it);
|
2016-07-21 07:01:14 +05:30
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemEnum(ref enum_def, ref generics) => {
|
2015-06-22 20:55:57 -06:00
|
|
|
// We could process the whole enum, but handling the variants
|
|
|
|
// with discriminant expressions one by one gives more specific,
|
|
|
|
// less redundant output.
|
|
|
|
for variant in &enum_def.variants {
|
|
|
|
if let Some(_) = variant.node.disr_expr {
|
2016-07-21 07:01:14 +05:30
|
|
|
let mut recursion_visitor = CheckItemRecursionVisitor::new(self,
|
|
|
|
&variant.span);
|
2015-06-22 20:55:57 -06:00
|
|
|
recursion_visitor.populate_enum_discriminants(enum_def);
|
2015-10-02 16:14:20 +03:00
|
|
|
recursion_visitor.visit_variant(variant, generics, it.id);
|
2015-06-22 20:55:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
2015-11-17 17:51:44 -05:00
|
|
|
intravisit::walk_item(self, it)
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
fn visit_trait_item(&mut self, ti: &'ast hir::TraitItem) {
|
2015-03-15 19:35:25 -06:00
|
|
|
match ti.node {
|
2016-12-04 04:21:06 +02:00
|
|
|
hir::TraitItemKind::Const(_, ref default) => {
|
2015-06-22 20:55:57 -06:00
|
|
|
if let Some(_) = *default {
|
2016-07-21 07:01:14 +05:30
|
|
|
let mut recursion_visitor = CheckItemRecursionVisitor::new(self, &ti.span);
|
2015-03-15 19:35:25 -06:00
|
|
|
recursion_visitor.visit_trait_item(ti);
|
|
|
|
}
|
|
|
|
}
|
2015-06-22 20:55:57 -06:00
|
|
|
_ => {}
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
2015-11-17 17:51:44 -05:00
|
|
|
intravisit::walk_trait_item(self, ti)
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
fn visit_impl_item(&mut self, ii: &'ast hir::ImplItem) {
|
2015-03-15 19:35:25 -06:00
|
|
|
match ii.node {
|
2015-11-12 15:57:51 +01:00
|
|
|
hir::ImplItemKind::Const(..) => {
|
2016-07-21 07:01:14 +05:30
|
|
|
let mut recursion_visitor = CheckItemRecursionVisitor::new(self, &ii.span);
|
2015-03-15 19:35:25 -06:00
|
|
|
recursion_visitor.visit_impl_item(ii);
|
|
|
|
}
|
2015-06-22 20:55:57 -06:00
|
|
|
_ => {}
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
2015-11-17 17:51:44 -05:00
|
|
|
intravisit::walk_impl_item(self, ii)
|
2014-09-14 17:21:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 00:11:31 +03:00
|
|
|
pub fn check_crate<'ast>(sess: &Session, ast_map: &ast_map::Map<'ast>) -> CompileResult {
|
2016-01-29 15:04:07 -05:00
|
|
|
let _task = ast_map.dep_graph.in_task(DepNode::CheckStaticRecursion);
|
|
|
|
|
2014-09-14 17:21:25 -07:00
|
|
|
let mut visitor = CheckCrateVisitor {
|
|
|
|
sess: sess,
|
2015-06-22 20:55:57 -06:00
|
|
|
ast_map: ast_map,
|
2016-09-10 00:11:31 +03:00
|
|
|
discriminant_map: NodeMap(),
|
|
|
|
detected_recursive_ids: NodeSet(),
|
2014-09-14 17:21:25 -07:00
|
|
|
};
|
2016-01-27 19:01:01 +13:00
|
|
|
sess.track_errors(|| {
|
2016-11-02 18:22:59 -04:00
|
|
|
// FIXME(#37712) could use ItemLikeVisitor if trait items were item-like
|
|
|
|
ast_map.krate().visit_all_item_likes(&mut visitor.as_deep_visitor());
|
2016-01-27 19:01:01 +13:00
|
|
|
})
|
2014-09-14 17:21:25 -07:00
|
|
|
}
|
|
|
|
|
2016-09-10 00:11:31 +03:00
|
|
|
struct CheckItemRecursionVisitor<'a, 'b: 'a, 'ast: 'b> {
|
|
|
|
root_span: &'b Span,
|
|
|
|
sess: &'b Session,
|
|
|
|
ast_map: &'b ast_map::Map<'ast>,
|
|
|
|
discriminant_map: &'a mut NodeMap<Option<&'ast hir::Expr>>,
|
2015-06-22 20:55:57 -06:00
|
|
|
idstack: Vec<ast::NodeId>,
|
2016-09-10 00:11:31 +03:00
|
|
|
detected_recursive_ids: &'a mut NodeSet,
|
2014-09-14 17:21:25 -07:00
|
|
|
}
|
|
|
|
|
2016-09-10 00:11:31 +03:00
|
|
|
impl<'a, 'b: 'a, 'ast: 'b> CheckItemRecursionVisitor<'a, 'b, 'ast> {
|
|
|
|
fn new(v: &'a mut CheckCrateVisitor<'b, 'ast>, span: &'b Span) -> Self {
|
2015-03-15 19:35:25 -06:00
|
|
|
CheckItemRecursionVisitor {
|
|
|
|
root_span: span,
|
|
|
|
sess: v.sess,
|
|
|
|
ast_map: v.ast_map,
|
2016-09-10 00:11:31 +03:00
|
|
|
discriminant_map: &mut v.discriminant_map,
|
2015-06-22 20:55:57 -06:00
|
|
|
idstack: Vec::new(),
|
2016-09-10 00:11:31 +03:00
|
|
|
detected_recursive_ids: &mut v.detected_recursive_ids,
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 22:46:52 +03:00
|
|
|
fn with_item_id_pushed<F>(&mut self, id: ast::NodeId, f: F, span: Span)
|
2016-07-21 07:01:14 +05:30
|
|
|
where F: Fn(&mut Self)
|
|
|
|
{
|
2015-07-02 14:07:42 -07:00
|
|
|
if self.idstack.iter().any(|&x| x == id) {
|
2016-09-10 00:11:31 +03:00
|
|
|
if self.detected_recursive_ids.contains(&id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.detected_recursive_ids.insert(id);
|
2015-07-02 14:07:42 -07:00
|
|
|
let any_static = self.idstack.iter().any(|&x| {
|
|
|
|
if let ast_map::NodeItem(item) = self.ast_map.get(x) {
|
2015-07-31 00:04:06 -07:00
|
|
|
if let hir::ItemStatic(..) = item.node {
|
2015-07-02 14:07:42 -07:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if any_static {
|
|
|
|
if !self.sess.features.borrow().static_recursion {
|
2016-09-24 18:42:54 +02:00
|
|
|
emit_feature_err(&self.sess.parse_sess,
|
2015-07-02 14:07:42 -07:00
|
|
|
"static_recursion",
|
2016-07-21 07:01:14 +05:30
|
|
|
*self.root_span,
|
|
|
|
GateIssue::Language,
|
|
|
|
"recursive static");
|
2015-07-02 14:07:42 -07:00
|
|
|
}
|
|
|
|
} else {
|
2016-08-30 22:46:52 +03:00
|
|
|
struct_span_err!(self.sess, span, E0265, "recursive constant")
|
|
|
|
.span_label(span, &format!("recursion not allowed in constant"))
|
|
|
|
.emit();
|
2015-07-02 14:07:42 -07:00
|
|
|
}
|
2015-03-15 19:35:25 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.idstack.push(id);
|
|
|
|
f(self);
|
|
|
|
self.idstack.pop();
|
|
|
|
}
|
2015-06-22 20:55:57 -06:00
|
|
|
// If a variant has an expression specifying its discriminant, then it needs
|
|
|
|
// to be checked just like a static or constant. However, if there are more
|
|
|
|
// variants with no explicitly specified discriminant, those variants will
|
|
|
|
// increment the same expression to get their values.
|
|
|
|
//
|
|
|
|
// So for every variant, we need to track whether there is an expression
|
|
|
|
// somewhere in the enum definition that controls its discriminant. We do
|
|
|
|
// this by starting from the end and searching backward.
|
2016-09-10 00:11:31 +03:00
|
|
|
fn populate_enum_discriminants(&mut self, enum_definition: &'ast hir::EnumDef) {
|
2015-06-22 20:55:57 -06:00
|
|
|
// Get the map, and return if we already processed this enum or if it
|
|
|
|
// has no variants.
|
|
|
|
match enum_definition.variants.first() {
|
2016-07-21 07:01:14 +05:30
|
|
|
None => {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-10 00:11:31 +03:00
|
|
|
Some(variant) if self.discriminant_map.contains_key(&variant.node.data.id()) => {
|
2015-06-22 20:55:57 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through all the variants.
|
|
|
|
let mut variant_stack: Vec<ast::NodeId> = Vec::new();
|
|
|
|
for variant in enum_definition.variants.iter().rev() {
|
2015-10-10 03:28:40 +03:00
|
|
|
variant_stack.push(variant.node.data.id());
|
2015-06-22 20:55:57 -06:00
|
|
|
// When we find an expression, every variant currently on the stack
|
|
|
|
// is affected by that expression.
|
|
|
|
if let Some(ref expr) = variant.node.disr_expr {
|
|
|
|
for id in &variant_stack {
|
2016-09-10 00:11:31 +03:00
|
|
|
self.discriminant_map.insert(*id, Some(expr));
|
2015-06-22 20:55:57 -06:00
|
|
|
}
|
|
|
|
variant_stack.clear()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we are at the top, that always starts at 0, so any variant on the
|
|
|
|
// stack has a default value and does not need to be checked.
|
|
|
|
for id in &variant_stack {
|
2016-09-10 00:11:31 +03:00
|
|
|
self.discriminant_map.insert(*id, None);
|
2015-06-22 20:55:57 -06:00
|
|
|
}
|
|
|
|
}
|
2014-09-14 17:21:25 -07:00
|
|
|
}
|
|
|
|
|
2016-09-10 00:11:31 +03:00
|
|
|
impl<'a, 'b: 'a, 'ast: 'b> Visitor<'ast> for CheckItemRecursionVisitor<'a, 'b, 'ast> {
|
2016-11-28 14:00:26 -05:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'ast> {
|
|
|
|
NestedVisitorMap::OnlyBodies(&self.ast_map)
|
2016-10-29 15:01:11 +02:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
fn visit_item(&mut self, it: &'ast hir::Item) {
|
2016-08-30 22:46:52 +03:00
|
|
|
self.with_item_id_pushed(it.id, |v| intravisit::walk_item(v, it), it.span);
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
|
|
|
|
2016-07-21 07:01:14 +05:30
|
|
|
fn visit_enum_def(&mut self,
|
|
|
|
enum_definition: &'ast hir::EnumDef,
|
|
|
|
generics: &'ast hir::Generics,
|
|
|
|
item_id: ast::NodeId,
|
|
|
|
_: Span) {
|
2015-06-22 20:55:57 -06:00
|
|
|
self.populate_enum_discriminants(enum_definition);
|
2015-11-17 17:51:44 -05:00
|
|
|
intravisit::walk_enum_def(self, enum_definition, generics, item_id);
|
2015-06-22 20:55:57 -06:00
|
|
|
}
|
|
|
|
|
2016-07-21 07:01:14 +05:30
|
|
|
fn visit_variant(&mut self,
|
|
|
|
variant: &'ast hir::Variant,
|
|
|
|
_: &'ast hir::Generics,
|
|
|
|
_: ast::NodeId) {
|
2015-10-10 03:28:40 +03:00
|
|
|
let variant_id = variant.node.data.id();
|
2015-06-22 20:55:57 -06:00
|
|
|
let maybe_expr;
|
2016-09-10 00:11:31 +03:00
|
|
|
if let Some(get_expr) = self.discriminant_map.get(&variant_id) {
|
2015-06-22 20:55:57 -06:00
|
|
|
// This is necessary because we need to let the `discriminant_map`
|
|
|
|
// borrow fall out of scope, so that we can reborrow farther down.
|
|
|
|
maybe_expr = (*get_expr).clone();
|
|
|
|
} else {
|
2016-03-28 23:03:47 +02:00
|
|
|
span_bug!(variant.span,
|
|
|
|
"`check_static_recursion` attempted to visit \
|
|
|
|
variant with unknown discriminant")
|
2015-06-22 20:55:57 -06:00
|
|
|
}
|
|
|
|
// If `maybe_expr` is `None`, that's because no discriminant is
|
|
|
|
// specified that affects this variant. Thus, no risk of recursion.
|
|
|
|
if let Some(expr) = maybe_expr {
|
2016-08-30 22:46:52 +03:00
|
|
|
self.with_item_id_pushed(expr.id, |v| intravisit::walk_expr(v, expr), expr.span);
|
2015-06-22 20:55:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
fn visit_trait_item(&mut self, ti: &'ast hir::TraitItem) {
|
2016-08-30 22:46:52 +03:00
|
|
|
self.with_item_id_pushed(ti.id, |v| intravisit::walk_trait_item(v, ti), ti.span);
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
fn visit_impl_item(&mut self, ii: &'ast hir::ImplItem) {
|
2016-08-30 22:46:52 +03:00
|
|
|
self.with_item_id_pushed(ii.id, |v| intravisit::walk_impl_item(v, ii), ii.span);
|
2014-09-14 17:21:25 -07:00
|
|
|
}
|
|
|
|
|
2016-09-10 00:11:31 +03:00
|
|
|
fn visit_path(&mut self, path: &'ast hir::Path, _: ast::NodeId) {
|
|
|
|
match path.def {
|
|
|
|
Def::Static(def_id, _) |
|
|
|
|
Def::AssociatedConst(def_id) |
|
|
|
|
Def::Const(def_id) => {
|
|
|
|
if let Some(node_id) = self.ast_map.as_local_node_id(def_id) {
|
|
|
|
match self.ast_map.get(node_id) {
|
|
|
|
ast_map::NodeItem(item) => self.visit_item(item),
|
|
|
|
ast_map::NodeTraitItem(item) => self.visit_trait_item(item),
|
|
|
|
ast_map::NodeImplItem(item) => self.visit_impl_item(item),
|
|
|
|
ast_map::NodeForeignItem(_) => {}
|
|
|
|
_ => {
|
|
|
|
span_bug!(path.span,
|
|
|
|
"expected item, found {}",
|
|
|
|
self.ast_map.node_to_string(node_id));
|
2015-06-22 20:55:57 -06:00
|
|
|
}
|
|
|
|
}
|
2016-09-10 00:11:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// For variants, we only want to check expressions that
|
|
|
|
// affect the specific variant used, but we need to check
|
|
|
|
// the whole enum definition to see what expression that
|
|
|
|
// might be (if any).
|
|
|
|
Def::VariantCtor(variant_id, CtorKind::Const) => {
|
|
|
|
if let Some(variant_id) = self.ast_map.as_local_node_id(variant_id) {
|
|
|
|
let variant = self.ast_map.expect_variant(variant_id);
|
|
|
|
let enum_id = self.ast_map.get_parent(variant_id);
|
|
|
|
let enum_item = self.ast_map.expect_item(enum_id);
|
|
|
|
if let hir::ItemEnum(ref enum_def, ref generics) = enum_item.node {
|
|
|
|
self.populate_enum_discriminants(enum_def);
|
|
|
|
self.visit_variant(variant, generics, enum_id);
|
|
|
|
} else {
|
|
|
|
span_bug!(path.span,
|
|
|
|
"`check_static_recursion` found \
|
|
|
|
non-enum in Def::VariantCtor");
|
2014-09-14 17:21:25 -07:00
|
|
|
}
|
|
|
|
}
|
2016-07-21 07:01:14 +05:30
|
|
|
}
|
|
|
|
_ => (),
|
2014-09-14 17:21:25 -07:00
|
|
|
}
|
2016-09-10 00:11:31 +03:00
|
|
|
intravisit::walk_path(self, path);
|
2014-09-14 17:21:25 -07:00
|
|
|
}
|
|
|
|
}
|