Commit 5667190e

5667190e6facMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

analyze: infer unary -@/+@/~ on a poly receiver as poly/int, not a user class

A poly-receiver call falls through to a method-dispatch that unifies the return type over every class defining the method name. Unary -@/+@ were not handled in the poly-operand arithmetic arm, so -@cents (with @cents widened to poly) bound to the enclosing Money class's def -@ and inferred Money, cascading a bogus object type into the ctor param. Resolve -@/+@ to poly and ~ to int in the poly arm, before the dispatch. Fixes unary_operator_methods.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
5667190e6fac
Parent
233226f9c93f

src/analyze_infer.cmodifiedView file

@@ -1812,6 +1812,13 @@
18121812 (!strcmp(name, "+") || !strcmp(name, "-") || !strcmp(name, "*") ||
18131813 !strcmp(name, "/") || !strcmp(name, "%") || !strcmp(name, "**")))
18141814 return TY_POLY;
1815+ /* unary numeric operators on a poly receiver: negation/unary-plus stay
1816+ poly, bitwise complement yields int. Resolve them here so the poly
1817+ method-dispatch below does not bind `-@`/`+@` to a user class that
1818+ happens to define one (e.g. `-@cents` with @cents widened to poly must
1819+ not infer the enclosing Money type). */
1820+ if (argc == 0 && (!strcmp(name, "-@") || !strcmp(name, "+@"))) return TY_POLY;
1821+ if (argc == 0 && !strcmp(name, "~")) return TY_INT;
18151822 if (!strcmp(name, "<") || !strcmp(name, ">") || !strcmp(name, "<=") ||
18161823 !strcmp(name, ">=") || !strcmp(name, "==") || !strcmp(name, "!=") ||
18171824 !strcmp(name, "nil?") || !strcmp(name, "is_a?") || !strcmp(name, "kind_of?") ||