Skip to content

Commit 36907fc

Browse files
committed
Also put comparing raw pointers behind a feature gate
1 parent 1fc7580 commit 36907fc

File tree

7 files changed

+22
-46
lines changed

7 files changed

+22
-46
lines changed

src/librustc_mir/diagnostics.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,36 +1113,6 @@ fn main() {
11131113
```
11141114
"##,
11151115

1116-
E0395: r##"
1117-
The value assigned to a constant scalar must be known at compile time,
1118-
which is not the case when comparing raw pointers.
1119-
1120-
Erroneous code example:
1121-
1122-
```compile_fail,E0395
1123-
static FOO: i32 = 42;
1124-
static BAR: i32 = 42;
1125-
1126-
static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) };
1127-
// error: raw pointers cannot be compared in statics!
1128-
```
1129-
1130-
The address assigned by the linker to `FOO` and `BAR` may or may not
1131-
be identical, so the value of `BAZ` can't be determined.
1132-
1133-
If you want to do the comparison, please do it at run-time.
1134-
1135-
For example:
1136-
1137-
```
1138-
static FOO: i32 = 42;
1139-
static BAR: i32 = 42;
1140-
1141-
let baz: bool = { (&FOO as *const i32) == (&BAR as *const i32) };
1142-
// baz isn't a constant expression so it's ok
1143-
```
1144-
"##,
1145-
11461116
E0161: r##"
11471117
A value was moved. However, its size was not known at compile time, and only
11481118
values of a known size can be moved.

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -752,14 +752,13 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
752752

753753
self.add(Qualif::NOT_CONST);
754754
if self.mode != Mode::Fn {
755-
struct_span_err!(
756-
self.tcx.sess, self.span, E0395,
757-
"raw pointers cannot be compared in {}s",
758-
self.mode)
759-
.span_label(
755+
emit_feature_err(
756+
&self.tcx.sess.parse_sess,
757+
"const_compare_raw_pointers",
760758
self.span,
761-
"comparing raw pointers in static")
762-
.emit();
759+
GateIssue::Language,
760+
&format!("comparing raw pointers inside {}", self.mode),
761+
);
763762
}
764763
}
765764
}

src/libsyntax/feature_gate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ declare_features! (
225225
// Allows dereferencing raw pointers during const eval
226226
(active, const_raw_ptr_deref, "1.27.0", Some(51911), None),
227227

228+
// Allows comparing raw pointers during const eval
229+
(active, const_compare_raw_pointers, "1.27.0", Some(53020), None),
230+
228231
// Allows using #[prelude_import] on glob `use` items.
229232
//
230233
// rustc internal

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
static FOO: i32 = 42;
1212
static BAR: i32 = 42;
1313

14-
static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) }; //~ ERROR E0395
14+
static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) }; //~ ERROR issue #53020
1515
fn main() {
1616
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
error[E0395]: raw pointers cannot be compared in statics
1+
error[E0658]: comparing raw pointers inside static (see issue #53020)
22
--> $DIR/E0395.rs:14:22
33
|
4-
LL | static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) }; //~ ERROR E0395
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comparing raw pointers in static
4+
LL | static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) }; //~ ERROR issue #53020
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: add #![feature(const_compare_raw_pointers)] to the crate attributes to enable
68

79
error: aborting due to previous error
810

9-
For more information about this error, try `rustc --explain E0395`.
11+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/issue-25826.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
fn id<T>(t: T) -> T { t }
1212
fn main() {
1313
const A: bool = id::<u8> as *const () < id::<u16> as *const ();
14-
//~^ ERROR raw pointers cannot be compared in constants [E0395]
14+
//~^ ERROR comparing raw pointers inside constant
1515
println!("{}", A);
1616
}

src/test/ui/issue-25826.stderr

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
error[E0395]: raw pointers cannot be compared in constants
1+
error[E0658]: comparing raw pointers inside constant (see issue #53020)
22
--> $DIR/issue-25826.rs:13:21
33
|
44
LL | const A: bool = id::<u8> as *const () < id::<u16> as *const ();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comparing raw pointers in static
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: add #![feature(const_compare_raw_pointers)] to the crate attributes to enable
68

79
error: aborting due to previous error
810

9-
For more information about this error, try `rustc --explain E0395`.
11+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)