Skip to content

Commit a091a65

Browse files
committed
Make sure the feature gate actually works and never allows promoting these operations
1 parent aa0884e commit a091a65

File tree

6 files changed

+138
-20
lines changed

6 files changed

+138
-20
lines changed

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -491,20 +491,21 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
491491
this.super_place(place, context, location);
492492
match proj.elem {
493493
ProjectionElem::Deref => {
494-
this.add(Qualif::NOT_CONST);
495-
496-
let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
497-
if let ty::TyRawPtr(_) = base_ty.sty {
498-
if this.mode != Mode::Fn &&
499-
!this.tcx.sess.features_untracked().const_raw_ptr_deref {
500-
emit_feature_err(
501-
&this.tcx.sess.parse_sess, "const_raw_ptr_deref",
502-
this.span, GateIssue::Language,
503-
&format!(
504-
"dereferencing raw pointers in {}s is unstable",
505-
this.mode,
506-
),
507-
);
494+
if let Mode::Fn = this.mode {
495+
this.add(Qualif::NOT_CONST);
496+
} else {
497+
let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
498+
if let ty::TyRawPtr(_) = base_ty.sty {
499+
if !this.tcx.sess.features_untracked().const_raw_ptr_deref {
500+
emit_feature_err(
501+
&this.tcx.sess.parse_sess, "const_raw_ptr_deref",
502+
this.span, GateIssue::Language,
503+
&format!(
504+
"dereferencing raw pointers in {}s is unstable",
505+
this.mode,
506+
),
507+
);
508+
}
508509
}
509510
}
510511
}
@@ -726,9 +727,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
726727
match (cast_in, cast_out) {
727728
(CastTy::Ptr(_), CastTy::Int(_)) |
728729
(CastTy::FnPtr, CastTy::Int(_)) => {
729-
self.add(Qualif::NOT_CONST);
730-
if self.mode != Mode::Fn &&
731-
!self.tcx.sess.features_untracked().const_raw_ptr_to_usize_cast {
730+
if let Mode::Fn = self.mode {
731+
self.add(Qualif::NOT_CONST);
732+
} else if !self.tcx.sess.features_untracked().const_raw_ptr_to_usize_cast {
732733
emit_feature_err(
733734
&self.tcx.sess.parse_sess, "const_raw_ptr_to_usize_cast",
734735
self.span, GateIssue::Language,
@@ -750,8 +751,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
750751
op == BinOp::Ge || op == BinOp::Gt ||
751752
op == BinOp::Offset);
752753

753-
self.add(Qualif::NOT_CONST);
754-
if self.mode != Mode::Fn {
754+
if let Mode::Fn = self.mode {
755+
self.add(Qualif::NOT_CONST);
756+
} else if !self.tcx.sess.features_untracked().const_compare_raw_pointers {
755757
emit_feature_err(
756758
&self.tcx.sess.parse_sess,
757759
"const_compare_raw_pointers",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2018 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+
#![feature(const_raw_ptr_to_usize_cast, const_compare_raw_pointers, const_raw_ptr_deref)]
12+
13+
fn main() {}
14+
15+
// unconst and bad, will thus error in miri
16+
const X: bool = &1 as *const i32 == &2 as *const i32; //~ ERROR cannot be used
17+
// unconst and fine
18+
const X2: bool = 42 as *const i32 == 43 as *const i32;
19+
// unconst and fine
20+
const Y: usize = 42usize as *const i32 as usize + 1;
21+
// unconst and bad, will thus error in miri
22+
const Y2: usize = &1 as *const i32 as usize + 1; //~ ERROR cannot be used
23+
// unconst and fine
24+
const Z: i32 = unsafe { *(&1 as *const i32) };
25+
// unconst and bad, will thus error in miri
26+
const Z2: i32 = unsafe { *(42 as *const i32) }; //~ ERROR cannot be used
27+
const Z3: i32 = unsafe { *(44 as *const i32) }; //~ ERROR cannot be used
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error: this constant cannot be used
2+
--> $DIR/const_raw_ptr_ops.rs:16:1
3+
|
4+
LL | const X: bool = &1 as *const i32 == &2 as *const i32; //~ ERROR cannot be used
5+
| ^^^^^^^^^^^^^^^^------------------------------------^
6+
| |
7+
| "pointer arithmetic or comparison" needs an rfc before being allowed inside constants
8+
|
9+
= note: #[deny(const_err)] on by default
10+
11+
error: this constant cannot be used
12+
--> $DIR/const_raw_ptr_ops.rs:22:1
13+
|
14+
LL | const Y2: usize = &1 as *const i32 as usize + 1; //~ ERROR cannot be used
15+
| ^^^^^^^^^^^^^^^^^^-----------------------------^
16+
| |
17+
| "pointer arithmetic or comparison" needs an rfc before being allowed inside constants
18+
19+
error: this constant cannot be used
20+
--> $DIR/const_raw_ptr_ops.rs:26:1
21+
|
22+
LL | const Z2: i32 = unsafe { *(42 as *const i32) }; //~ ERROR cannot be used
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^-------------------^^^
24+
| |
25+
| tried to access memory with alignment 2, but alignment 4 is required
26+
27+
error: this constant cannot be used
28+
--> $DIR/const_raw_ptr_ops.rs:27:1
29+
|
30+
LL | const Z3: i32 = unsafe { *(44 as *const i32) }; //~ ERROR cannot be used
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^-------------------^^^
32+
| |
33+
| a memory access tried to interpret some bytes as a pointer
34+
35+
error: aborting due to 4 previous errors
36+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2018 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+
#![feature(const_raw_ptr_to_usize_cast, const_compare_raw_pointers, const_raw_ptr_deref)]
12+
13+
fn main() {
14+
let x: &'static bool = &(42 as *const i32 == 43 as *const i32);
15+
//~^ ERROR does not live long enough
16+
let y: &'static usize = &(&1 as *const i32 as usize + 1); //~ ERROR does not live long enough
17+
let z: &'static i32 = &(unsafe { *(42 as *const i32) }); //~ ERROR does not live long enough
18+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0597]: borrowed value does not live long enough
2+
--> $DIR/promoted_raw_ptr_ops.rs:14:29
3+
|
4+
LL | let x: &'static bool = &(42 as *const i32 == 43 as *const i32);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
6+
...
7+
LL | }
8+
| - temporary value only lives until here
9+
|
10+
= note: borrowed value must be valid for the static lifetime...
11+
12+
error[E0597]: borrowed value does not live long enough
13+
--> $DIR/promoted_raw_ptr_ops.rs:16:30
14+
|
15+
LL | let y: &'static usize = &(&1 as *const i32 as usize + 1); //~ ERROR does not live long enough
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
17+
LL | let z: &'static i32 = &(unsafe { *(42 as *const i32) }); //~ ERROR does not live long enough
18+
LL | }
19+
| - temporary value only lives until here
20+
|
21+
= note: borrowed value must be valid for the static lifetime...
22+
23+
error[E0597]: borrowed value does not live long enough
24+
--> $DIR/promoted_raw_ptr_ops.rs:17:28
25+
|
26+
LL | let z: &'static i32 = &(unsafe { *(42 as *const i32) }); //~ ERROR does not live long enough
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
28+
LL | }
29+
| - temporary value only lives until here
30+
|
31+
= note: borrowed value must be valid for the static lifetime...
32+
33+
error: aborting due to 3 previous errors
34+
35+
For more information about this error, try `rustc --explain E0597`.

src/test/ui/error-codes/E0395.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: comparing raw pointers inside static (see issue #53020)
2-
--> $DIR/E0395.rs:14:22
2+
--> $DIR/E0395.rs:16:22
33
|
44
LL | static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) }; //~ ERROR issue #53020
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)