Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluation with inconsistent where-clauses #37

Open
lcnr opened this issue Mar 23, 2022 · 0 comments
Open

Evaluation with inconsistent where-clauses #37

lcnr opened this issue Mar 23, 2022 · 0 comments
Labels
A-generic-exprs Generic const expressions C-design-docs Category: This is part of our design documentation K-impl Document Kind: regarding implementation P-necessary Priority: will be needed at some point S-active

Comments

@lcnr
Copy link
Contributor

lcnr commented Mar 23, 2022

What is this

This is a design document for const generics. Any discussions about its content should be on zulip. The conclusions of these discussions should then be edited back into this issue. Please do not post any comments directly in this issue.

Content

When checking where-bounds containing anonymous constants, we evaluate these constants without first checking whether the required where-bounds hold for constant.
play

trait Foo {
    type Assoc;
    const ASSOC: <Self as Foo>::Assoc;
}

impl Foo for i32 {
    type Assoc = i64;
    const ASSOC: i64 = 3;
}

const fn bar<T>(x: T) -> usize {
    std::mem::forget(x);
    3
}

fn foo<T>() where
    T: Foo<Assoc = T>,
    [u8; bar::<T>(<T as Foo>::ASSOC)]: Sized,
{}

fn main() {
    foo::<i32>();
}

When using foo we have to first prove that it is well formed. This is done by proving its where-clauses. Checking that this is the case requires us to evaluate bar::<T>(<T as Foo>::ASSOC). During const evaluation we assume that everything we evaluate is well formed. As we evaluate the anonymous constant while checking that the where-clauses of foo hold, this is actually not the case.

Evaluating constants containing inference variables

In the past we ended up with quite a few ICE after evaluating constants which contain inference variables. The underlying reason behind these ICE is exactly this issue, where instead of having incorrect substs because we're currently checking the where-clauses containing the constant, we only have insufficient information about the inference variables, preventing us from proving the necessary predicates.

Stop requiring constants to be well formed during CTFE

We rely on constants being well-formed during mir building in quite a few - potentially quite subtle - ways.

To prevent CTFE from causing errors or ICE when run with inconsistent bounds we would have to fix all of these issues, which already seems difficult. Even worse, by allow more incoherent states in the mir, we may also fail to detect compiler bugs.

Check bounds of anonymous constants separately before evaluation

This would generally work but breaks on #36 for constants in where-clauses, as we would have to prove that constants are evaluatable while trying to evaluate them.

Typecheck constants with substs applied before evaluation

Using the param_env of the caller and the generic arguments supplied by the caller, typecheck constants before evaluation after applying the generic arguments.

While this should fix this issue, it probably causes a fairly large performance impact and generally just doesn't feel great.

@lcnr lcnr added C-design-docs Category: This is part of our design documentation K-impl Document Kind: regarding implementation P-necessary Priority: will be needed at some point S-active A-generic-exprs Generic const expressions labels Mar 23, 2022
@rust-lang rust-lang locked and limited conversation to collaborators Mar 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-generic-exprs Generic const expressions C-design-docs Category: This is part of our design documentation K-impl Document Kind: regarding implementation P-necessary Priority: will be needed at some point S-active
Projects
None yet
Development

No branches or pull requests

1 participant