Skip to content

Run more ordinary Fe at compile time - #1582

Merged
sbillig merged 11 commits into
masterfrom
pr/const-eval-capabilities
Sep 29, 2026
Merged

sbillig merged 11 commits into
masterfrom
pr/const-eval-capabilities

Conversation

@micahscopes

@micahscopes micahscopes commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

This lets compile-time evaluation run more ordinary Fe, so a helper does not need a separate const-only version.

  • A const fn can update a local through a mut borrow

    const fn set(value: mut u8) { value = 42 }
    
    const fn answer() -> u8 {
        let mut x: u8 = 0
        set(value: mut x)
        x
    }
    

    answer() now evaluates to 42; on master, evaluation fails with 3-0054 (invalid effect provider in const context). Borrows through pointers still fail, since compile-time evaluation has no memory outside its own call frames. Evaluation also fails if a function returns a borrow of its own local. Forming a borrow checks its projections, as at runtime, so an out-of-bounds ref values[i] fails evaluation even when the borrow is never read. An earlier version of this PR let it evaluate, and optional constant folding could then drop the call's bounds-check revert; Sean's 66df370 checks projections when the borrow is formed. Whether a call returns a borrow is decided from the instance's normalized result type (Sean's 6fd490f), so a result type that becomes a borrow only after projection, such as T::Out with type Out = mut u8, is preserved.

  • A const fn can take an immutable trait provider with uses, and a caller can supply one with with

    trait Compute {
        type Item
        const fn compute(self) -> Self::Item
    }
    
    const fn consume<T>() -> T uses (evidence: Compute<Item = T>) {
        evidence.compute()
    }
    
    const fn answer() -> u256 {
        with (Compute<Item = u256> = Number { value: 11 }) { consume<u256>() }
    }
    

    On master, the uses clause and the with are both errors. A test runs the same code at compile time and at runtime and checks that the results agree.

    During evaluation, a provider is an ordinary value held by the call that uses it, so it can be borrowed with ref like a local: a ref self method such as table.get(1) works, and so does passing ref table to a helper. Sean's 86e3f30 admits these borrows. Tests compare these calls at compile time and at runtime, including default trait methods, generic impls, enum providers, and contract init and recv bodies. A provider cannot be borrowed with mut (8-0066), and a borrow of it cannot be returned from that call (16-0003), both as at runtime.

  • Rebuild the compiler when a file is added to core or std. Before, a new library file did not trigger a rebuild, so a cached build kept the old library. A build script, crates/common/build.rs, hashes both trees into FE_BUILTIN_FINGERPRINT, which only invalidates caches and is never compared, so its per-platform value is harmless.

A few decisions:

  • Providers are passed as values, matched to the callee's effect by binding rather than by argument position
  • Mutable providers, storage effects, and extern functions with effects remain rejected
  • The error for with in a const fn (8-0056) is removed, so that code is no longer emitted. The two remaining errors for effects in a const fn (8-0055, 8-0063) now say which effects are supported

How this fits mainline:

  • Fix CTFE evaluation of symbolic array repeats #1556 made constant evaluation transactional: an evaluation that needs a value still given by a generic parameter stops, is thrown away, and is rerun once the parameter is known. Both features fit that design without changes to it. A borrow points into the evaluator's own call frames, which are thrown away with the evaluation. A call with providers stops and reruns like any other call, and a test checks that it evaluates with its provider once the parameter is known. Extraction of symbolic terms already skips calls with providers, so a provider never ends up in a stored term. One consequence: a generic array length computed by a helper that supplies a provider with with stays opaque until its generic arguments are known. docs/ctfe-deferral-audit.md notes this, and a fixture pins it. Fix CTFE evaluation of symbolic array repeats #1556 had kept the borrow restriction on purpose, with a test pinning it; that test now checks that the borrowed read evaluates, and the "Borrowing scope" paragraph of docs/ctfe-deferral-audit.md is updated.
  • Preserve effect provider identity across repeated calls #1564 evaluates a with provider such as Number { value: 11 } once per with entry and may pass it by place. Const evaluation reads a provider passed by place from that place.
  • fix: complete generic function call defaults #1535 makes the substitution each evaluation is keyed by (GenericSubst) range over the owner's full parameter schema. The check for a returned borrow reads the instance's normalized result type, which is built from that substitution.
  • Keep integer width for view-typed values in constant evaluation #1594 makes the integer width lookup in consts.rs see through view types. This PR does not change that code, and Keep integer width for view-typed values in constant evaluation #1594's ctfe_view_int_bitnot.fe passes.

Overlap with open PRs: #1576 conflicts in const_check.rs and machine.rs. It moves the const fn effects check, which this PR changes from "any effects" to const_effects_supported, so whichever lands second carries that change into the moved check. Draft #1455 conflicts in const_check.rs as well. #1589, #1600 and #1606 merge cleanly.

Testing. The full workspace test run passes locally at the head of this branch, 3600 of 3600 tests, and fmt, clippy and the newsfragment check are clean.

Planned later PRs in this series (trait evidence, named implementation selection, generated code) build on the provider support here.

micahscopes added a commit that referenced this pull request Sep 23, 2026
Rename the placeholder newsfragments to the pull request number, following the repository's <PR>.<type>.md convention.
micahscopes added a commit that referenced this pull request Sep 23, 2026
Parse boolean conditions in `where` clauses next to type bounds, and
check each condition that mentions no generic parameter where it is
declared, even if the item is unused. A condition passes only when it
evaluates to `true`; type errors, non-const operations and failed
evaluation are errors. Const conditions in generic scopes are rejected
until use-site checking exists.

`where T` without `:` now parses as a const condition, so a lone path
that names a type reports the missing trait bound instead of a type
used as a value.

The tree-sitter grammar, its vendored wasm build and the formatter
learn the new predicate form.

A predicate block goes through semantic borrow checking like any const
body. Its test needs frame-local borrows in const evaluation (#1582)
to evaluate the valid case, so it is added with that change.
@micahscopes
micahscopes force-pushed the pr/const-eval-capabilities branch from 82f1b5e to ff45c5a Compare September 23, 2026 13:19
micahscopes added a commit that referenced this pull request Sep 23, 2026
Parse boolean conditions in `where` clauses next to type bounds, and
check each condition that mentions no generic parameter where it is
declared, even if the item is unused. A condition passes only when it
evaluates to `true`; type errors, non-const operations and failed
evaluation are errors. Const conditions in generic scopes are rejected
until use-site checking exists.

`where T` without `:` now parses as a const condition, so a lone path
that names a type reports the missing trait bound instead of a type
used as a value.

The tree-sitter grammar, its vendored wasm build and the formatter
learn the new predicate form.

A predicate block goes through semantic borrow checking like any const
body. Its test needs frame-local borrows in const evaluation (#1582)
to evaluate the valid case, so it is added with that change.
@micahscopes
micahscopes force-pushed the pr/const-eval-capabilities branch from ff45c5a to 08e8b62 Compare September 25, 2026 04:15
micahscopes added a commit that referenced this pull request Sep 25, 2026
Parse boolean conditions in `where` clauses next to type bounds, and
check each condition that mentions no generic parameter where it is
declared, even if the item is unused. A condition passes only when it
evaluates to `true`; type errors, non-const operations and failed
evaluation are errors. Const conditions in generic scopes are rejected
until use-site checking exists.

`where T` without `:` now parses as a const condition, so a lone path
that names a type reports the missing trait bound instead of a type
used as a value.

The tree-sitter grammar, its vendored wasm build and the formatter
learn the new predicate form.

A predicate block goes through semantic borrow checking like any const
body. Its test needs frame-local borrows in const evaluation (#1582)
to evaluate the valid case, so it is added with that change.
@micahscopes
micahscopes marked this pull request as ready for review September 25, 2026 04:51
Const evaluation rejected every borrow that carries address-space
metadata. Explicit borrows of locals (`mut x`, `ref x`, and `mut`
arguments) carry `Memory` metadata too, so a const fn that updated a
local in place, or passed one to a helper by `mut`, failed to evaluate.

Accept a `Memory` borrow whose place is rooted in a frame local and does
not go through a dereference. Borrows through pointers and of
provider-backed locals still fail with `InvalidProviderUse`, since
constant evaluation has no memory outside its frames. A request cannot
carry a pointer argument (request inputs are verified before
execution), so the pointer case is tested inside the machine tests.

When a frame returns a reference into itself, the value is read out if
the declared result type is not a borrow, and the evaluation fails with
`InvalidBorrow` if it is, so a dangling reference cannot escape. A
reference into a caller's frame is returned unchanged.

This fits the transactional evaluator from #1556 as is: machine
references stay private to one attempt, and a blocked attempt discards
its frames, so a replay after specialization starts from the original
locals. #1556 recorded the borrowing restriction as intentionally kept
(`docs/ctfe-deferral-audit.md`, "Borrowing scope") and pinned it with
`ctfe_typed_read_preserves_provider_borrow_rejection`. That test now
checks that the typed read through `ref value` evaluates to 7, under
the name `ctfe_typed_read_through_frame_local_borrow_evaluates`, and the
audit's borrowing paragraph describes the admitted borrows.
fe-common embeds `ingots/core` and `ingots/std` with rust-embed. The
compiler records only the files that the previous expansion embedded as
dependencies, so adding a new file to either library (for example a new
`core` module) did not rebuild fe-common, and a cached build kept
serving the old library.

Add a build script that reruns when either directory changes and hashes
the paths and contents of both trees into `FE_BUILTIN_FINGERPRINT`.
`stdlib.rs` reads that variable, so rustc and compiler caches such as
sccache treat the library's file set as an input.

The fingerprint hashes native paths and raw file bytes, so its value
differs between platforms and line-ending settings. It is only used to
invalidate caches and is never compared.
Const functions could not use effects at all: a `uses` clause on a
`const fn` was an error, a call to any function with effects was an
error, and every `with` expression in a `const fn` was rejected. A
trait provider is an ordinary value, so an immutable one can be
evaluated at compile time like any other argument.

Allow a `const fn` to declare immutable trait effects and to supply
them with `with`. During evaluation a call passes each provider as a
value into the callee's effect slot, matched by the effect's binding
index rather than by its position among ordinary arguments. A `with`
provider that lowering captured as a place (#1564) is read from that
place. The checker still rejects mutable effects, type-keyed (storage)
effects, and extern functions with effects.

Deferral needs no special case. Since #1556, a call whose input is
still generic blocks the whole attempt, which is discarded and replayed
after specialization; the replay recomputes the providers from the
caller's body. Term extraction already declines calls with effect
arguments, so a provider never enters a retained term. A test checks
that an effectful call blocks while generic and evaluates with its
provider once specialized, like a pure call.

Effect slots follow the same rules as ordinary argument slots: a
request that supplies no providers for an effectful function is
unsupported (`NotConstEvaluable`), and a slot count or index mismatch
is an invalid operation, like the existing argument arity check.

The `with`-in-const-fn diagnostic (8-0056) is removed, and the two
remaining const-effect messages now say what is supported.
micahscopes added a commit that referenced this pull request Sep 28, 2026
Parse boolean conditions in `where` clauses next to type bounds, and
check each condition that mentions no generic parameter where it is
declared, even if the item is unused. A condition passes only when it
evaluates to `true`; type errors, non-const operations and failed
evaluation are errors. Const conditions in generic scopes are rejected
until use-site checking exists.

`where T` without `:` now parses as a const condition, so a lone path
that names a type reports the missing trait bound instead of a type
used as a value.

The tree-sitter grammar, its vendored wasm build and the formatter
learn the new predicate form.

A predicate block goes through semantic borrow checking like any const
body. Its test needs frame-local borrows in const evaluation (#1582)
to evaluate the valid case, so it is added with that change.
@micahscopes
micahscopes force-pushed the pr/const-eval-capabilities branch from 08e8b62 to 71b96ab Compare September 28, 2026 02:54
@micahscopes
micahscopes requested a review from sbillig September 28, 2026 15:51
CTFE resolved a borrowed place without validating its projections, so an
out-of-bounds borrow that was never read or written evaluated successfully.
Optional constant folding then replaced ordinary calls and dropped their
required bounds-check revert. This affected explicit `ref`/`mut` borrows as
well as method-receiver autoborrows.
CTFE rejected any borrow whose root had provider provenance, so a `ref self`
method on an immutable trait provider failed with an invalid provider use even
though the same call works at runtime. Admission already restricts providers to
immutable, by-value memory objects, and each effect slot owns a copy of its
value, so the admitted provider is an ordinary frame local. Borrows still reject
other address spaces and pointer dereferences.
Deciding whether a returned reference is preserved or read reselected the
owner's declared result type, substituted it and normalized it with only the
impl environment. Use the instance's normalized result type instead, which is
the same query semantic lowering uses and includes the full instance
assumptions. Cover a result type that becomes a borrow only after projection.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-29T03:34:30.479266Z 6fd490f New commits
🔒 Security Review ✅ Completed 2026-09-29T03:34:45.378215Z 6fd490f New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6fd490f857

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

fn check_call_target(&mut self, expr: ExprId) {
if let Some(callable) = self.typed_body.callable_expr(expr) {
self.check_callable(expr.span(self.body).into(), callable);
if self.typed_body.call_effect_args(expr).is_some_and(|args| {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid reporting unsupported effectful calls twice

When a const caller invokes a const function with unsupported effects (for example, a callee requiring a mutable or type-keyed provider), check_callable already emits ConstFnEffectfulCall, and this independent effect-argument check emits the identical diagnostic again at the same call expression. Combine these checks or skip the argument-level diagnostic when the callee itself has already been rejected so users receive one error per call.

Useful? React with 👍 / 👎.

Allowing borrows of admitted providers during constant evaluation lets a
`ref self` provider method and an explicit `ref` of a provider evaluate.
Its fixture covers one `ref self` method and one explicit borrow on a
non-`Copy` provider. These fixtures cover more of the shapes that reach
that borrow, and the rules that keep it sound.

`fe_test/const_trait_provider_borrows.fe` compares compile-time and
runtime results for: `ref self` calls on a provider held by the call, an
explicit `ref` kept in a local and passed to a generic helper, a
provider forwarded to two callees, a default trait method that borrows
the provider again for each call, a generic impl and an enum provider
declared in another module, an inherent method and a trait method called
through a bound, and an associated const. At 71b96ab it fails with
3-0054 at `table.get(0)` and `table.pair(0, 3)`; it passes at 6fd490f.

Two reject fixtures pin what stays rejected, as at runtime, and pass
before and after: `ty_check/const_fn/trait_provider_mut_borrow.fe`
(8-0066 for `mut self` and `mut table`) and
`semantic_borrowck/const_trait_provider_borrows.fe` (16-0003 for a
returned borrow of the provider, 16-0001 for a `mut` borrow of a `with`
source while it provides).
Const evaluation now admits borrows of an admitted provider, so a `ref
self` method on a provider evaluates at compile time. The feature entry
listed forwarding and const trait method calls but not this, so it now
names `ref` borrows of a provider as well.
A generic array length computed by a helper that supplies a provider
with `with`, such as `[u8; wrap<N>()]` where `wrap` calls
`plus<N>()` inside `with (Compute<Item = usize> = Size { value: 1 })`,
does not unify with the equivalent provider-free length `plain<N>()`
(`N + 1`) while `N` is generic: the checker reports "expected
`[u8; (const N: usize + 1)]`, but `[u8; wrap<const N: usize>()]` is
given". Concrete lengths evaluate, so `Wrapped<2>` and `Plain<2>`
agree.

This follows from a documented rule rather than a bug: symbolic term
extraction skips calls that pass providers, so such a length stays an
opaque call until its generic arguments are known. The Normalization
row of docs/ctfe-deferral-audit.md now says so, and the new fixture
`ty_check/const_fn/trait_provider_generic_extent.fe` pins both the
concrete case (accepted) and the generic case (the mismatch above). It
passes at 71b96ab and after, since behavior does not change.
A call from a `const fn` to a non-const function that needs a `mut`
provider reported two errors on the same span: 8-0062 ("`runtime_mut`
is not a `const fn`") and 8-0063 ("unsupported effectful call"). Both
describe the one call that cannot run at compile time.

Cause: `check_call_target` reported the callee through
`check_callable`, then checked the providers the call passes and
reported again, without regard to the first result.

Fix: `check_callable` returns whether it reported the callee, and the
provider check runs only for a callee that a `const fn` may call.

Failing first, at 71b96ab: the new fixture
`ty_check/const_fn/nonconst_call_with_mut_provider.fe` produces both
8-0062 and 8-0063 at line 22 (snapshot mismatch with
INSTA_UPDATE=no); with the fix it reports only 8-0062. No existing
snapshot changes.
The provider-borrow fixtures covered free functions, methods, impls and
associated consts, but no contract-owned body. Contract `init` and
`recv` bodies reach compile-time evaluation through const items and
array lengths, and they can also supply a storage field as a provider
at runtime.

The new fe_test fixture `const_trait_provider_contract.fe` deploys a
contract whose `init` and `recv` arms use a `ref self` provider method
at compile time (the const item `SUM` and the array length `count()`)
and at runtime (`sum()`), and a `recv` arm that passes a storage field
to the same const fn with `with (Lookup = table)` at runtime. All
calls return the values computed at compile time.

Failing first, at 71b96ab: the fixture fails with 3-0054 at
`table.get(0)` in `ends()`, followed by a knock-on 8-0031 ("type
annotation is needed") at the array length in the `Length` arm. With
the provider-borrow fix (86e3f30) it passes. The storage-field arm
passes before and after, since it runs only at runtime.
micahscopes added a commit that referenced this pull request Sep 29, 2026
Parse boolean conditions in `where` clauses next to type bounds, and
check each condition that mentions no generic parameter where it is
declared, even if the item is unused. A condition passes only when it
evaluates to `true`; type errors, non-const operations and failed
evaluation are errors. Const conditions in generic scopes are rejected
until use-site checking exists.

`where T` without `:` now parses as a const condition, so a lone path
that names a type reports the missing trait bound instead of a type
used as a value.

The tree-sitter grammar, its vendored wasm build and the formatter
learn the new predicate form.

A predicate block goes through semantic borrow checking like any const
body. Its test needs frame-local borrows in const evaluation (#1582)
to evaluate the valid case, so it is added with that change.
@sbillig
sbillig merged commit 6f07b75 into master Sep 29, 2026
9 checks passed
@sbillig
sbillig deleted the pr/const-eval-capabilities branch September 29, 2026 19:52
sbillig added a commit that referenced this pull request Sep 29, 2026
The fix and its regressions landed with #1582, which carried the same
borrow validation, but no release note describes the bug it fixes:
forming a method-receiver borrow in CTFE skipped the index bounds check.
micahscopes added a commit that referenced this pull request Sep 30, 2026
Parse boolean conditions in `where` clauses next to type bounds, and
check each condition that mentions no generic parameter where it is
declared, even if the item is unused. A condition passes only when it
evaluates to `true`; type errors, non-const operations and failed
evaluation are errors. Const conditions in generic scopes are rejected
until use-site checking exists.

`where T` without `:` now parses as a const condition, so a lone path
that names a type reports the missing trait bound instead of a type
used as a value.

The tree-sitter grammar, its vendored wasm build and the formatter
learn the new predicate form.

A predicate block goes through semantic borrow checking like any const
body. Its test needs frame-local borrows in const evaluation (#1582)
to evaluate the valid case, so it is added with that change.
micahscopes added a commit that referenced this pull request Sep 30, 2026
Const trait providers (#1582) and const `where` predicates meet in a
const fn that states a condition and takes a provider. Nothing tested the
two together.

`fe_test/const_where_trait_providers.fe` evaluates such a function at
compile time and at runtime and checks that both agree, once for a
provider whose method borrows it (`ref self`) and once for one taken by
value, with a generic caller that forwards the condition and the
provider. The uitest `trait_provider_callees_check_their_requirements`
calls `at<4>()` against `N < 4` inside a `with` block and gets one
`8-0093` at the call.

Both pass on the parent commit; they pin behavior that no fix here
changes.
micahscopes added a commit that referenced this pull request Sep 30, 2026
A predicate block goes through semantic borrow checking like any const
body, and evaluating one that borrows a local needs frame-local borrows
in constant evaluation, which #1582 added. The test for both was left
out until that landed.

The accept fixture `predicate_blocks_evaluate_frame_local_borrows`
passes a local by `mut` to a const fn in a ground predicate and writes
through a `mut` alias in a generic one, and evaluates both. The
`semantic_borrowck/const_where` fixture has a ground and a generic
predicate that hold but take `ref x` while `mut x` is live, and gets a
borrow conflict in each.

A predicate that fails reports only its evaluation error: borrow
checking runs after type checking succeeds, as for other bodies. So the
conflicting predicates here hold.

Both pass on the parent commit.
micahscopes added a commit that referenced this pull request Sep 30, 2026
Parse boolean conditions in `where` clauses next to type bounds, and
check each condition that mentions no generic parameter where it is
declared, even if the item is unused. A condition passes only when it
evaluates to `true`; type errors, non-const operations and failed
evaluation are errors. Const conditions in generic scopes are rejected
until use-site checking exists.

`where T` without `:` now parses as a const condition, so a lone path
that names a type reports the missing trait bound instead of a type
used as a value.

The tree-sitter grammar, its vendored wasm build and the formatter
learn the new predicate form.

A predicate block goes through semantic borrow checking like any const
body. Its test needs frame-local borrows in const evaluation (#1582)
to evaluate the valid case, so it is added with that change.
micahscopes added a commit that referenced this pull request Sep 30, 2026
Const trait providers (#1582) and const `where` predicates meet in a
const fn that states a condition and takes a provider. Nothing tested the
two together.

`fe_test/const_where_trait_providers.fe` evaluates such a function at
compile time and at runtime and checks that both agree, once for a
provider whose method borrows it (`ref self`) and once for one taken by
value, with a generic caller that forwards the condition and the
provider. The uitest `trait_provider_callees_check_their_requirements`
calls `at<4>()` against `N < 4` inside a `with` block and gets one
`8-0093` at the call.

Both pass on the parent commit; they pin behavior that no fix here
changes.
micahscopes added a commit that referenced this pull request Sep 30, 2026
A predicate block goes through semantic borrow checking like any const
body, and evaluating one that borrows a local needs frame-local borrows
in constant evaluation, which #1582 added. The test for both was left
out until that landed.

The accept fixture `predicate_blocks_evaluate_frame_local_borrows`
passes a local by `mut` to a const fn in a ground predicate and writes
through a `mut` alias in a generic one, and evaluates both. The
`semantic_borrowck/const_where` fixture has a ground and a generic
predicate that hold but take `ref x` while `mut x` is live, and gets a
borrow conflict in each.

A predicate that fails reports only its evaluation error: borrow
checking runs after type checking succeeds, as for other bodies. So the
conflicting predicates here hold.

Both pass on the parent commit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants