rust-lang / rust-lang/rfcs

rustc requires parentheses when calling a function pointer or closure from a struct field

Open
#3,230 0 comments 18 reactions 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.