Skip to content

Commit 47c3dc2

Browse files
Add E0395 error explanation
1 parent 4c2587c commit 47c3dc2

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/librustc_typeck/diagnostics.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,8 +1538,60 @@ 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+
15411592
}
15421593

1594+
15431595
register_diagnostics! {
15441596
E0068,
15451597
E0074,

0 commit comments

Comments
 (0)