Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-03-14
04:52 • Closed ticket [8f741de59d]: switch compilation difference with end of opts marke... artifact: de939e7188 user: apnadkarni
2025-12-09
09:00 • Ticket [8f741de59d]: 3 changes artifact: 9daedf38f9 user: jan.nijtmans
2025-11-10
08:47 • New ticket [8f741de59d]. artifact: 8bdba0dc55 user: juliannoble2

switch compilation difference with end of opts marker

Closed / Wont Fix8f741de59dbc79b0d6e1b71abe338391081492f5Created 1 year agoUpdated 264 days ago
Descriptionplain2025-11-10 08:47:37
The below comment block in tclCompCmdSz.c suggests the intention is for 

switch -exact    word {pattern body ...}
to be treated the same as
switch -exact -- word {pattern body ...}

as far as compilation is concerned.
Similarly for -glob and -regexp

However, the -- seems to be required for the compilation to proceed
as after the option processing loop we have:
    if (numWords < 3) {
	return TCL_ERROR;
    }


#comments from tclCompCmdsSZ.c TclCompileSwitchCmd

#    /*
#     * Only handle the following versions:
#     *   switch         ?--? word {pattern body ...}
#     *   switch -exact  ?--? word {pattern body ...}
#     *   switch -glob   ?--? word {pattern body ...}
#     *   switch -regexp ?--? word {pattern body ...}
#     *   switch         --   word simpleWordPattern simpleWordBody ...
#     *   switch -exact  --   word simpleWordPattern simpleWordBody ...
#     *   switch -glob   --   word simpleWordPattern simpleWordBody ...
#     *   switch -regexp --   word simpleWordPattern simpleWordBody ...
#     * When the mode is -glob, can also handle a -nocase flag.
#     *
#     * First off, we don't care how the command's word was generated; we're
#     * compiling it anyway! So skip it...
#     */


It is verifiable with tcl::unsupported::disassemble that (for the combined pattern body versions)
switch -exact     word {pattern body ...}
compiles differently to
switch -exact --  word {pattern body ...}
(as with -glob and -exact)

Is this intentional?   It seems at odds with the comment block and also the switch man page states for the end of options marker:
"This is not required when the matching patterns and bodies are grouped together in a single argument."

Well it's not *required* as such - but it seems to me it is required if you want the best performance from a switch statement.
jan.nijtmanswiki2025-12-09 09:00:25

> Is this intentional?

I don't think this is intentional. Would you like to provide a patch? That would be the quickest way to resolve this (anyone else has to dive into this, which you already did ....)

apnadkarnimarkdown2026-03-14 04:52:57

Julian, I believe this is intentional based on these comments.

Please re-open if you disagree with the ease of implementation reference in the comment referenced.

Log in (anonymous is fine) to add a comment.