Skip to content

Commit

Permalink
Rollup merge of rust-lang#84808 - estebank:issue-84769, r=petrochenkov
Browse files Browse the repository at this point in the history
Account for unsatisfied bounds in E0599

Fix rust-lang#84769, follow up to rust-lang#84499, rust-lang#83667.
  • Loading branch information
Dylan-DPC authored May 4, 2021
2 parents 89f38a0 + 1e89b58 commit 7ac6a01
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
6 changes: 5 additions & 1 deletion compiler/rustc_typeck/src/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}

let mut restrict_type_params = false;
let mut unsatisfied_bounds = false;
if !unsatisfied_predicates.is_empty() {
let def_span = |def_id| {
self.tcx.sess.source_map().guess_head_span(self.tcx.def_span(def_id))
Expand Down Expand Up @@ -739,6 +740,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.note(&format!(
"the following trait bounds were not satisfied:\n{bound_list}"
));
unsatisfied_bounds = true;
}
}

Expand All @@ -752,6 +754,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
source,
out_of_scope_traits,
&unsatisfied_predicates,
unsatisfied_bounds,
);
}

Expand Down Expand Up @@ -984,9 +987,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
source: SelfSource<'tcx>,
valid_out_of_scope_traits: Vec<DefId>,
unsatisfied_predicates: &[(ty::Predicate<'tcx>, Option<ty::Predicate<'tcx>>)],
unsatisfied_bounds: bool,
) {
let mut alt_rcvr_sugg = false;
if let SelfSource::MethodCall(rcvr) = source {
if let (SelfSource::MethodCall(rcvr), false) = (source, unsatisfied_bounds) {
debug!(?span, ?item_name, ?rcvr_ty, ?rcvr);
let skippable = [
self.tcx.lang_items().clone_trait(),
Expand Down
9 changes: 8 additions & 1 deletion src/test/ui/suggestions/import-trait-for-method-call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@ fn next_u64() -> u64 {
h.finish() //~ ERROR no method named `finish` found for struct `DefaultHasher`
}

fn main() {}
trait Bar {}
impl Bar for String {}

fn main() {
let s = String::from("hey");
let x: &dyn Bar = &s;
x.as_ref(); //~ ERROR the method `as_ref` exists for reference `&dyn Bar`, but its trait bounds
}
18 changes: 17 additions & 1 deletion src/test/ui/suggestions/import-trait-for-method-call.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ help: the following trait is implemented but not in scope; perhaps add a `use` f
LL | use std::hash::Hasher;
|

error: aborting due to previous error
error[E0599]: the method `as_ref` exists for reference `&dyn Bar`, but its trait bounds were not satisfied
--> $DIR/import-trait-for-method-call.rs:15:7
|
LL | trait Bar {}
| --------- doesn't satisfy `dyn Bar: AsRef<_>`
...
LL | x.as_ref();
| ^^^^^^ method cannot be called on `&dyn Bar` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`dyn Bar: AsRef<_>`
which is required by `&dyn Bar: AsRef<_>`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `as_ref`, perhaps you need to implement it:
candidate #1: `AsRef`

error: aborting due to 2 previous errors

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

0 comments on commit 7ac6a01

Please sign in to comment.