Skip to content

Commit 450c7a9

Browse files
committed
patch 9.0.1207: error when object type is expected but getting "any"
Problem: Error when object type is expected but getting "any". Solution: When actual type is "any" use a runtime type check. (closes #11826)
1 parent 5a57a5e commit 450c7a9

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/testdir/test_vim9_class.vim

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,26 @@ def Test_object_type()
537537
assert_equal(5, o.GetMember())
538538
END
539539
v9.CheckScriptSuccess(lines)
540+
541+
lines =<< trim END
542+
vim9script
543+
544+
class Num
545+
this.n: number = 0
546+
endclass
547+
548+
def Ref(name: string): func(Num): Num
549+
return (arg: Num): Num => {
550+
return eval(name)(arg)
551+
}
552+
enddef
553+
554+
const Fn = Ref('Double')
555+
var Double = (m: Num): Num => Num.new(m.n * 2)
556+
557+
echo Fn(Num.new(4))
558+
END
559+
v9.CheckScriptSuccess(lines)
540560
enddef
541561

542562
def Test_class_member()

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ static char *(features[]) =
695695

696696
static int included_patches[] =
697697
{ /* Add new patch number below this line */
698+
/**/
699+
1207,
698700
/**/
699701
1206,
700702
/**/

src/vim9type.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,11 @@ check_type_maybe(
878878
}
879879
else if (expected->tt_type == VAR_OBJECT)
880880
{
881+
if (actual->tt_type == VAR_ANY)
882+
return MAYBE; // use runtime type check
883+
if (actual->tt_type != VAR_OBJECT)
884+
return FAIL; // don't use tt_member
885+
881886
// check the class, base class or an implemented interface matches
882887
class_T *cl;
883888
for (cl = (class_T *)actual->tt_member; cl != NULL;

0 commit comments

Comments
 (0)