Skip to content

Commit bdff6df

Browse files
committed
fix: a field's value can arrive through another instance of its class
class Cell: def __init__(self, v: int) -> None: self.nxt = None def chain(self, other: "Cell") -> None: other.nxt = self # attribute value '!py.contract<"Cell">' is not assignable to field # '!py.literal<None>' -- the way every linked structure links. The field walk read only `self.<name>` targets, so a method assigning through ANOTHER instance of its own class said nothing about the field, and the None the constructor gave it stood. ⛔ Only when the target's owner infers to THIS class: through anything else the target names another class's field, which this walk does not own and must not decide. ⛔ And `self` itself had to be bound in that pre-pass scope. It is the one parameter with no annotation to read, so `collectInitArgTypes` skips it -- and `other.nxt = self` therefore typed as nothing at all, which is the same store by the other end. ⛔ `object` IS THE PRE-PASS SAYING IT DOES NOT KNOW, and it was being taken as an answer: a store this walk cannot type turned a NoneType field into `object | None`, which is a field whose reads carry nothing and whose diagnostic then names the wrong thing. A store that types as `object` now contributes nothing, which is what it knows. Claude-Session: https://claude.ai/code/session_015SCJyt2UgUigFCdKuBfizX
1 parent acdb454 commit bdff6df

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎src/lython/emitter/lib/EmitterClasses.cpp‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2759,6 +2759,13 @@ void ModuleEmitter::collectClassFields(
27592759
mlir::Type payload = existingIsNone ? fresh : existing;
27602760
if (!mlir::isa_and_nonnull<py::ContractType>(payload))
27612761
return mlir::Type();
2762+
// ⛔ `object` IS THE PRE-PASS SAYING IT DOES NOT KNOW, not a type to
2763+
// widen a field to. A store this walk cannot type -- a local it did not
2764+
// bind, a call it cannot resolve -- would otherwise turn a NoneType field
2765+
// into `object | None`, which is a field whose reads carry nothing and
2766+
// whose diagnostic names the wrong thing.
2767+
if (py::isPyObjectType(payload))
2768+
return mlir::Type();
27622769
return types.join({payload, types.none()});
27632770
};
27642771
for (auto [index, existing] : llvm::enumerate(fieldNames)) {
@@ -2812,8 +2819,32 @@ void ModuleEmitter::collectClassFields(
28122819
if (target.kind != "Attribute")
28132820
return;
28142821
const parser::Node *object = ast::node(target, "value");
2815-
if (!object || !ast::isName(*object, "self"))
2822+
if (!object)
28162823
return;
2824+
// ⭐ AND THROUGH A LOCAL OF THIS CLASS, not only through `self`. A method
2825+
// that walks its own structure assigns through the node it is standing on:
2826+
//
2827+
// def add(self, w: str) -> None:
2828+
// node = self
2829+
// ...
2830+
// node.word = w # the same field of the same class
2831+
//
2832+
// and reading only `self.<name>` left that field with whatever `__init__`
2833+
// said, which for `self.word = None` is NoneType.
2834+
//
2835+
// ⛔ Only when the local's inferred type IS this class: through anything
2836+
// else the target names another class's field, which this walk does not
2837+
// own and must not decide.
2838+
if (!ast::isName(*object, "self")) {
2839+
if (object->kind != "Name" || collectFieldsRefineOnly == false)
2840+
return;
2841+
auto className = ast::string(classDef, "name");
2842+
mlir::Type ownerType = types.widenLiteral(types.inferExpr(object));
2843+
auto contract = mlir::dyn_cast_if_present<py::ContractType>(ownerType);
2844+
if (!className || !contract ||
2845+
contract.getContractName() != llvm::StringRef(*className))
2846+
return;
2847+
}
28172848
if (auto attr = ast::string(target, "attr")) {
28182849
// `self.<prop> = ...` runs the property setter; it declares no field.
28192850
if (propertyNames.contains(*attr))
@@ -2868,6 +2899,15 @@ void ModuleEmitter::collectClassFields(
28682899
llvm::StringMap<mlir::Type> initArgTypes;
28692900
collectInitArgTypes(method, initArgTypes);
28702901
TypeSystem::Scope initScope = types.pushScope();
2902+
// ⭐ `self` IS THE CLASS, and this scope did not say so: it is the one
2903+
// parameter `collectInitArgTypes` skips (it has no annotation to read),
2904+
// so `other.nxt = self` -- how a linked structure links -- typed as
2905+
// nothing and the field kept whatever `__init__` gave it.
2906+
if (auto className = ast::string(classDef, "name"))
2907+
if (std::optional<mlir::Type> ownContract =
2908+
types.lookupClass(*className))
2909+
if (*ownContract)
2910+
types.bindLocalSymbol("self", *ownContract);
28712911
// A name the pre-pass cannot type must SHADOW any outer binding of the
28722912
// same name; resolving `self.f = x` to a module global that happens to
28732913
// share the local's spelling would type the field off the wrong value.

‎tests/golden/cases/a_field_that_starts_as_none.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ def show(self) -> str:
4545
print(c.show(), c.tags)
4646

4747

48+
# A method may also give the field its value through ANOTHER instance of the
49+
# same class -- the shape every linked structure is written in.
50+
class Cell:
51+
def __init__(self, v: int) -> None:
52+
self.v = v
53+
self.nxt = None
54+
55+
def chain(self, other: "Cell") -> None:
56+
other.nxt = self
57+
58+
59+
first = Cell(1)
60+
second = Cell(2)
61+
first.chain(second)
62+
print(second.nxt.v if second.nxt is not None else -1, first.nxt)
63+
64+
4865
# The other order too: a value first, cleared to None later.
4966
class Slot:
5067
def __init__(self) -> None:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
1 2
22
empty None
33
a:3 ['a']
4+
1 None
45
1
56
None

0 commit comments

Comments
 (0)