Skip to content

Commit 155126b

Browse files
authored
update section for type system constants (rust-lang#1329)
* update section for type system constants * Update src/constants.md
1 parent 0e4b961 commit 155126b

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/constants.md

+29-20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,33 @@ or a generic parameter, e.g. `[u8; N]`, converting a constant to its [`ty::Const
1616
returns an unevaluated constant. Even fully concrete constants which do not depend on
1717
generic parameters are not evaluated right away.
1818

19+
Anonymous constants are typechecked separately from their containing item, e.g.
20+
```rust
21+
fn foo<const N: usize>() -> [u8; N + 1] {
22+
[0; N + 1]
23+
}
24+
```
25+
is treated as
26+
```rust
27+
const ANON_CONST_1<const N: usize> = N + 1;
28+
const ANON_CONST_2<const N: usize> = N + 1;
29+
fn foo<const N: usize>() -> [u8; ANON_CONST_1::<N>] {
30+
[0; ANON_CONST_2::<N>]
31+
}
32+
```
33+
34+
### Unifying constants
35+
36+
For the compiler, `ANON_CONST_1` and `ANON_CONST_2` are completely different, so
37+
we have to somehow look into unevaluated constants to check whether they should
38+
unify.
39+
40+
For this we use [InferCtxt::try_unify_abstract_consts](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_infer/infer/struct.InferCtxt.html#method.try_unify_abstract_consts).
41+
This builds a custom AST for the two inputs from their THIR. This is then used for
42+
the actual comparison.
43+
44+
### Lazy normalization for constants
45+
1946
We do not eagerly evaluate constant as they can be used in the `where`-clauses of their
2047
parent item, for example:
2148

@@ -32,7 +59,7 @@ its parents caller bounds, but is also part of another bound itself.
3259
If we were to eagerly evaluate this constant while computing its parents bounds
3360
this would cause a query cycle.
3461

35-
### Generic arguments of anonymous constants
62+
### Unused generic arguments of anonymous constants
3663

3764
Anonymous constants inherit the generic parameters of their parent, which is
3865
why the array length in `foo<const N: usize>() -> [u8; N + 1]` can use `N`.
@@ -41,25 +68,7 @@ Without any manual adjustments, this causes us to include parameters even if
4168
the constant doesn't use them in any way. This can cause
4269
[some interesting errors][pcg-unused-substs] and breaks some already stable code.
4370

44-
To deal with this, we intend to look at the generic parameters explicitly mentioned
45-
by the constants and then search the predicates of its parents to figure out which
46-
of the other generic parameters are reachable by our constant.
47-
48-
**TODO**: Expand this section once the parameter filtering is implemented.
49-
50-
As constants can be part of their parents `where`-clauses, we mention unevaluated
51-
constants in their parents predicates. It is therefore necessary to mention unevaluated
52-
constants before we have computed the generic parameters
53-
available to these constants.
54-
55-
To do this unevaluated constants start out with [`substs_`] being `None` while assuming
56-
that their generic arguments could be arbitrary generic parameters.
57-
When first accessing the generic arguments of an unevaluated constants, we then replace
58-
`substs_` with the actual default arguments of a constants, which are the generic parameters
59-
of their parent we assume to be used by this constant.
60-
6171
[`ty::Const`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Const.html
6272
[`ty::ConstKind`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.ConstKind.html
6373
[`ty::TyKind`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.TyKind.html
64-
[pcg-unused-substs]: https://github.com/rust-lang/project-const-generics/blob/master/design-docs/anon-const-substs.md#unused-substs
65-
[`substs_`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/consts/kind/struct.Unevaluated.html#structfield.substs_
74+
[pcg-unused-substs]: https://github.com/rust-lang/project-const-generics/blob/master/design-docs/anon-const-substs.md#unused-substs

0 commit comments

Comments
 (0)