Skip to content

Commit c01f38b

Browse files
committed
yaaay
1 parent df1201a commit c01f38b

File tree

5 files changed

+71
-4
lines changed

5 files changed

+71
-4
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
/book
2+
*.js
3+
*.css

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,5 @@ This repository is also an mdbook project. You can view and build it using the
6060
following command.
6161

6262
```
63-
mdbook serve
63+
mdbook-skill-tree install && mdbook serve
6464
```

SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- [❗ Constraining generic parameters](./design/constraining-generic-parameters.md)
2222
- [❗⚖️🔄 Generic const parameter types](./design/generic-const-param-types.md)
2323
- [❗🔙 Leaking implementation details](./design/leaking-implementation-details.md)
24+
- [❗🔙 Opaque and transparent associated constants](./design/opaque-and-transparent-assoc-consts.md)
2425
- [❗🔙 Restrictions on const evaluation](./design/const-eval-requirements.md)
2526
- [❗🔙 ⚖️ Structural equality](./design/structural-equality.md)
2627
- [❗🔙 🔄 Unused substs](./design/unused-substs.md)

design/README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,25 @@ This feature interacts with the following topics:
7070
## Generic constants in the type system ([`generic_const_exprs`](https://github.com/rust-lang/rust/issues/76560))
7171

7272
- [❗🔙 🔄 Unused substs](./unused-substs.md)
73+
- ❗ Anonymous constants in binders
7374
- ❗🔄 Silence evaluation errors during selection
7475
- ❗🔄 Self referential where clauses
7576
- ❗🔄 Evaluation without first checking where-clauses
7677
- ❔ Partially evaluating anonymous constants
7778
### Unifying generic constants
7879

7980
- [❗🔙 Restrictions on const evaluation](./const-eval-requirements.html)
80-
- [❗🔙 Leaking implementation details](./leaking-implementation-details.md)
81-
- Opaque and transparent associated constants
81+
- [❗🔙 Leaking implementation details](./leaking-implementation-details.html)
82+
- [❗🔙 Opaque and transparent associated constants](./opaque-and-transparent-assoc-consts.html)
8283
- ❗ Splitting constants during unification
8384
- [❗ Constraining generic parameters](./constraining-generic-parameters.html)
8485
- ❔⚖️ Extending unification logic
8586

8687
### ❔ Const evaluatable bounds
8788

8889
- ❗ Using subtrees to fulfill bounds
89-
- ❗❔ Discarding bounds and soundness
90+
- ❔ Discarding bounds and soundness
91+
- ❔ Compile time conditionals
9092
- ❔⚖️ Improving const evaluatability checks
9193

9294
ALL OF THIS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# ❗🔙 Opaque and transparent associated constants
2+
3+
As we want to be able to use associated constants in types, we have to look into them for unification.
4+
5+
```rust
6+
trait Encode {
7+
const LENGTH: usize;
8+
fn encode(&self) -> [u8; Self::LENGTH];
9+
}
10+
11+
struct Dummy<const N: usize>;
12+
impl<const N: usize> for Dummy<N> {
13+
const LENGTH: usize = N + 1;
14+
fn encode(&self) -> [u8; Self::LENGTH] {
15+
[0; N + 1]
16+
}
17+
}
18+
```
19+
20+
For this to compile, we have to unify `Self::LENGTH` with `N + 1`. That means that we have to look into
21+
this associated constant, i.e. we have to treat it as transparent.
22+
23+
Treating all associated constants causes some concerns however.
24+
25+
#### Additional stability requirements
26+
27+
Consider a library which has a public associated constant
28+
29+
```rust
30+
use std::mem::size_of;
31+
trait WithAssoc {
32+
const ASSOC: usize;
33+
}
34+
35+
struct MyType<T>(Vec<T>);
36+
impl<T> WithAssoc for MyType<T> {
37+
const ASSOC: usize = size_of::<Vec<T>>();
38+
}
39+
```
40+
41+
In a new minor version, they've cleaned up their code and changed the associated constant to `size_of::<Self>()`.
42+
Without transparent associated constant, this can't break anything. If `ASSOC` is treated transparently,
43+
this change is now theoretically breaking.
44+
45+
#### Overly complex associated constants
46+
47+
Our abstract representation used during unification will not be able to represent arbitrary user code.
48+
This means that there will be some - potentially already existing - associated constants we cannot convert
49+
to this abstract representation.
50+
51+
It is unclear how much code is affected by this and what the right solution for this would be.
52+
We should probably either silently keep these associated constants opaque or keep them opaque and emit a warning.
53+
54+
### Keeping some associated constants opaque
55+
56+
Still undecided how this should be handled. We can make transparent constants opt-in, potentially making associated constants
57+
used in some other type of the trait impl transparent by default. We could also make constants transparent by default
58+
and keep constants for which the conversion failed opaque, either emitting a warning or just silently.
59+
60+
## Status
61+
62+
**Blocked**: We should wait for `feature(generic_const_exprs)` to be closer to stable before thinking about this.

0 commit comments

Comments
 (0)