comparison src/typval.c @ 25278:55c85c3a43a0 v8.2.3176

patch 8.2.3176: Vim9: no type error for comparing number with string Commit: https://github.com/vim/vim/commit/0c35752d04f70408a3c560d5b3edbafcaddff302 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jul 18 14:43:43 2021 +0200 patch 8.2.3176: Vim9: no type error for comparing number with string Problem: Vim9: no type error for comparing number with string. Solution: Add a runtime type check. (closes https://github.com/vim/vim/issues/8571)
author Bram Moolenaar <Bram@vim.org>
date Sun, 18 Jul 2021 14:45:04 +0200
parents 712e867f9721
children 4d3c68196d05
comparison
equal deleted inserted replaced
25277:df8e28f6b0e1 25278:55c85c3a43a0
935 case EXPR_MATCH: 935 case EXPR_MATCH:
936 default: break; // avoid gcc warning 936 default: break; // avoid gcc warning
937 } 937 }
938 } 938 }
939 else if (in_vim9script() && (typ1->v_type == VAR_BOOL 939 else if (in_vim9script() && (typ1->v_type == VAR_BOOL
940 || typ2->v_type == VAR_BOOL)) 940 || typ2->v_type == VAR_BOOL
941 || (typ1->v_type == VAR_SPECIAL
942 && typ2->v_type == VAR_SPECIAL)))
941 { 943 {
942 if (typ1->v_type != typ2->v_type) 944 if (typ1->v_type != typ2->v_type)
943 { 945 {
944 semsg(_(e_cannot_compare_str_with_str), 946 semsg(_(e_cannot_compare_str_with_str),
945 vartype_name(typ1->v_type), vartype_name(typ2->v_type)); 947 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
953 case EXPR_IS: 955 case EXPR_IS:
954 case EXPR_EQUAL: n1 = (n1 == n2); break; 956 case EXPR_EQUAL: n1 = (n1 == n2); break;
955 case EXPR_ISNOT: 957 case EXPR_ISNOT:
956 case EXPR_NEQUAL: n1 = (n1 != n2); break; 958 case EXPR_NEQUAL: n1 = (n1 != n2); break;
957 default: 959 default:
958 emsg(_(e_invalid_operation_for_bool)); 960 semsg(_(e_invalid_operation_for_str),
961 vartype_name(typ1->v_type));
959 clear_tv(typ1); 962 clear_tv(typ1);
960 return FAIL; 963 return FAIL;
961 } 964 }
962 } 965 }
963 else 966 else
964 { 967 {
968 if (in_vim9script()
969 && ((typ1->v_type != VAR_STRING && typ1->v_type != VAR_SPECIAL)
970 || (typ2->v_type != VAR_STRING && typ2->v_type != VAR_SPECIAL)))
971 {
972 semsg(_(e_cannot_compare_str_with_str),
973 vartype_name(typ1->v_type), vartype_name(typ2->v_type));
974 clear_tv(typ1);
975 return FAIL;
976 }
965 s1 = tv_get_string_buf(typ1, buf1); 977 s1 = tv_get_string_buf(typ1, buf1);
966 s2 = tv_get_string_buf(typ2, buf2); 978 s2 = tv_get_string_buf(typ2, buf2);
967 if (type != EXPR_MATCH && type != EXPR_NOMATCH) 979 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
968 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2); 980 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
969 else 981 else