Skip to content

Commit 9cf5709

Browse files
Suggest expressions' fields even if they're not ADTs
1 parent e4417cf commit 9cf5709

14 files changed

+80
-6
lines changed

compiler/rustc_typeck/src/check/expr.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,15 +2611,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26112611
// up to a depth of three
26122612
None
26132613
} else {
2614-
// recursively search fields of `candidate_field` if it's a ty::Adt
26152614
field_path.push(candidate_field.ident(self.tcx).normalize_to_macros_2_0());
26162615
let field_ty = candidate_field.ty(self.tcx, subst);
2617-
if let Some((nested_fields, subst)) = self.get_field_candidates(span, field_ty) {
2618-
for field in nested_fields.iter() {
2616+
if matches(candidate_field, field_ty) {
2617+
return Some(field_path);
2618+
} else if let Some((nested_fields, subst)) = self.get_field_candidates(span, field_ty) {
2619+
// recursively search fields of `candidate_field` if it's a ty::Adt
2620+
for field in nested_fields {
26192621
if field.vis.is_accessible_from(id, self.tcx) {
2620-
if matches(candidate_field, field_ty) {
2621-
return Some(field_path);
2622-
} else if let Some(field_path) = self.check_for_nested_field_satisfying(
2622+
if let Some(field_path) = self.check_for_nested_field_satisfying(
26232623
span,
26242624
matches,
26252625
field,

src/test/ui/copy-a-resource.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ LL | let _y = x.clone();
1010
= help: items from traits can only be used if the trait is implemented and in scope
1111
= note: the following trait defines an item `clone`, perhaps you need to implement it:
1212
candidate #1: `Clone`
13+
help: one of the expressions' fields has a method of the same name
14+
|
15+
LL | let _y = x.i.clone();
16+
| ++
1317

1418
error: aborting due to previous error
1519

src/test/ui/issues/issue-2823.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ LL | let _d = c.clone();
1010
= help: items from traits can only be used if the trait is implemented and in scope
1111
= note: the following trait defines an item `clone`, perhaps you need to implement it:
1212
candidate #1: `Clone`
13+
help: one of the expressions' fields has a method of the same name
14+
|
15+
LL | let _d = c.x.clone();
16+
| ++
1317

1418
error: aborting due to previous error
1519

src/test/ui/issues/issue-31173.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ LL | pub struct TakeWhile<I, P> {
3333
which is required by `Cloned<TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:6:39: 6:43]>>: Iterator`
3434
`Cloned<TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:6:39: 6:43]>>: Iterator`
3535
which is required by `&mut Cloned<TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:6:39: 6:43]>>: Iterator`
36+
help: one of the expressions' fields has a method of the same name
37+
|
38+
LL | .it.collect();
39+
| +++
3640

3741
error: aborting due to 2 previous errors
3842

src/test/ui/issues/issue-39175.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ LL | Command::new("echo").arg("hello").exec();
55
| ^^^^ method not found in `&mut Command`
66
|
77
= help: items from traits can only be used if the trait is in scope
8+
help: one of the expressions' fields has a method of the same name
9+
|
10+
LL | Command::new("echo").arg("hello").inner.exec();
11+
| ++++++
812
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
913
|
1014
LL | use std::os::unix::process::CommandExt;

src/test/ui/mismatched_types/issue-36053-2.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ LL | pub struct Filter<I, P> {
3535
which is required by `Filter<Fuse<std::iter::Once<&str>>, [closure@$DIR/issue-36053-2.rs:7:39: 7:48]>: Iterator`
3636
`Filter<Fuse<std::iter::Once<&str>>, [closure@$DIR/issue-36053-2.rs:7:39: 7:48]>: Iterator`
3737
which is required by `&mut Filter<Fuse<std::iter::Once<&str>>, [closure@$DIR/issue-36053-2.rs:7:39: 7:48]>: Iterator`
38+
help: one of the expressions' fields has a method of the same name
39+
|
40+
LL | once::<&str>("str").fuse().filter(|a: &str| true).iter.count();
41+
| +++++
3842

3943
error: aborting due to 2 previous errors
4044

src/test/ui/noncopyable-class.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ LL | let _y = x.clone();
1010
= help: items from traits can only be used if the trait is implemented and in scope
1111
= note: the following trait defines an item `clone`, perhaps you need to implement it:
1212
candidate #1: `Clone`
13+
help: one of the expressions' fields has a method of the same name
14+
|
15+
LL | let _y = x.i.clone();
16+
| ++
17+
help: one of the expressions' fields has a method of the same name
18+
|
19+
LL | let _y = x.j.x.clone();
20+
| ++++
1321

1422
error: aborting due to previous error
1523

src/test/ui/suggestions/import-trait-for-method-call.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ LL | fn finish(&self) -> u64;
1010
| ------ the method is available for `DefaultHasher` here
1111
|
1212
= help: items from traits can only be used if the trait is in scope
13+
help: one of the expressions' fields has a method of the same name
14+
|
15+
LL | h.0.finish()
16+
| ++
1317
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
1418
|
1519
LL | use std::hash::Hasher;

src/test/ui/suggestions/mut-borrow-needed-by-trait.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ LL | pub struct BufWriter<W: Write> {
4141
`&dyn std::io::Write: std::io::Write`
4242
which is required by `BufWriter<&dyn std::io::Write>: std::io::Write`
4343
= note: this error originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)
44+
help: one of the expressions' fields has a method of the same name
45+
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
46+
|
47+
LL | $dst.inner.write_fmt($crate::format_args_nl!($($arg)*))
48+
| ++++++
49+
help: one of the expressions' fields has a method of the same name
50+
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
51+
|
52+
LL | $dst.buf.write_fmt($crate::format_args_nl!($($arg)*))
53+
| ++++
4454

4555
error: aborting due to 3 previous errors
4656

src/test/ui/suggestions/suggest-using-chars.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ help: because of the in-memory representation of `&str`, to obtain an `Iterator`
2525
|
2626
LL | let _ = String::from("bar").chars();
2727
| ~~~~~
28+
help: one of the expressions' fields has a method of the same name
29+
|
30+
LL | let _ = String::from("bar").vec.iter();
31+
| ++++
2832

2933
error[E0599]: no method named `iter` found for reference `&String` in the current scope
3034
--> $DIR/suggest-using-chars.rs:5:36
@@ -36,6 +40,10 @@ help: because of the in-memory representation of `&str`, to obtain an `Iterator`
3640
|
3741
LL | let _ = (&String::from("bar")).chars();
3842
| ~~~~~
43+
help: one of the expressions' fields has a method of the same name
44+
|
45+
LL | let _ = (&String::from("bar")).vec.iter();
46+
| ++++
3947

4048
error[E0599]: no method named `iter` found for type `{integer}` in the current scope
4149
--> $DIR/suggest-using-chars.rs:6:15

src/test/ui/union/union-derive-clone.mirunsafeck.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ help: consider annotating `CloneNoCopy` with `#[derive(Clone, Copy)]`
4242
|
4343
LL | #[derive(Clone, Copy)]
4444
|
45+
help: one of the expressions' fields has a method of the same name
46+
|
47+
LL | let w = u.a.clone();
48+
| ++
4549

4650
error: aborting due to 2 previous errors
4751

src/test/ui/union/union-derive-clone.thirunsafeck.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ help: consider annotating `CloneNoCopy` with `#[derive(Clone, Copy)]`
4242
|
4343
LL | #[derive(Clone, Copy)]
4444
|
45+
help: one of the expressions' fields has a method of the same name
46+
|
47+
LL | let w = u.a.clone();
48+
| ++
4549

4650
error: aborting due to 2 previous errors
4751

src/test/ui/unique-object-noncopyable.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ LL | | >(Unique<T>, A);
2323
which is required by `Box<dyn Foo>: Clone`
2424
`dyn Foo: Clone`
2525
which is required by `Box<dyn Foo>: Clone`
26+
help: one of the expressions' fields has a method of the same name
27+
|
28+
LL | let _z = y.0.clone();
29+
| ++
30+
help: one of the expressions' fields has a method of the same name
31+
|
32+
LL | let _z = y.1.clone();
33+
| ++
2634

2735
error: aborting due to previous error
2836

src/test/ui/unique-pinned-nocopy.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ help: consider annotating `R` with `#[derive(Clone)]`
2525
|
2626
LL | #[derive(Clone)]
2727
|
28+
help: one of the expressions' fields has a method of the same name
29+
|
30+
LL | let _j = i.0.clone();
31+
| ++
32+
help: one of the expressions' fields has a method of the same name
33+
|
34+
LL | let _j = i.1.clone();
35+
| ++
2836

2937
error: aborting due to previous error
3038

0 commit comments

Comments
 (0)