Skip to content

Commit

Permalink
Make is_importable stop rejecting assoc const and fn
Browse files Browse the repository at this point in the history
  • Loading branch information
frank-king committed Jan 15, 2025
1 parent b759390 commit 2427364
Show file tree
Hide file tree
Showing 33 changed files with 490 additions and 264 deletions.
36 changes: 15 additions & 21 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,33 +829,27 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
Err(Undetermined) => indeterminate_count += 1,
// Don't update the resolution, because it was never added.
Err(Determined) if target.name == kw::Underscore => {}
Ok(binding)
if binding.is_importable()
|| binding.is_assoc_const_or_fn()
&& this.tcx.features().import_trait_associated_functions() =>
{
Ok(binding) if binding.is_importable() => {
if binding.is_assoc_const_or_fn()
&& !this.tcx.features().import_trait_associated_functions()
{
feature_err(
this.tcx.sess,
sym::import_trait_associated_functions,
import.span,
"`use` associated items of traits is unstable",
)
.emit();
}
let imported_binding = this.import(binding, import);
target_bindings[ns].set(Some(imported_binding));
this.define(parent, target, ns, imported_binding);
}
source_binding @ (Ok(..) | Err(Determined)) => {
if let Ok(binding) = source_binding {
if binding.is_assoc_const_or_fn() {
feature_err(
this.tcx.sess,
sym::import_trait_associated_functions,
import.span,
"`use` associated items of traits is unstable",
)
if source_binding.is_ok() {
this.dcx()
.create_err(IsNotDirectlyImportable { span: import.span, target })
.emit();
} else {
this.dcx()
.create_err(IsNotDirectlyImportable {
span: import.span,
target,
})
.emit();
}
}
let key = BindingKey::new(target, ns);
this.update_resolution(parent, key, false, |_, resolution| {
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,10 +920,7 @@ impl<'ra> NameBindingData<'ra> {
}

fn is_importable(&self) -> bool {
!matches!(
self.res(),
Res::Def(DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy, _)
)
!matches!(self.res(), Res::Def(DefKind::AssocTy, _))
}

fn is_assoc_const_or_fn(&self) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ error[E0576]: cannot find function `method` in this scope
|
LL | method(..): Send,
| ^^^^^^ not found in this scope
|
help: consider importing this associated function
|
LL + use Tr::method;
|

error[E0412]: cannot find type `method` in this scope
--> $DIR/not-a-method.rs:36:5
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/delegation/bad-resolve.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ error[E0425]: cannot find function `foo` in this scope
|
LL | reuse foo { &self.0 }
| ^^^ not found in this scope
|
help: consider importing this associated function
|
LL + use Trait::foo;
|

error[E0425]: cannot find function `foo2` in trait `Trait`
--> $DIR/bad-resolve.rs:37:18
Expand Down
32 changes: 24 additions & 8 deletions tests/ui/delegation/explicit-paths.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,58 @@ error[E0425]: cannot find function `foo4` in `S`
|
LL | reuse S::foo4;
| ^^^^ not found in `S`
|
note: associated function `trait_assoc_fn_to_other::Trait2::foo4` exists but is inaccessible
--> $DIR/explicit-paths.rs:65:9
|
LL | reuse F::foo4 { &F }
| ^^^^^^^^^^^^^ not accessible

error[E0425]: cannot find function `foo4` in `F`
--> $DIR/explicit-paths.rs:38:18
|
LL | reuse F::foo4 { &self.0 }
| ^^^^ not found in `F`
|
note: function `fn_to_other::foo4` exists but is inaccessible
note: these items exist but are inaccessible
--> $DIR/explicit-paths.rs:27:5
|
LL | reuse S::foo4;
| ^^^^^^^^^^^^^^ not accessible
| ^^^^^^^^^^^^^^ `fn_to_other::foo4`: not accessible
...
LL | reuse F::foo4 { &F }
| ^^^^^^^^^^^^^ `trait_assoc_fn_to_other::Trait2::foo4`: not accessible

error[E0425]: cannot find function `foo4` in `F`
--> $DIR/explicit-paths.rs:51:18
|
LL | reuse F::foo4 { &self.0 }
| ^^^^ not found in `F`
|
note: function `fn_to_other::foo4` exists but is inaccessible
note: these items exist but are inaccessible
--> $DIR/explicit-paths.rs:27:5
|
LL | reuse S::foo4;
| ^^^^^^^^^^^^^^ not accessible
| ^^^^^^^^^^^^^^ `fn_to_other::foo4`: not accessible
...
LL | reuse F::foo4 { &F }
| ^^^^^^^^^^^^^ `trait_assoc_fn_to_other::Trait2::foo4`: not accessible

error[E0425]: cannot find function `foo4` in `F`
--> $DIR/explicit-paths.rs:65:18
|
LL | reuse F::foo4 { &F }
| ^^^^ not found in `F`
|
note: function `fn_to_other::foo4` exists but is inaccessible
--> $DIR/explicit-paths.rs:27:5
help: consider importing this associated function
|
LL + use trait_assoc_fn_to_other::Trait2::foo4;
|
help: if you import `foo4`, refer to it directly
|
LL - reuse F::foo4 { &F }
LL + reuse foo4 { &F }
|
LL | reuse S::foo4;
| ^^^^^^^^^^^^^^ not accessible

error[E0119]: conflicting implementations of trait `Trait` for type `S`
--> $DIR/explicit-paths.rs:74:5
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/delegation/ice-issue-124342.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ error[E0425]: cannot find function `foo` in module `to_reuse`
|
LL | reuse to_reuse::foo { foo }
| ^^^ not found in `to_reuse`
|
help: consider importing this associated function
|
LL + use Trait::foo;
|
help: if you import `foo`, refer to it directly
|
LL - reuse to_reuse::foo { foo }
LL + reuse foo { foo }
|

error[E0425]: cannot find value `foo` in this scope
--> $DIR/ice-issue-124342.rs:7:27
Expand All @@ -14,6 +24,10 @@ help: you might have meant to refer to the associated function
|
LL | reuse to_reuse::foo { Self::foo }
| ++++++
help: consider importing this associated function
|
LL + use Trait::foo;
|

error: aborting due to 2 previous errors

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/import-trait-method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ trait Foo {

use Foo::foo; //~ ERROR `use` associated items of traits is unstable [E0658]

fn main() { foo(); }
fn main() { foo(); } //~ ERROR type annotations needed
13 changes: 11 additions & 2 deletions tests/ui/imports/import-trait-method.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ LL | use Foo::foo;
= 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 1 previous error
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 2 previous errors

For more information about this error, try `rustc --explain E0658`.
Some errors have detailed explanations: E0283, E0658.
For more information about an error, try `rustc --explain E0283`.
54 changes: 33 additions & 21 deletions tests/ui/macros/rfc-3086-metavar-expr/syntax-errors.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -313,39 +313,26 @@ LL | unknown_metavar!(a);
|
= note: this error originates in the macro `unknown_metavar` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0425]: cannot find value `i` in this scope
--> $DIR/syntax-errors.rs:23:36
error[E0425]: cannot find function `count` in this scope
--> $DIR/syntax-errors.rs:23:30
|
LL | ( $( $i:ident ),* ) => { count(i) };
| ^ not found in this scope
| ^^^^^ not found in this scope
...
LL | no_curly__no_rhs_dollar__round!(a, b, c);
| ---------------------------------------- in this macro invocation
|
= note: this error originates in the macro `no_curly__no_rhs_dollar__round` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0425]: cannot find value `i` in this scope
--> $DIR/syntax-errors.rs:30:29
|
LL | ( $i:ident ) => { count(i) };
| ^ not found in this scope
...
LL | no_curly__no_rhs_dollar__no_round!(a);
| ------------------------------------- in this macro invocation
help: consider importing this associated function
|
= note: this error originates in the macro `no_curly__no_rhs_dollar__no_round` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0425]: cannot find value `a` in this scope
--> $DIR/syntax-errors.rs:152:37
LL + use std::iter::Iterator::count;
|
LL | no_curly__rhs_dollar__no_round!(a);
| ^ not found in this scope

error[E0425]: cannot find function `count` in this scope
--> $DIR/syntax-errors.rs:23:30
error[E0425]: cannot find value `i` in this scope
--> $DIR/syntax-errors.rs:23:36
|
LL | ( $( $i:ident ),* ) => { count(i) };
| ^^^^^ not found in this scope
| ^ not found in this scope
...
LL | no_curly__no_rhs_dollar__round!(a, b, c);
| ---------------------------------------- in this macro invocation
Expand All @@ -358,6 +345,21 @@ error[E0425]: cannot find function `count` in this scope
LL | ( $i:ident ) => { count(i) };
| ^^^^^ not found in this scope
...
LL | no_curly__no_rhs_dollar__no_round!(a);
| ------------------------------------- in this macro invocation
|
= note: this error originates in the macro `no_curly__no_rhs_dollar__no_round` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing this associated function
|
LL + use std::iter::Iterator::count;
|

error[E0425]: cannot find value `i` in this scope
--> $DIR/syntax-errors.rs:30:29
|
LL | ( $i:ident ) => { count(i) };
| ^ not found in this scope
...
LL | no_curly__no_rhs_dollar__no_round!(a);
| ------------------------------------- in this macro invocation
|
Expand All @@ -373,6 +375,16 @@ LL | no_curly__rhs_dollar__no_round!(a);
| ---------------------------------- in this macro invocation
|
= note: this error originates in the macro `no_curly__rhs_dollar__no_round` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing this associated function
|
LL + use std::iter::Iterator::count;
|

error[E0425]: cannot find value `a` in this scope
--> $DIR/syntax-errors.rs:152:37
|
LL | no_curly__rhs_dollar__no_round!(a);
| ^ not found in this scope

error: aborting due to 39 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ error[E0425]: cannot find function `consume` in this scope
|
LL | consume(right);
| ^^^^^^^ not found in this scope
|
help: consider importing this associated function
|
LL + use std::io::BufRead::consume;
|

error: aborting due to 1 previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ error[E0425]: cannot find function `len` in this scope
LL | let hello = len(vec![]);
| ^^^ not found in this scope
|
help: use the `.` operator to call the method `len` on `&Vec<_>`
help: consider importing this associated function
|
LL - let hello = len(vec![]);
LL + let hello = vec![].len();
LL + use std::iter::ExactSizeIterator::len;
|

error: aborting due to 1 previous error
Expand Down
17 changes: 10 additions & 7 deletions tests/ui/resolve/associated-fn-called-as-fn.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
struct S;
//~^ HELP consider importing one of these associated functions
//~| HELP consider importing one of these associated functions

impl Foo for S {
fn parse(s:&str) {
fn parse(s: &str) {
for c in s.chars() {
match c {
'0'..='9' => collect_primary(&c), //~ ERROR cannot find function `collect_primary`
//~^ HELP you might have meant to call the associated function
'+' | '-' => println!("We got a sign: {}", c),
_ => println!("Not a number!")
_ => println!("Not a number!"),
}
}
}
}
trait Foo {
fn collect_primary(ch:&char) { }
fn parse(s:&str);
fn collect_primary(ch: &char) {}
fn parse(s: &str);
}
trait Bar {
fn collect_primary(ch:&char) { }
fn parse(s:&str) {
fn collect_primary(ch: &char) {}
fn parse(s: &str) {
for c in s.chars() {
match c {
'0'..='9' => collect_primary(&c), //~ ERROR cannot find function `collect_primary`
//~^ HELP you might have meant to call the associated function
'+' | '-' => println!("We got a sign: {}", c),
_ => println!("Not a number!")
_ => println!("Not a number!"),
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions tests/ui/resolve/associated-fn-called-as-fn.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0425]: cannot find function `collect_primary` in this scope
--> $DIR/associated-fn-called-as-fn.rs:6:30
--> $DIR/associated-fn-called-as-fn.rs:9:30
|
LL | '0'..='9' => collect_primary(&c),
| ^^^^^^^^^^^^^^^
Expand All @@ -8,9 +8,15 @@ help: you might have meant to call the associated function
|
LL | '0'..='9' => Self::collect_primary(&c),
| ++++++
help: consider importing one of these associated functions
|
LL + use Bar::collect_primary;
|
LL + use Foo::collect_primary;
|

error[E0425]: cannot find function `collect_primary` in this scope
--> $DIR/associated-fn-called-as-fn.rs:23:30
--> $DIR/associated-fn-called-as-fn.rs:26:30
|
LL | '0'..='9' => collect_primary(&c),
| ^^^^^^^^^^^^^^^
Expand All @@ -19,6 +25,12 @@ help: you might have meant to call the associated function
|
LL | '0'..='9' => Self::collect_primary(&c),
| ++++++
help: consider importing one of these associated functions
|
LL + use Bar::collect_primary;
|
LL + use Foo::collect_primary;
|

error: aborting due to 2 previous errors

Expand Down
Loading

0 comments on commit 2427364

Please sign in to comment.