Skip to content

[mypyc] Fix bad C for attribute names that are C keywords when mangled - #22526

Merged
JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1217-attr-c-keywords
Nov 19, 2026
Merged

JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1217-attr-c-keywords

Conversation

@JukkaL

@JukkaL JukkaL commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Fixes mypyc/mypyc#1217. Fixes mypyc/mypyc#1187.

The C struct field name of a native attribute is the attribute name with a "_" prefix, which can produce a C keyword or some other name reserved by C compilers, resulting in confusing C compilation errors:

class C:
    def __init__(self) -> None:
        self._restrict = 0  # Field "__restrict" is a keyword in gcc/clang

Many names names were affected.

Now if the prefixed name is reserved by the C standard (it starts with two underscores or an underscore followed by an uppercase letter) or is a known MSVC keyword, an additional "m" prefix is used. This can't collide with other field names, since they all start with "_".

…ngled

Fixes mypyc/mypyc#1217. Fixes mypyc/mypyc#1187.

The C struct field name of a native attribute is the attribute name
with a "_" prefix, which can produce a C keyword or some other name
reserved by C compilers, resulting in confusing C compilation errors:

```python
class C:
    def __init__(self) -> None:
        self._restrict = 0  # Field "__restrict" is a keyword in gcc/clang
```

Many names names were affected.

Now if the prefixed name is reserved by the C standard (it starts with
two underscores or an underscore followed by an uppercase letter) or is
a known MSVC keyword, an additional "m" prefix is used. This can't
collide with other field names, since they all start with "_".
@JukkaL

JukkaL commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

The test failure is a flake (I'm working on a fix).

@JukkaL
JukkaL merged commit 1a2cbf7 into master Nov 19, 2026
17 of 18 checks passed
@JukkaL
JukkaL deleted the mypyc-bulkfix-1217-attr-c-keywords branch November 19, 2026 16:33
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.

Name mangling collides with compiler keywords (__restrict, __single) mypyc should avoid emitting MSVC reserved keywords

2 participants