Repository navigation
Give C# operators the operand the AST leaves implicit [patch] - #230
Merged
Merged
Conversation
The AST's operators are members, as in C++ and Rust: operator+(rhs) is binary and the instance is its left-hand operand. C# operators are static and take every operand explicitly, so the generator wrote a binary operator as a unary one, and a unary operator or conversion with no parameters at all (CS1019). An operator or conversion that belongs to a type now takes that type as a leading `self` parameter, the name the C lowering already uses, so bodies reading self.x mean the same thing. An operator is also never written readonly any more: it is static in C# whether or not the declaration says so, and a read-only operator on a struct failed with CS0106. Adds a compile test covering CompiledExemplar.Plus, Negate and ToDouble on Point, with a driver that uses a + b, -a and the conversion. Fixes #126 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LJsBos8vaJcor3uRqngfwE
|
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.



Fixes #126
What changed
In the AST, operators are members, like C++'s
Money operator+(Money rhs): the instance is the implicit left-hand operand. C# operators are static and take every operand explicitly.CSharpGeneratorwrote the AST's parameters as they were, which caused three problems:+with one parameter compiled as unary+, so everya + bcall site failed;static readonly, which fails with CS0106. The guard added in Write C# member modifiers only where C# allows them [patch] #225 only checkedIsStatic, and an operator is static without the declaration saying so.Now any operator or conversion that belongs to a type takes that type as a leading
selfparameter (TakesImplicitOperand).selfis the name the C lowering already gives its receiver, so a body that readsself.xmeans the same thing in both targets. Operators are no longer writtenreadonly.Tests
CSharpGeneratedSourceCompilesTests.Operators_Compile. It buildsCompiledExemplar.Point()withPlus(),Negate()andToDouble("self.x"), then runsdotnet buildon it with a driver that usesa + b,-aand the implicit conversion. If the generator change is reverted, the test fails (CS1534, plus thereadonlyCS0106 errors).🤖 Generated with Claude Code
https://claude.ai/code/session_01LJsBos8vaJcor3uRqngfwE
Generated by Claude Code