Skip to content

Require variable names to be globally unique #402

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

Closed
wants to merge 6 commits into from
Closed
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
88 changes: 84 additions & 4 deletions spec/formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,32 @@ The resolution of a _text_ or _literal_ token MUST always succeed.
To resolve the value of a _variable_,
its _name_ is used to identify either a local variable,
or a variable defined elsewhere.
If a local variable and an externally defined one use the same name,
the local variable takes precedence.

It is an error for a local variable definition to
refer to a local variable that's defined after it in the message.
When a _variable_is used as an _operand_,
the _name_ of this variable MUST either be:
1. The _name_ of a _variable_ of a _declaration_ that occurs before the
_operand_, or:
1. An externally defined variable.

In case 1, the `entire` _declaration_ MUST appear before the _operand_.
For example, resolution of the following declaration should fail:

```
let $var = {$var}
```

because although a _variable_ with _name_ `var` occurs before
the expression `{$var}`, the entire declaration `let $var = {$var}`
does not occur before the expression `{$var}`.

Any _name_ MUST NOT appear as the _name_ of the _variable_
in more than one _declaration_ within the same message.
If the _variable_ in a _declaration_ also appears
as the _variable_ in a previous _declaration_,
a Variable Redefinition error MUST be emitted.
If the _variable_ in a declaration is also
an externally defined variable,
a Formatting error MUST be emitted.

The resolution of a _variable_ MAY fail if no value is identified for its _name_.
If this happens, an Unresolved Variable error MUST be emitted.
Expand Down Expand Up @@ -230,6 +251,39 @@ rather than the _expression_ in the _selector_ or _pattern_.

_Pattern selection_ is not supported for _fallback values_.

When there are multiple _declarations_ for the same _variable_,
or when a _declaration_ redefines a _variable_
that is externally defined,
the fallback string is formatted based on the _expression_ of
the first local declaration of that _variable_.

> For example, attempting to format the following message:
>
> ```
> let $var = {|cart|}
> let $var = {|horse|}
> {The value is {$var}.}
> ```
>
> would result in this formatted string representation:
>
> ```
> The value is {|cart|}.
> ```
>
> Also, attempting to format the following message:
> ```
> let $var = {|cart|}
> {The value is {$var}.}
> ```
>
> in a context where `var` is externally defined
> would result in the same formatted string representation:
>
> ```
> The value is {|cart|}.
> ```

## Pattern Selection

When a _message_ contains a _match_ construct with one or more _expressions_,
Expand Down Expand Up @@ -704,6 +758,32 @@ These are divided into the following categories:
> when * {The value is not one.}
> ```

- **Variable Redefinition errors** occur when
a `declaration` contains a `variable` that is already defined
either by a previous local variable declaration,
or by an external definition.

> For example, attempting to format the following message
> results in a Variable Redefinition error,
> because there is another declaration for `var`
> that textually precedes
> the declaration of `var` to be `{|horse|}`.
>
> ```
> let $var = {|cart|}
> let $var = {|horse|}
> {The value is {$var}.}
> ```
>
> Attempting to format the following message in a context
> where `var` is externally defined
> also results in a Variable Redefinition error.
>
> ```
> let $var = {|horse|}
> {The value is {$var}.}
> ```

- **Unknown Function errors** occur when an _expression_ includes
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: would be good to have a code example with a function here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean a function in the declaration of $var, or a function in the expression that uses $var, or both?

a reference to a function which cannot be resolved.

Expand Down