Skip to content

Commit f829a53

Browse files
Add back the E0018 error code long explanation
1 parent 852d7da commit f829a53

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/librustc_error_codes/error_codes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ E0014: include_str!("./error_codes/E0014.md"),
2323
E0015: include_str!("./error_codes/E0015.md"),
2424
E0016: include_str!("./error_codes/E0016.md"),
2525
E0017: include_str!("./error_codes/E0017.md"),
26+
E0018: include_str!("./error_codes/E0018.md"),
2627
E0019: include_str!("./error_codes/E0019.md"),
2728
E0023: include_str!("./error_codes/E0023.md"),
2829
E0025: include_str!("./error_codes/E0025.md"),
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
3+
The value of static and constant integers must be known at compile time. You
4+
can't cast a pointer to an integer because the address of a pointer can vary.
5+
6+
For example, if you write:
7+
8+
```compile_fail,E0658
9+
static MY_STATIC: u32 = 42;
10+
static MY_STATIC_ADDR: usize = &MY_STATIC as *const _ as usize;
11+
static WHAT: usize = (MY_STATIC_ADDR^17) + MY_STATIC_ADDR;
12+
```
13+
14+
Then `MY_STATIC_ADDR` would contain the address of `MY_STATIC`. However,
15+
the address can change when the program is linked, as well as change
16+
between different executions due to ASLR, and many linkers would
17+
not be able to calculate the value of `WHAT`.
18+
19+
On the other hand, static and constant pointers can point either to
20+
a known numeric address or to the address of a symbol.
21+
22+
```
23+
static MY_STATIC: u32 = 42;
24+
static MY_STATIC_ADDR: &'static u32 = &MY_STATIC;
25+
const CONST_ADDR: *const u8 = 0x5f3759df as *const u8;
26+
```
27+
28+
This does not pose a problem by itself because they can't be
29+
accessed directly.

0 commit comments

Comments
 (0)