feat: add where clauses for generic function bounds - #812
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds the
whereclause 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 spellingfn f[T: Sized + Display](v: T)means. The clause is desugared at parse time onto the samegeneric_paramAST 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.whereis a contextual keyword (matched as an identifier after the signature), so existing code usingwhereas 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 aT: Sizedbound at a call site instantiatingT = 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.pithgolden: 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.wherespelling (fmt on all changed files, no diff churn).make bootstrap-verify— fixed point holds aftermake refresh-bootstrap-seed(the parser change re-seeds).make check-invalid-only(53 passed) andmake run-regressions-only(370 passed).examples/generics.pithgained a where-clause demo;examples/expected/generics.txtregenerated.docs/limitations.mdinterface-depth entry rewritten for the two spellings;docs/errors.mdgained the E264 section.