Commit 6f40f601

6f40f601bd2aMerged

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

Description

fix(rbs_extract): cross-file module reopen resolves bare Const (#658)

Followup to #656 (commit 97bf268) which added the lexical-chain walk for unqualified type references. The walk consulted a known_names table populated by a per-file pre-pass, but the per-file pre-pass+emit cycle interleaved with the directory walk: when sibling .rbs files each reopened the same module, the file visited first ran its emit pass before the sibling's pre-pass registered names. A bare Base inside errors.rbs's reopen of module ActiveRecord then couldn't see ActiveRecord::Base from base.rbs, falling through to top-level ::Base (undefined).

Split process_file into two phases: PHASE_COLLECT_NAMES -- parse + walk only collect_decl_names. PHASE_EMIT -- parse + walk only the emit pass.

walk() now takes a phase param; main() runs walk twice over each top-level arg. The full set of qualified names is in known_names before any unqualified reference is resolved.

Test (manual, since spinel/test/*.rb has no .rbs sidecars): sig/bar.rbs declares Foo::Bar sig/baz.rbs declares Foo::Baz with @b: Bar -- bare Bar sig/qux.rbs declares Foo::Qux with @bar: Bar, @baz: Baz spinel --rbs sig program.rb now resolves both bare references regardless of readdir order.

Closes #658.

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

Details

Commit
6f40f601bd2a
Parent
d77a44b94cd2

spinel_rbs_extract.cmodifiedView file

@@ -782,7 +782,17 @@
782782 return buf;
783783 }
784784
785-static void process_file(const char *path, FILE *out) {
785+/* Per-pass selector for process_file. Splitting the per-file work
786+ * into two phases (collect names; emit decls) lets walk() run twice
787+ * over the directory tree so cross-file module reopens have every
788+ * class registered before any unqualified-Const reference is
789+ * resolved. Issue #658. */
790+typedef enum {
791+ PHASE_COLLECT_NAMES = 0,
792+ PHASE_EMIT = 1
793+} process_phase_t;
794+
795+static void process_file_phase(const char *path, FILE *out, process_phase_t phase) {
786796 size_t len = 0;
787797 char *src = slurp(path, &len);
788798 if (src == NULL) {
@@ -795,31 +805,37 @@
795805 rbs_signature_t *sig = NULL;
796806 bool ok = rbs_parse_signature(p, &sig);
797807 if (!ok || sig == NULL) {
798- fprintf(stderr, "spinel_rbs_extract: parse failed in %s\n", path);
799- if (p->error != NULL && p->error->message != NULL) {
800- fprintf(stderr, " %s\n", p->error->message);
808+ if (phase == PHASE_EMIT) {
809+ fprintf(stderr, "spinel_rbs_extract: parse failed in %s\n", path);
810+ if (p->error != NULL && p->error->message != NULL) {
811+ fprintf(stderr, " %s\n", p->error->message);
812+ }
801813 }
802814 rbs_parser_free(p);
803815 free(src);
804816 return;
805817 }
806- /* Pre-pass: register every Class/Module's qualified name so the
807- * emit pass can disambiguate unqualified type references via
808- * lexical-chain walk (issue #656). */
809- rbs_node_list_node_t *pre = sig->declarations->head;
810- while (pre != NULL) {
811- collect_decl_names(p, pre->node, "");
812- pre = pre->next;
813- }
814- rbs_node_list_node_t *cur = sig->declarations->head;
815- while (cur != NULL) {
816- traverse_decl(p, cur->node, "", out);
817- cur = cur->next;
818+ if (phase == PHASE_COLLECT_NAMES) {
819+ rbs_node_list_node_t *pre = sig->declarations->head;
820+ while (pre != NULL) {
821+ collect_decl_names(p, pre->node, "");
822+ pre = pre->next;
823+ }
824+ } else {
825+ rbs_node_list_node_t *cur = sig->declarations->head;
826+ while (cur != NULL) {
827+ traverse_decl(p, cur->node, "", out);
828+ cur = cur->next;
829+ }
818830 }
819831 rbs_parser_free(p);
820832 free(src);
821833 }
822834
835+static void process_file(const char *path, FILE *out) {
836+ process_file_phase(path, out, PHASE_EMIT);
837+}
838+
823839 /* ---- directory walk ---------------------------------------- */
824840
825841 static bool ends_with(const char *s, const char *suffix) {
@@ -828,14 +844,16 @@
828844 return strcmp(s + sl - su, suffix) == 0;
829845 }
830846
831-static void walk(const char *path, FILE *out) {
847+static void walk_phase(const char *path, FILE *out, process_phase_t phase) {
832848 struct stat st;
833849 if (stat(path, &st) != 0) {
834- fprintf(stderr, "spinel_rbs_extract: %s: not found\n", path);
850+ if (phase == PHASE_EMIT) {
851+ fprintf(stderr, "spinel_rbs_extract: %s: not found\n", path);
852+ }
835853 return;
836854 }
837855 if (S_ISREG(st.st_mode)) {
838- if (ends_with(path, ".rbs")) process_file(path, out);
856+ if (ends_with(path, ".rbs")) process_file_phase(path, out, phase);
839857 return;
840858 }
841859 if (!S_ISDIR(st.st_mode)) return;
@@ -847,22 +865,35 @@
847865 char full[4096];
848866 int n = snprintf(full, sizeof(full), "%s/%s", path, ent->d_name);
849867 if (n < 0 || (size_t) n >= sizeof(full)) {
850- fprintf(stderr, "spinel_rbs_extract: path too long, skipping: %s/%s\n",
851- path, ent->d_name);
868+ if (phase == PHASE_EMIT) {
869+ fprintf(stderr, "spinel_rbs_extract: path too long, skipping: %s/%s\n",
870+ path, ent->d_name);
871+ }
852872 continue;
853873 }
854- walk(full, out);
874+ walk_phase(full, out, phase);
855875 }
856876 closedir(d);
857877 }
858878
879+static void walk(const char *path, FILE *out) { walk_phase(path, out, PHASE_EMIT); }
880+
859881 int main(int argc, char **argv) {
860882 if (argc < 2) {
861883 fprintf(stderr, "Usage: spinel_rbs_extract DIR [DIR ...]\n");
862884 return 1;
863885 }
886+ /* Two-pass walk: first phase collects every Class / Module's
887+ * qualified name across all .rbs files into known_names; second
888+ * phase emits decls. Splitting the pre-pass from the emit pass
889+ * lets a bare `Bar` inside `baz.rbs`'s reopen of `module Foo`
890+ * see `Foo::Bar` from a sibling `bar.rbs` regardless of which
891+ * file readdir visited first. Issue #658 (followup to #656). */
864892 for (int i = 1; i < argc; i++) {
865- walk(argv[i], stdout);
893+ walk_phase(argv[i], stdout, PHASE_COLLECT_NAMES);
894+ }
895+ for (int i = 1; i < argc; i++) {
896+ walk_phase(argv[i], stdout, PHASE_EMIT);
866897 }
867898 return 0;
868899 }