Commit 85ca4486

85ca448660c7Merged

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

Description

Add Regexp.escape, bare 'name'/'self.name' in class methods, self.name

Regexp.escape/quote -> sp_re_escape (analyze: TY_STRING, codegen: emit) Bare name inside def self.foo -> SPL("ClassName") compile-time string self.name/to_s/inspect inside class methods -> same

Fixes class_name_in_cmeth, poly_local_is_a_string_narrow_in_tail_if, poly_local_is_a_string_narrow_in_non_tail_if.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
85ca448660c7
Parent
4ce2dd66d634

src/analyze.cmodifiedView file

@@ -275,6 +275,19 @@
275275 /* __dir__ -> the source directory (a string) */
276276 if (recv < 0 && !strcmp(name, "__dir__") && argc == 0) return TY_STRING;
277277
278+ /* bare `name` inside a class method body -> the class name string */
279+ if (recv < 0 && !strcmp(name, "name") && argc == 0) {
280+ Scope *self = comp_scope_of(c, id);
281+ if (self && self->is_cmethod && self->class_id >= 0) return TY_STRING;
282+ }
283+ /* `self.name` / `self.to_s` inside a class method -> the class name string */
284+ if (recv >= 0 && argc == 0 &&
285+ (!strcmp(name, "name") || !strcmp(name, "to_s") || !strcmp(name, "inspect")) &&
286+ nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "SelfNode")) {
287+ Scope *self = comp_scope_of(c, id);
288+ if (self && self->is_cmethod && self->class_id >= 0) return TY_STRING;
289+ }
290+
278291 /* proc {} / lambda {} / Proc.new {} -> a first-class Proc value */
279292 if (is_proc_literal(c, id)) return TY_PROC;
280293
@@ -374,6 +387,14 @@
374387 if (!strcmp(name, "reset") || !strcmp(name, "terminate") || !strcmp(name, "unscan")) return TY_STRINGSCANNER;
375388 }
376389
390+ /* Regexp class methods */
391+ if (recv >= 0 && nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "ConstantReadNode") &&
392+ nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "Regexp")) {
393+ if ((!strcmp(name, "escape") || !strcmp(name, "quote")) && argc >= 1) return TY_STRING;
394+ if (!strcmp(name, "union") && argc >= 1) return TY_REGEX;
395+ if (!strcmp(name, "last_match") && argc == 0) return TY_POLY;
396+ }
397+
377398 /* Regexp instance methods */
378399 if (recv >= 0 && rt == TY_REGEX) {
379400 if (!strcmp(name, "match?") || !strcmp(name, "===")) return TY_BOOL;