Skip to content

Commit 98834a4

Browse files
committed
Altered RFC to allow super-trait imports.
1 parent 9737ae5 commit 98834a4

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

text/0000-import-trait-methods.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Importing a method from a trait does not import the trait. If you want to call `
5353

5454
```rust
5555
mod a {
56-
trait A {
56+
pub trait A {
5757
fn new() -> Self;
5858
fn do_something(&self);
5959
}
@@ -97,10 +97,10 @@ impl S {
9797
}
9898
```
9999

100-
You cannot import a parent trait method from a sub-trait:
100+
You can also import a parent trait method from a sub-trait:
101101

102102
```rust
103-
use num_traits::float::Float::zero; // Error: try `use num_traits::identities::Zero::zero` instead.
103+
use num_traits::float::Float::zero;
104104

105105
fn main() {
106106
let x : f64 = zero();
@@ -116,7 +116,8 @@ When
116116
```rust
117117
use Trait::method as m;
118118
```
119-
occurs, a new item `m` is made available in the function namespace of the current module. Any attempts to call this item are treated calling the trait method explicitly qualified. As always, the `as` qualifier is optional, in which case the name of the new item is identical with the name of the method in the trait. In other words, the example:
119+
occurs, we first find the supertrait in which `method` occurs.
120+
A new item `m` is made available in the function namespace of the current module. Any attempts to call this item are treated calling the (super-)trait method explicitly qualified. As always, the `as` qualifier is optional, in which case the name of the new item is identical with the name of the method in the trait. In other words, the example:
120121

121122
```rust
122123
use Default::default;
@@ -166,7 +167,26 @@ is sugar for
166167
use some_module::Trait;
167168
```
168169

169-
The restriction on importing parent trait methods is a consequence of this desugaring, see https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=51bef9ba69ce1fc20248e987bf106bd4 for examples of the errors you get when you try to call parent trait methods through a child trait. We will likely want better error messages than this if a user tries to import a parent method.
170+
Finally, given traits
171+
```rust
172+
trait Super {
173+
fn f();
174+
}
175+
176+
trait Sub : Super {
177+
}
178+
```
179+
the usage
180+
```rust
181+
use module::Sub::f;
182+
f();
183+
```
184+
desugars to
185+
```rust
186+
use module::Sub::f;
187+
Super::f();
188+
```
189+
**not** `Sub::f();` as that desugaring will cause compiler errors, see https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=51bef9ba69ce1fc20248e987bf106bd4.
170190

171191
# Drawbacks
172192
[drawbacks]: #drawbacks
@@ -190,6 +210,8 @@ In [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Wr
190210

191211
Option 1 is what is proposed here. It has the simplest semantics, and I believe it best matches the user intent when they import a trait method; the desire is to make that method available as-if it were a regular function. Furthermore, it is more minimalist than the other two options in the sense that you can get to option 2 simply by importing the trait also. Option 3 seems like extra complexity for almost no added value.
192212

213+
An earlier version of this RFC proposed not allowing `use Trait::super_trait_method`. This was changed because comments indicated this usecase would be common.
214+
193215
## What is the impact of not doing this?
194216

195217
Users of the language continue to create helper methods to access trait methods with infix syntax.

0 commit comments

Comments
 (0)