Skip to content

Split ConstValue #54738

Closed
Closed
@RalfJung

Description

@RalfJung

The ConstValue type serves different purposes currently. As return value of const_eval for statics, consts and promoteds, we could instead always return an AllocId + offset (i.e., a Pointer) and avoid even thinking about whether we want the value to be immediate instead. Some places want an immediate (array lengths, pattern matching), they could get a different query returning a different type that "wraps" the main query.

So, I imagine something like: We have one type that serves the current role of ConstValue

enum Const<'tcx> {
   Unevaluated(DefId, &'tcx Substs<'tcx>),
   Evaluated(Pointer),
}

and a new one

enum ConstValue {
  Scalar(Scalar),
  ScalarPair(Scalar, Scalar),
}

(I called the latter ConstValue for symmetry with the Value type. But maybe it's better to not change too many names, and call the 2nd type ConstImmediate or so.)

Then we make the const_eval query return a Pointer, and we add a new query const_eval_value returning a ConstValue.

Overall, this uses the type system to encode that the queries never return Unevaluated, and it lets most callers handle everything uniformly (no need to deal with immediates unless needed).

Cc @oli-obk @eddyb -- does this make any sense?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-cleanupCategory: PRs that clean code up or issues documenting cleanup.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions