Skip to content

Commit d378cd5

Browse files
Rollup merge of #166042 - itsfuad:fix/165389-out-of-scope-method-suggestion, r=estebank
Fix out of scope method suggestion Fixes #165389 This fix removes suggestions from the same unimported trait while preserving import advice and valid typo suggestions. It also fixes a related case: an in-scope method was not suggested when the unimported trait had a method with the same name. I added regression coverage and updated three diagnostic snapshots. Test program: ```rs fn main() { let (_pipe_rx, mut pipe_tx) = std::io::pipe().unwrap(); pipe_tx.write_all(b"1234"); } ``` Output: ``` error[E0599]: no method named `write_all` found for struct `PipeWriter` in the current scope --> /tmp/rust-165389.zqqzMN/example.rs:3:13 | 3 | pipe_tx.write_all(b"1234"); | ^^^^^^^^^ method not found in `PipeWriter` | ::: library/core/src/io/write.rs:214:8 | 214 | fn write_all(&mut self, mut buf: &[u8]) -> Result<()> { | --------- the method is available for `PipeWriter` here | = help: items from traits can only be used if the trait is in scope help: trait `Write` which provides `write_all` is implemented but not in scope; perhaps you want to import it | 1 + use std::io::Write; | error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0599`. ```
2 parents ad34a0e + b1c409e commit d378cd5

7 files changed

Lines changed: 244 additions & 36 deletions

File tree

‎compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3479,7 +3479,7 @@ impl<'a, 'tcx> ArgsCtxt<'a, 'tcx> {
34793479
callee_ty.peel_refs(),
34803480
self.call_ctxt.callee_expr.unwrap().hir_id,
34813481
TraitsInScope,
3482-
|mut ctxt| ctxt.probe_for_similar_candidate(),
3482+
|mut ctxt| ctxt.probe_for_similar_candidate(&[]),
34833483
)
34843484
&& assoc.is_method()
34853485
{

‎compiler/rustc_hir_typeck/src/method/probe.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
13051305
if let Some((kind, def_id)) = private_candidate {
13061306
return Err(MethodError::PrivateMatch(kind, def_id, out_of_scope_traits));
13071307
}
1308-
let similar_candidate = self.probe_for_similar_candidate()?;
1308+
let similar_candidate = self.probe_for_similar_candidate(&out_of_scope_traits)?;
13091309

13101310
Err(MethodError::NoMatch(NoMatchData {
13111311
static_candidates,
@@ -2519,6 +2519,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
25192519
#[instrument(level = "debug", skip(self))]
25202520
pub(crate) fn probe_for_similar_candidate(
25212521
&mut self,
2522+
out_of_scope_traits: &[DefId],
25222523
) -> Result<Option<ty::AssocItem>, MethodError<'tcx>> {
25232524
debug!("probing for method names similar to {:?}", self.method_name);
25242525

@@ -2547,6 +2548,11 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
25472548
pcx.method_name = Some(method_name);
25482549
pcx.assemble_inherent_candidates();
25492550
pcx.assemble_extension_candidates_for_all_traits();
2551+
// Methods of out-of-scope traits can't be called here. Drop them before
2552+
// picking, or they could win over (or be ambiguous with) one that can.
2553+
pcx.extension_candidates.retain(|candidate| {
2554+
!out_of_scope_traits.contains(&candidate.item.container_id(self.tcx))
2555+
});
25502556
pcx.pick_core(&mut Vec::new()).and_then(|pick| pick.ok()).map(|pick| pick.item)
25512557
})
25522558
.collect();

‎tests/ui/impl-trait/no-method-suggested-traits.stderr‎

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no method named `method` found for type `u32` in the current scope
22
--> $DIR/no-method-suggested-traits.rs:26:10
33
|
44
LL | 1u32.method();
5-
| ^^^^^^
5+
| ^^^^^^ method not found in `u32`
66
|
77
= help: items from traits can only be used if the trait is in scope
88
help: the following traits which provide `method` are implemented but not in scope; perhaps you want to import one of them
@@ -11,16 +11,12 @@ LL + use crate::foo::Bar;
1111
|
1212
LL + use crate::no_method_suggested_traits::foo::PubPub;
1313
|
14-
help: there is a method `method2` with a similar name
15-
|
16-
LL | 1u32.method2();
17-
| +
1814

1915
error[E0599]: no method named `method` found for struct `Rc<&mut Box<&u32>>` in the current scope
2016
--> $DIR/no-method-suggested-traits.rs:29:44
2117
|
2218
LL | std::rc::Rc::new(&mut Box::new(&1u32)).method();
23-
| ^^^^^^
19+
| ^^^^^^ method not found in `Rc<&mut Box<&u32>>`
2420
|
2521
= help: items from traits can only be used if the trait is in scope
2622
help: the following traits which provide `method` are implemented but not in scope; perhaps you want to import one of them
@@ -29,10 +25,6 @@ LL + use crate::foo::Bar;
2925
|
3026
LL + use crate::no_method_suggested_traits::foo::PubPub;
3127
|
32-
help: there is a method `method2` with a similar name
33-
|
34-
LL | std::rc::Rc::new(&mut Box::new(&1u32)).method2();
35-
| +
3628

3729
error[E0599]: no method named `method` found for type `char` in the current scope
3830
--> $DIR/no-method-suggested-traits.rs:33:9
@@ -41,39 +33,31 @@ LL | fn method(&self) {}
4133
| ------ the method is available for `char` here
4234
...
4335
LL | 'a'.method();
44-
| ^^^^^^
36+
| ^^^^^^ method not found in `char`
4537
|
4638
= help: items from traits can only be used if the trait is in scope
4739
help: trait `Bar` which provides `method` is implemented but not in scope; perhaps you want to import it
4840
|
4941
LL + use crate::foo::Bar;
5042
|
51-
help: there is a method `method2` with a similar name
52-
|
53-
LL | 'a'.method2();
54-
| +
5543

5644
error[E0599]: no method named `method` found for struct `Rc<&mut Box<&char>>` in the current scope
5745
--> $DIR/no-method-suggested-traits.rs:35:43
5846
|
5947
LL | std::rc::Rc::new(&mut Box::new(&'a')).method();
60-
| ^^^^^^
48+
| ^^^^^^ method not found in `Rc<&mut Box<&char>>`
6149
|
6250
= help: items from traits can only be used if the trait is in scope
6351
help: trait `Bar` which provides `method` is implemented but not in scope; perhaps you want to import it
6452
|
6553
LL + use crate::foo::Bar;
6654
|
67-
help: there is a method `method2` with a similar name
68-
|
69-
LL | std::rc::Rc::new(&mut Box::new(&'a')).method2();
70-
| +
7155

7256
error[E0599]: no method named `method` found for type `i32` in the current scope
7357
--> $DIR/no-method-suggested-traits.rs:38:10
7458
|
7559
LL | 1i32.method();
76-
| ^^^^^^
60+
| ^^^^^^ method not found in `i32`
7761
|
7862
::: $DIR/auxiliary/no_method_suggested_traits.rs:8:12
7963
|
@@ -85,26 +69,18 @@ help: trait `PubPub` which provides `method` is implemented but not in scope; pe
8569
|
8670
LL + use crate::no_method_suggested_traits::foo::PubPub;
8771
|
88-
help: there is a method `method3` with a similar name
89-
|
90-
LL | 1i32.method3();
91-
| +
9272

9373
error[E0599]: no method named `method` found for struct `Rc<&mut Box<&i32>>` in the current scope
9474
--> $DIR/no-method-suggested-traits.rs:40:44
9575
|
9676
LL | std::rc::Rc::new(&mut Box::new(&1i32)).method();
97-
| ^^^^^^
77+
| ^^^^^^ method not found in `Rc<&mut Box<&i32>>`
9878
|
9979
= help: items from traits can only be used if the trait is in scope
10080
help: trait `PubPub` which provides `method` is implemented but not in scope; perhaps you want to import it
10181
|
10282
LL + use crate::no_method_suggested_traits::foo::PubPub;
10383
|
104-
help: there is a method `method3` with a similar name
105-
|
106-
LL | std::rc::Rc::new(&mut Box::new(&1i32)).method3();
107-
| +
10884

10985
error[E0599]: no method named `method` found for struct `Foo` in the current scope
11086
--> $DIR/no-method-suggested-traits.rs:43:9

‎tests/ui/macros/missing-writer-issue-139830.stderr‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: cannot write into `String`
22
--> $DIR/missing-writer-issue-139830.rs:7:20
33
|
44
LL | let _ = write!(buf, "foo");
5-
| ^^^
5+
| -------^^^-------- method not found in `String`
66
|
77
--> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
88
|
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Regression test for #165389: don't suggest another method from the same missing trait.
2+
3+
mod writer {
4+
pub struct Writer;
5+
6+
pub trait Write {
7+
fn write(&self) {}
8+
fn write_all(&self) {}
9+
}
10+
11+
impl Write for Writer {}
12+
13+
impl Writer {
14+
pub fn flush(&self) {}
15+
}
16+
}
17+
18+
trait Read {
19+
fn read(&self) {}
20+
}
21+
22+
impl Read for writer::Writer {}
23+
24+
fn generic<T: writer::Write>(writer: T) {
25+
writer.writ();
26+
//~^ ERROR no method named `writ` found
27+
}
28+
29+
fn trait_object(writer: &dyn writer::Write) {
30+
writer.writ();
31+
//~^ ERROR no method named `writ` found
32+
}
33+
34+
fn main() {
35+
let writer = writer::Writer;
36+
writer.write_all();
37+
//~^ ERROR no method named `write_all` found
38+
writer.reed();
39+
//~^ ERROR no method named `reed` found
40+
writer.flus();
41+
//~^ ERROR no method named `flus` found
42+
}
43+
44+
// A method from the missing trait must not hide a similarly named method that can be called.
45+
mod hidden {
46+
pub struct ByValue;
47+
pub struct Ambiguous;
48+
pub struct Inherent;
49+
50+
pub trait Hidden {
51+
fn method(&self) {}
52+
fn method2(self)
53+
where
54+
Self: Sized,
55+
{
56+
}
57+
}
58+
59+
impl Hidden for ByValue {}
60+
impl Hidden for Inherent {}
61+
62+
pub trait HiddenRef {
63+
fn method(&self) {}
64+
fn method2(&self) {}
65+
}
66+
67+
impl HiddenRef for Ambiguous {}
68+
69+
impl Inherent {
70+
pub fn method2(&self) {}
71+
}
72+
}
73+
74+
trait Visible {
75+
fn method2(&self) {}
76+
}
77+
78+
impl Visible for hidden::ByValue {}
79+
impl Visible for hidden::Ambiguous {}
80+
81+
fn in_scope_candidates() {
82+
hidden::ByValue.method();
83+
//~^ ERROR no method named `method` found
84+
hidden::Ambiguous.method();
85+
//~^ ERROR no method named `method` found
86+
hidden::Inherent.method();
87+
//~^ ERROR no method named `method` found
88+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
error[E0599]: no method named `writ` found for type parameter `T` in the current scope
2+
--> $DIR/similar-method-out-of-scope-trait.rs:25:12
3+
|
4+
LL | fn generic<T: writer::Write>(writer: T) {
5+
| - method `writ` not found for this type parameter
6+
LL | writer.writ();
7+
| ^^^^
8+
|
9+
help: there is a method `write` with a similar name
10+
|
11+
LL | writer.write();
12+
| +
13+
14+
error[E0599]: no method named `writ` found for reference `&dyn writer::Write` in the current scope
15+
--> $DIR/similar-method-out-of-scope-trait.rs:30:12
16+
|
17+
LL | writer.writ();
18+
| ^^^^
19+
|
20+
help: there is a method `write` with a similar name
21+
|
22+
LL | writer.write();
23+
| +
24+
25+
error[E0599]: no method named `write_all` found for struct `Writer` in the current scope
26+
--> $DIR/similar-method-out-of-scope-trait.rs:36:12
27+
|
28+
LL | pub struct Writer;
29+
| ----------------- method `write_all` not found for this struct
30+
...
31+
LL | fn write_all(&self) {}
32+
| --------- the method is available for `Writer` here
33+
...
34+
LL | writer.write_all();
35+
| ^^^^^^^^^ method not found in `Writer`
36+
|
37+
= help: items from traits can only be used if the trait is in scope
38+
help: trait `Write` which provides `write_all` is implemented but not in scope; perhaps you want to import it
39+
|
40+
LL + use crate::writer::Write;
41+
|
42+
43+
error[E0599]: no method named `reed` found for struct `Writer` in the current scope
44+
--> $DIR/similar-method-out-of-scope-trait.rs:38:12
45+
|
46+
LL | pub struct Writer;
47+
| ----------------- method `reed` not found for this struct
48+
...
49+
LL | writer.reed();
50+
| ^^^^
51+
|
52+
help: there is a method `read` with a similar name
53+
|
54+
LL - writer.reed();
55+
LL + writer.read();
56+
|
57+
58+
error[E0599]: no method named `flus` found for struct `Writer` in the current scope
59+
--> $DIR/similar-method-out-of-scope-trait.rs:40:12
60+
|
61+
LL | pub struct Writer;
62+
| ----------------- method `flus` not found for this struct
63+
...
64+
LL | writer.flus();
65+
| ^^^^
66+
|
67+
help: there is a method `flush` with a similar name
68+
|
69+
LL | writer.flush();
70+
| +
71+
72+
error[E0599]: no method named `method` found for struct `ByValue` in the current scope
73+
--> $DIR/similar-method-out-of-scope-trait.rs:82:21
74+
|
75+
LL | pub struct ByValue;
76+
| ------------------ method `method` not found for this struct
77+
...
78+
LL | fn method(&self) {}
79+
| ------ the method is available for `ByValue` here
80+
...
81+
LL | hidden::ByValue.method();
82+
| ^^^^^^
83+
|
84+
= help: items from traits can only be used if the trait is in scope
85+
help: trait `Hidden` which provides `method` is implemented but not in scope; perhaps you want to import it
86+
|
87+
LL + use crate::hidden::Hidden;
88+
|
89+
help: there is a method `method2` with a similar name
90+
|
91+
LL | hidden::ByValue.method2();
92+
| +
93+
94+
error[E0599]: no method named `method` found for struct `Ambiguous` in the current scope
95+
--> $DIR/similar-method-out-of-scope-trait.rs:84:23
96+
|
97+
LL | pub struct Ambiguous;
98+
| -------------------- method `method` not found for this struct
99+
...
100+
LL | fn method(&self) {}
101+
| ------ the method is available for `Ambiguous` here
102+
...
103+
LL | hidden::Ambiguous.method();
104+
| ^^^^^^
105+
|
106+
= help: items from traits can only be used if the trait is in scope
107+
help: trait `HiddenRef` which provides `method` is implemented but not in scope; perhaps you want to import it
108+
|
109+
LL + use crate::hidden::HiddenRef;
110+
|
111+
help: there is a method `method2` with a similar name
112+
|
113+
LL | hidden::Ambiguous.method2();
114+
| +
115+
116+
error[E0599]: no method named `method` found for struct `Inherent` in the current scope
117+
--> $DIR/similar-method-out-of-scope-trait.rs:86:22
118+
|
119+
LL | pub struct Inherent;
120+
| ------------------- method `method` not found for this struct
121+
...
122+
LL | fn method(&self) {}
123+
| ------ the method is available for `Inherent` here
124+
...
125+
LL | hidden::Inherent.method();
126+
| ^^^^^^
127+
|
128+
= help: items from traits can only be used if the trait is in scope
129+
help: trait `Hidden` which provides `method` is implemented but not in scope; perhaps you want to import it
130+
|
131+
LL + use crate::hidden::Hidden;
132+
|
133+
help: there is a method `method2` with a similar name
134+
|
135+
LL | hidden::Inherent.method2();
136+
| +
137+
138+
error: aborting due to 8 previous errors
139+
140+
For more information about this error, try `rustc --explain E0599`.

‎tests/ui/suggestions/import-visible-path-39175.stderr‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ error[E0599]: no method named `exec` found for mutable reference `&mut Command`
22
--> $DIR/import-visible-path-39175.rs:13:47
33
|
44
LL | let _ = Command::new("echo").arg("hello").exec();
5-
| ^^^^
5+
| ^^^^ method not found in `&mut Command`
66
|
77
= help: items from traits can only be used if the trait is in scope
8-
help: there is a method `pre_exec` with a similar name, but with different arguments
9-
--> $SRC_DIR/std/src/os/unix/process.rs:LL:COL
108
help: trait `CommandExt` which provides `exec` is implemented but not in scope; perhaps you want to import it
119
|
1210
LL + use std::os::unix::process::CommandExt;

0 commit comments

Comments
 (0)