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

Add support for use Trait::func #3591

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

Conversation

obsgolem
Copy link

@obsgolem obsgolem commented Mar 19, 2024

Rendered

This feature fully supplants rust-lang/rust#73001, allowing you to do things like:

use Default::default;

struct S {
    a: HashMap<i32, i32>,
}

impl S {
    fn new() -> S {
        S {
            a: default()
        }
    }
}

and more.

This is my first RFC, please forgive any missteps I make in the process.

Partially completes #1995.

@ehuss ehuss added the T-lang Relevant to the language team, which will review and decide on the RFC. label Mar 20, 2024
@Evian-Zhang
Copy link

From The Rust Programming Language section 7.4:

Creating Idiomatic use Paths

Although both Listing 7-11 and 7-13 accomplish the same task, Listing 7-11 is the idiomatic way to bring a function into scope with use. Bringing the function’s parent module into scope with use means we have to specify the parent module when calling the function. Specifying the parent module when calling the function makes it clear that the function isn’t locally defined while still minimizing repetition of the full path.

We have been encouraged for a long time to always import the parent module when calling the function to distinguish locally defined functions and imported functions. However, the syntax sugar suggested in this RFC contradicts to this convention.

I think it is more appropriate to discuss this convention in the RFC to make it more clear how we should use this feature. :)

@crumblingstatue
Copy link

I'd like to reiterate here what I said on #1995

Many libraries define free functions for constructors to mathy types, like vectors.

Some examples:
https://docs.rs/glam/0.24.2/src/glam/f32/vec2.rs.html#12
https://docs.rs/egui/latest/egui/fn.pos2.html

It would be nice if instead of having to create wrapper functions, the user (or the lib author with pub use) could just do use Vec2::new as vec2;

This is some additional motivation that could go into the motivation section. Default::default is hardly the only use case for this.

@crumblingstatue
Copy link

That being said, I noticed that this RFC doesn't talk about importing inherent methods, which I think should be addressed.
If this RFC only proposes importing trait methods, but not inherent methods, then the use cases I mentioned above are nullified.
Why should we only support importing trait methods, but not inherent methods?

@obsgolem
Copy link
Author

I discussed that in the future work section. use Type::method is out of scope for this RFC. The difficulty is impl blocks with arbitrary where clauses.

Copy link

@kpreid kpreid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RFC text uses the term “method” where it should be using the term “associated function”.

  • An associated function is a function defined in an impl or declared in a trait. (reference)
  • A method is an associated function that has a self parameter (and therefore may be used in .func_name() method-call syntax). (reference)

I recommend that this be corrected to avoid creating confusion. In particular, the central use case of this feature is importing associated functions that are less concise to call because they are not methods, such as Default::default.

text/0000-import-trait-methods.md Outdated Show resolved Hide resolved
text/0000-import-trait-methods.md Outdated Show resolved Hide resolved
@obsgolem obsgolem changed the title Add support for use Trait::method Add support for use Trait::func Mar 31, 2024
@joshtriplett
Copy link
Member

I'm going to go ahead and start the process of seeing if we have consensus on this:

@rfcbot merge

Please do either add support for associated constants inline in the RFC (if it's straightforward to do so) or mention them in future work (if not):

@rfcbot concern associated-constants

@rfcbot
Copy link
Collaborator

rfcbot commented May 13, 2024

Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Currently awaiting signoff of all team members in order to enter the final comment period. disposition-merge This RFC is in PFCP or FCP with a disposition to merge it. labels May 13, 2024

Because of this context sensitivity, we should allow developers to choose when removing the extra context makes sense for their codebase.

Another drawback mentioned during review for this RFC was that this adds more complication to the name resolution rules. On an implementation side, I am assured that this feature is straightforward to implement. From a user perspective, the name lookup rules for the function name are exactly the same as those used to look up any other function name. The lookup rules used to resolve the `impl` are also exactly the same ones used for non-fully qualified trait function calls. There is no fundamentally new kind of lookup happening here, just a remixing of existing lookup rules.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On an implementation side, I am assured that this feature is straightforward to implement.

Note that this may be the case for rustc, but not necessarily for alternative implementations. Wearing my rust-analyzer hat I'd expect this to be doable as well, but a bit annoying. Not saying this is a blocker (it's not impossible to implement) but mainly to keep that in mind until we attempt to implement it in r-a.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered writing a section with some considerations on UX for IDEs. This does bring in a new level of complexity for auto-imports in IDEs. I decided to keep this focused on rustc and the core language however.

@obsgolem
Copy link
Author

Apologies for the delay, wasn't able to work up motivation to work on this again until today. Thanks for the review!

```rust
use Trait::item as m;
```
occurs, a new item `m` is made available in the value namespace of the current module. Any attempts to use this item are treated as using the associated item explicitly qualified. `item` must be either an associated function or an associated constant. As always, the `as` qualifier is optional, in which case the name of the new item is identical with the name of the associated item in the trait. In other words, the example:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, "value namespace" here surprised me. What happens if item is actually an associated type?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

item must be either an associated function or an associated constant

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See this discussion for motivation for including associated constants, which wasn't in my first draft.

@scottmcm
Copy link
Member

👍 to the motivation here.

@rfcbot concern what-about-turbofish

One thing I think needs to be addressed in the reference section is what's supposed to happen with generic parameters.

If one has

trait Trait<A> {
    fn method<B>();
}

and does a

use othermod::Trait::method;

What happens? Can you turbofish it? Which generics are there there?

The precedent from enum variants doesn't cover generics on the associated items, so something needs to specified for it.

@obsgolem
Copy link
Author

This can be inferred directly from the desugaring described in the RFC. If the trait has generics, they must be inferrable in order for Trait::method() to work, hence the same is true for the imported version. I can spell that out explicitly if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This RFC is in PFCP or FCP with a disposition to merge it. proposed-final-comment-period Currently awaiting signoff of all team members in order to enter the final comment period. T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet