Skip to content

Suggest both of immutable and mutable trait implementations #89263

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

Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -714,22 +714,28 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
let mut_substs = self.tcx.mk_substs_trait(mut_borrowed_found_ty, &[]);

// Try to apply the original trait binding obligation by borrowing.
let mut try_borrowing = |new_trait_ref: ty::TraitRef<'tcx>,
let mut try_borrowing = |new_imm_trait_ref: ty::TraitRef<'tcx>,
new_mut_trait_ref: ty::TraitRef<'tcx>,
expected_trait_ref: ty::TraitRef<'tcx>,
mtbl: bool,
blacklist: &[DefId]|
-> bool {
if blacklist.contains(&expected_trait_ref.def_id) {
return false;
}

let new_obligation = Obligation::new(
let imm_result = self.predicate_must_hold_modulo_regions(&Obligation::new(
ObligationCause::dummy(),
param_env,
ty::Binder::dummy(new_trait_ref).without_const().to_predicate(self.tcx),
);
ty::Binder::dummy(new_imm_trait_ref).without_const().to_predicate(self.tcx),
));

if self.predicate_must_hold_modulo_regions(&new_obligation) {
let mut_result = self.predicate_must_hold_modulo_regions(&Obligation::new(
ObligationCause::dummy(),
param_env,
ty::Binder::dummy(new_mut_trait_ref).without_const().to_predicate(self.tcx),
));

if imm_result || mut_result {
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
Expand Down Expand Up @@ -773,15 +779,24 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
// }
// ```

err.span_suggestion(
span,
&format!(
"consider{} borrowing here",
if mtbl { " mutably" } else { "" }
),
format!("&{}{}", if mtbl { "mut " } else { "" }, snippet),
Applicability::MaybeIncorrect,
);
if imm_result && mut_result {
err.span_suggestions(
span,
"consider borrowing here",
[format!("&{}", snippet), format!("&mut {}", snippet)].into_iter(),
Applicability::MaybeIncorrect,
);
} else {
err.span_suggestion(
span,
&format!(
"consider{} borrowing here",
if mut_result { " mutably" } else { "" }
),
format!("&{}{}", if mut_result { "mut " } else { "" }, snippet),
Applicability::MaybeIncorrect,
);
}
}
return true;
}
Expand All @@ -795,29 +810,16 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
ty::TraitRef::new(obligation.parent_trait_ref.def_id(), imm_substs);
let new_mut_trait_ref =
ty::TraitRef::new(obligation.parent_trait_ref.def_id(), mut_substs);
if try_borrowing(new_imm_trait_ref, expected_trait_ref, false, &[]) {
return true;
} else {
return try_borrowing(new_mut_trait_ref, expected_trait_ref, true, &[]);
}
return try_borrowing(new_imm_trait_ref, new_mut_trait_ref, expected_trait_ref, &[]);
} else if let ObligationCauseCode::BindingObligation(_, _)
| ObligationCauseCode::ItemObligation(_) = &*code
{
if try_borrowing(
return try_borrowing(
ty::TraitRef::new(trait_ref.def_id, imm_substs),
ty::TraitRef::new(trait_ref.def_id, mut_substs),
trait_ref,
false,
&never_suggest_borrow[..],
) {
return true;
} else {
return try_borrowing(
ty::TraitRef::new(trait_ref.def_id, mut_substs),
trait_ref,
true,
&never_suggest_borrow[..],
);
}
);
} else {
false
}
Expand Down
44 changes: 28 additions & 16 deletions src/test/ui/suggestions/slice-issue-87994.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ error[E0277]: the size for values of type `[i32]` cannot be known at compilation
--> $DIR/slice-issue-87994.rs:3:12
|
LL | for _ in v[1..] {
| ^^^^^^
| |
| expected an implementor of trait `IntoIterator`
| help: consider borrowing here: `&v[1..]`
| ^^^^^^ expected an implementor of trait `IntoIterator`
|
= note: the trait bound `[i32]: IntoIterator` is not satisfied
= note: required because of the requirements on the impl of `IntoIterator` for `[i32]`
Expand All @@ -14,15 +11,18 @@ note: required by `into_iter`
|
LL | fn into_iter(self) -> Self::IntoIter;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider borrowing here
|
LL | for _ in &v[1..] {
| ~~~~~~~
LL | for _ in &mut v[1..] {
| ~~~~~~~~~~~

error[E0277]: `[i32]` is not an iterator
--> $DIR/slice-issue-87994.rs:3:12
|
LL | for _ in v[1..] {
| ^^^^^^
| |
| expected an implementor of trait `IntoIterator`
| help: consider borrowing here: `&v[1..]`
| ^^^^^^ expected an implementor of trait `IntoIterator`
|
= note: the trait bound `[i32]: IntoIterator` is not satisfied
= note: required because of the requirements on the impl of `IntoIterator` for `[i32]`
Expand All @@ -31,15 +31,18 @@ note: required by `into_iter`
|
LL | fn into_iter(self) -> Self::IntoIter;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider borrowing here
|
LL | for _ in &v[1..] {
| ~~~~~~~
LL | for _ in &mut v[1..] {
| ~~~~~~~~~~~

error[E0277]: the size for values of type `[K]` cannot be known at compilation time
--> $DIR/slice-issue-87994.rs:11:13
|
LL | for i2 in v2[1..] {
| ^^^^^^^
| |
| expected an implementor of trait `IntoIterator`
| help: consider borrowing here: `&v2[1..]`
| ^^^^^^^ expected an implementor of trait `IntoIterator`
|
= note: the trait bound `[K]: IntoIterator` is not satisfied
= note: required because of the requirements on the impl of `IntoIterator` for `[K]`
Expand All @@ -48,15 +51,18 @@ note: required by `into_iter`
|
LL | fn into_iter(self) -> Self::IntoIter;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider borrowing here
|
LL | for i2 in &v2[1..] {
| ~~~~~~~~
LL | for i2 in &mut v2[1..] {
| ~~~~~~~~~~~~

error[E0277]: `[K]` is not an iterator
--> $DIR/slice-issue-87994.rs:11:13
|
LL | for i2 in v2[1..] {
| ^^^^^^^
| |
| expected an implementor of trait `IntoIterator`
| help: consider borrowing here: `&v2[1..]`
| ^^^^^^^ expected an implementor of trait `IntoIterator`
|
= note: the trait bound `[K]: IntoIterator` is not satisfied
= note: required because of the requirements on the impl of `IntoIterator` for `[K]`
Expand All @@ -65,6 +71,12 @@ note: required by `into_iter`
|
LL | fn into_iter(self) -> Self::IntoIter;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider borrowing here
|
LL | for i2 in &v2[1..] {
| ~~~~~~~~
LL | for i2 in &mut v2[1..] {
| ~~~~~~~~~~~~

error: aborting due to 4 previous errors

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
trait Trait {}

struct S;

impl Trait for &S {}
impl Trait for &mut S {}

fn foo<X: Trait>(_: X) {}

fn main() {
let s = S;
foo(s); //~ ERROR the trait bound `S: Trait` is not satisfied
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0277]: the trait bound `S: Trait` is not satisfied
--> $DIR/suggest-both-imm-and-mut-trait-implementation.rs:12:9
|
LL | foo(s);
| --- ^ expected an implementor of trait `Trait`
| |
| required by a bound introduced by this call
|
note: required by a bound in `foo`
--> $DIR/suggest-both-imm-and-mut-trait-implementation.rs:8:11
|
LL | fn foo<X: Trait>(_: X) {}
| ^^^^^ required by this bound in `foo`
help: consider borrowing here
|
LL | foo(&s);
| ~~
LL | foo(&mut s);
| ~~~~~~

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ error[E0277]: `main::TestType` cannot be sent between threads safely
--> $DIR/negated-auto-traits-error.rs:66:13
|
LL | is_sync(Outer2(TestType));
| ------- ^^^^^^^^^^^^^^^^
| | |
| | expected an implementor of trait `Sync`
| | help: consider borrowing here: `&Outer2(TestType)`
| ------- ^^^^^^^^^^^^^^^^ expected an implementor of trait `Sync`
| |
| required by a bound introduced by this call
|
= note: the trait bound `main::TestType: Sync` is not satisfied
Expand All @@ -119,6 +117,12 @@ note: required by a bound in `is_sync`
|
LL | fn is_sync<T: Sync>(_: T) {}
| ^^^^ required by this bound in `is_sync`
help: consider borrowing here
|
LL | is_sync(&Outer2(TestType));
| ~~~~~~~~~~~~~~~~~
LL | is_sync(&mut Outer2(TestType));
| ~~~~~~~~~~~~~~~~~~~~~

error: aborting due to 7 previous errors

Expand Down