patch 9.1.2101: Vim9: more truthiness issues

Problem:  Vim9: more truthiness issues
          (kennypete)
Solution: Class, enum and typealias cannot be used with the falsy
          operator (Yegappan Lakshmanan)

related: #19213
fixes:   #19173
closes:  #19216

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Yegappan Lakshmanan
2026-01-20 19:46:33 +00:00
committed by Christian Brabandt
parent ff0e5d994c
commit a7d195415b
3 changed files with 68 additions and 2 deletions
+61
View File
@@ -263,6 +263,67 @@ def Test_expr1_falsy()
END
v9.CheckSourceScriptSuccess(lines)
# class cannot be used with the falsy operator
lines =<< trim END
vim9script
class A
endclass
echo A ?? 'falsy'
END
v9.CheckSourceScriptFailure(lines, 'E1405: Class "A" cannot be used as a value')
lines =<< trim END
vim9script
class B
endclass
echo !B
END
v9.CheckSourceScriptFailure(lines, 'E1405: Class "B" cannot be used as a value')
lines =<< trim END
vim9script
echo null_class ?? 'falsy'
END
v9.CheckSourceScriptFailure(lines, 'E1405: Class "" cannot be used as a value')
lines =<< trim END
vim9script
echo !null_class
END
v9.CheckSourceScriptFailure(lines, 'E1405: Class "" cannot be used as a value')
# enum cannot be used with the falsy operator
lines =<< trim END
vim9script
enum E1
endenum
echo E1 ?? 'falsy'
END
v9.CheckSourceScriptFailure(lines, 'E1421: Enum "E1" cannot be used as a value')
lines =<< trim END
vim9script
enum E2
endenum
echo !E2
END
v9.CheckSourceScriptFailure(lines, 'E1421: Enum "E2" cannot be used as a value')
# typealias cannot be used with the falsy operator
lines =<< trim END
vim9script
type T1 = list<bool>
echo T1 ?? 'falsy'
END
v9.CheckSourceScriptFailure(lines, 'E1403: Type alias "T1" cannot be used as a value')
lines =<< trim END
vim9script
type T2 = list<bool>
echo !T2
END
v9.CheckSourceScriptFailure(lines, 'E1403: Type alias "T2" cannot be used as a value')
var msg = "White space required before and after '??'"
call v9.CheckDefAndScriptFailure(["var x = 1?? 'one' : 'two'"], msg, 1)
call v9.CheckDefAndScriptFailure(["var x = 1 ??'one' : 'two'"], msg, 1)
+2
View File
@@ -734,6 +734,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
2101,
/**/
2100,
/**/
+5 -2
View File
@@ -8131,12 +8131,15 @@ tv2bool(typval_T *tv)
case VAR_OBJECT:
return tv->vval.v_object != NULL;
case VAR_CLASS:
case VAR_TYPEALIAS:
check_typval_is_value(tv);
break;
case VAR_UNKNOWN:
case VAR_ANY:
case VAR_VOID:
case VAR_INSTR:
case VAR_CLASS:
case VAR_TYPEALIAS:
break;
}
return FALSE;