Skip to content

Commit 8c48c6e

Browse files
estebankMark-Simulacrum
authored andcommitted
Account for unsatisfied bounds in E0599
Fix rust-lang#84769, follow up to rust-lang#84499, rust-lang#83667.
1 parent 40e2c34 commit 8c48c6e

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

compiler/rustc_typeck/src/check/method/suggest.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
579579
}
580580

581581
let mut restrict_type_params = false;
582+
let mut unsatisfied_bounds = false;
582583
if !unsatisfied_predicates.is_empty() {
583584
let def_span = |def_id| {
584585
self.tcx.sess.source_map().guess_head_span(self.tcx.def_span(def_id))
@@ -739,6 +740,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
739740
err.note(&format!(
740741
"the following trait bounds were not satisfied:\n{bound_list}"
741742
));
743+
unsatisfied_bounds = true;
742744
}
743745
}
744746

@@ -752,6 +754,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
752754
source,
753755
out_of_scope_traits,
754756
&unsatisfied_predicates,
757+
unsatisfied_bounds,
755758
);
756759
}
757760

@@ -984,9 +987,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
984987
source: SelfSource<'tcx>,
985988
valid_out_of_scope_traits: Vec<DefId>,
986989
unsatisfied_predicates: &[(ty::Predicate<'tcx>, Option<ty::Predicate<'tcx>>)],
990+
unsatisfied_bounds: bool,
987991
) {
988992
let mut alt_rcvr_sugg = false;
989-
if let SelfSource::MethodCall(rcvr) = source {
993+
if let (SelfSource::MethodCall(rcvr), false) = (source, unsatisfied_bounds) {
990994
debug!(?span, ?item_name, ?rcvr_ty, ?rcvr);
991995
let skippable = [
992996
self.tcx.lang_items().clone_trait(),

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,11 @@ fn next_u64() -> u64 {
66
h.finish() //~ ERROR no method named `finish` found for struct `DefaultHasher`
77
}
88

9-
fn main() {}
9+
trait Bar {}
10+
impl Bar for String {}
11+
12+
fn main() {
13+
let s = String::from("hey");
14+
let x: &dyn Bar = &s;
15+
x.as_ref(); //~ ERROR the method `as_ref` exists for reference `&dyn Bar`, but its trait bounds
16+
}

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ help: the following trait is implemented but not in scope; perhaps add a `use` f
1515
LL | use std::hash::Hasher;
1616
|
1717

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

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

0 commit comments

Comments
 (0)