comparison src/vim9class.c @ 33498:bff8ac203a22 v9.0.1999

patch 9.0.1999: Vim9: some error messages can be improved Commit: https://github.com/vim/vim/commit/e6c9aa5e6a88d539a412a9b5526f41ea101aa185 Author: Ernie Rael <errael@raelity.com> Date: Fri Oct 6 19:55:52 2023 +0200 patch 9.0.1999: Vim9: some error messages can be improved Problem: Vim9: some error messages can be improved Solution: Mention the defining class for variable access error message closes: #13272 Signed-off-by: Christian Brabandt <cb@256bit.org> Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com> Co-authored-by: Ernie Rael <errael@raelity.com>
author Christian Brabandt <cb@256bit.org>
date Fri, 06 Oct 2023 20:00:06 +0200
parents 156d07bea2ac
children f61713271934
comparison
equal deleted inserted replaced
33497:5bb56da92a88 33498:bff8ac203a22
2165 if (m == NULL) 2165 if (m == NULL)
2166 return FAIL; 2166 return FAIL;
2167 2167
2168 if (*name == '_') 2168 if (*name == '_')
2169 { 2169 {
2170 semsg(_(e_cannot_access_private_variable_str), m->ocm_name, 2170 emsg_var_cl_define(e_cannot_access_private_variable_str,
2171 cl->class_name); 2171 m->ocm_name, 0, cl);
2172 return FAIL; 2172 return FAIL;
2173 } 2173 }
2174 2174
2175 // The object only contains a pointer to the class, the member 2175 // The object only contains a pointer to the class, the member
2176 // values array follows right after that. 2176 // values array follows right after that.
2327 return FAIL; 2327 return FAIL;
2328 } 2328 }
2329 2329
2330 if (*name == '_') 2330 if (*name == '_')
2331 { 2331 {
2332 semsg(_(e_cannot_access_private_variable_str), m->ocm_name, 2332 emsg_var_cl_define(e_cannot_access_private_variable_str,
2333 cl->class_name); 2333 m->ocm_name, 0, cl);
2334 return FAIL; 2334 return FAIL;
2335 } 2335 }
2336 2336
2337 typval_T *tv = &cl->class_members_tv[m_idx]; 2337 typval_T *tv = &cl->class_members_tv[m_idx];
2338 copy_tv(tv, rettv); 2338 copy_tv(tv, rettv);
2579 else 2579 else
2580 return object_member_lookup(cl, name, namelen, idx); 2580 return object_member_lookup(cl, name, namelen, idx);
2581 } 2581 }
2582 2582
2583 /* 2583 /*
2584 * Find the class that defines the named member. Look up the hierarchy
2585 * starting at "cl".
2586 *
2587 * Return the class that defines the member "name", else NULL.
2588 * Fill in "p_m", if specified, for ocmember_T in found class.
2589 */
2590 // NOTE: if useful for something could also indirectly return vartype and idx.
2591 static class_T *
2592 class_defining_member(class_T *cl, char_u *name, size_t len, ocmember_T **p_m)
2593 {
2594 class_T *cl_found = NULL;
2595 vartype_T vartype = VAR_UNKNOWN;
2596 ocmember_T *m_found = NULL;
2597
2598 len = len != 0 ? len : STRLEN(name);
2599
2600 // Loop assumes if member is not defined in "cl", then it is not
2601 // defined in any super class; the last class where it's found is the
2602 // class where it is defined. Once the vartype is found, the other
2603 // type is no longer checked.
2604 for (class_T *super = cl; super != NULL; super = super->class_extends)
2605 {
2606 class_T *cl_tmp = NULL;
2607 ocmember_T *m = NULL;
2608 if (vartype == VAR_UNKNOWN || vartype == VAR_OBJECT)
2609 {
2610 if ((m = object_member_lookup(super, name, len, NULL)) != NULL)
2611 {
2612 cl_tmp = super;
2613 vartype = VAR_OBJECT;
2614 }
2615 }
2616 if (vartype == VAR_UNKNOWN || vartype == VAR_CLASS)
2617 {
2618 if (( m = class_member_lookup(super, name, len, NULL)) != NULL)
2619 {
2620 cl_tmp = super;
2621 vartype = VAR_OBJECT;
2622 }
2623 }
2624 if (cl_tmp == NULL)
2625 break; // member is not in this or any super class.
2626 cl_found = cl_tmp;
2627 m_found = m;
2628 }
2629 if (p_m != NULL)
2630 *p_m = m_found;
2631 return cl_found;
2632 }
2633
2634 /*
2584 * Lookup a class or object method by name. If v_type is VAR_CLASS, then 2635 * Lookup a class or object method by name. If v_type is VAR_CLASS, then
2585 * lookup a class method and if it is VAR_OBJECT, then lookup a object method. 2636 * lookup a class method and if it is VAR_OBJECT, then lookup a object method.
2586 * 2637 *
2587 * Returns a pointer to the method structure if variable is found. 2638 * Returns a pointer to the method structure if variable is found.
2588 * Otherwise returns NULL. The method variable index is set in "*idx". 2639 * Otherwise returns NULL. The method variable index is set in "*idx".
2852 next_nonref_obj = NULL; 2903 next_nonref_obj = NULL;
2853 return did_free; 2904 return did_free;
2854 } 2905 }
2855 2906
2856 /* 2907 /*
2908 * Output message which takes a variable name and the class that defines it.
2909 * "cl" is that class where the name was found. Search "cl"'s hierarchy to
2910 * find the defining class.
2911 */
2912 void
2913 emsg_var_cl_define(char *msg, char_u *name, size_t len, class_T *cl)
2914 {
2915 ocmember_T *m;
2916 class_T *cl_def = class_defining_member(cl, name, len, &m);
2917 if (cl_def != NULL)
2918 semsg(_(msg), m->ocm_name, cl_def->class_name);
2919 else
2920 emsg(_(e_internal_error_please_report_a_bug));
2921 }
2922
2923 /*
2857 * Echo a class or object method not found message. 2924 * Echo a class or object method not found message.
2858 */ 2925 */
2859 void 2926 void
2860 method_not_found_msg(class_T *cl, vartype_T v_type, char_u *name, size_t len) 2927 method_not_found_msg(class_T *cl, vartype_T v_type, char_u *name, size_t len)
2861 { 2928 {