From d2bdaa8deb2728fad7de31a2077ee68a861c032a Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Tue, 26 Nov 2019 12:10:44 -0800 Subject: [PATCH] Also test shared borrows of `Cell` for good errors --- src/test/ui/consts/const-multi-ref.rs | 12 +++++++++++- src/test/ui/consts/const-multi-ref.stderr | 11 +++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/test/ui/consts/const-multi-ref.rs b/src/test/ui/consts/const-multi-ref.rs index 45269042328..5e2be0d4f3f 100644 --- a/src/test/ui/consts/const-multi-ref.rs +++ b/src/test/ui/consts/const-multi-ref.rs @@ -1,7 +1,7 @@ // Ensure that we point the user to the erroneous borrow but not to any subsequent borrows of that // initial one. -const _X: i32 = { +const _: i32 = { let mut a = 5; let p = &mut a; //~ ERROR references in constants may only refer to immutable values @@ -11,4 +11,14 @@ const _X: i32 = { ***ppp }; +const _: std::cell::Cell = { + let mut a = std::cell::Cell::new(5); + let p = &a; //~ ERROR cannot borrow a constant which may contain interior mutability + + let reborrow = {p}; + let pp = &reborrow; + let ppp = &pp; + a +}; + fn main() {} diff --git a/src/test/ui/consts/const-multi-ref.stderr b/src/test/ui/consts/const-multi-ref.stderr index 50533b0b1cc..ed3837e9c9e 100644 --- a/src/test/ui/consts/const-multi-ref.stderr +++ b/src/test/ui/consts/const-multi-ref.stderr @@ -4,6 +4,13 @@ error[E0017]: references in constants may only refer to immutable values LL | let p = &mut a; | ^^^^^^ constants require immutable values -error: aborting due to previous error +error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead + --> $DIR/const-multi-ref.rs:16:13 + | +LL | let p = &a; + | ^^ -For more information about this error, try `rustc --explain E0017`. +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0017, E0492. +For more information about an error, try `rustc --explain E0017`.