rust-lang / rfcs

RFCs for changes to Rust
https://rust-lang.github.io/rfcs/
Apache License 2.0
5.78k stars 1.55k forks source link

Add support for `use Trait::func` #3591

Open obsgolem opened 3 months ago

obsgolem commented 3 months ago

Rendered

This feature fully supplants https://github.com/rust-lang/rust/pull/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.

Evian-Zhang commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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.

joshtriplett commented 1 month ago

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 commented 1 month ago

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.

obsgolem commented 1 month ago

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

scottmcm commented 1 month ago

👍 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 commented 1 month ago

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.