about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2021-09-28 20:00:16 +0200
committerGitHub <noreply@github.com>2021-09-28 20:00:16 +0200
commit48b5d110ae8b0917d09b71cdcfa6e5107a4fc224 (patch)
treefa6719e53509e39ddd87795732f64600c5834ab8
parente601554dc0e2443a286cf529c1463b00d8e1fdde (diff)
parent3d08ff1c19360e89a351184b9191454045059823 (diff)
downloadrust-48b5d110ae8b0917d09b71cdcfa6e5107a4fc224.tar.gz
rust-48b5d110ae8b0917d09b71cdcfa6e5107a4fc224.zip
Rollup merge of #89255 - FabianWolff:issue-88806, r=cjgillot
Fix incorrect disambiguation suggestion for associated items

Fixes #88806. I have not added a new test case, because the erroneous behavior is already present in existing test cases.
-rw-r--r--compiler/rustc_typeck/src/check/method/suggest.rs10
-rw-r--r--src/test/ui/associated-consts/associated-const-ambiguity-report.stderr8
-rw-r--r--src/test/ui/error-codes/E0034.stderr8
-rw-r--r--src/test/ui/methods/method-ambig-two-traits-from-impls2.stderr8
-rw-r--r--src/test/ui/span/issue-7575.stderr14
5 files changed, 28 insertions, 20 deletions
diff --git a/compiler/rustc_typeck/src/check/method/suggest.rs b/compiler/rustc_typeck/src/check/method/suggest.rs
index 91a164ce063..9c7853dd78d 100644
--- a/compiler/rustc_typeck/src/check/method/suggest.rs
+++ b/compiler/rustc_typeck/src/check/method/suggest.rs
@@ -176,6 +176,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                                 sugg_span,
                                 idx,
                                 self.tcx.sess.source_map(),
+                                item.fn_has_self_parameter,
                             );
                         }
                     }
@@ -218,6 +219,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                             sugg_span,
                             idx,
                             self.tcx.sess.source_map(),
+                            item.fn_has_self_parameter,
                         );
                     }
                 }
@@ -1736,6 +1738,7 @@ fn print_disambiguation_help(
     span: Span,
     candidate: Option<usize>,
     source_map: &source_map::SourceMap,
+    fn_has_self_parameter: bool,
 ) {
     let mut applicability = Applicability::MachineApplicable;
     let (span, sugg) = if let (ty::AssocKind::Fn, Some(args)) = (kind, args) {
@@ -1754,9 +1757,14 @@ fn print_disambiguation_help(
                 .collect::<Vec<_>>()
                 .join(", "),
         );
+        let trait_name = if !fn_has_self_parameter {
+            format!("<{} as {}>", rcvr_ty, trait_name)
+        } else {
+            trait_name
+        };
         (span, format!("{}::{}{}", trait_name, item_name, args))
     } else {
-        (span.with_hi(item_name.span.lo()), format!("{}::", trait_name))
+        (span.with_hi(item_name.span.lo()), format!("<{} as {}>::", rcvr_ty, trait_name))
     };
     err.span_suggestion_verbose(
         span,
diff --git a/src/test/ui/associated-consts/associated-const-ambiguity-report.stderr b/src/test/ui/associated-consts/associated-const-ambiguity-report.stderr
index d4c12b8e061..60cf9a533cd 100644
--- a/src/test/ui/associated-consts/associated-const-ambiguity-report.stderr
+++ b/src/test/ui/associated-consts/associated-const-ambiguity-report.stderr
@@ -16,12 +16,12 @@ LL |     const ID: i32 = 3;
    |     ^^^^^^^^^^^^^^^^^^
 help: disambiguate the associated constant for candidate #1
    |
-LL | const X: i32 = Foo::ID;
-   |                ~~~~~
+LL | const X: i32 = <i32 as Foo>::ID;
+   |                ~~~~~~~~~~~~~~
 help: disambiguate the associated constant for candidate #2
    |
-LL | const X: i32 = Bar::ID;
-   |                ~~~~~
+LL | const X: i32 = <i32 as Bar>::ID;
+   |                ~~~~~~~~~~~~~~
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/error-codes/E0034.stderr b/src/test/ui/error-codes/E0034.stderr
index 83718a1e273..e2962170265 100644
--- a/src/test/ui/error-codes/E0034.stderr
+++ b/src/test/ui/error-codes/E0034.stderr
@@ -16,12 +16,12 @@ LL |     fn foo() {}
    |     ^^^^^^^^
 help: disambiguate the associated function for candidate #1
    |
-LL |     Trait1::foo()
-   |     ~~~~~~~~
+LL |     <Test as Trait1>::foo()
+   |     ~~~~~~~~~~~~~~~~~~
 help: disambiguate the associated function for candidate #2
    |
-LL |     Trait2::foo()
-   |     ~~~~~~~~
+LL |     <Test as Trait2>::foo()
+   |     ~~~~~~~~~~~~~~~~~~
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/methods/method-ambig-two-traits-from-impls2.stderr b/src/test/ui/methods/method-ambig-two-traits-from-impls2.stderr
index e0a58aed8f4..4ba778e0ef7 100644
--- a/src/test/ui/methods/method-ambig-two-traits-from-impls2.stderr
+++ b/src/test/ui/methods/method-ambig-two-traits-from-impls2.stderr
@@ -16,12 +16,12 @@ LL |     fn foo() {}
    |     ^^^^^^^^
 help: disambiguate the associated function for candidate #1
    |
-LL |     A::foo();
-   |     ~~~
+LL |     <AB as A>::foo();
+   |     ~~~~~~~~~~~
 help: disambiguate the associated function for candidate #2
    |
-LL |     B::foo();
-   |     ~~~
+LL |     <AB as B>::foo();
+   |     ~~~~~~~~~~~
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/span/issue-7575.stderr b/src/test/ui/span/issue-7575.stderr
index 288c1042a26..783f5aca417 100644
--- a/src/test/ui/span/issue-7575.stderr
+++ b/src/test/ui/span/issue-7575.stderr
@@ -27,16 +27,16 @@ LL |     fn f9(_: usize) -> usize;
            candidate #3: `UnusedTrait`
 help: disambiguate the associated function for candidate #1
    |
-LL |     u.f8(42) + CtxtFn::f9(u, 342) + m.fff(42)
-   |                ~~~~~~~~~~~~~~~~~~
+LL |     u.f8(42) + <usize as CtxtFn>::f9(u, 342) + m.fff(42)
+   |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 help: disambiguate the associated function for candidate #2
    |
-LL |     u.f8(42) + OtherTrait::f9(u, 342) + m.fff(42)
-   |                ~~~~~~~~~~~~~~~~~~~~~~
+LL |     u.f8(42) + <usize as OtherTrait>::f9(u, 342) + m.fff(42)
+   |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 help: disambiguate the associated function for candidate #3
    |
-LL |     u.f8(42) + UnusedTrait::f9(u, 342) + m.fff(42)
-   |                ~~~~~~~~~~~~~~~~~~~~~~~
+LL |     u.f8(42) + <usize as UnusedTrait>::f9(u, 342) + m.fff(42)
+   |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 error[E0599]: no method named `fff` found for struct `Myisize` in the current scope
   --> $DIR/issue-7575.rs:62:30
@@ -72,7 +72,7 @@ LL |     fn is_str() -> bool {
    = help: items from traits can only be used if the type parameter is bounded by the trait
 help: disambiguate the associated function for the candidate
    |
-LL |     ManyImplTrait::is_str(t)
+LL |     <T as ManyImplTrait>::is_str(t)
    |
 
 error: aborting due to 3 previous errors