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

parse_str::<Expr>() failure is non-obvious when "full" feature is disabled #187

Closed
Arnavion opened this issue Jul 19, 2017 · 2 comments
Closed

Comments

@Arnavion
Copy link
Contributor

Arnavion commented Jul 19, 2017

This is kind of a usability regression from v0.11. That version only exposed syn::parse_expr if full feature was enabled:

// syn = { version = "0.11.11", features = ["full"] }

let _ = syn::parse_expr("|| 5").unwrap(); // Expr { Closure { .. } }

which meant the user obviously knew to enable the feature if they wanted to be able to parse expressions.

Now that parse_* have been replaced by syn::parse_str, the user can write this code that only fails at runtime with a somewhat unclear message:

// syn = { git = "https://github.com/dtolnay/syn/", rev = "24f128215d93f197ae0d7750c5248eee7df0272e" }

let _: syn::Expr = syn::parse_str("|| 5").unwrap(); // ParseError(Some("failed to parse expression: failed to parse"))

... until the user reads the source of syn and realizes that the full feature is (still) necessary:

// syn = { git = "https://github.com/dtolnay/syn/", rev = "24f128215d93f197ae0d7750c5248eee7df0272e", features = ["full"] }

let _: syn::Expr = syn::parse_str("|| 5".unwrap(); // Expr { Closure { .. } }

The example for syn::parse_str even shows itself parsing an Expr successfully, which further confused me until I read the code. I guess it can't be a compiler error since it seems intentional that some expressions are allowed to be parsed even when full is disabled (and it's a trait method anyway). Is there a way to make the feature requirement more obvious, like in the doc comment of parse_str or the error message?

@hcpl
Copy link
Contributor

hcpl commented Jan 19, 2018

Another code that breaks from moving 0.11 -> 0.12 (0.12.7 as of writing) looks like:

enum Foo {
    A = (1 + 2) + 3,
}

due to ExprParen only available with "full" feature.

Though this is not something users of proc_macro_derive type of crates likely write, I think it is useful to document corner cases which break from 0.11 -> 0.12 migration to smooth the transition of the whole syn-related ecosystem.

@dtolnay
Copy link
Owner

dtolnay commented Sep 1, 2018

Fixed in 6e1e505 with a suggestion to enable features = ["full"].

error: unsupported expression; enable syn's features=["full"]
 --> src/main.rs:8:5
  |
8 |     || 5
  |     ^^

@dtolnay dtolnay closed this as completed Sep 1, 2018
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

No branches or pull requests

3 participants