rust-lang / rust-lang/rfcs

Allow else and else if blocks to attach to the end of macro expantions

Open
#2,335 13 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-lang
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Currently, even though macro expansion is done during the parse phase of the compiler it does not fully inline the production because if the ending of macro is an if block a following else block is not paired up with it.
I have been told that this is intended, though I believe that it is rather strange behaviour, because if someone was to output the --extended=pretty output of the compiler from said macro expansion it would look like the else block should match up.

Motivation:
If this was allowed then macro writers would have a very nice and clean way to have an optional fallout case that is syntactically nice to look at.

Example:
Lets say you want to write an initialiser for an object that relies on some item but have the option for a default value that is written by the user, with the else block you could write the following:

let item = init_foo!{ parms } else { Foo::new(15) };

Here the initialisation might go ahead normally but if not there is a custom default value.

Contributor guide

No contributing guide indexed for this repository

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

No source files or tests are named. Start by reviewing the macro-expansion and parser behavior described in the issue and its example, then define the language and RFC changes needed; done means the proposed else and else-if attachment behavior has an agreed specification.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.