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

Allow self as an identifier in patterns #3684

Open
fiveseven-lambda opened this issue Aug 26, 2024 · 2 comments
Open

Allow self as an identifier in patterns #3684

fiveseven-lambda opened this issue Aug 26, 2024 · 2 comments
Labels
A-resolve Proposals relating to name resolution. A-syntax Syntax related proposals & ideas T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@fiveseven-lambda
Copy link

fiveseven-lambda commented Aug 26, 2024

Proposal

Allow the use of self as an identifier in identifier patterns (foo @ Foo { bar, baz }).

Motivation

As seen in the following search results within the rust-lang/rust repository, there are numerous instances where developers want to destructure a struct in a let statement with self on the right-hand side.

Search results in the rust-lang/rust repository

For example, consider the following code in library/std/src/sys/pal/sgx/net.rs:

fn try_into_inner(self) -> Result<FileDesc, Socket> {
    let Socket { inner, local_addr } = self;
    Arc::try_unwrap(inner).map_err(|inner| Socket { inner, local_addr })
}

If self were allowed as an identifier in identifier patterns, this could be written more concisely as:

fn try_into_inner(self @ Socket { inner, local_addr }) -> Result<FileDesc, Socket> {
    Arc::try_unwrap(inner).map_err(|inner| Socket { inner, local_addr })
}

This is not only more concise but also more natural from a language specification standpoint.

@fiveseven-lambda fiveseven-lambda added the T-compiler Relevant to the compiler team, which will review and decide on the RFC. label Aug 26, 2024
@fmease fmease removed T-compiler Relevant to the compiler team, which will review and decide on the RFC. to-announce labels Aug 26, 2024
@fmease fmease transferred this issue from rust-lang/compiler-team Aug 26, 2024
@rust-lang rust-lang deleted a comment from rustbot Aug 26, 2024
@jieyouxu jieyouxu added T-lang Relevant to the language team, which will review and decide on the RFC. A-resolve Proposals relating to name resolution. A-syntax Syntax related proposals & ideas labels Aug 26, 2024
@fiveseven-lambda
Copy link
Author

But what about &self and &mut self? We can write &self as a shorthand for self: &Self, but it might be confusing if &self @ Foo { bar, baz } was desugared into self @ Foo { bar, baz }: &Self. There are two possible ways:

  1. Allow only self in patterns, and disallow &self and &mut self (so that we have to write as self @ Foo { bar, baz }: &Self)
  2. Let &self @ Foo { bar, baz } and &mut self @ Foo { bar, baz } be shorthands for self @ Foo { bar, baz }: &Self and self @ Foo { bar, baz }: &mut Self, respectively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-resolve Proposals relating to name resolution. A-syntax Syntax related proposals & ideas T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

5 participants
@fmease @jieyouxu @fiveseven-lambda @rustbot and others