Skip to content

Commit e9e27e6

Browse files
committed
Auto merge of #54715 - oli-obk:nll_deref_promotion, r=RalfJung
Fix #54224 (const promotion regression) r? @eddyb
2 parents 849a0e9 + 76f8a90 commit e9e27e6

File tree

3 files changed

+59
-14
lines changed

3 files changed

+59
-14
lines changed

src/librustc_mir/transform/qualify_consts.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -495,20 +495,22 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
495495
this.super_place(place, context, location);
496496
match proj.elem {
497497
ProjectionElem::Deref => {
498-
if let Mode::Fn = this.mode {
499-
this.add(Qualif::NOT_CONST);
500-
} else {
501-
let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
502-
if let ty::RawPtr(_) = base_ty.sty {
503-
if !this.tcx.sess.features_untracked().const_raw_ptr_deref {
504-
emit_feature_err(
505-
&this.tcx.sess.parse_sess, "const_raw_ptr_deref",
506-
this.span, GateIssue::Language,
507-
&format!(
508-
"dereferencing raw pointers in {}s is unstable",
509-
this.mode,
510-
),
511-
);
498+
this.add(Qualif::NOT_CONST);
499+
let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
500+
match this.mode {
501+
Mode::Fn => {},
502+
_ => {
503+
if let ty::RawPtr(_) = base_ty.sty {
504+
if !this.tcx.sess.features_untracked().const_raw_ptr_deref {
505+
emit_feature_err(
506+
&this.tcx.sess.parse_sess, "const_raw_ptr_deref",
507+
this.span, GateIssue::Language,
508+
&format!(
509+
"dereferencing raw pointers in {}s is unstable",
510+
this.mode,
511+
),
512+
);
513+
}
512514
}
513515
}
514516
}
@@ -732,8 +734,11 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
732734
(CastTy::Ptr(_), CastTy::Int(_)) |
733735
(CastTy::FnPtr, CastTy::Int(_)) => {
734736
if let Mode::Fn = self.mode {
737+
// in normal functions, mark such casts as not promotable
735738
self.add(Qualif::NOT_CONST);
736739
} else if !self.tcx.sess.features_untracked().const_raw_ptr_to_usize_cast {
740+
// in const fn and constants require the feature gate
741+
// FIXME: make it unsafe inside const fn and constants
737742
emit_feature_err(
738743
&self.tcx.sess.parse_sess, "const_raw_ptr_to_usize_cast",
739744
self.span, GateIssue::Language,
@@ -756,8 +761,11 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
756761
op == BinOp::Offset);
757762

758763
if let Mode::Fn = self.mode {
764+
// raw pointer operations are not allowed inside promoteds
759765
self.add(Qualif::NOT_CONST);
760766
} else if !self.tcx.sess.features_untracked().const_compare_raw_pointers {
767+
// require the feature gate inside constants and const fn
768+
// FIXME: make it unsafe to use these operations
761769
emit_feature_err(
762770
&self.tcx.sess.parse_sess,
763771
"const_compare_raw_pointers",

src/test/ui/consts/issue-54224.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(nll)]
2+
3+
const FOO: Option<&[[u8; 3]]> = Some(&[*b"foo"]); //~ ERROR temporary value dropped while borrowed
4+
5+
use std::borrow::Cow;
6+
7+
pub const X: [u8; 3] = *b"ABC";
8+
pub const Y: Cow<'static, [ [u8; 3] ]> = Cow::Borrowed(&[X]);
9+
10+
11+
pub const Z: Cow<'static, [ [u8; 3] ]> = Cow::Borrowed(&[*b"ABC"]);
12+
//~^ ERROR temporary value dropped while borrowed
13+
14+
fn main() {}

src/test/ui/consts/issue-54224.stderr

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0716]: temporary value dropped while borrowed
2+
--> $DIR/issue-54224.rs:3:39
3+
|
4+
LL | const FOO: Option<&[[u8; 3]]> = Some(&[*b"foo"]); //~ ERROR temporary value dropped while borrowed
5+
| ^^^^^^^^^- temporary value is freed at the end of this statement
6+
| |
7+
| creates a temporary which is freed while still in use
8+
|
9+
= note: borrowed value must be valid for the static lifetime...
10+
11+
error[E0716]: temporary value dropped while borrowed
12+
--> $DIR/issue-54224.rs:11:57
13+
|
14+
LL | pub const Z: Cow<'static, [ [u8; 3] ]> = Cow::Borrowed(&[*b"ABC"]);
15+
| ^^^^^^^^^- temporary value is freed at the end of this statement
16+
| |
17+
| creates a temporary which is freed while still in use
18+
|
19+
= note: borrowed value must be valid for the static lifetime...
20+
21+
error: aborting due to 2 previous errors
22+
23+
For more information about this error, try `rustc --explain E0716`.

0 commit comments

Comments
 (0)