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

in stmt context, seq!(() "") accepted while seq!("" 4) rejected #29799

Closed
pnkfelix opened this issue Nov 12, 2015 · 2 comments
Closed

in stmt context, seq!(() "") accepted while seq!("" 4) rejected #29799

pnkfelix opened this issue Nov 12, 2015 · 2 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@pnkfelix
Copy link
Member

(spawned off of #25658)

This may not necessarily be a macro expansion issue, but that seems like the most likely candidate.

The following code:

macro_rules! seq {
    ($($e:expr)*) => ($($e)*);
}

fn main() {
    {      (); "";  }
    { seq!(()  ""); }

    {      "";  4;  }
    { seq!(""   4); }
}

yields the following compilation error:

% rustc /tmp/seq.rs
/tmp/seq.rs:10:12: 10:14 error: mismatched types:
 expected `()`,
    found `&'static str`
(expected (),
    found &-ptr) [E0308]
/tmp/seq.rs:10     { seq!(""   4); }
                          ^~
/tmp/seq.rs:10:7: 10:20 note: in this expansion of seq! (defined in /tmp/seq.rs)
/tmp/seq.rs:10:12: 10:14 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to previous error
% 

but according to rustc -Z unstable-options --pretty expanded, there is no difference between the second seq! and the expression that precedes it:

% rustc --version
rustc 1.6.0-dev (eacd35984 2015-11-03)
% rustc -Z unstable-options --pretty expanded /tmp/seq.rs
#![feature(no_std, prelude_import)]
#![no_std]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std as std;

fn main() {
    { (); ""; }
    { (); ""; }

    { ""; 4; }
    { ""; 4; }
}
% 

(My current suspicion is that the --pretty expanded is not showing us the closest match to the actual AST being produced by expansion.)

@steveklabnik steveklabnik added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Nov 12, 2015
@pnkfelix pnkfelix added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Nov 12, 2015
@nikomatsakis
Copy link
Contributor

This is almost certainly related to the "statements that do not have semicolons must have unit type". I imagine it's related to the parser's behavior when it encounters an "expr token". The pretty printer output is probably somewhat misleading.

@Mark-Simulacrum
Copy link
Member

The example now doesn't compile with a different error, which seems correct, since () "" isn't valid Rust. This appears to be because the macro no longer inserts semicolons after expressions automatically, if the macro is edited to insert them (($($e:expr)*) => ($($e;)*); then the code does, indeed, compile.

New error:

error: expected one of `.`, `;`, `?`, `}`, or an operator, found `""`
 --> src/lib.rs:2:25
  |
2 |     ($($e:expr)*) => ($($e)*);
  |                         ^^ expected one of `.`, `;`, `?`, `}`, or an operator here

error: expected one of `.`, `;`, `?`, `}`, or an operator, found `4`
 --> src/lib.rs:2:25
  |
2 |     ($($e:expr)*) => ($($e)*);
  |                         ^^ expected one of `.`, `;`, `?`, `}`, or an operator here

error: aborting due to 2 previous errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants