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

Fails to parse left shift after a macro metavariable in type position #1229

Closed
dtolnay opened this issue Oct 7, 2022 · 0 comments · Fixed by #1230
Closed

Fails to parse left shift after a macro metavariable in type position #1229

dtolnay opened this issue Oct 7, 2022 · 0 comments · Fixed by #1230
Labels

Comments

@dtolnay
Copy link
Owner

dtolnay commented Oct 7, 2022

Rustc's parser allows this, but syn does not.

macro_rules! m {
    ($ty:ty) => {
        #[parse_as_syn_item]
        fn f() {
            let _ = 0 as $ty << 1;
        }
    };
}

m!(u8);
error: expected one of: `for`, parentheses, `fn`, `unsafe`, `extern`, identifier, `::`, `<`, square brackets, `*`, `&`, `!`, `impl`, `_`, lifetime
  --> src/main.rs:5:33
   |
5  |             let _ = 0 as $ty << 1;
   |                                 ^
...
10 | m!(u8);
   | ------ in this macro invocation
   |
   = note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)

Ordinarily this would not be valid syntax

type Type = u8;

fn f() {
    let _ = 0 as Type << 1;
}
error: `<<` is interpreted as a start of generic arguments for `Type`, not a shift
 --> src/main.rs:4:23
  |
4 |     let _ = 0 as Type << 1;
  |                       ^^ - interpreted as generic arguments
  |                       |
  |                       not interpreted as shift
  |
help: try shifting the cast value
  |
4 |     let _ = (0 as Type) << 1;
  |             +         +

but the presence of a macro metavariable behaves like invisible parentheses, which makes it into:

type Type = u8;

fn f() {
    let _ = 0 as (Type) << 1;
}

which is supposed to be valid (warn(unused_parens) notwithstanding).

warning: unnecessary parentheses around type
 --> src/main.rs:4:18
  |
4 |     let _ = 0 as (Type) << 1;
  |                  ^    ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
4 -     let _ = 0 as (Type) << 1;
4 +     let _ = 0 as Type << 1;
  |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant