rustc requires parentheses when calling a function pointer or closure from a struct field
Nobody has claimed this yet.
- Dominant language
- Markdown
- Stars
- 6.6k
- Forks
- 1.7k
- Avg merge
- 16h 14m
- Merged PRs (30d)
- 1
Description
Let's start with the Cacher<T> struct from the Book chapter 13.1:
struct Cacher<T>
where
T: Fn(u32) -> u32,
{
calculation: T,
value: Option<u32>,
}
At the moment, if I wanted to call a closure stored in a Cacher, I would need to call (cacher.calculation)(32). This is because rustc assumes
field_access ::= ident '.' field_name
associated_function_call ::= ident '.' associated_function_name '(' args ')'
While this is helpful to avoid ambiguity in the context of getters, where convention is to name the field and the associated function the same, it should be possible for rustc to assume in the case that there is only a field with the name and not an associated function that the user is attempting to call the closure stored in the function. In the case that there is both a closure and an associated function with that name, the current behavior can serve to disambiguate.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Cacher example in Book chapter 13.1 and the rustc field-access and associated-function-call grammar quoted in the issue. Compare calls through a closure field when no same-named associated function exists with the ambiguous case, and define a resolved proposal for the intended syntax and behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100