rust-lang / rust-lang/reference

Document Restrictions::NO_STRUCT_LITERAL better

Open
#1,808 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-grammar
Dominant language
Rust
Stars
1.6k
Forks
607
PR merge metrics
PR metrics pending

Description

The Restrictions::NO_STRUCT_LITERAL restriction could be better documented. Currently we have:

This restriction has the consequences in the following examples:

// This is parsed as `S` as the condition, followed by the consequent block, followed by an empty block.
// Fails because `S` is a type, not a bool value.
if S{} {}

// This is parsed as `x==S` as the condition, followed by the consequent block, followed by an empty block.
// Fails because `S` is a type, not a value.
if x == S{} {}

// This fails to parse because colons are not valid syntax in a block.
if S{field1: i32} {}

This was introduced with RFC 92.

Various things to consider:

  • Need to double check that the existing parts are covered correctly and completely.
  • Make it clearer how this is a "sticky" state, and that this state gets reset or removed in various places.
    • One particularly odd part is parse_expr_opt which drops all restrictions.
    • Where this restriction "ends" probably hasn't been done intentionally (or at least with broad awareness), so we'll need to decide what should be documented.
  • It would be good to link the _except_ grammar suffixes to expr.struct.brace-restricted-positions.
  • It might be good for expr.struct.brace-restricted-positions to have a note explaining why the restriction exists, and to show an example.

I do not think it is practical to encode this in the grammar, though others may disagree. Essentially we'd have to duplicate the majority of the Expression grammar with things like ExpressionNoStructLiteral, and duplicate every sub-rule. I'm concerned that would be duplicating a huge amount of the grammar, but also that it is not sustainable since there are other restrictions that would cause an explosion of permutations.

See https://github.com/rust-lang/reference/issues/569 for the previous issue for this.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the restriction definition in compiler/rustc_parse/src/parser/mod.rs with the grammar references and expr.struct.brace-restricted-positions in the Rust Reference. Review RFC 92 and the linked Reference issue to determine which restriction transitions and examples need documenting. Done means the relevant grammar links, sticky-state behavior, reset points, rationale, and examples are documented consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.