View Ticket
Not logged in
Ticket UUID: c0bc269178df938974e19cbe60e69fc0e711da5
Title: switch -regexp -matchvar -indexvar broken when optional subexpression does not match
Type: Bug Version: 8.6.13
Submitter: apnadkarni Created on: 2022-09-20 18:40:42
Subsystem: 18. Commands M-Z Assigned To: jan.nijtmans
Priority: 7 High Severity: Minor
Status: Closed Last Modified: 2022-09-21 13:15:16
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2022-09-21 13:15:16
Description:
The third element of m below should be the empty string as the (b)? is not
matched.

```
% switch -regexp -matchvar m -indexvar i ac {
{(a)(b)?(c)} {puts m:$m; puts i:$i}
}
m:ac a ac c
i:{0 1} {0 0} {-1 -1} {1 1}
% info patch
8.7a6
```

core-8-6-branch also exhibits this bug.

**However, 8.6.12 shows correct behavior**

```
D:\src\ruff\doc\nroff>d:\tcl\magic\bin\tclsh.exe
% switch -regexp -matchvar m -indexvar i ac {
{(a)(b)?(c)} {puts m:$m; puts i:$i}
}
m:ac a {} c
i:{0 1} {0 0} {-1 -1} {1 1}
% info patch
8.6.12
```

So clearly something was broken post-8.6.12 release.
User Comments: jan.nijtmans added on 2022-09-21 13:15:16:

Fix committed [92ab43ecd6cdd9a4|here]


jan.nijtmans added on 2022-09-20 20:44:39:

Proposed fix [ea165f0333f93885|here]

Thanks for the report, and for the testcase!


dgp added on 2022-09-20 19:06:49:
How did Tcl behave in release 8.6.10 ?

See TIP 623 for more background.

apnadkarni added on 2022-09-20 18:50:44:
The bug seems to have been introduced with [this](https://core.tcl-lang.org/tcl/info/26539e78a71993cc) checkin on this [line](https://core.tcl-lang.org/tcl/file?ci=94f71d598512687b&name=generic/tclStringObj.c&ln=823).

However, it seems a deliberate change so not clear to me that just reverting is the correct fix.

/Ashok