Skip to content
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

Suggest associated type when the specified one cannot be found #67406

Merged
merged 1 commit into from
Dec 19, 2019

Conversation

ohadravid
Copy link
Contributor

@ohadravid ohadravid commented Dec 18, 2019

Fixes #67386, so code like this:

use std::ops::Deref;

fn homura<T: Deref<Trget = i32>>(_: T) {}

fn main() {}

results in:

error[E0220]: associated type `Trget` not found for `std::ops::Deref`
 --> type-binding.rs:6:20
  |
6 | fn homura<T: Deref<Trget = i32>>(_: T) {}
  |                    ^^^^^^^^^^^ help: there is an associated type with a similar name: `Target`

error: aborting due to previous error

(The help is new)

I used an all_candidates: impl Fn() -> Iterator<...> instead of collecting to avoid the cost of allocating the Vec when no errors are found, at the expense of a little added complexity.

r? @estebank

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 18, 2019
Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good and love the new output.

Left some nitpicks inline.

.filter_map(|(p, _)| p.to_opt_poly_trait_ref());

traits::transitive_bounds(tcx, bounds)
},
&param_name.as_str(),
assoc_name,
span)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clean this up to be a bit terser?

        self.one_bound_for_assoc_type(
            || traits::transitive_bounds(tcx, predicates.iter()
                .filter_map(|(p, _)| p.to_opt_poly_trait_ref())),
            &param_name.as_str(),
            assoc_name,
            span,
        )

Comment on lines 1561 to 1596
let mut err = struct_span_err!(self.tcx().sess, span, E0220,
"associated type `{}` not found for `{}`",
assoc_name,
ty_param_name)
.span_label(span, format!("associated type `{}` not found", assoc_name))
.emit();
ty_param_name);

let all_candidate_names: Vec<_> = all_candidates()
.map(|r| self.tcx().associated_items(r.def_id()))
.flatten()
.filter_map(|item|
if item.kind == ty::AssocKind::Type {
Some(item.ident.name)
} else {
None
}
)
.collect();

if let Some(suggested_name) = find_best_match_for_name(
all_candidate_names.iter(),
&assoc_name.as_str(),
None,
) {
err.span_suggestion(
span,
"there is an associated type with a similar name",
suggested_name.to_string(),
Applicability::MaybeIncorrect,
);
} else {
err.span_label(
span,
format!("associated type `{}` not found", assoc_name)
);
}

err.emit();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move all this to a separate method called complain_about_assoc_type_not_found?

@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Dec 18, 2019

📌 Commit a4a2fc0 has been approved by estebank

@bors
Copy link
Contributor

bors commented Dec 18, 2019

🌲 The tree is currently closed for pull requests below priority 100, this pull request will be tested once the tree is reopened

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 18, 2019
Centril added a commit to Centril/rust that referenced this pull request Dec 19, 2019
…ebank

Suggest associated type when the specified one cannot be found

Fixes rust-lang#67386, so code like this:
```
use std::ops::Deref;

fn homura<T: Deref<Trget = i32>>(_: T) {}

fn main() {}
```

results in:
```
error[E0220]: associated type `Trget` not found for `std::ops::Deref`
 --> type-binding.rs:6:20
  |
6 | fn homura<T: Deref<Trget = i32>>(_: T) {}
  |                    ^^^^^^^^^^^ help: there is an associated type with a similar name: `Target`

error: aborting due to previous error
```

(The `help` is new)

I used an `all_candidates: impl Fn() -> Iterator<...>` instead of `collect`ing to avoid the cost of allocating the Vec when no errors are found, at the expense of a little added complexity.

r? @estebank
bors added a commit that referenced this pull request Dec 19, 2019
Rollup of 8 pull requests

Successful merges:

 - #67189 (Unify binop wording)
 - #67270 (std: Implement `LineWriter::write_vectored`)
 - #67286 (Fix the configure.py TOML field for a couple LLVM options)
 - #67321 (make htons const fn)
 - #67382 (Remove some unnecessary `ATTR_*` constants.)
 - #67389 (Remove `SO_NOSIGPIPE` dummy variable on platforms that don't use it.)
 - #67394 (Remove outdated references to @t from comments)
 - #67406 (Suggest associated type when the specified one cannot be found)

Failed merges:

r? @ghost
@bors bors merged commit a4a2fc0 into rust-lang:master Dec 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typos in associated types should receive suggestions based on lev distance
4 participants