Skip to content

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

@Arnavion

Description

@Arnavion

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions