Skip to content

Commit

Permalink
Disallow cell borrowing
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdrz committed Dec 2, 2019
1 parent e31a136 commit 683f5c9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
50 changes: 26 additions & 24 deletions src/librustc_mir/transform/check_consts/ops.rs
@@ -1,7 +1,6 @@
//! Concrete error types for all operations which may be invalid in a certain const context.

use rustc::hir::def_id::DefId;
use rustc::mir::BorrowKind;
use rustc::session::config::nightly_options;
use rustc::ty::TyCtxt;
use syntax::feature_gate::feature_err;
Expand Down Expand Up @@ -181,36 +180,39 @@ impl NonConstOp for Loop {
}

#[derive(Debug)]
pub struct MutBorrow(pub BorrowKind);
pub struct CellBorrow;
impl NonConstOp for CellBorrow {
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
span_err!(item.tcx.sess, span, E0492,
"cannot borrow a constant which may contain \
interior mutability, create a static instead");
}
}

#[derive(Debug)]
pub struct MutBorrow;
impl NonConstOp for MutBorrow {
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
Some(tcx.features().const_mut_refs)
}

fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
let kind = self.0;
if let BorrowKind::Mut { .. } = kind {
let mut err = struct_span_err!(item.tcx.sess, span, E0017,
"references in {}s may only refer \
to immutable values", item.const_kind());
err.span_label(span, format!("{}s require immutable values",
item.const_kind()));
if item.tcx.sess.teach(&err.get_code().unwrap()) {
err.note("References in statics and constants may only refer \
to immutable values.\n\n\
Statics are shared everywhere, and if they refer to \
mutable data one might violate memory safety since \
holding multiple mutable references to shared data \
is not allowed.\n\n\
If you really want global mutable state, try using \
static mut or a global UnsafeCell.");
}
err.emit();
} else {
span_err!(item.tcx.sess, span, E0492,
"cannot borrow a constant which may contain \
interior mutability, create a static instead");
let mut err = struct_span_err!(item.tcx.sess, span, E0017,
"references in {}s may only refer \
to immutable values", item.const_kind());
err.span_label(span, format!("{}s require immutable values",
item.const_kind()));
if item.tcx.sess.teach(&err.get_code().unwrap()) {
err.note("References in statics and constants may only refer \
to immutable values.\n\n\
Statics are shared everywhere, and if they refer to \
mutable data one might violate memory safety since \
holding multiple mutable references to shared data \
is not allowed.\n\n\
If you really want global mutable state, try using \
static mut or a global UnsafeCell.");
}
err.emit();
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/librustc_mir/transform/check_consts/validation.rs
Expand Up @@ -360,7 +360,11 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
};

if !is_allowed {
self.check_op(ops::MutBorrow(kind));
if let BorrowKind::Mut{ .. } = kind {
self.check_op(ops::MutBorrow);
} else {
self.check_op(ops::CellBorrow);
}
}
}

Expand All @@ -385,7 +389,11 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
);

if borrowed_place_has_mut_interior {
self.check_op(ops::MutBorrow(kind));
if let BorrowKind::Mut{ .. } = kind {
self.check_op(ops::MutBorrow);
} else {
self.check_op(ops::CellBorrow);
}
}
}

Expand Down Expand Up @@ -452,7 +460,6 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
}
}
}

fn visit_projection_elem(
&mut self,
place_base: &PlaceBase<'tcx>,
Expand Down

0 comments on commit 683f5c9

Please sign in to comment.