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

Fix suggestion to borrow in struct #71829

Merged
merged 4 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,13 +562,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
param_env,
new_trait_ref.without_const().to_predicate(),
);

if self.predicate_must_hold_modulo_regions(&new_obligation) {
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
// We have a very specific type of error, where just borrowing this argument
// might solve the problem. In cases like this, the important part is the
// original type obligation, not the last one that failed, which is arbitrary.
// Because of this, we modify the error to refer to the original obligation and
// return early in the caller.

let msg = format!(
"the trait bound `{}: {}` is not satisfied",
found,
Expand All @@ -591,12 +593,23 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
obligation.parent_trait_ref.skip_binder().print_only_trait_path(),
),
);
err.span_suggestion(
span,
"consider borrowing here",
format!("&{}", snippet),
Applicability::MaybeIncorrect,
);

// This if is to prevent a special edge-case
if !span.from_expansion() {
// We don't want a borrowing suggestion on the fields in structs,
// ```
// struct Foo {
// the_foos: Vec<Foo>
// }
// ```

err.span_suggestion(
span,
"consider borrowing here",
format!("&{}", snippet),
Applicability::MaybeIncorrect,
);
}
return true;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/traits/traits-issue-71136.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struct Foo(u8);

#[derive(Clone)]
struct FooHolster {
the_foos: Vec<Foo>, //~ERROR Clone
}

fn main() {}
13 changes: 13 additions & 0 deletions src/test/ui/traits/traits-issue-71136.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0277]: the trait bound `Foo: std::clone::Clone` is not satisfied
--> $DIR/traits-issue-71136.rs:5:5
|
LL | the_foos: Vec<Foo>,
| ^^^^^^^^^^^^^^^^^^ expected an implementor of trait `std::clone::Clone`
|
= note: required because of the requirements on the impl of `std::clone::Clone` for `std::vec::Vec<Foo>`
= note: required by `std::clone::Clone::clone`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.