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

Literal from macro expansion is not accepted in typescript_custom_section since rust 1.47.0 #2393

Closed
Frizi opened this issue Dec 7, 2020 · 3 comments · Fixed by #2395
Closed
Labels

Comments

@Frizi
Copy link

Frizi commented Dec 7, 2020

Describe the Bug

Since the update to rust 1.47.0, typescript_custom_section doesn't work with literal captures expanded by macro.
In previous versions of rust, everything worked as expected.

The reported error is

Expected a string literal to be used with #[wasm_bindgen(typescript_custom_section)]

Reproduction

macro_rules! generate_ts {
    ($lit:literal) => {
        #[wasm_bindgen(typescript_custom_section)]
        const _: &str = $lit;
    };
}

generate_ts!("TEST");

The snippet above represent the code that used to work, but doesn't anymore. I've printed the actual AST that syn::ItemConst::macro_parse received, and got this:

Group(
  	ExprGroup {
		attrs: [],
		group_token: Group,
		expr: Lit(
			ExprLit {
				attrs: [],
				lit: Str(LitStr { token: "TEST" })
			}
		)
	}
)

Which means, now rust expands the literal (and possibly other) captures as an expression group with one item, and this should probably be handled in various places in the parser.

Current workaround

I managed to fix it in my specific case by using tt capture instead of literal, but this is obviously more general than i would like.

@Frizi Frizi added the bug label Dec 7, 2020
@alexcrichton
Copy link
Contributor

Hm can you confirm what version of Rust you're using? 1.17 is quite old, and I'd recommend using something more in the 1.40 range

@Frizi
Copy link
Author

Frizi commented Dec 7, 2020

@alexcrichton oh sorry, that was totally a typo on my side, the version is 1.48.0, I have confirmed it not working since 1.47.0, and working on 1.46.0.

@Frizi Frizi changed the title Literal from macro expansion is not accepted in typescript_custom_section since rust 1.17.0 Literal from macro expansion is not accepted in typescript_custom_section since rust 1.47.0 Dec 7, 2020
@alexcrichton
Copy link
Contributor

Aha makes sense! I'll try to investigate this this week.

alexcrichton added a commit to alexcrichton/wasm-bindgen that referenced this issue Dec 9, 2020
We explicitly match on `Expr` in a few locations so it's necessary to
peel back `Expr::Group` like we do with `Type::Group`.

Closes rustwasm#2393
alexcrichton added a commit that referenced this issue Dec 9, 2020
We explicitly match on `Expr` in a few locations so it's necessary to
peel back `Expr::Group` like we do with `Type::Group`.

Closes #2393
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.

2 participants