Skip to content

Reject template arguments with unresolved operator namespaces - #841

Merged
VictorTaelin merged 0 commit into
bendlang:mainfrom
leofmarciano:main
Sep 19, 2026
Merged

VictorTaelin merged 0 commit into
bendlang:mainfrom
leofmarciano:main

Conversation

@leofmarciano

Copy link
Copy Markdown
Contributor

A template argument containing an infix operator could be specialized before an enclosing ( ... : T) expression resolved the operator namespace.

For example, ~Unit + Unit was specialized as Nat.add(Unit, Unit) even when the source expression later resolved it as Bar.add(Unit, Unit), allowing the checker to accept an invalid Empty inhabitant.

This change rejects template arguments that still contain unresolved operators and adds a regression test covering the issue. The existing operator-namespace check was updated to expect the new parse error.

PoC of the exploit here: Bend2 Wrong namespace operator

@VictorTaelin
VictorTaelin merged commit 31d2633 into bendlang:main Sep 19, 2026
VictorTaelin added a commit that referenced this pull request Sep 19, 2026
…s as unsafe in the verdict (#841)

A ( .. : T) around a template call renames the operators in its ~
arguments when it closes; the instance was built at the call's (,
before that, with Nat's operators. The call now keeps its Ref and the
instance is picked, and the Ref renamed in place, when the outermost
term has been parsed. The parser expands templates and the checker
takes that on trust, so until instances are built after parsing and
checked against their arguments, a def that is an instance counts as
unsafe: a file with a template prints no bare "All terms check."
@VictorTaelin

Copy link
Copy Markdown
Contributor

Real, and fixed in 2.0.16 differently from the PR: a template instance is now picked when the outermost term has been parsed, after every enclosing ( .. : T) has renamed its operators, so (Pick(~Unit + Unit) : Bar) instantiates with Bar.add and your file fails with expected Empty, observed Unit (tests/check/template_ns_late.bend). Refusing pending operators would have forbidden Pick(~(i + 1)) everywhere. Also since 2.0.16, a def that is a template instance counts as unsafe in the verdict, so a file with a template no longer prints a bare "All terms check.": the parser expands templates and the checker does not yet verify that expansion; that redesign is next.

Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

VictorTaelin added a commit that referenced this pull request Sep 19, 2026
…on, and from nothing else

The SYNTAX table gives the annotation to the operator expression it
closes: (a + b : T) is T.add(a, b). The parser instead pushed every
leading-dot operator on a book-wide stack, and a closing ( .. : T)
renamed in place all of them pushed since the paren opened, through
call arguments, lambda bodies, constructor fields and ~ arguments; the
array literal and the index spliced the same stack; a template call
was deferred (Parse.ts, Parse.lv) until the outermost term closed so
that rename could still reach its ~ argument; and an operator no
annotation reached fell back to Nat in term_higher. parse_term_ns now
walks the annotated term through operator applications (grouping
parens leave no node, so a chain passes through them) and names what
it reaches; the stack, the deferral, its two fields and the Nat
default are gone, and a template instance is built at its call again.
An operator that no annotation names is refused at the operator, with
the repair spelled with that operator. The corpus' 41 bare Nat sites
(35 in tests, 3 in demos, 3 in evals) now carry ( .. : Nat), Synth.noise
annotates its shifts at the operator, op_ns_late pinned the propagation
and is deleted, and chain_default_nat pinned the default and is
chain_nat_suffix. (#841)
Phylliida pushed a commit to Phylliida/bend that referenced this pull request Sep 27, 2026
…s as unsafe in the verdict (bendlang#841)

A ( .. : T) around a template call renames the operators in its ~
arguments when it closes; the instance was built at the call's (,
before that, with Nat's operators. The call now keeps its Ref and the
instance is picked, and the Ref renamed in place, when the outermost
term has been parsed. The parser expands templates and the checker
takes that on trust, so until instances are built after parsing and
checked against their arguments, a def that is an instance counts as
unsafe: a file with a template prints no bare "All terms check."
Phylliida pushed a commit to Phylliida/bend that referenced this pull request Sep 27, 2026
…on, and from nothing else

The SYNTAX table gives the annotation to the operator expression it
closes: (a + b : T) is T.add(a, b). The parser instead pushed every
leading-dot operator on a book-wide stack, and a closing ( .. : T)
renamed in place all of them pushed since the paren opened, through
call arguments, lambda bodies, constructor fields and ~ arguments; the
array literal and the index spliced the same stack; a template call
was deferred (Parse.ts, Parse.lv) until the outermost term closed so
that rename could still reach its ~ argument; and an operator no
annotation reached fell back to Nat in term_higher. parse_term_ns now
walks the annotated term through operator applications (grouping
parens leave no node, so a chain passes through them) and names what
it reaches; the stack, the deferral, its two fields and the Nat
default are gone, and a template instance is built at its call again.
An operator that no annotation names is refused at the operator, with
the repair spelled with that operator. The corpus' 41 bare Nat sites
(35 in tests, 3 in demos, 3 in evals) now carry ( .. : Nat), Synth.noise
annotates its shifts at the operator, op_ns_late pinned the propagation
and is deleted, and chain_default_nat pinned the default and is
chain_nat_suffix. (bendlang#841)
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