Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: b878790f250b4e425b0f3226d79383b8f2e801de
Ticket: 0f42ff78717afacc72710ec0c79aa71a48abb5f3
[tailcall] punches through [next]
User & Date: aspect on 2015-05-12 05:47:44
Changes

  1. icomment:
    Starting from dkf's examples, I have tried to make a comprehensive test suite
    for tcloo + tailcall interaction.  This should cover all the significant parts
    visible on the script level .. if I've missed anything, feel free to add it!
    
    These tests all pass if compilation of [next] is inhibited by uncommenting the
    block near the top.  Otherwise, every test involving [next] fails in exactly the same way.  This suggests to me we're dealing with a single bug here, and
    the proposed fix at [5c3a32ed23] is sound.
    
    Things I still don't understand:
    
      * testnrelevels - clearly needs to be included with these tests, but I'm not
        clear how to set up good tests (ref [0527225cc]).
      * tailcall under catch, uplevel or both requires careful consideration, but
        I don't think that directly impacts this issue.  I'm investigating those
        separately.
    
    ======
    # dkf's "Principles Leading to a Fix"
    #
    #   A method ought to work "the same" whether or not it has been overridden by
    #   a subclass. A tailcalled command ought to have as parent stack the same
    #   thing you'd get with uplevel 1. A subclass will often expect the
    #   superclass's result to be the result that would be returned if the subclass
    #   was not there. 
    #
    package require tcltest
    namespace import -force ::tcltest::*
    
    ;# uncomment this to inhibit compilation of [next] and [nextto]
    ;# .. which permits all the tests to pass
    ;# A simpler, per-test inhibition is possible by using
    ;# [[string cat next]] inline.
    if 0 {
        rename ::oo::Helpers::next ::oo::Helpers::_next
        proc ::oo::Helpers::next args {
            tailcall _next {*}$args
        }
        rename ::oo::Helpers::nextto ::oo::Helpers::_nextto
        proc ::oo::Helpers::nextto args {
            tailcall _nextto {*}$args
        }
    }
    
    ;# common setup:
    ;#  any invocation of bar should emit "abc\nhi\n" then return to its caller
    set testopts {
        -setup {
            oo::class create Foo {
                method bar {} {
                    puts abc
                    tailcall puts hi
                    puts xyz
                }
            }
        }
        -cleanup {
            catch {Foo destroy}
            catch {Foo2 destroy}    ;# created by some tests
        }
    }
    
    ;# these succeed, showing that without [next] the bug doesn't fire
    test next-tailcall-simple-1 "trivial case with one method" {*}$testopts -body {
        [Foo create foo] bar
    } -output [join {abc hi} \n]\n
    
    test next-tailcall-simple-2 "my bar" {*}$testopts -body {
        oo::define Foo method baz {} {
            puts a
            my bar
            puts b
        }
        [Foo create foo] baz
    } -output [join {a abc hi b} \n]\n
    
    test next-tailcall-simple-3 "\[self\] bar" {*}$testopts -body {
        oo::define Foo method baz {} {
            puts a
            [self] bar
            puts b
        }
        [Foo create foo] baz
    } -output [join {a abc hi b} \n]\n
    
    test next-tailcall-simple-4 "foo bar" {*}$testopts -body {
        oo::define Foo method baz {} {
            puts a
            foo bar
            puts b
        }
        [Foo create foo] baz
    } -output [join {a abc hi b} \n]\n
    
    
    ;# everything from here on uses [next], and fails on 8.6.4 with compilation
    test next-tailcall-superclass-1 "next superclass" {*}$testopts -body {
        oo::class create Foo2 {
            superclass Foo
            method bar {} {
                puts a
                next
                puts b
            }
        }
        [Foo2 create foo] bar
    } -output [join {a abc hi b} \n]\n
    
    test next-tailcall-superclass-2 "nextto superclass" {*}$testopts -body {
        oo::class create Foo2 {
            superclass Foo
            method bar {} {
                puts a
                nextto Foo
                puts b
            }
        }
        [Foo2 create foo] bar
    } -output [join {a abc hi b} \n]\n
    
    
    test next-tailcall-mixin-1 "class mixin" {*}$testopts -body {
        oo::class create Foo2 {
            method Bar {} {
                puts a
                next
                puts b
            }
            filter Bar
        }
        oo::define Foo mixin Foo2
        Foo create foo
        foo bar
    } -output [join {a abc hi b} \n]\n
    
    test next-tailcall-objmixin-1 "object mixin" {*}$testopts -body {
        oo::class create Foo2 {
            method Bar {} {
                puts a
                next
                puts b
            }
            filter Bar
        }
        Foo create foo
        oo::objdefine foo mixin Foo2
        foo bar
    } -output [join {a abc hi b} \n]\n
    
    test next-tailcall-filter-1 "filter method" {*}$testopts -body {
        oo::define Foo method Filter {} {
            puts a
            next
            puts b
        }
        oo::define Foo filter Filter
        [Foo new] bar
    } -output [join {a abc hi b} \n]\n
    
    test next-tailcall-forward-1 "forward method" {*}$testopts -body {
        proc foobar {} {
            puts "abc"
            tailcall puts "hi"
            puts "xyz"
        }
        oo::define Foo forward foobar foobar
        oo::class create Foo2 {
            superclass Foo
            method foobar {} {
                puts a
                next
                puts b
            }
        }
        [Foo2 new] foobar
    } -output [join {a abc hi b} \n]\n
    
    test next-tailcall-constructor-1 "next in constructor" -body {
        oo::class create Foo {
            constructor {} {
                puts abc
                tailcall puts hi
                puts xyz
            }
        }
        oo::class create Foo2 {
            superclass Foo
            constructor {} {
                puts a
                next
                puts b
            }
        }
        list [Foo new] [Foo2 new]
        return ""
    } -cleanup {
        Foo destroy
    } -output [join {abc hi a abc hi b} \n]\n
    
    
    test next-tailcall-destructor-1 "next in destructor" -body {
        oo::class create Foo {
            destructor {
                puts abc
                tailcall puts hi
                puts xyz
            }
        }
        oo::class create Foo2 {
            superclass Foo
            destructor {
                puts a
                next
                puts b
            }
        }
        Foo  create foo
        Foo2 create foo2
        foo destroy
        foo2 destroy
    } -output [join {abc hi a abc hi b} \n]\n
    
    ======
    
  2. login: "aspect"
  3. mimetype: "text/plain"