@@ -3633,6 +3633,8 @@ typedef struct {
36333633 const char * module_qn ;
36343634 const char * * imports ;
36353635 int import_count ;
3636+ /* Inferring an initializer's type: a label match is a guess, never a type. */
3637+ bool strict ;
36363638} swift_inference_t ;
36373639
36383640static const char * swift_initializer_type (const swift_inference_t * context ,
@@ -3686,11 +3688,17 @@ static const char *swift_binding_cached_type(swift_binding_work_t *work,
36863688 return "" ;
36873689}
36883690
3689- /* Conflicting block-local bindings stay unknown rather than selecting a
3690- * same-name method on either type. */
3691+ /* Where a receiver's type came from: no binding, a declaration (an annotation,
3692+ * a parameter, a constructor call), or only an initializer whose type was
3693+ * inferred from the call it makes. */
3694+ typedef enum { SWIFT_ORIGIN_NONE , SWIFT_ORIGIN_DECLARED , SWIFT_ORIGIN_INFERRED } swift_origin_t ;
3695+
3696+ /* Conflicting block-local bindings ("") select no same-name method on either
3697+ * type. A binding whose initializer type could not be inferred leaves the
3698+ * receiver unknown (NULL), so the call keeps the label match (#2061). */
36913699static const char * swift_binding_type (const swift_inference_t * context , swift_binding_work_t * work ,
36923700 const char * name , const char * scope , const char * class_qn ,
3693- const char * module_qn , bool field ) {
3701+ const char * module_qn , bool field , swift_origin_t * origin ) {
36943702 const CBMFileResult * file = context -> file ;
36953703 int best = 0 ;
36963704 for (int i = 0 ; i < file -> type_assigns .count ; i ++ ) {
@@ -3700,22 +3708,33 @@ static const char *swift_binding_type(const swift_inference_t *context, swift_bi
37003708 best = rank ;
37013709 }
37023710 }
3711+ * origin = best ? SWIFT_ORIGIN_INFERRED : SWIFT_ORIGIN_NONE ;
37033712 if (!best ) {
37043713 return NULL ;
37053714 }
37063715 const char * type = NULL ;
3716+ bool unknown = false;
37073717 for (int i = 0 ; i < file -> type_assigns .count ; i ++ ) {
37083718 const CBMTypeAssign * a = & file -> type_assigns .items [i ];
37093719 if (swift_assignment_rank (a , name , scope , class_qn , module_qn , field ) != best ) {
37103720 continue ;
37113721 }
3712- const char * candidate = a -> type_name ? a -> type_name : swift_binding_cached_type (work , a );
3722+ const char * candidate = a -> type_name ;
3723+ if (candidate ) {
3724+ * origin = SWIFT_ORIGIN_DECLARED ;
3725+ } else {
3726+ candidate = swift_binding_cached_type (work , a );
3727+ if (!candidate [0 ]) {
3728+ unknown = true;
3729+ continue ;
3730+ }
3731+ }
37133732 if (type && strcmp (type , candidate ) != 0 ) {
37143733 return "" ;
37153734 }
37163735 type = candidate ;
37173736 }
3718- return type ;
3737+ return unknown ? NULL : type ;
37193738}
37203739
37213740static void swift_enclosing_class (const CBMCall * call , char * class_qn ) {
@@ -3734,8 +3753,9 @@ static void swift_enclosing_class(const CBMCall *call, char *class_qn) {
37343753
37353754static const char * swift_file_receiver (const swift_inference_t * context , swift_binding_work_t * work ,
37363755 const CBMCall * call , const char * module_qn , char * expression ,
3737- char * class_qn , char * * member ) {
3756+ char * class_qn , char * * member , swift_origin_t * origin ) {
37383757 swift_enclosing_class (call , class_qn );
3758+ * origin = SWIFT_ORIGIN_DECLARED ;
37393759 char * dot = strchr (expression , '.' );
37403760 * dot = '\0' ;
37413761 * member = dot + SKIP_ONE ;
@@ -3746,13 +3766,14 @@ static const char *swift_file_receiver(const swift_inference_t *context, swift_b
37463766 }
37473767 * dot = '\0' ;
37483768 const char * type = swift_binding_type (context , work , * member , call -> enclosing_func_qn ,
3749- class_qn , module_qn , true);
3769+ class_qn , module_qn , true, origin );
37503770 * member = dot + SKIP_ONE ;
37513771 return type ;
37523772 }
37533773 const char * type = swift_binding_type (context , work , expression , call -> enclosing_func_qn ,
3754- class_qn , module_qn , false);
3755- if (!type && isupper ((unsigned char )expression [0 ])) {
3774+ class_qn , module_qn , false, origin );
3775+ if (* origin == SWIFT_ORIGIN_NONE && isupper ((unsigned char )expression [0 ])) {
3776+ * origin = SWIFT_ORIGIN_DECLARED ;
37563777 return expression ;
37573778 }
37583779 return type ;
@@ -3867,6 +3888,34 @@ static const char *swift_function_return_type(const swift_inference_t *context,
38673888 return NULL ; /* No declaring-file imports are available for a remote free function. */
38683889}
38693890
3891+ /* `func run<Result>() -> Result`: the callee's own generic parameter names no
3892+ * type, so the value it returns stays unknown (#2061). The QN keeps the
3893+ * declaration's generic clause unless the signature was capped. */
3894+ static bool swift_returns_generic_parameter (const char * qn , const char * member ,
3895+ const char * returned ) {
3896+ char nominal [CBM_SZ_4K ];
3897+ if (!member || !swift_receiver_type_text (returned , nominal , sizeof (nominal ))) {
3898+ return false;
3899+ }
3900+ const char * clause = qn + cbm_qn_callable_base_len_named (qn , member );
3901+ size_t length = clause [0 ] == '<' ? swift_generic_prefix_len (clause , strlen (clause )) : 0 ;
3902+ size_t name_length = strlen (nominal );
3903+ unsigned depth = 0 ;
3904+ for (size_t i = SKIP_ONE ; i + SKIP_ONE < length && clause [i ] != ';' ; i ++ ) {
3905+ bool entry = depth == 0 && (i == SKIP_ONE || clause [i - SKIP_ONE ] == ',' );
3906+ if (entry && strncmp (clause + i , nominal , name_length ) == 0 && clause [i + name_length ] &&
3907+ strchr (":,;>" , clause [i + name_length ])) {
3908+ return true;
3909+ }
3910+ if (clause [i ] == '<' ) {
3911+ depth ++ ;
3912+ } else if (clause [i ] == '>' && clause [i - SKIP_ONE ] != '=' && depth ) {
3913+ depth -- ;
3914+ }
3915+ }
3916+ return false;
3917+ }
3918+
38703919static const char * swift_candidates_return_type (const swift_inference_t * context , int count ,
38713920 const char * * out , const char * receiver ,
38723921 const char * member ) {
@@ -3875,7 +3924,8 @@ static const char *swift_candidates_return_type(const swift_inference_t *context
38753924 const char * spelling = NULL ;
38763925 for (int i = 0 ; i < count ; i ++ ) {
38773926 const char * returned = swift_chain_return_spelling (r , out [i ]);
3878- if (!returned || (spelling && strcmp (spelling , returned ) != 0 )) {
3927+ if (!returned || (spelling && strcmp (spelling , returned ) != 0 ) ||
3928+ swift_returns_generic_parameter (out [i ], member , returned )) {
38793929 return NULL ;
38803930 }
38813931 const char * label = cbm_ht_get (r -> exact , out [i ]);
@@ -3970,12 +4020,15 @@ static int swift_initializer_member_candidates(const swift_inference_t *context,
39704020 }
39714021 (void )snprintf (expression , sizeof (expression ), "%s" , call -> callee_name );
39724022 char * member = NULL ;
3973- const char * type =
3974- swift_file_receiver (context , work , call , context -> module_qn , expression , owner , & member );
4023+ swift_origin_t origin ;
4024+ const char * type = swift_file_receiver (context , work , call , context -> module_qn , expression ,
4025+ owner , & member , & origin );
39754026 if (!type || !type [0 ]) {
39764027 return SWIFT_INCOMPATIBLE ; /* Unknown roots cannot establish an initializer's type. */
39774028 }
3978- return swift_candidates_for_file (context , work , call , out , REG_MAX_CANDIDATES );
4029+ swift_inference_t strict = * context ;
4030+ strict .strict = true;
4031+ return swift_candidates_for_file (& strict , work , call , out , REG_MAX_CANDIDATES );
39794032}
39804033
39814034static int swift_initializer_candidates (const swift_inference_t * context ,
@@ -4078,6 +4131,44 @@ static bool swift_call_uses_named_receiver(const CBMFileResult *file, const CBMC
40784131 (isupper ((unsigned char )call -> callee_name [0 ]) && !strchr (call -> callee_name , '(' ));
40794132}
40804133
4134+ /* Walk the receiver chain from its root type: each `name(...)` segment
4135+ * continues on the return type its compatible candidates agree on. */
4136+ static int swift_receiver_candidates (const swift_inference_t * context , const CBMCall * call ,
4137+ char * expression , const char * type , char * member ,
4138+ const char * * out , int out_cap ) {
4139+ const cbm_registry_t * r = context -> registry ;
4140+ bool from_chain = r -> swift_owners && cbm_ht_has (r -> swift_owners -> by_qn , type );
4141+ while (type && type [0 ]) {
4142+ char type_text [CBM_SZ_4K ];
4143+ if (!from_chain && !swift_receiver_type_text (type , type_text , sizeof (type_text ))) {
4144+ return SWIFT_INCOMPATIBLE ;
4145+ }
4146+ const char * receiver =
4147+ from_chain
4148+ ? cbm_ht_get_key (r -> exact , type )
4149+ : swift_receiver_qn (r , context -> file , context -> graph , type_text , context -> module_qn ,
4150+ context -> imports , context -> import_count );
4151+ if (!receiver ) {
4152+ return SWIFT_INCOMPATIBLE ; /* known non-project receiver */
4153+ }
4154+ char * args = strchr (member , '(' );
4155+ if (!args ) {
4156+ return swift_typed_candidates (r , context -> graph , context -> file , context -> module_qn ,
4157+ call , receiver , member , out , out_cap );
4158+ }
4159+ char * after = swift_after_arguments (args );
4160+ if (!after || * after != '.' ) {
4161+ return SWIFT_INCOMPATIBLE ;
4162+ }
4163+ * args = '\0' ;
4164+ const CBMCall * inner = swift_inner_call (context -> file , call , (size_t )(args - expression ));
4165+ type = swift_chain_return (context , inner , receiver , member , out , out_cap );
4166+ from_chain = true;
4167+ member = after + SKIP_ONE ;
4168+ }
4169+ return SWIFT_INCOMPATIBLE ;
4170+ }
4171+
40814172static int swift_candidates_for_file (const swift_inference_t * context , swift_binding_work_t * work ,
40824173 const CBMCall * call , const char * * out , int out_cap ) {
40834174 const cbm_registry_t * r = context -> registry ;
@@ -4104,40 +4195,22 @@ static int swift_candidates_for_file(const swift_inference_t *context, swift_bin
41044195 }
41054196 (void )snprintf (expression , sizeof (expression ), "%s" , call -> callee_name );
41064197 char * member = NULL ;
4198+ swift_origin_t origin ;
41074199 const char * type =
4108- swift_file_receiver (context , work , call , module_qn , expression , class_qn , & member );
4200+ swift_file_receiver (context , work , call , module_qn , expression , class_qn , & member , & origin );
41094201 if (!type ) {
41104202 return cbm_registry_swift_candidates (r , call , module_qn , import_vals , import_count , out ,
41114203 out_cap );
41124204 }
4113- bool from_chain = r -> swift_owners && cbm_ht_has (r -> swift_owners -> by_qn , type );
4114- while (type && type [0 ]) {
4115- char type_text [CBM_SZ_4K ];
4116- if (!from_chain && !swift_receiver_type_text (type , type_text , sizeof (type_text ))) {
4117- return SWIFT_INCOMPATIBLE ;
4118- }
4119- const char * receiver = from_chain ? cbm_ht_get_key (r -> exact , type )
4120- : swift_receiver_qn (r , file , graph , type_text , module_qn ,
4121- import_vals , import_count );
4122- if (!receiver ) {
4123- return SWIFT_INCOMPATIBLE ; /* known non-project receiver */
4124- }
4125- char * args = strchr (member , '(' );
4126- if (!args ) {
4127- return swift_typed_candidates (r , graph , file , module_qn , call , receiver , member , out ,
4128- out_cap );
4129- }
4130- char * after = swift_after_arguments (args );
4131- if (!after || * after != '.' ) {
4132- return SWIFT_INCOMPATIBLE ;
4133- }
4134- * args = '\0' ;
4135- const CBMCall * inner = swift_inner_call (file , call , (size_t )(args - expression ));
4136- type = swift_chain_return (context , inner , receiver , member , out , out_cap );
4137- from_chain = true;
4138- member = after + SKIP_ONE ;
4205+ int found = swift_receiver_candidates (context , call , expression , type , member , out , out_cap );
4206+ /* A type inferred from an initializer call only narrows the label match:
4207+ * when it has no member with this name and these labels, keep the label
4208+ * match, as before that inference existed (#2061). */
4209+ if (found <= 0 && origin == SWIFT_ORIGIN_INFERRED && !context -> strict ) {
4210+ return cbm_registry_swift_candidates (r , call , module_qn , import_vals , import_count , out ,
4211+ out_cap );
41394212 }
4140- return SWIFT_INCOMPATIBLE ;
4213+ return found ;
41414214}
41424215
41434216int cbm_registry_swift_candidates_in_file (const cbm_registry_t * r , const CBMCall * call ,
0 commit comments