-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Implement use
associated items of traits
#134754
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
Merged
bors
merged 1 commit into
rust-lang:master
from
frank-king:feature/import_trait_associated_functions
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
Attempt was made to import an unimportable value. This can happen when trying | ||
to import a method from a trait. | ||
Attempt was made to import an unimportable type. This can happen when trying | ||
to import a type from a trait. | ||
|
||
Erroneous code example: | ||
|
||
```compile_fail,E0253 | ||
mod foo { | ||
pub trait MyTrait { | ||
fn do_something(); | ||
type SomeType; | ||
} | ||
} | ||
|
||
use foo::MyTrait::do_something; | ||
// error: `do_something` is not directly importable | ||
use foo::MyTrait::SomeType; | ||
// error: `SomeType` is not directly importable | ||
|
||
fn main() {} | ||
``` | ||
|
||
It's invalid to directly import methods belonging to a trait or concrete type. | ||
It's invalid to directly import types belonging to a trait. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
mod foo { | ||
pub trait MyTrait { | ||
fn do_something(); | ||
type SomeType; | ||
} | ||
} | ||
|
||
use foo::MyTrait::do_something; | ||
use foo::MyTrait::SomeType; | ||
//~^ ERROR E0253 | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
tests/ui/feature-gates/feature-gate-import-trait-associated-functions.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//@ edition:2018 | ||
use std::collections::HashMap; | ||
|
||
use A::{DEFAULT, new}; | ||
//~^ ERROR `use` associated items of traits is unstable [E0658] | ||
//~| ERROR `use` associated items of traits is unstable [E0658] | ||
use Default::default; | ||
//~^ ERROR `use` associated items of traits is unstable [E0658] | ||
|
||
struct S { | ||
a: HashMap<i32, i32>, | ||
} | ||
|
||
impl S { | ||
fn new() -> S { | ||
S { a: default() } | ||
} | ||
} | ||
|
||
trait A: Sized { | ||
const DEFAULT: Option<Self> = None; | ||
fn new() -> Self; | ||
fn do_something(&self); | ||
} | ||
|
||
mod b { | ||
use super::A::{self, DEFAULT, new}; | ||
//~^ ERROR `use` associated items of traits is unstable [E0658] | ||
//~| ERROR `use` associated items of traits is unstable [E0658] | ||
|
||
struct B(); | ||
|
||
impl A for B { | ||
const DEFAULT: Option<Self> = Some(B()); | ||
fn new() -> Self { | ||
B() | ||
} | ||
|
||
fn do_something(&self) {} | ||
} | ||
|
||
fn f() { | ||
let b: B = new(); | ||
b.do_something(); | ||
let c: B = DEFAULT.unwrap(); | ||
} | ||
} | ||
|
||
impl A for S { | ||
fn new() -> Self { | ||
S::new() | ||
} | ||
|
||
fn do_something(&self) {} | ||
} | ||
|
||
fn f() { | ||
let s: S = new(); | ||
s.do_something(); | ||
let t: Option<S> = DEFAULT; | ||
} | ||
|
||
fn main() {} |
53 changes: 53 additions & 0 deletions
53
tests/ui/feature-gates/feature-gate-import-trait-associated-functions.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
error[E0658]: `use` associated items of traits is unstable | ||
--> $DIR/feature-gate-import-trait-associated-functions.rs:4:9 | ||
| | ||
LL | use A::{DEFAULT, new}; | ||
| ^^^^^^^ | ||
| | ||
= note: see issue #134691 <https://github.com/rust-lang/rust/issues/134691> for more information | ||
= help: add `#![feature(import_trait_associated_functions)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error[E0658]: `use` associated items of traits is unstable | ||
--> $DIR/feature-gate-import-trait-associated-functions.rs:4:18 | ||
| | ||
LL | use A::{DEFAULT, new}; | ||
| ^^^ | ||
| | ||
= note: see issue #134691 <https://github.com/rust-lang/rust/issues/134691> for more information | ||
= help: add `#![feature(import_trait_associated_functions)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error[E0658]: `use` associated items of traits is unstable | ||
--> $DIR/feature-gate-import-trait-associated-functions.rs:7:5 | ||
| | ||
LL | use Default::default; | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
= note: see issue #134691 <https://github.com/rust-lang/rust/issues/134691> for more information | ||
= help: add `#![feature(import_trait_associated_functions)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error[E0658]: `use` associated items of traits is unstable | ||
--> $DIR/feature-gate-import-trait-associated-functions.rs:27:26 | ||
| | ||
LL | use super::A::{self, DEFAULT, new}; | ||
| ^^^^^^^ | ||
| | ||
= note: see issue #134691 <https://github.com/rust-lang/rust/issues/134691> for more information | ||
= help: add `#![feature(import_trait_associated_functions)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error[E0658]: `use` associated items of traits is unstable | ||
--> $DIR/feature-gate-import-trait-associated-functions.rs:27:35 | ||
| | ||
LL | use super::A::{self, DEFAULT, new}; | ||
| ^^^ | ||
| | ||
= note: see issue #134691 <https://github.com/rust-lang/rust/issues/134691> for more information | ||
= help: add `#![feature(import_trait_associated_functions)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,22 @@ | ||
error[E0253]: `foo` is not directly importable | ||
error[E0658]: `use` associated items of traits is unstable | ||
--> $DIR/import-trait-method.rs:5:5 | ||
| | ||
LL | use Foo::foo; | ||
| ^^^^^^^^ cannot be imported directly | ||
| ^^^^^^^^ | ||
| | ||
= note: see issue #134691 <https://github.com/rust-lang/rust/issues/134691> for more information | ||
= help: add `#![feature(import_trait_associated_functions)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error[E0283]: type annotations needed | ||
--> $DIR/import-trait-method.rs:7:13 | ||
| | ||
LL | fn main() { foo(); } | ||
| ^^^^^ cannot infer type | ||
| | ||
= note: cannot satisfy `_: Foo` | ||
|
||
error: aborting due to 1 previous error | ||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0253`. | ||
Some errors have detailed explanations: E0283, E0658. | ||
For more information about an error, try `rustc --explain E0283`. |
39 changes: 39 additions & 0 deletions
39
tests/ui/suggestions/fn-to-method.import_trait_associated_functions.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
error[E0425]: cannot find function `cmp` in this scope | ||
--> $DIR/fn-to-method.rs:12:13 | ||
| | ||
LL | let x = cmp(&1, &2); | ||
| ^^^ not found in this scope | ||
| | ||
help: consider importing one of these associated functions | ||
| | ||
LL + use std::cmp::Ord::cmp; | ||
| | ||
LL + use std::iter::Iterator::cmp; | ||
| | ||
|
||
error[E0425]: cannot find function `len` in this scope | ||
--> $DIR/fn-to-method.rs:16:13 | ||
| | ||
LL | let y = len([1, 2, 3]); | ||
| ^^^ not found in this scope | ||
| | ||
help: consider importing this associated function | ||
| | ||
LL + use std::iter::ExactSizeIterator::len; | ||
| | ||
|
||
error[E0425]: cannot find function `bar` in this scope | ||
--> $DIR/fn-to-method.rs:20:13 | ||
| | ||
LL | let z = bar(Foo); | ||
| ^^^ not found in this scope | ||
| | ||
help: use the `.` operator to call the method `bar` on `Foo` | ||
| | ||
LL - let z = bar(Foo); | ||
LL + let z = Foo.bar(); | ||
| | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0425`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.