about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2021-10-30 20:30:28 +0200
committerGitHub <noreply@github.com>2021-10-30 20:30:28 +0200
commit197da45e183097f108c6f5f283a30070845f182d (patch)
tree8820759ebacf4078fb73111d6660626db112643a
parent06bb1ff1b58c8663493d2c3c8f3eee7a0cce46cc (diff)
parentcad2d21cb669b21b6a31edc0c60b2d0748f3a4f2 (diff)
downloadrust-197da45e183097f108c6f5f283a30070845f182d.tar.gz
rust-197da45e183097f108c6f5f283a30070845f182d.zip
Rollup merge of #90399 - yuvaldolev:as-ref-overly-verbose-diagnostic, r=estebank
Skipping verbose diagnostic suggestions when calling .as_ref() on type not implementing AsRef

Addresses #89806

Skipping suggestions when calling `.as_ref()` for types that do not implement the `AsRef` trait.

r? `@estebank`
-rw-r--r--compiler/rustc_typeck/src/check/method/suggest.rs9
-rw-r--r--src/test/ui/typeck/issue-89806.rs3
-rw-r--r--src/test/ui/typeck/issue-89806.stderr9
3 files changed, 19 insertions, 2 deletions
diff --git a/compiler/rustc_typeck/src/check/method/suggest.rs b/compiler/rustc_typeck/src/check/method/suggest.rs
index 28b19981c2d..8007b9f2377 100644
--- a/compiler/rustc_typeck/src/check/method/suggest.rs
+++ b/compiler/rustc_typeck/src/check/method/suggest.rs
@@ -15,7 +15,7 @@ use rustc_middle::ty::print::with_crate_prefix;
 use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness};
 use rustc_span::lev_distance;
 use rustc_span::symbol::{kw, sym, Ident};
-use rustc_span::{source_map, FileName, MultiSpan, Span};
+use rustc_span::{source_map, FileName, MultiSpan, Span, Symbol};
 use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
 use rustc_trait_selection::traits::{FulfillmentError, Obligation};
 
@@ -1251,6 +1251,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 self.tcx.lang_items().deref_trait(),
                 self.tcx.lang_items().deref_mut_trait(),
                 self.tcx.lang_items().drop_trait(),
+                self.tcx.get_diagnostic_item(sym::AsRef),
             ];
             // Try alternative arbitrary self types that could fulfill this call.
             // FIXME: probe for all types that *could* be arbitrary self-types, not
@@ -1300,7 +1301,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                             // We don't want to suggest a container type when the missing
                             // method is `.clone()` or `.deref()` otherwise we'd suggest
                             // `Arc::new(foo).clone()`, which is far from what the user wants.
-                            let skip = skippable.contains(&did);
+                            // Explicitly ignore the `Pin::as_ref()` method as `Pin` does not
+                            // implement the `AsRef` trait.
+                            let skip = skippable.contains(&did)
+                                || (("Pin::new" == *pre)
+                                    && (Symbol::intern("as_ref") == item_name.name));
                             // Make sure the method is defined for the *actual* receiver: we don't
                             // want to treat `Box<Self>` as a receiver if it only works because of
                             // an autoderef to `&self`
diff --git a/src/test/ui/typeck/issue-89806.rs b/src/test/ui/typeck/issue-89806.rs
new file mode 100644
index 00000000000..69cec08652a
--- /dev/null
+++ b/src/test/ui/typeck/issue-89806.rs
@@ -0,0 +1,3 @@
+fn main() {
+    0u8.as_ref(); //~ ERROR no method named `as_ref` found for type `u8` in the current scope
+}
diff --git a/src/test/ui/typeck/issue-89806.stderr b/src/test/ui/typeck/issue-89806.stderr
new file mode 100644
index 00000000000..c36b4967ee9
--- /dev/null
+++ b/src/test/ui/typeck/issue-89806.stderr
@@ -0,0 +1,9 @@
+error[E0599]: no method named `as_ref` found for type `u8` in the current scope
+  --> $DIR/issue-89806.rs:2:9
+   |
+LL |     0u8.as_ref();
+   |         ^^^^^^ method not found in `u8`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0599`.