@@ -11723,4 +11723,120 @@ def Test_use_object_method_in_a_method_call()
1172311723 v9.CheckSourceFailure (lines , ' E1326: Variable "NewCost" not found in object "Foo"' )
1172411724enddef
1172511725
11726+ " Test for referencing an object variable which is not yet initialized
11727+ def Test_uninitialized_object_var ()
11728+ var lines = << trim END
11729+ vim9script
11730+ class Foo
11731+ const two: number = Foo.Two (this)
11732+ const one: number = 1
11733+
11734+ static def Two (that: Foo): number
11735+ return that.one + 2
11736+ enddef
11737+ endclass
11738+
11739+ echo Foo.Two (Foo.new ())
11740+ END
11741+ v9.CheckSourceFailure (lines , " E1430: Uninitialized object variable 'one' referenced" )
11742+
11743+ lines = << trim END
11744+ vim9script
11745+ class Foo
11746+ const one: number = Foo.One (this)
11747+
11748+ static def One (that: Foo): number
11749+ return 1
11750+ enddef
11751+ endclass
11752+
11753+ assert_equal (1 , Foo.One (Foo.new ()))
11754+ END
11755+ v9.CheckSourceSuccess (lines )
11756+
11757+ lines = << trim END
11758+ vim9script
11759+ class Foo
11760+ const one: number = 1
11761+ const two: number = Foo.Two (this)
11762+
11763+ static def Two (that: Foo): number
11764+ return that.one + 1
11765+ enddef
11766+ endclass
11767+
11768+ assert_equal (2 , Foo.Two (Foo.new ()))
11769+ END
11770+ v9.CheckSourceSuccess (lines )
11771+
11772+ lines = << trim END
11773+ vim9script
11774+ class Foo
11775+ const Id: func (any): any = ((_) = > (v ) = > v )(this)
11776+
11777+ static def Id (that: Foo): func (any): any
11778+ return that.Id
11779+ enddef
11780+ endclass
11781+
11782+ assert_equal (5 , Foo.Id (Foo.new ())(5 ))
11783+ assert_equal (7 , Foo.new ().Id (7 ))
11784+ END
11785+ v9.CheckSourceSuccess (lines )
11786+
11787+ lines = << trim END
11788+ vim9script
11789+ class Foo
11790+ const Id: func (any): any = ((that) = > (_) = > that)(this)
11791+
11792+ static def Id (that: Foo): func (any): any
11793+ return that.Id
11794+ enddef
11795+ endclass
11796+
11797+ const Id0: func (any): any = Foo.Id (Foo.new ())
11798+ const Id1: func (any): any = Foo.new ().Id
11799+ END
11800+ v9.CheckSourceSuccess (lines )
11801+
11802+ lines = << trim END
11803+ vim9script
11804+ class Foo
11805+ const Id: any = Foo.Id (this)
11806+
11807+ static def Id (that: Foo): any
11808+ return that.Id
11809+ enddef
11810+ endclass
11811+
11812+ const Id2: any = Foo.Id (Foo.new ())
11813+ const Id3: any = Foo.new ().Id
11814+ END
11815+ v9.CheckSourceFailure (lines , " E1430: Uninitialized object variable 'Id' referenced" )
11816+
11817+ lines = << trim END
11818+ vim9script
11819+
11820+ class Foo
11821+ var x : string = ' '
11822+ var Y: func (): string = () = > this.x
11823+ endclass
11824+
11825+ var foo = Foo.new (' ok' )
11826+ assert_equal (' ok' , foo.Y ())
11827+ END
11828+ v9.CheckSourceSuccess (lines )
11829+
11830+ lines = << trim END
11831+ vim9script
11832+
11833+ class Foo
11834+ var x : string = this.x
11835+ endclass
11836+
11837+ var foo = Foo.new (' ok' )
11838+ END
11839+ v9.CheckSourceFailure (lines , " E1430: Uninitialized object variable 'x' referenced" )
11840+ enddef
11841+
1172611842" vim: ts = 8 sw = 2 sts = 2 expandtab tw = 80 fdm = marker
0 commit comments