Skip to content

Commit 3ce88c7

Browse files
authored
Rollup merge of #42490 - gaurikholkar:master, r=eddyb
Changing error message from `contains interior mutability` to `may contain interior mutability` Fixes #40313 . I have changed the message from `contains interior mutability` to `may contain interior mutability` for the following example ``` use std::cell::Cell; use std::panic::catch_unwind; fn main() { let mut x = Cell::new(22); catch_unwind(|| { x.set(23); }); } ``` which has been added as a ui test. Also, the message [here](https://github.com/gaurikholkar/rust/blob/master/src/librustc_mir/transform/qualify_consts.rs#L666) and it's respective `compile-fail` test have been modified. cc @nikomatsakis @Mark-Simulacrum @eddyb
2 parents 0362891 + 980a5b0 commit 3ce88c7

File tree

6 files changed

+39
-9
lines changed

6 files changed

+39
-9
lines changed

src/librustc_mir/diagnostics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT};
309309
310310
const A: AtomicUsize = ATOMIC_USIZE_INIT;
311311
static B: &'static AtomicUsize = &A;
312-
// error: cannot borrow a constant which contains interior mutability, create a
313-
// static instead
312+
// error: cannot borrow a constant which may contain interior mutability,
313+
// create a static instead
314314
```
315315
316316
A `const` represents a constant value that should never change. If one takes
@@ -338,8 +338,8 @@ use std::cell::Cell;
338338
339339
const A: Cell<usize> = Cell::new(1);
340340
const B: &'static Cell<usize> = &A;
341-
// error: cannot borrow a constant which contains interior mutability, create
342-
// a static instead
341+
// error: cannot borrow a constant which may contain interior mutability,
342+
// create a static instead
343343
344344
// or:
345345
struct C { a: Cell<usize> }

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
663663
self.add(Qualif::NOT_CONST);
664664
if self.mode != Mode::Fn {
665665
span_err!(self.tcx.sess, self.span, E0492,
666-
"cannot borrow a constant which contains \
666+
"cannot borrow a constant which may contain \
667667
interior mutability, create a static instead");
668668
}
669669
}

src/libstd/panic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub trait UnwindSafe {}
112112
/// This is a "helper marker trait" used to provide impl blocks for the
113113
/// `UnwindSafe` trait, for more information see that documentation.
114114
#[stable(feature = "catch_unwind", since = "1.9.0")]
115-
#[rustc_on_unimplemented = "the type {Self} contains interior mutability \
115+
#[rustc_on_unimplemented = "the type {Self} may contain interior mutability \
116116
and a reference may not be safely transferrable \
117117
across a catch_unwind boundary"]
118118
pub trait RefUnwindSafe {}

src/test/compile-fail/issue-17718-const-borrow.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ use std::cell::UnsafeCell;
1414

1515
const A: UnsafeCell<usize> = UnsafeCell::new(1);
1616
const B: &'static UnsafeCell<usize> = &A;
17-
//~^ ERROR: cannot borrow a constant which contains interior mutability
17+
//~^ ERROR: cannot borrow a constant which may contain interior mutability
1818

1919
struct C { a: UnsafeCell<usize> }
2020
const D: C = C { a: UnsafeCell::new(1) };
2121
const E: &'static UnsafeCell<usize> = &D.a;
22-
//~^ ERROR: cannot borrow a constant which contains interior mutability
22+
//~^ ERROR: cannot borrow a constant which may contain interior mutability
2323
const F: &'static C = &D;
24-
//~^ ERROR: cannot borrow a constant which contains interior mutability
24+
//~^ ERROR: cannot borrow a constant which may contain interior mutability
2525

2626
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2016 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.
4+
//
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.
10+
11+
use std::cell::Cell;
12+
use std::panic::catch_unwind;
13+
fn main() {
14+
let mut x = Cell::new(22);
15+
catch_unwind(|| { x.set(23); });
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0277]: the trait bound `std::cell::UnsafeCell<i32>: std::panic::RefUnwindSafe` is not satisfied in `std::cell::Cell<i32>`
2+
--> $DIR/interior-mutability.rs:15:5
3+
|
4+
15 | catch_unwind(|| { x.set(23); });
5+
| ^^^^^^^^^^^^ the type std::cell::UnsafeCell<i32> may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
6+
|
7+
= help: within `std::cell::Cell<i32>`, the trait `std::panic::RefUnwindSafe` is not implemented for `std::cell::UnsafeCell<i32>`
8+
= note: required because it appears within the type `std::cell::Cell<i32>`
9+
= note: required because of the requirements on the impl of `std::panic::UnwindSafe` for `&std::cell::Cell<i32>`
10+
= note: required because it appears within the type `[closure@$DIR/interior-mutability.rs:15:18: 15:35 x:&std::cell::Cell<i32>]`
11+
= note: required by `std::panic::catch_unwind`
12+
13+
error: aborting due to previous error(s)
14+

0 commit comments

Comments
 (0)