Rollup merge of #98509 - rust-lang:notriddle/precise-pin-diag, r=compiler-errors

diagnostics: consider parameter count when suggesting smart pointers

Fixes #96834
This commit is contained in:
Yuki Okushi 2022-06-26 13:15:02 +09:00 committed by GitHub
commit c3b2291dc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 1 deletions

View File

@ -994,6 +994,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
span,
rcvr_ty,
item_name,
args.map(|args| args.len()),
source,
out_of_scope_traits,
&unsatisfied_predicates,
@ -1732,6 +1733,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
span: Span,
rcvr_ty: Ty<'tcx>,
item_name: Ident,
inputs_len: Option<usize>,
source: SelfSource<'tcx>,
valid_out_of_scope_traits: Vec<DefId>,
unsatisfied_predicates: &[(
@ -1808,7 +1810,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Explicitly ignore the `Pin::as_ref()` method as `Pin` does not
// implement the `AsRef` trait.
let skip = skippable.contains(&did)
|| (("Pin::new" == *pre) && (sym::as_ref == item_name.name));
|| (("Pin::new" == *pre) && (sym::as_ref == item_name.name))
|| inputs_len.map_or(false, |inputs_len| pick.item.kind == ty::AssocKind::Fn && self.tcx.fn_sig(pick.item.def_id).skip_binder().inputs().len() != inputs_len);
// 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`

View File

@ -0,0 +1,15 @@
// https://github.com/rust-lang/rust/issues/96834
//
// This test case verifies that rustc does not make an unhelpful suggestion:
//
// help: consider wrapping the receiver expression with the appropriate type
// |
// 14 | Pin::new(&mut a).set(0, 3);
// | +++++++++++++ +
//
// We can tell that it isn't helpful, because `Pin::set` takes two parameters (including
// the receiver), but the function call on line 14 supplies three.
fn main() {
let mut a = [0u8; 1];
a.set(0, 3); //~ERROR
}

View File

@ -0,0 +1,9 @@
error[E0599]: no method named `set` found for array `[u8; 1]` in the current scope
--> $DIR/dont-suggest-pin-array-dot-set.rs:14:7
|
LL | a.set(0, 3);
| ^^^ help: there is an associated function with a similar name: `get`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0599`.