-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
base: master
Are you sure you want to change the base?
Conversation
From The Rust Programming Language section 7.4:
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. :) |
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: It would be nice if instead of having to create wrapper functions, the user (or the lib author with This is some additional motivation that could go into the motivation section. |
That being said, I noticed that this RFC doesn't talk about importing inherent methods, which I think should be addressed. |
I discussed that in the future work section. |
There was a problem hiding this 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 atrait
. (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
.
use Trait::method
use Trait::func
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. |
|
||
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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: |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
👍 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
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. |
This can be inferred directly from the desugaring described in the RFC. If the trait has generics, they must be inferrable in order for |
Just wanted to ping this, is there anything I can do to keep this moving? |
Nominated for lang discussion. |
This is currently implemented on nightly as part of #![expect(incomplete_features)]
#![feature(fn_delegation)]
reuse Default::default;
fn type_name_of_val<T>(_: &T) -> &'static str {
std::any::type_name::<T>()
}
fn main() {
let x: u32 = default();
let y = default::<u64>();
let name = type_name_of_val(&default::<()>);
dbg!(x, y, name);
} |
@rfcbot reviewed
I would very much like to see this, though I am okay waiting for a future RFC once the implementation challenges are worked out.
I would also very much like to see this, but yes it belongs in another RFC. While I would prefer to lift the subtrait/supertrait restriction part of this RFC, it sounds annoying to do so it can wait for the next RFC.
Those errors are bizarre and I agree we should make them better (and as I said, eventually remove them altogether). |
Trying to get outstanding concerns resolved: I think the request by @scottmcm was that it be spelled out in the reference section. I don't see a drawback to including implications like this as subsections, even if they are implied by the earlier text. (The paragraph you have on the restriction on important parent trait functions is an example of this.) |
Rendered
This feature fully supplants rust-lang/rust#73001, allowing you to do things like:
and more.
This is my first RFC, please forgive any missteps I make in the process.
Partially completes #1995.