Tcl Source Code

View Ticket
Login
Ticket UUID: 5d5b1052280c976ea3d4873c2088e6ec13049472
Title: execution trace on nested switch - inconsistent line depending on options
Type: Bug Created on: 2025-11-10 08:02:44
Submitter: juliannoble2 Assigned to: nobody
Subsystem: 18. Commands M-Z Severity: Important
Priority: 5 Medium Last modified: 2026-07-14 05:58:59
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.6+
Description:
For a nested switch statement such as the one below,
The reported line for a frame of type eval during an execution enterstep trace does not match the line in the matched script, for the first 1, 2 or 3 pattern scripts.

This is not related to the source file lines - as the situation arises when the proc has been loaded by some means such that source file line numbers are not available.
(see test script switch_test.tcl attached)

It is also probably not related to line-continuations - another source of apparent mismatches, as those have not been used in this test.

This issue occurs when patterns and scripts are passed as a single block - it does not seem to occur when the separated pattern script arguments are used.

Depending on the number of options used on the nested switch - different results will occur.
Usually the first 2 script blocks will have misreported line numbers in all 'eval' callbacks.
    
The switch statements in the test script are all of the basic form as shown in the script below,
the only difference being the options passed before the string argument.

In the case below, the line number for the return arms aa and ab are misreported as 4
The subsequent arms ac and ad report 2 correctly, and when extended to more pattern/scripts there are no further bad line numbers (even if script blocks are modified to have comments, empty lines, and more code)
The largest number of incorrect results I have seen is 3, in the 5 argument case (switch -exact -nocase -- $string $patternblock)

The fact that subsequent arms after the first 2 or 3 can always correctly report line numbers relative to the start of each sub script, and that the error does not occur at all in the 2 argument case (switch $string $patternblock) suggests to me that there probably shouldn't fundamentally be a reason why the first arms can't report line numbers correctly.

 
    #3arg: one switch + matchstring + single patternscript body
    proc switch_nested_3arg {s} {
        set ch1 [::tcl::string::index $s 0]
        switch $ch1 {
            a {
                set ch2 [::tcl::string::index $s 1]
                switch  -- $ch2 {
                    a {
                        return aa
                    }
                    b {
                        return ab
                    }
                    c {
                        return ac
                    }
                    d {
                        return ad
                    }
                }
            }
            default {
                return _
            }
        }
    }
User Comments:
juliannoble2 added on 2026-07-14 05:58:59:
Fable 5 pure tcl repro

http://paste.tclers.tk/6227

Attachments: