Skip to content

A parameter named self (or this in C++) breaks Python, Go, Rust and C++ output: it collides with the generated receiver, or is declared escaped but referenced unescaped #403

Open

Description

@matt-edmondson

What's wrong

self is an ordinary identifier in C# and in the AST, but the generators treat it as the receiver. There are two separate failures.

1. The parameter collides with the receiver the generator inserts (Python, Go).

  • Python WriteMethodSignature always writes self first, then the declared parameters:
    bool needsSeparator = !method.IsStatic;
    if (needsSeparator)
    {
    code.Write("self");
    }
    for (int index = 0; index < method.Parameters.Count; index++)
    {
    if (needsSeparator)
    {
    code.Write(", ");
    }
    GenerateParameter(method.Parameters[index], code, index);
    needsSeparator = true;
    }
  • Go names every method receiver self:
    private const string ReceiverName = "self";
  • Neither generator renames a user parameter or local that has the same name.

2. The declaration is escaped but references to it are not (Rust, C++).

  • self (Rust) and this (C++) are in ReservedWords, so the parameter declaration goes through EscapeIdentifier and becomes self_ / this_.
  • EscapeReference deliberately skips segment 0 when IsReceiverWord matches:
    string[] segments = reference.Split('.');
    for (int i = 0; i < segments.Length; i++)
    {
    if (ReservedWords.Contains(segments[i]) && !(i == 0 && IsReceiverWord(segments[i])))
    {
    segments[i] = EscapeIdentifier(segments[i]);
    }
  • So a VariableReference("self") that names the parameter is written as the bare receiver word.
  • IsReceiverWord overrides:
    protected override bool IsReceiverWord(string word) => word is "self" or "Self" or "crate" or "super" or "true" or "false";
    and
    protected override bool IsReceiverWord(string word) => word is "this" or "true" or "false" or "nullptr";

Failure scenario

Input: a class Vec with a method int dot(int self) { return self * 2; }, and a free function int twice(int self) { return self + self; }.

Target Output Result
Python def dot(self, self: int) SyntaxError: duplicate argument 'self'
Go func (self *Vec) dot(self int) int self redeclared in this block
Rust (free fn) pub fn twice(self_: i32) -> i32 { return (self + self); } E0424 self is not available in a non-method
Rust (method) self * 2 E0369, since self is &mut Vec and not the i32 parameter
C++ (param this) int twice(int this_) { return (this + this); } does not compile

Suggested fix

  • When a parameter or local has the target's receiver name (self in Python/Go/Rust, this in C++), rename it with the escaped spelling at its declaration. Make every VariableReference that resolves to that parameter or local use the same spelling.
  • In EscapeReference, apply the IsReceiverWord exemption only when segment 0 is not a parameter or local in scope. That keeps the promise in the doc comment that a declaration and its uses can't come out spelled differently.

Acceptance criteria

  • Each case above produces output that parses or compiles in Python (py_compile), Go (go vet), Rust (rustc) and C++ (g++ -fsyntax-only).
  • A body that genuinely refers to the receiver (self.x inside a method, with no parameter named self) is unchanged.

Activity

  1. added
    bug
    on Oct 31, 2026
  2. matt-edmondson commented on Oct 31, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions