Skip to content

Fix query-cycle panic on const blocks with generic calls in parameter types - #1669

Merged
sbillig merged 1 commit into
masterfrom
fix/const-arg-cycle
Oct 1, 2026
Merged

sbillig merged 1 commit into
masterfrom
fix/const-arg-cycle

Conversation

@micahscopes

Copy link
Copy Markdown
Collaborator

This snippet makes fe check panic with a query cycle (param_schema):

trait Mk { type Out }
struct Holder<const N: u256> {}
impl Mk for Holder<5> { type Out = u8 }
const fn id<const N: u256>() -> u256 { N }
fn f(a: Holder<{ id<5>() }>::Out, b: Holder<{ id<5>() }>::Out) {}

What cycled. Building f's parameter list lowers its parameter types. Resolving ::Out type-checks the block
{ id<5>() }, which evaluates the 5 in id<5>. evaluate_const_ty built that constant's capture environment (the
enclosing function's parameters) at its start, and that asks for f's parameter list again. The literal never reads
the environment.

The fix. Build the capture environment on first use. It is still built, with the same value, wherever it is read:
generic parameter lookup, trait and inherent consts, and CTFE. Evaluations that never read it no longer depend on the
owner's parameter list. No check changes. The same change fixes trait methods of this shape, which hit an
index-out-of-bounds panic in param_schema.

One case still cycles: a const block passed as a generic argument inside such a type, e.g.
Holder<{ id<{ id<5>() }>() }>::Out twice. That inner block needs the environment for evaluation, so it needs a
different fix, which I've left out of this PR.

Tests. New ty_check uitests:

  • a positive fixture covering free functions, methods, associated and trait functions, generic owners, arithmetic and
    const-path arguments, aliases and fields; it is also run through fe check in crash_regressions
  • a negative fixture showing the projected type is still checked

Both panicked before this change.

This follows up #1535; thanks for the generic defaults work.

`fn f(a: Holder<{ id<5>() }>::Out, b: Holder<{ id<5>() }>::Out)` made
the compiler panic with a query cycle. Building f's parameter list
lowers its parameter types. Resolving `::Out` type-checks the const
block, which evaluates the `5` in `id<5>`. That evaluation built its
capture environment up front, which asks for f's parameter list again.

Since #1535 the capture environment is built at the start of
evaluate_const_ty, even for a literal or arithmetic that never reads
it. Build it on first use instead. The value is the same wherever it
is used, so results do not change; only the needless dependency goes
away. A const block passed as a generic argument inside such a
parameter type (`id<{ id<5>() }>`) still needs the environment and
still cycles; that is left for a separate change.
@sbillig
sbillig merged commit b0e935d into master Oct 1, 2026
9 checks passed
@sbillig
sbillig deleted the fix/const-arg-cycle branch October 1, 2026 17:24
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