Skip to content

Commit 197da45

Browse files
Rollup merge of rust-lang#90399 - yuvaldolev:as-ref-overly-verbose-diagnostic, 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`
2 parents 06bb1ff + cad2d21 commit 197da45

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

Diff for: compiler/rustc_typeck/src/check/method/suggest.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::ty::print::with_crate_prefix;
1515
use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness};
1616
use rustc_span::lev_distance;
1717
use rustc_span::symbol::{kw, sym, Ident};
18-
use rustc_span::{source_map, FileName, MultiSpan, Span};
18+
use rustc_span::{source_map, FileName, MultiSpan, Span, Symbol};
1919
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
2020
use rustc_trait_selection::traits::{FulfillmentError, Obligation};
2121

@@ -1251,6 +1251,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12511251
self.tcx.lang_items().deref_trait(),
12521252
self.tcx.lang_items().deref_mut_trait(),
12531253
self.tcx.lang_items().drop_trait(),
1254+
self.tcx.get_diagnostic_item(sym::AsRef),
12541255
];
12551256
// Try alternative arbitrary self types that could fulfill this call.
12561257
// FIXME: probe for all types that *could* be arbitrary self-types, not
@@ -1300,7 +1301,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13001301
// We don't want to suggest a container type when the missing
13011302
// method is `.clone()` or `.deref()` otherwise we'd suggest
13021303
// `Arc::new(foo).clone()`, which is far from what the user wants.
1303-
let skip = skippable.contains(&did);
1304+
// Explicitly ignore the `Pin::as_ref()` method as `Pin` does not
1305+
// implement the `AsRef` trait.
1306+
let skip = skippable.contains(&did)
1307+
|| (("Pin::new" == *pre)
1308+
&& (Symbol::intern("as_ref") == item_name.name));
13041309
// Make sure the method is defined for the *actual* receiver: we don't
13051310
// want to treat `Box<Self>` as a receiver if it only works because of
13061311
// an autoderef to `&self`

Diff for: src/test/ui/typeck/issue-89806.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
0u8.as_ref(); //~ ERROR no method named `as_ref` found for type `u8` in the current scope
3+
}

Diff for: src/test/ui/typeck/issue-89806.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0599]: no method named `as_ref` found for type `u8` in the current scope
2+
--> $DIR/issue-89806.rs:2:9
3+
|
4+
LL | 0u8.as_ref();
5+
| ^^^^^^ method not found in `u8`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)