const generics general stuff idk
Where do const expressions appear / come from
- Types
- Array
[T; C]
โ anonymous constants
- Type parameter
S<{C}>
- Predicates
- Definitions
- Expressions
const { expr }
โ inline constant
- "basically" equivalent to
{const N: Ty = expr; N}
- except that it can refer to generic parameters from the surrounding scope
N
(path that refers to a constant)
[Expr; C]
N::<{C}>
- Grammar question
- C = N | Literal | '{' Expr '}'
Generics
- Extend the set of generics with a const kind
Const values
- Known value =
- Integer literals
- Allocation ==> ValTree eventually
- note that there are constants that can't be represented with a valtree, those values can appear in the values of statics or constant expressions that don't wind up in types
- Generic parameter
N
/ Placeholder !C
- Unevaluated
D<Substs...>
- currently, the
Substs
can be 'lazy'
- used because we don't know the set of parameters to start
- Inference variable
?C
Examples
hir -> ty happens here
Foo<22>
โ the 22 is a integer literal
impl<const N: usize> { ... Foo<N> ... }
โ ConstKind::Param
impl<const N: usize> { ... Foo<{N}> ... }
โ ConstKind::Param
... Foo<_> ...
anywhere where types can be inferred โ [ConstKind::Infer
] but has feature gate and lcnr doesn't like the way it is implemented
const N: usize = 22; ... Foo<{N}> ...
โ ConstKind::Unevaluated
with the def-id of the parameter (the {N}
in Foo<{N}>
)
Foo< ... >
โ as above for all other cases
Inference rules
Unifying two constants:
- Known, Known ==> "just do it"
- InfVar, InfVar ==> "just do it"
- InfVar, Uneval ==> Map InfVar to Uneval
- Uneval, Known | Uneval, Uneval ==>
ConstEquate
code in trait system
Unclear
how to extend to generic computations
ConstEval
code in rustc_middle::mir::interpret::queries
- Known => done
- InfVar => error
- Uneval(DefId, Substs) =>
- get the MIR for DefId
- evaluate it with Substs in scope
- if this encounters:
Niko talking random things
Cycle problems
Variance computation
- We were evaluating constants to compute variance of a type
- We didn't need to, fixed
Anonymous constants can reference themselves in predicate
Using associated consts in where bounds
https://github.com/rust-lang/rust/issues/79356#issuecomment-772179196
False inference variable cycles
Evaluatable
ConstEvaluatable(Unevaluated<'tcx, ()>)
Match
Structural equality
https://github.com/rust-lang/lang-team/issues/94