7878 */
7979
8080#include "ruby_lsp.h"
81+ #include "lsp_work.h"
8182#include "../helpers.h"
8283#include "../arena.h"
8384#include <ctype.h>
@@ -470,12 +471,53 @@ static const CBMRegisteredFunc *ruby_own_singleton_method(RubyLSPContext *ctx, c
470471 * from that point on no hit is trustworthy and callers must emit nothing —
471472 * including NOT falling through to a same-named top-level def or to the
472473 * Object typing fallback. Hits found BEFORE the unseen ancestor in Ruby's
473- * lookup order are still returned. */
474+ * lookup order are still returned.
475+ *
476+ * Each lookup is one walk: the `blocked` flag plus a visit stamp. Ruby
477+ * linearizes ancestors, so a module reached a second time in one walk
478+ * (`prepend M; include M`, two includes sharing a module) was already
479+ * searched and missed: a hit or a block would have ended the walk. Skipping
480+ * the repeat keeps a lookup linear in the ancestors it can see; without it
481+ * every module that both prepends and includes the next one doubles the
482+ * paths per level. */
483+ typedef struct {
484+ bool blocked ;
485+ unsigned int stamp ;
486+ } RubyLookupWalk ;
487+
488+ static RubyLookupWalk ruby_walk_begin (RubyLSPContext * ctx ) {
489+ if (++ ctx -> lookup_stamp == 0 ) {
490+ /* Wrapped: clear every mark so no class looks visited. */
491+ for (int i = 0 ; i < ctx -> class_count ; i ++ ) {
492+ ctx -> classes [i ].seen_instance = 0 ;
493+ ctx -> classes [i ].seen_singleton = 0 ;
494+ }
495+ ctx -> lookup_stamp = 1 ;
496+ }
497+ RubyLookupWalk w = {false, ctx -> lookup_stamp };
498+ return w ;
499+ }
500+
501+ /* Marks `class_qn` searched on one side (instance or singleton) in this walk;
502+ * true when it already was. Only the file's class table is marked: mixins
503+ * and superclass refs are recorded for those classes alone, so a type outside
504+ * the table (a stdlib type) has nothing to expand and is never a repeat. */
505+ static bool ruby_walk_seen (RubyLSPContext * ctx , RubyLookupWalk * w , const char * class_qn ,
506+ bool singleton ) {
507+ RubyClassInfo * ci = ruby_class_by_qn (ctx , class_qn );
508+ if (!ci )
509+ return false;
510+ unsigned int * mark = singleton ? & ci -> seen_singleton : & ci -> seen_instance ;
511+ if (* mark == w -> stamp )
512+ return true;
513+ * mark = w -> stamp ;
514+ return false;
515+ }
474516
475517static const CBMRegisteredFunc * ruby_lookup_instance_method_depth (RubyLSPContext * ctx ,
476518 const char * class_qn ,
477519 const char * method_name ,
478- int depth , bool * blocked );
520+ int depth , RubyLookupWalk * w );
479521
480522/* True when the class declares a superclass the finalize step could not
481523 * resolve (stdlib-seeded conventions like ApplicationRecord DO resolve). */
@@ -489,18 +531,18 @@ static bool ruby_superclass_unresolved(RubyLSPContext *ctx, const char *class_qn
489531 * before any hit blocks the walk. */
490532static const CBMRegisteredFunc * ruby_lookup_in_mixins (RubyLSPContext * ctx , const char * owner_qn ,
491533 RubyMixinKind kind , const char * method_name ,
492- int depth , bool * blocked ) {
534+ int depth , RubyLookupWalk * w ) {
493535 for (int i = ctx -> mixin_count - 1 ; i >= 0 ; i -- ) {
494536 RubyMixinInfo * mi = & ctx -> mixins [i ];
495537 if (mi -> kind != kind || strcmp (mi -> owner_qn , owner_qn ) != 0 )
496538 continue ;
497539 if (!mi -> module_qn ) {
498- * blocked = true; /* unseen module — it may define/shadow the method */
540+ w -> blocked = true; /* unseen module — it may define/shadow the method */
499541 return NULL ;
500542 }
501543 const CBMRegisteredFunc * f =
502- ruby_lookup_instance_method_depth (ctx , mi -> module_qn , method_name , depth + 1 , blocked );
503- if (f || * blocked )
544+ ruby_lookup_instance_method_depth (ctx , mi -> module_qn , method_name , depth + 1 , w );
545+ if (f || w -> blocked )
504546 return f ;
505547 }
506548 return NULL ;
@@ -509,29 +551,32 @@ static const CBMRegisteredFunc *ruby_lookup_in_mixins(RubyLSPContext *ctx, const
509551static const CBMRegisteredFunc * ruby_lookup_instance_method_depth (RubyLSPContext * ctx ,
510552 const char * class_qn ,
511553 const char * method_name ,
512- int depth , bool * blocked ) {
554+ int depth , RubyLookupWalk * w ) {
513555 if (!ctx || !class_qn || !method_name || depth > RUBY_LOOKUP_MAX_DEPTH )
514556 return NULL ;
557+ CBM_LSP_WORK (1 );
558+ if (ruby_walk_seen (ctx , w , class_qn , false))
559+ return NULL ; /* searched earlier in this walk: a repeat can only miss */
515560 /* Prepended modules shadow the class's own methods — an unresolved
516561 * prepend therefore blocks even an own-method hit. */
517562 const CBMRegisteredFunc * f =
518- ruby_lookup_in_mixins (ctx , class_qn , RUBY_MIXIN_PREPEND , method_name , depth , blocked );
519- if (f || * blocked )
563+ ruby_lookup_in_mixins (ctx , class_qn , RUBY_MIXIN_PREPEND , method_name , depth , w );
564+ if (f || w -> blocked )
520565 return f ;
521566 f = ruby_own_instance_method (ctx , class_qn , method_name );
522567 if (f )
523568 return f ;
524569 /* An unresolved include stops the walk before the superclass chain. */
525- f = ruby_lookup_in_mixins (ctx , class_qn , RUBY_MIXIN_INCLUDE , method_name , depth , blocked );
526- if (f || * blocked )
570+ f = ruby_lookup_in_mixins (ctx , class_qn , RUBY_MIXIN_INCLUDE , method_name , depth , w );
571+ if (f || w -> blocked )
527572 return f ;
528573 if (ruby_superclass_unresolved (ctx , class_qn )) {
529- * blocked = true;
574+ w -> blocked = true;
530575 return NULL ;
531576 }
532577 const char * sup = ruby_superclass_qn (ctx , class_qn );
533578 if (sup && strcmp (sup , class_qn ) != 0 )
534- return ruby_lookup_instance_method_depth (ctx , sup , method_name , depth + 1 , blocked );
579+ return ruby_lookup_instance_method_depth (ctx , sup , method_name , depth + 1 , w );
535580 return NULL ;
536581}
537582
@@ -540,10 +585,10 @@ static const CBMRegisteredFunc *ruby_lookup_instance_method_depth(RubyLSPContext
540585static const CBMRegisteredFunc * ruby_lookup_instance_method_b (RubyLSPContext * ctx ,
541586 const char * class_qn ,
542587 const char * method_name ,
543- bool * blocked ) {
588+ RubyLookupWalk * w ) {
544589 const CBMRegisteredFunc * f =
545- ruby_lookup_instance_method_depth (ctx , class_qn , method_name , 0 , blocked );
546- if (f || * blocked )
590+ ruby_lookup_instance_method_depth (ctx , class_qn , method_name , 0 , w );
591+ if (f || w -> blocked )
547592 return f ;
548593 /* Universal receiver fallback (Object / ActiveSupport predicates) —
549594 * typing only; Object methods are stdlib entries, never project defs. */
@@ -554,37 +599,44 @@ static const CBMRegisteredFunc *ruby_lookup_instance_method_b(RubyLSPContext *ct
554599
555600const CBMRegisteredFunc * ruby_lookup_instance_method (RubyLSPContext * ctx , const char * class_qn ,
556601 const char * method_name ) {
557- bool blocked = false;
558- return ruby_lookup_instance_method_b (ctx , class_qn , method_name , & blocked );
602+ if (!ctx )
603+ return NULL ;
604+ RubyLookupWalk w = ruby_walk_begin (ctx );
605+ return ruby_lookup_instance_method_b (ctx , class_qn , method_name , & w );
559606}
560607
561608static const CBMRegisteredFunc * ruby_lookup_singleton_method_depth (RubyLSPContext * ctx ,
562609 const char * class_qn ,
563610 const char * method_name ,
564- int depth , bool * blocked ) {
611+ int depth , RubyLookupWalk * w ) {
565612 if (!ctx || !class_qn || !method_name || depth > RUBY_LOOKUP_MAX_DEPTH )
566613 return NULL ;
614+ CBM_LSP_WORK (1 );
615+ if (ruby_walk_seen (ctx , w , class_qn , true))
616+ return NULL ; /* searched earlier in this walk: a repeat can only miss */
567617 const CBMRegisteredFunc * f = ruby_own_singleton_method (ctx , class_qn , method_name );
568618 if (f )
569619 return f ;
570620 /* `extend Mod` adds Mod's instance methods class-side. */
571- f = ruby_lookup_in_mixins (ctx , class_qn , RUBY_MIXIN_EXTEND , method_name , depth , blocked );
572- if (f || * blocked )
621+ f = ruby_lookup_in_mixins (ctx , class_qn , RUBY_MIXIN_EXTEND , method_name , depth , w );
622+ if (f || w -> blocked )
573623 return f ;
574624 if (ruby_superclass_unresolved (ctx , class_qn )) {
575- * blocked = true;
625+ w -> blocked = true;
576626 return NULL ;
577627 }
578628 const char * sup = ruby_superclass_qn (ctx , class_qn );
579629 if (sup && strcmp (sup , class_qn ) != 0 )
580- return ruby_lookup_singleton_method_depth (ctx , sup , method_name , depth + 1 , blocked );
630+ return ruby_lookup_singleton_method_depth (ctx , sup , method_name , depth + 1 , w );
581631 return NULL ;
582632}
583633
584634const CBMRegisteredFunc * ruby_lookup_singleton_method (RubyLSPContext * ctx , const char * class_qn ,
585635 const char * method_name ) {
586- bool blocked = false;
587- return ruby_lookup_singleton_method_depth (ctx , class_qn , method_name , 0 , & blocked );
636+ if (!ctx )
637+ return NULL ;
638+ RubyLookupWalk w = ruby_walk_begin (ctx );
639+ return ruby_lookup_singleton_method_depth (ctx , class_qn , method_name , 0 , & w );
588640}
589641
590642/* ── ActiveRecord model detection (typing special case) ─────────── */
@@ -878,13 +930,11 @@ static const CBMType *ruby_eval_call_type(RubyLSPContext *ctx, TSNode call, bool
878930 if (ctx -> self_unknown )
879931 return cbm_type_unknown (); /* receiver is a rebound self — zero-edge */
880932 if (ctx -> enclosing_class_qn ) {
881- bool blocked = false ;
933+ RubyLookupWalk w = ruby_walk_begin ( ctx ) ;
882934 const CBMRegisteredFunc * f =
883935 ctx -> in_singleton_method
884- ? ruby_lookup_singleton_method_depth (ctx , ctx -> enclosing_class_qn , mname , 0 ,
885- & blocked )
886- : ruby_lookup_instance_method_depth (ctx , ctx -> enclosing_class_qn , mname , 0 ,
887- & blocked );
936+ ? ruby_lookup_singleton_method_depth (ctx , ctx -> enclosing_class_qn , mname , 0 , & w )
937+ : ruby_lookup_instance_method_depth (ctx , ctx -> enclosing_class_qn , mname , 0 , & w );
888938 if (f ) {
889939 if (emit )
890940 ruby_emit_resolved (ctx , f -> qualified_name , "ruby_self_dispatch" , RUBY_CONF_HIGH ,
@@ -893,7 +943,7 @@ static const CBMType *ruby_eval_call_type(RubyLSPContext *ctx, TSNode call, bool
893943 }
894944 /* An unseen ancestor may define this method: do NOT fall through
895945 * to a same-named top-level def. */
896- if (blocked )
946+ if (w . blocked )
897947 return cbm_type_unknown ();
898948 }
899949 const CBMRegisteredFunc * f =
@@ -915,12 +965,11 @@ static const CBMType *ruby_eval_call_type(RubyLSPContext *ctx, TSNode call, bool
915965 if (strcmp (rk , "self" ) == 0 ) {
916966 if (!ctx -> enclosing_class_qn || ctx -> self_unknown )
917967 return cbm_type_unknown ();
918- bool blocked = false;
919- const CBMRegisteredFunc * f = ctx -> in_singleton_method
920- ? ruby_lookup_singleton_method_depth (
921- ctx , ctx -> enclosing_class_qn , mname , 0 , & blocked )
922- : ruby_lookup_instance_method_depth (
923- ctx , ctx -> enclosing_class_qn , mname , 0 , & blocked );
968+ RubyLookupWalk w = ruby_walk_begin (ctx );
969+ const CBMRegisteredFunc * f =
970+ ctx -> in_singleton_method
971+ ? ruby_lookup_singleton_method_depth (ctx , ctx -> enclosing_class_qn , mname , 0 , & w )
972+ : ruby_lookup_instance_method_depth (ctx , ctx -> enclosing_class_qn , mname , 0 , & w );
924973 if (f ) {
925974 if (emit )
926975 ruby_emit_resolved (ctx , f -> qualified_name , "ruby_self_dispatch" , RUBY_CONF_HIGH ,
@@ -967,22 +1016,22 @@ static void ruby_resolve_super(RubyLSPContext *ctx, TSNode site) {
9671016 const char * mname = strrchr (fq , '.' );
9681017 mname = mname ? mname + 1 : fq ;
9691018 const CBMRegisteredFunc * f = NULL ;
970- bool blocked = false ;
1019+ RubyLookupWalk w = ruby_walk_begin ( ctx ) ;
9711020 /* Search order mirrors Ruby: includes of the class, then superclass
9721021 * chain — the class's OWN def is the caller and must be skipped. An
9731022 * unresolved mixin or superclass on the way blocks the walk. */
9741023 f = ruby_lookup_in_mixins (ctx , ctx -> enclosing_class_qn ,
9751024 ctx -> in_singleton_method ? RUBY_MIXIN_EXTEND : RUBY_MIXIN_INCLUDE ,
976- mname , 0 , & blocked );
977- if (!f && !blocked && !ruby_superclass_unresolved (ctx , ctx -> enclosing_class_qn )) {
1025+ mname , 0 , & w );
1026+ if (!f && !w . blocked && !ruby_superclass_unresolved (ctx , ctx -> enclosing_class_qn )) {
9781027 const char * sup = ruby_superclass_qn (ctx , ctx -> enclosing_class_qn );
9791028 if (sup ) {
9801029 f = ctx -> in_singleton_method
981- ? ruby_lookup_singleton_method_depth (ctx , sup , mname , 0 , & blocked )
982- : ruby_lookup_instance_method_depth (ctx , sup , mname , 0 , & blocked );
1030+ ? ruby_lookup_singleton_method_depth (ctx , sup , mname , 0 , & w )
1031+ : ruby_lookup_instance_method_depth (ctx , sup , mname , 0 , & w );
9831032 }
9841033 }
985- if (f && !blocked )
1034+ if (f && !w . blocked )
9861035 ruby_emit_resolved (ctx , f -> qualified_name , "ruby_method_super" , RUBY_CONF_HIGH , site );
9871036 /* No known parent or unresolved — zero-edge guarantee. */
9881037}
0 commit comments