Auto merge of #75585 - RalfJung:demotion, r=oli-obk
Do not promote &mut of a non-ZST ever Since ~pre-1.0~ 1.36, we have accepted code like this: ```rust static mut TEST: &'static mut [i32] = { let x = &mut [1,2,3]; x }; ``` I tracked it back to https://github.com/rust-lang/rust/pull/21744, but unfortunately could not find any discussion or RFC that would explain why we thought this was a good idea. And it's not, it breaks all sorts of things -- see https://github.com/rust-lang/rust/issues/75556. To fix https://github.com/rust-lang/rust/issues/75556, we have to stop promoting non-ZST mutable references no matter the context, which is what this PR does. It's a breaking change. Notice that this still works, since it does not rely on promotion: ```rust static mut TEST: &'static mut [i32] = &mut [0,1,2]; ``` Cc `@rust-lang/wg-const-eval`
This commit is contained in:
commit
e82584a77d
4 changed files with 52 additions and 16 deletions
|
@ -364,15 +364,7 @@ impl<'tcx> Validator<'_, 'tcx> {
|
|||
// In theory, any zero-sized value could be borrowed
|
||||
// mutably without consequences. However, only &mut []
|
||||
// is allowed right now, and only in functions.
|
||||
if self.const_kind
|
||||
== Some(hir::ConstContext::Static(hir::Mutability::Mut))
|
||||
{
|
||||
// Inside a `static mut`, &mut [...] is also allowed.
|
||||
match ty.kind() {
|
||||
ty::Array(..) | ty::Slice(_) => {}
|
||||
_ => return Err(Unpromotable),
|
||||
}
|
||||
} else if let ty::Array(_, len) = ty.kind() {
|
||||
if let ty::Array(_, len) = ty.kind() {
|
||||
// FIXME(eddyb) the `self.is_non_const_fn` condition
|
||||
// seems unnecessary, given that this is merely a ZST.
|
||||
match len.try_eval_usize(self.tcx, self.param_env) {
|
||||
|
@ -673,13 +665,7 @@ impl<'tcx> Validator<'_, 'tcx> {
|
|||
// In theory, any zero-sized value could be borrowed
|
||||
// mutably without consequences. However, only &mut []
|
||||
// is allowed right now, and only in functions.
|
||||
if self.const_kind == Some(hir::ConstContext::Static(hir::Mutability::Mut)) {
|
||||
// Inside a `static mut`, &mut [...] is also allowed.
|
||||
match ty.kind() {
|
||||
ty::Array(..) | ty::Slice(_) => {}
|
||||
_ => return Err(Unpromotable),
|
||||
}
|
||||
} else if let ty::Array(_, len) = ty.kind() {
|
||||
if let ty::Array(_, len) = ty.kind() {
|
||||
// FIXME(eddyb): We only return `Unpromotable` for `&mut []` inside a
|
||||
// const context which seems unnecessary given that this is merely a ZST.
|
||||
match len.try_eval_usize(self.tcx, self.param_env) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue