Skip to content
Tcl Source Code Artifact [17176c75b4]
Login

Artifact [17176c75b4]

Artifact 17176c75b498ac77a34aaf9879e22935e09da1132516a33c228499e7c81f56bb:


--- a/generic/tclCompExpr.c	2026-10-13 14:16:06.760027576 +0200
+++ b/generic/tclCompExpr.c	2026-10-13 14:16:06.762429339 +0200
@@ -522,6 +522,7 @@
 			    CompileEnv *envPtr, int optimize);
 static const char *	FindTupleTarget(const char *start, Tcl_Size numBytes);
 static int		IsTypeAhead(const char *start, Tcl_Size numBytes);
+static Tcl_Size		ElementTargetLen(const char *start, Tcl_Size numBytes);
 static void		EmitTupleStores(CompileEnv *envPtr, OpNode *nodes,
 			    int index, int *path, int depth, int *counter,
 			    int root);
@@ -638,6 +639,50 @@
 /*
  *----------------------------------------------------------------------
  *
+ * ElementTargetLen --
+ *
+ *	START points at the '(' directly following a bareword. If the
+ *	parenthesized text is followed by a single "=" or by the "is"
+ *	operator, return its length (so that "z(re)" is a variable name),
+ *	else 0.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static Tcl_Size
+ElementTargetLen(
+    const char *start,
+    Tcl_Size numBytes)
+{
+    Tcl_Size i = 0, n;
+    int depth = 0;
+
+    while (i < numBytes) {
+	if (start[i] == '(') {
+	    depth++;
+	} else if (start[i] == ')' && --depth == 0) {
+	    i++;
+	    break;
+	}
+	i++;
+    }
+    if (depth != 0) {
+	return 0;
+    }
+    n = TclParseAllWhiteSpace(start + i, numBytes - i);
+    if (i + n < numBytes && start[i + n] == '='
+	    && (i + n + 1 >= numBytes || start[i + n + 1] != '=')) {
+	return i;
+    }
+    if (IsTypeAhead(start + i, numBytes - i)) {
+	return i;
+    }
+    return 0;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
  * FindTupleTarget --
  *
  *	Destructuring assignment "(a,b,c) = $list". START points at an open
@@ -972,7 +1017,21 @@
 		 * is a legal literal boolean value, we accept that as well.
 		 */
 
-		if (start[scanned+TclParseAllWhiteSpace(
+		if (literal && start[scanned] == '(' && ElementTargetLen(
+			start + scanned, numBytes - scanned) > 0) {
+		    /* "z(re) = value", "a(1) is int": array element or field
+		     * name used as a variable name. */
+		    Tcl_Size total = scanned + ElementTargetLen(
+			    start + scanned, numBytes - scanned);
+
+		    Tcl_DecrRefCount(literal);
+		    literal = Tcl_NewStringObj(start, total);
+		    Tcl_ListObjAppendElement(NULL, litList, literal);
+		    complete = lastParsed = OT_LITERAL;
+		    start += total;
+		    numBytes -= total;
+		    continue;
+		} else if (start[scanned+TclParseAllWhiteSpace(
 			start+scanned, numBytes-scanned)] == '(') {
 		    lexeme = FUNCTION;
 
@@ -2879,6 +2938,17 @@
 		}
 		break;
 	    case IS_TYPE:
+		if (nodes[nodePtr->p.parent].lexeme == ASSIGN
+			&& nodes[nodePtr->p.parent].left == (int) (nodePtr - nodes)) {
+		    /*
+		     * "var is type = value": leave [name type] on the stack; the
+		     * ASSIGN declares and sets the variable.
+		     */
+
+		    convert = 0;
+		    break;
+		}
+
 		/*
 		 * "var is type": the stack holds [name type]. Invoke
 		 * ::tcl::mathop::is name type.
@@ -2892,7 +2962,19 @@
 		convert = 0;
 		break;
 	    case ASSIGN:
-		if (nodes[nodePtr->left].lexeme == LIST) {
+		if (nodes[nodePtr->left].lexeme == IS_TYPE) {
+		    /*
+		     * "var is type = value": the stack holds [name type value].
+		     * Invoke ::tcl::unsupported::declare name type value.
+		     */
+
+		    TclEmitPush(TclRegisterLiteral(envPtr,
+			    "::tcl::unsupported::declare", 27, LITERAL_CMD_NAME),
+			    envPtr);
+		    TclEmitInstInt4(INST_REVERSE, 4, envPtr);
+		    TclEmitInstInt4(INST_REVERSE, 3, envPtr);
+		    TclEmitInvoke(envPtr, INST_INVOKE_STK1, 4);
+		} else if (nodes[nodePtr->left].lexeme == LIST) {
 		    /*
 		     * Destructuring assignment "(a,(b,c),d) = value": the stack holds
 		     * the (nested) list of names and the value. Store each leaf
--- a/generic/tclExecute.c	2026-10-13 14:16:06.754805080 +0200
+++ b/generic/tclExecute.c	2026-10-13 14:16:06.757442602 +0200
@@ -3215,6 +3215,36 @@
 		    NEXT_INST_V(1, 3, 1);
 		}
 	    }
+	    if (part2Ptr == NULL && storeFlags == TCL_LEAVE_ERR_MSG) {
+		/*
+		 * Name written "z(field)" in one word (expr {z(re) = 3}):
+		 * split it and retry as a typed-variable element write.
+		 */
+
+		Tcl_Size nameLen;
+		const char *nm = Tcl_GetStringFromObj(part1Ptr, &nameLen);
+		const char *open = (nameLen > 2 && nm[nameLen - 1] == ')')
+			? strchr(nm, '(') : NULL;
+
+		if (open != NULL && open > nm) {
+		    Tcl_Obj *p1 = Tcl_NewStringObj(nm, open - nm);
+		    Tcl_Obj *p2 = Tcl_NewStringObj(open + 1,
+			    (nm + nameLen - 1) - (open + 1));
+		    int trc;
+
+		    Tcl_IncrRefCount(p1);
+		    Tcl_IncrRefCount(p2);
+		    DECACHE_STACK_INFO();
+		    trc = TclTypedVarSetElement(interp, p1, p2, valuePtr,
+			    &objResultPtr);
+		    CACHE_STACK_INFO();
+		    Tcl_DecrRefCount(p1);
+		    Tcl_DecrRefCount(p2);
+		    if (trc == TCL_OK) {
+			NEXT_INST_V(1, 2, 1);
+		    }
+		}
+	    }
 	    TRACE_ERROR(interp);
 	    goto gotError;
 	}