Skip to content

Commit

Permalink
Rollup merge of rust-lang#90399 - yuvaldolev:as-ref-overly-verbose-di…
Browse files Browse the repository at this point in the history
…agnostic, r=estebank

Skipping verbose diagnostic suggestions when calling .as_ref() on type not implementing AsRef

Addresses rust-lang#89806

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

r? `@estebank`
  • Loading branch information
GuillaumeGomez authored Oct 30, 2021
2 parents 06bb1ff + cad2d21 commit 197da45
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
9 changes: 7 additions & 2 deletions compiler/rustc_typeck/src/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/typeck/issue-89806.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
0u8.as_ref(); //~ ERROR no method named `as_ref` found for type `u8` in the current scope
}
9 changes: 9 additions & 0 deletions src/test/ui/typeck/issue-89806.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 197da45

Please sign in to comment.