Skip to content

feat: add where clauses for generic function bounds - #812

Merged
kacy merged 1 commit into
mainfrom
trait-where-clauses
Aug 19, 2026
Merged

feat: add where clauses for generic function bounds#812
kacy merged 1 commit into
mainfrom
trait-where-clauses

Conversation

@kacy

@kacy kacy commented Aug 19, 2026

Copy link
Copy Markdown
Owner

Adds the where clause spelling for generic function bounds, closing the last item of the traits plan.

what it does

fn f[T](v: T) -> String where T: Sized + Display: now parses and means exactly what the inline spelling fn f[T: Sized + Display](v: T) means. The clause is desugared at parse time onto the same generic_param AST nodes the bracket spelling produces, so the checker and emitter need zero changes — bound enforcement flows through the existing E226/E222 machinery. Multiple clauses separated by commas, and +-joined bounds within a clause, both work; inline and where-clause bounds on the same parameter merge.

where is a contextual keyword (matched as an identifier after the signature), so existing code using where as a name is unaffected. Clauses attach to functions and methods (impl methods route through the same declaration parser); struct, interface, and impl headers keep inline bounds only, which docs/limitations.md now states.

A clause naming something that is not a declared type parameter is a new error, E264, with a hint to declare it in the bracket list.

noticed, not fixed

impl Sized for Box[Int] (an impl on a generic instance) does not satisfy a T: Sized bound at a call site instantiating T = Box[Int] — E226 fires. Verified pre-existing: the inline spelling fails identically. The example uses a plain struct instead.

what was tested

  • tests/cases/test_where_clauses.pith golden: single clause, multi-bound clause (+), multiple comma-separated clauses, and a where-clause method on an impl, all executed through interface dispatch.
  • tests/invalid/where_unknown_param.pith — E264 on a clause naming an undeclared parameter.
  • tests/invalid/where_bound_violated.pith — E226 on a call violating a where-declared bound, proving the desugar reaches existing enforcement.
  • Formatter round-trips the where spelling (fmt on all changed files, no diff churn).
  • make bootstrap-verify — fixed point holds after make refresh-bootstrap-seed (the parser change re-seeds).
  • make check-invalid-only (53 passed) and make run-regressions-only (370 passed).
  • examples/generics.pith gained a where-clause demo; examples/expected/generics.txt regenerated.
  • docs: docs/limitations.md interface-depth entry rewritten for the two spellings; docs/errors.md gained the E264 section.

@kacy
kacy merged commit 50525f4 into main Aug 19, 2026
2 checks passed
@kacy
kacy deleted the trait-where-clauses branch August 19, 2026 12:06
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.

1 participant