Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[osh] Fix ${unset_variable@Q} wrongly producing '' #2186

Merged
merged 8 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/runtime.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module runtime
coerced = Int | Float | Neither

# evaluation state for BracedVarSub
VarSubState = (bool join_array, bool is_type_query, bool has_test_op)
VarSubState = (bool join_array, bool is_type_query, bool has_test_op, bool has_nullary_op)

# A Cell is a wrapper for a value.
# TODO: add location for declaration for 'assigning const' error
Expand Down
32 changes: 31 additions & 1 deletion doc/ref/chap-word-lang.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,36 @@ The pattern can also be a glob:

### op-format

${x@P} evaluates x as a prompt string, e.g. the string that would be printed if
${x@P} evaluates x as a prompt string, i.e. the string that would be printed if
PS1=$x.

---

`${x@Q}` quotes the value of `x`, if necessary, so that it can be evaluated as
a shell word.

$ x='<'
$ echo "value = $x, quoted = ${x@Q}."
value = <, quoted = '<'.

$ x=a
$ echo "value = $x, quoted = ${x@Q}."
value = a, quoted = a.

In the second case, the string `a` doesn't need to be quoted.

---

Format operations like `@Q` generally treat **empty** variables differently
than **unset** variables.

That is, `${empty@Q}` is the string `''`, while `${unset@Q}` is an empty
string:

$ x=''
$ echo "value = $x, quoted = ${x@Q}."
value = , quoted = ''.

$ unset -v x
$ echo "value = $x, quoted = ${x@Q}."
value = , quoted = .
2 changes: 1 addition & 1 deletion doc/ref/toc-osh.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ X [Unsupported] enable
op-patsub ${x//y/z}
op-index ${a[i+1}
op-slice ${a[@]:0:1}
op-format ${x@P}
op-format ${x@P} ${x@Q} etc.
```

<h2 id="mini-lang">
Expand Down
20 changes: 14 additions & 6 deletions osh/word_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ def _PerformSlice(
i = i + 1

if has_length:
strs = bash_impl.SparseArray_GetValues(val)[i:i+length]
strs = bash_impl.SparseArray_GetValues(val)[i:i +
length]
else:
strs = bash_impl.SparseArray_GetValues(val)[i:]

Expand Down Expand Up @@ -1043,7 +1044,9 @@ def _Nullary(self, val, op, var_name):
op_id = op.id
if op_id == Id.VOp0_P:
with tagswitch(val) as case:
if case(value_e.Str):
if case(value_e.Undef):
result = value.Str('')
elif case(value_e.Str):
str_val = cast(value.Str, UP_val)
prompt = self.prompt_ev.EvalPrompt(str_val)
# readline gets rid of these, so we should too.
Expand All @@ -1054,7 +1057,9 @@ def _Nullary(self, val, op, var_name):

elif op_id == Id.VOp0_Q:
with tagswitch(val) as case:
if case(value_e.Str):
if case(value_e.Undef):
result = value.Str('')
elif case(value_e.Str):
str_val = cast(value.Str, UP_val)
result = value.Str(j8_lite.MaybeShellEncode(str_val.s))
# oddly, 'echo ${x@Q}' is equivalent to 'echo "${x@Q}"' in
Expand Down Expand Up @@ -1120,7 +1125,8 @@ def _WholeArray(self, val, part, quoted, vsub_state):
if self.exec_opts.strict_array():
e_die("Can't index string with %s" % op_str,
loc.WordPart(part))
elif case2(value_e.BashArray, value_e.SparseArray, value_e.BashAssoc):
elif case2(value_e.BashArray, value_e.SparseArray,
value_e.BashAssoc):
pass # no-op
else:
# The other YSH types such as List, Dict, and Float are not
Expand Down Expand Up @@ -1418,6 +1424,8 @@ def _EvalBracedVarSub(self, part, part_vals, quoted):
if case(suffix_op_e.Nullary):
suffix_op_ = cast(Token, UP_op)

vsub_state.has_nullary_op = True

# Type query ${array@a} is a STRING, not an array
# NOTE: ${array@Q} is ${array[0]@Q} in bash, which is different than
# ${array[@]@Q}
Expand Down Expand Up @@ -1476,7 +1484,8 @@ def _EvalBracedVarSub(self, part, part_vals, quoted):
raise AssertionError(part.prefix_op)

else:
if not vsub_state.has_test_op: # undef -> '' if no prefix op
# undef -> '' if no prefix op or ${x@P} up
if not vsub_state.has_test_op and not vsub_state.has_nullary_op:
val = self._EmptyStrOrError(val, part.token)

quoted2 = False # another bit for @Q
Expand All @@ -1487,7 +1496,6 @@ def _EvalBracedVarSub(self, part, part_vals, quoted):
if case(suffix_op_e.Nullary):
op = cast(Token, UP_op)
val, quoted2 = self._Nullary(val, op, var_name)

elif case(suffix_op_e.Unary):
op = cast(suffix_op.Unary, UP_op)
if consts.GetKind(op.op.id) == Kind.VTest:
Expand Down
8 changes: 0 additions & 8 deletions spec/var-op-bash.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,6 @@ status=0

status=0

status=0
## END
## OK osh STDOUT:

status=0
''
status=0

status=0
## END

Expand Down