@@ -31,7 +31,7 @@ use noirc_frontend::{
3131 } ,
3232 } ,
3333 hir_def:: traits:: Trait ,
34- node_interner:: { NodeInterner , ReferenceId , StructId } ,
34+ node_interner:: { FuncId , NodeInterner , ReferenceId , StructId } ,
3535 parser:: { Item , ItemKind , ParsedSubModule } ,
3636 token:: { MetaAttribute , Token , Tokens } ,
3737 Kind , ParsedModule , StructType , Type , TypeBinding ,
@@ -120,6 +120,8 @@ struct NodeFinder<'a> {
120120 use_segment_positions : UseSegmentPositions ,
121121 self_type : Option < Type > ,
122122 in_comptime : bool ,
123+ /// The function we are in, if any
124+ func_id : Option < FuncId > ,
123125}
124126
125127impl < ' a > NodeFinder < ' a > {
@@ -165,6 +167,7 @@ impl<'a> NodeFinder<'a> {
165167 use_segment_positions : UseSegmentPositions :: default ( ) ,
166168 self_type : None ,
167169 in_comptime : false ,
170+ func_id : None ,
168171 }
169172 }
170173
@@ -639,6 +642,13 @@ impl<'a> NodeFinder<'a> {
639642 function_completion_kind : FunctionCompletionKind ,
640643 self_prefix : bool ,
641644 ) {
645+ self . complete_trait_constraints_methods (
646+ typ,
647+ prefix,
648+ function_kind,
649+ function_completion_kind,
650+ ) ;
651+
642652 let Some ( methods_by_name) = self . interner . get_type_methods ( typ) else {
643653 return ;
644654 } ;
@@ -697,6 +707,31 @@ impl<'a> NodeFinder<'a> {
697707 }
698708 }
699709
710+ fn complete_trait_constraints_methods (
711+ & mut self ,
712+ typ : & Type ,
713+ prefix : & str ,
714+ function_kind : FunctionKind ,
715+ function_completion_kind : FunctionCompletionKind ,
716+ ) {
717+ let Some ( func_id) = self . func_id else {
718+ return ;
719+ } ;
720+
721+ let func_meta = self . interner . function_meta ( & func_id) ;
722+ for constraint in & func_meta. trait_constraints {
723+ if * typ == constraint. typ {
724+ let trait_ = self . interner . get_trait ( constraint. trait_bound . trait_id ) ;
725+ self . complete_trait_methods (
726+ trait_,
727+ prefix,
728+ function_kind,
729+ function_completion_kind,
730+ ) ;
731+ }
732+ }
733+ }
734+
700735 fn complete_trait_methods (
701736 & mut self ,
702737 trait_ : & Trait ,
@@ -1125,8 +1160,17 @@ impl<'a> Visitor for NodeFinder<'a> {
11251160 let old_in_comptime = self . in_comptime ;
11261161 self . in_comptime = noir_function. def . is_comptime ;
11271162
1163+ if let Some ( ReferenceId :: Function ( func_id) ) = self
1164+ . interner
1165+ . reference_at_location ( Location :: new ( noir_function. name_ident ( ) . span ( ) , self . file ) )
1166+ {
1167+ self . func_id = Some ( func_id) ;
1168+ }
1169+
11281170 noir_function. def . body . accept ( Some ( span) , self ) ;
11291171
1172+ self . func_id = None ;
1173+
11301174 self . in_comptime = old_in_comptime;
11311175 self . type_parameters = old_type_parameters;
11321176 self . self_type = None ;
@@ -1207,7 +1251,7 @@ impl<'a> Visitor for NodeFinder<'a> {
12071251
12081252 fn visit_trait_item_function (
12091253 & mut self ,
1210- _name : & Ident ,
1254+ name : & Ident ,
12111255 generics : & UnresolvedGenerics ,
12121256 parameters : & [ ( Ident , UnresolvedType ) ] ,
12131257 return_type : & noirc_frontend:: ast:: FunctionReturnType ,
@@ -1232,7 +1276,16 @@ impl<'a> Visitor for NodeFinder<'a> {
12321276 for ( name, _) in parameters {
12331277 self . local_variables . insert ( name. to_string ( ) , name. span ( ) ) ;
12341278 }
1279+
1280+ if let Some ( ReferenceId :: Function ( func_id) ) =
1281+ self . interner . reference_at_location ( Location :: new ( name. span ( ) , self . file ) )
1282+ {
1283+ self . func_id = Some ( func_id) ;
1284+ }
1285+
12351286 body. accept ( None , self ) ;
1287+
1288+ self . func_id = None ;
12361289 } ;
12371290
12381291 self . type_parameters = old_type_parameters;
0 commit comments