Skip to content

Commit b7e41d9

Browse files
Add E0396 error explanation
1 parent 47c3dc2 commit b7e41d9

File tree

2 files changed

+52
-54
lines changed

2 files changed

+52
-54
lines changed

src/librustc/diagnostics.rs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,57 @@ From [RFC 246]:
984984
[RFC 246]: https://github.com/rust-lang/rfcs/pull/246
985985
"##,
986986

987+
E0395: r##"
988+
The value assigned to a constant expression must be known at compile time,
989+
which is not the case when comparing raw pointers. Erroneous code example:
990+
991+
```
992+
static foo: i32 = 42;
993+
static bar: i32 = 43;
994+
995+
static baz: bool = { (&foo as *const i32) == (&bar as *const i32) };
996+
// error: raw pointers cannot be compared in statics!
997+
```
998+
999+
Please check that the result of the comparison can be determined at compile time
1000+
or isn't assigned to a constant expression. Example:
1001+
1002+
```
1003+
static foo: i32 = 42;
1004+
static bar: i32 = 43;
1005+
1006+
let baz: bool = { (&foo as *const i32) == (&bar as *const i32) };
1007+
// baz isn't a constant expression so it's ok
1008+
```
1009+
"##,
1010+
1011+
E0396: r##"
1012+
The value assigned to a constant expression must be known at compile time,
1013+
which is not the case when dereferencing raw pointers. Erroneous code
1014+
example:
1015+
1016+
```
1017+
const foo: i32 = 42;
1018+
const baz: *const i32 = (&foo as *const i32);
1019+
1020+
const deref: i32 = *baz;
1021+
// error: raw pointers cannot be dereferenced in constants!
1022+
```
1023+
1024+
To fix this error, please do not assign this value to a constant expression.
1025+
Example:
1026+
1027+
```
1028+
const foo: i32 = 42;
1029+
const baz: *const i32 = (&foo as *const i32);
1030+
1031+
unsafe { let deref: i32 = *baz; }
1032+
// baz isn't a constant expression so it's ok
1033+
```
1034+
1035+
You'll also note that this assignment must be done in an unsafe block!
1036+
"##,
1037+
9871038
E0397: r##"
9881039
It is not allowed for a mutable static to allocate or have destructors. For
9891040
example:
@@ -1039,7 +1090,5 @@ register_diagnostics! {
10391090
E0314, // closure outlives stack frame
10401091
E0315, // cannot invoke closure outside of its lifetime
10411092
E0316, // nested quantification of lifetimes
1042-
E0370, // discriminant overflow
1043-
E0395, // pointer comparison in const-expr
1044-
E0396 // pointer dereference in const-expr
1093+
E0370 // discriminant overflow
10451094
}

src/librustc_typeck/diagnostics.rs

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,57 +1538,6 @@ For more information see the [opt-in builtin traits RFC](https://github.com/rust
15381538
-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md).
15391539
"##
15401540

1541-
E0395: r##"
1542-
The value assigned to a constant expression must be known at compile time,
1543-
which is not the case when comparing raw pointers. Erroneous code example:
1544-
1545-
```
1546-
static foo: i32 = 42;
1547-
static bar: i32 = 43;
1548-
1549-
static baz: bool = { (&foo as *const i32) == (&bar as *const i32) };
1550-
// error: raw pointers cannot be compared in statics!
1551-
```
1552-
1553-
To fix this error, please not assign this value to a constant expression.
1554-
Example:
1555-
1556-
```
1557-
static foo: i32 = 42;
1558-
static bar: i32 = 43;
1559-
1560-
let baz: bool = { (&foo as *const i32) == (&bar as *const i32) };
1561-
// baz isn't a constant expression so it's ok
1562-
```
1563-
"##,
1564-
1565-
E0396: r##"
1566-
The value assigned to a constant expression must be known at compile time,
1567-
which is not the case when dereferencing raw pointers. Erroneous code
1568-
example:
1569-
1570-
```
1571-
const foo: i32 = 42;
1572-
const baz: *const i32 = (&foo as *const i32);
1573-
1574-
const deref: i32 = *baz;
1575-
// error: raw pointers cannot be dereferenced in constants!
1576-
```
1577-
1578-
To fix this error, please not assign this value to a constant expression.
1579-
Example:
1580-
1581-
```
1582-
const foo: i32 = 42;
1583-
const baz: *const i32 = (&foo as *const i32);
1584-
1585-
unsafe { let deref: i32 = *baz; }
1586-
// baz isn't a constant expression so it's ok
1587-
```
1588-
1589-
You'll also note that this assignation must be done in an unsafe block!
1590-
"##
1591-
15921541
}
15931542

15941543

0 commit comments

Comments
 (0)