1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // Verifies that the types and values of static items
12 // are safe. The rules enforced by this module are:
14 // - For each *mutable* static item, it checks that its **type**:
15 // - doesn't have a destructor
16 // - doesn't own an owned pointer
18 // - For each *immutable* static item, it checks that its **value**:
19 // - doesn't own owned, managed pointers
20 // - doesn't contain a struct literal or a call to an enum variant / struct constructor where
21 // - the type of the struct/enum is not freeze
22 // - the type of the struct/enum has a dtor
27 use syntax::codemap::Span;
28 use syntax::visit::Visitor;
30 use syntax::print::pprust;
33 fn safe_type_for_static_mut(cx: &ty::ctxt, e: &ast::Expr) -> Option<~str> {
34 let node_ty = ty::node_id_to_type(cx, e.id);
35 let tcontents = ty::type_contents(cx, node_ty);
36 debug!("safe_type_for_static_mut(dtor={}, managed={}, owned={})",
37 tcontents.has_dtor(), tcontents.owns_managed(), tcontents.owns_owned())
39 let suffix = if tcontents.has_dtor() {
41 } else if tcontents.owns_managed() {
43 } else if tcontents.owns_owned() {
49 Some(format!("mutable static items are not allowed to have {}", suffix))
52 struct CheckStaticVisitor<'a> {
56 pub fn check_crate(tcx: &ty::ctxt, krate: &ast::Crate) {
57 visit::walk_crate(&mut CheckStaticVisitor { tcx: tcx }, krate, false)
60 impl<'a> CheckStaticVisitor<'a> {
61 fn report_error(&self, span: Span, result: Option<~str>) -> bool {
65 self.tcx.sess.span_err(span, msg);
72 impl<'a> Visitor<bool> for CheckStaticVisitor<'a> {
74 fn visit_item(&mut self, i: &ast::Item, _is_const: bool) {
75 debug!("visit_item(item={})", pprust::item_to_str(i));
77 ast::ItemStatic(_, mutability, expr) => {
79 ast::MutImmutable => {
80 self.visit_expr(expr, true);
83 self.report_error(expr.span, safe_type_for_static_mut(self.tcx, expr));
87 _ => { visit::walk_item(self, i, false) }
91 /// This method is used to enforce the constraints on
92 /// immutable static items. It walks through the *value*
93 /// of the item walking down the expression and evaluating
94 /// every nested expression. if the expression is not part
95 /// of a static item, this method does nothing but walking
97 fn visit_expr(&mut self, e: &ast::Expr, is_const: bool) {
98 debug!("visit_expr(expr={})", pprust::expr_to_str(e));
101 return visit::walk_expr(self, e, is_const);
105 ast::ExprField(..) | ast::ExprVec(..) |
106 ast::ExprBlock(..) | ast::ExprTup(..) |
107 ast::ExprVstore(_, ast::ExprVstoreSlice) => {
108 visit::walk_expr(self, e, is_const);
110 ast::ExprVstore(_, ast::ExprVstoreMutSlice) => {
111 self.tcx.sess.span_err(e.span,
112 "static items are not allowed to have mutable slices");
114 ast::ExprUnary(ast::UnBox, _) => {
115 self.tcx.sess.span_err(e.span,
116 "static items are not allowed to have managed pointers");
119 ast::ExprUnary(ast::UnUniq, _) |
120 ast::ExprVstore(_, ast::ExprVstoreUniq) => {
121 self.tcx.sess.span_err(e.span,
122 "static items are not allowed to have owned pointers");
124 ast::ExprProc(..) => {
125 self.report_error(e.span,
126 Some(~"immutable static items must be `Freeze`"));
129 ast::ExprAddrOf(mutability, _) => {
132 self.report_error(e.span,
133 Some(~"immutable static items must be `Freeze`"));
140 let node_ty = ty::node_id_to_type(self.tcx, e.id);
142 match ty::get(node_ty).sty {
143 ty::ty_struct(did, _) |
144 ty::ty_enum(did, _) => {
145 if ty::has_dtor(self.tcx, did) {
146 self.report_error(e.span,
147 Some(~"static items are not allowed to have destructors"));
150 if Some(did) == self.tcx.lang_items.no_freeze_bound() {
151 self.report_error(e.span,
152 Some(~"immutable static items must be `Freeze`"));
158 visit::walk_expr(self, e, is_const);