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

Allow statement-generating braced macro invocations at the end of blocks #34436

Merged
merged 5 commits into from
Jun 28, 2016

Conversation

jseyfried
Copy link
Contributor

@jseyfried jseyfried commented Jun 23, 2016

syntax-[breaking-change] cc #31645

To allow these braced macro invocation, this PR removes the optional expression from ast::Block and instead uses a StmtKind::Expr at the end of the statement list.

Currently, braced macro invocations in blocks can expand into statements (and items) except when they are last in a block, in which case they can only expand into expressions.

For example,

macro_rules! make_stmt {
    () => { let x = 0; }
}

fn f() {
    make_stmt! {} //< This is OK...
    let x = 0; //< ... unless this line is commented out.
}

Fixes #34418.
r? @nrc

@jseyfried
Copy link
Contributor Author

cc @durka

expr: Option<P<Expr>>) -> P<ast::Block> {
self.block_all(span, stmts, expr)
fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
self.block_all(span, stmts)
Copy link
Member

Choose a reason for hiding this comment

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

Why do these two functions both exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No reason -- removed block_all.

jseyfried added a commit to jseyfried/rust that referenced this pull request Jun 26, 2016
To allow these braced macro invocation, this PR removes the optional expression from `ast::Block` and instead uses a `StmtKind::Expr` at the end of the statement list.

Currently, braced macro invocations in blocks can expand into statements (and items) except when they are last in a block, in which case they can only expand into expressions.

For example,
```rust
macro_rules! make_stmt {
    () => { let x = 0; }
}

fn f() {
    make_stmt! {} //< This is OK...
    let x = 0; //< ... unless this line is commented out.
}
```

Fixes rust-lang#34418.
bors added a commit that referenced this pull request Jun 26, 2016
Batch up libsyntax breaking changes

Batch of the following syntax-[breaking-change] changes:
 - #34213: Add a variant `Macro` to `TraitItemKind`
 - #34368: Merge the variant `QPath` of `PatKind` into the variant `PatKind::Path`
 - #34385: Move `syntax::ast::TokenTree` into a new module `syntax::tokenstream`
 - #33943:
  - Remove the type parameter from `visit::Visitor`
  - Remove `attr::WithAttrs` -- use `attr::HasAttrs` instead.
  - Change `fold_tt`/`fold_tts` to take token trees by value and avoid wrapping token trees in `Rc`.
  - Remove the field `ctxt` of `ast::Mac_`
  - Remove inherent method `attrs()` of types -- use the method `attrs` of `HasAttrs` instead.
 - #34316:
  - Remove `ast::Decl`/`ast::DeclKind` and add variants `Local` and `Item` to `StmtKind`.
  - Move the node id for statements from the `StmtKind` variants to a field of `Stmt` (making `Stmt` a struct instead of an alias for `Spanned<StmtKind>`)
  - Rename `ast::ExprKind::Again` to `Continue`.
 - #34339: Generalize and abstract `ThinAttributes` to `ThinVec<Attribute>`
  - Use `.into()` in convert between `Vec<Attribute>` and `ThinVec<Attribute>`
  - Use autoderef instead of `.as_attr_slice()`
 - #34436: Remove the optional expression from `ast::Block` and instead use a `StmtKind::Expr` at the end of the statement list.
 - #34403: Move errors into a separate crate (unlikely to cause breakage)
bors added a commit that referenced this pull request Jun 27, 2016
Batch up libsyntax breaking changes

Batch of the following syntax-[breaking-change] changes:
 - #34213: Add a variant `Macro` to `TraitItemKind`
 - #34368: Merge the variant `QPath` of `PatKind` into the variant `PatKind::Path`
 - #34385: Move `syntax::ast::TokenTree` into a new module `syntax::tokenstream`
 - #33943:
  - Remove the type parameter from `visit::Visitor`
  - Remove `attr::WithAttrs` -- use `attr::HasAttrs` instead.
  - Change `fold_tt`/`fold_tts` to take token trees by value and avoid wrapping token trees in `Rc`.
  - Remove the field `ctxt` of `ast::Mac_`
  - Remove inherent method `attrs()` of types -- use the method `attrs` of `HasAttrs` instead.
 - #34316:
  - Remove `ast::Decl`/`ast::DeclKind` and add variants `Local` and `Item` to `StmtKind`.
  - Move the node id for statements from the `StmtKind` variants to a field of `Stmt` (making `Stmt` a struct instead of an alias for `Spanned<StmtKind>`)
  - Rename `ast::ExprKind::Again` to `Continue`.
 - #34339: Generalize and abstract `ThinAttributes` to `ThinVec<Attribute>`
  - Use `.into()` in convert between `Vec<Attribute>` and `ThinVec<Attribute>`
  - Use autoderef instead of `.as_attr_slice()`
 - #34436: Remove the optional expression from `ast::Block` and instead use a `StmtKind::Expr` at the end of the statement list.
 - #34403: Move errors into a separate crate (unlikely to cause breakage)
@bors bors merged commit 8cad251 into rust-lang:master Jun 28, 2016
joshtriplett added a commit to joshtriplett/error-chain that referenced this pull request Dec 25, 2016
- Avoid using attributes on statements; use them on items instead.

- Change a test to use std::env::VarError instead of std::fmt::Error,
  because the latter didn't impl Error until Rust 1.11.

- Add an empty block after invocations of `error_chain!` inside
  functions, to work around rust-lang/rust#34436
  (fixed in Rust 1.11).

- Replace a single instance of `?` with `try!`.

This still fails to work with 1.10 due to
rust-lang/rust#22250 .  Because of that issue,
an invocation of `#[derive(Debug)]` inside a macro doesn't take cfg(...)
attributes into account, and fails to compile due to referencing enum
variants that don't exist.  Posted for reference only.
joshtriplett added a commit to joshtriplett/error-chain that referenced this pull request Dec 25, 2016
- Avoid using attributes on statements; use them on items instead.

- Change a test to use std::env::VarError instead of std::fmt::Error,
  because the latter didn't impl Error until Rust 1.11.

- Add an empty block after invocations of `error_chain!` inside
  functions, to work around rust-lang/rust#34436
  (fixed in Rust 1.11).

- Replace a single instance of `?` with `try!`.

This still fails to work with 1.10 due to
rust-lang/rust#22250 .  Because of that issue,
an invocation of `#[derive(Debug)]` inside a macro doesn't take cfg(...)
attributes into account, and fails to compile due to referencing enum
variants that don't exist.  Posted for reference only.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants