varname-empty.mk revision 1.6
1# $NetBSD: varname-empty.mk,v 1.6 2020/09/24 06:03:44 rillig Exp $
2#
3# Tests for the special variable with the empty name.
4#
5# There is no variable named "" at all, and this fact is used a lot in
6# variable expressions of the form ${:Ufallback}.  These expressions are
7# based on the variable named "" and use the :U modifier to assign a
8# fallback value to the expression (but not to the variable).
9#
10# This form of expressions is used to implement value substitution in the
11# .for loops.  Another use case is in a variable assignment of the form
12# ${:Uvarname}=value, which allows for characters in the variable name that
13# would otherwise be interpreted by the parser, such as whitespace, ':',
14# '=', '$', backslash.
15#
16# The only places where a variable is assigned a value are Var_Set and
17# Var_Append, and these places protect the variable named "" from being
18# defined.  This is different from read-only variables, as that flag can
19# only apply to variables that are defined.
20#
21# This is because it is heavily used in the .for loop expansion,
22# as well as to generate arbitrary strings, as in ${:Ufallback}.
23
24# Until 2020-08-22 it was possible to assign a value to the variable with
25# the empty name, leading to all kinds of unexpected effects.
26#
27# Before 2020-08-22, the simple assignment operator '=' had an off-by-one
28# bug that caused unrelated memory to be read in Parse_DoVar, invoking
29# undefined behavior.
30?=	default
31=	assigned	# undefined behavior until 2020-08-22
32+=	appended
33:=	subst
34!=	echo 'shell-output'
35
36# The .for loop expands the expression ${i} to ${:U1}, ${:U2} and so on.
37# This only works if the variable with the empty name is guaranteed to
38# be undefined.
39.for i in 1 2 3
40NUMBERS+=	${i}
41.endfor
42
43all:
44	@echo out: ${:Ufallback}
45	@echo out: ${NUMBERS}
46