// - For each *immutable* static item, it checks that its **value**:
// - doesn't own owned, managed pointers
// - doesn't contain a struct literal or a call to an enum variant / struct constructor where
-// - the type of the struct/enum is not freeze
// - the type of the struct/enum has a dtor
+//
+// Rules Enforced Elsewhere:
+// - It's not possible to take the address of a static item with unsafe interior. This is enforced
+// by borrowck::gather_loans
use middle::ty;
use syntax::print::pprust;
-fn safe_type_for_static_mut(cx: ty::ctxt, e: &ast::Expr) -> Option<~str> {
+fn safe_type_for_static_mut(cx: &ty::ctxt, e: &ast::Expr) -> Option<~str> {
let node_ty = ty::node_id_to_type(cx, e.id);
let tcontents = ty::type_contents(cx, node_ty);
debug!("safe_type_for_static_mut(dtor={}, managed={}, owned={})",
Some(format!("mutable static items are not allowed to have {}", suffix))
}
-struct CheckStaticVisitor {
- tcx: ty::ctxt,
+struct CheckStaticVisitor<'a> {
+ tcx: &'a ty::ctxt,
}
-pub fn check_crate(tcx: ty::ctxt, krate: &ast::Crate) {
+pub fn check_crate(tcx: &ty::ctxt, krate: &ast::Crate) {
visit::walk_crate(&mut CheckStaticVisitor { tcx: tcx }, krate, false)
}
-impl CheckStaticVisitor {
-
+impl<'a> CheckStaticVisitor<'a> {
fn report_error(&self, span: Span, result: Option<~str>) -> bool {
match result {
None => { false }
}
}
-impl Visitor<bool> for CheckStaticVisitor {
+impl<'a> Visitor<bool> for CheckStaticVisitor<'a> {
fn visit_item(&mut self, i: &ast::Item, _is_const: bool) {
debug!("visit_item(item={})", pprust::item_to_str(i));
ast::ExprVstore(_, ast::ExprVstoreSlice) => {
visit::walk_expr(self, e, is_const);
}
+ ast::ExprVstore(_, ast::ExprVstoreMutSlice) => {
+ self.tcx.sess.span_err(e.span,
+ "static items are not allowed to have mutable slices");
+ },
ast::ExprUnary(ast::UnBox, _) => {
self.tcx.sess.span_err(e.span,
"static items are not allowed to have managed pointers");
self.tcx.sess.span_err(e.span,
"static items are not allowed to have owned pointers");
}
- ast::ExprProc(..) => {
- self.report_error(e.span,
- Some(~"immutable static items must be `Freeze`"));
- return;
- }
- ast::ExprAddrOf(mutability, _) => {
- match mutability {
- ast::MutMutable => {
- self.report_error(e.span,
- Some(~"immutable static items must be `Freeze`"));
- return;
- }
- _ => {}
- }
- }
_ => {
let node_ty = ty::node_id_to_type(self.tcx, e.id);
ty::ty_enum(did, _) => {
if ty::has_dtor(self.tcx, did) {
self.report_error(e.span,
- Some(~"static items are not allowed to have destructors"));
- return;
- }
- if Some(did) == self.tcx.lang_items.no_freeze_bound() {
- self.report_error(e.span,
- Some(~"immutable static items must be `Freeze`"));
+ Some("static items are not allowed to have destructors".to_owned()));
return;
}
}