Skip to content

Commit 3e72705

Browse files
authored
Rollup merge of #93996 - notriddle:notriddle/magically-becomes-a-function, r=petrochenkov
Do not suggest "is a function" for free variables Part of #82323
2 parents 2c0df80 + 65fc705 commit 3e72705

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

compiler/rustc_typeck/src/check/method/suggest.rs

+12
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4242
Err(..) => return false,
4343
};
4444

45+
// This conditional prevents us from asking to call errors and unresolved types.
46+
// It might seem that we can use `predicate_must_hold_modulo_regions`,
47+
// but since a Dummy binder is used to fill in the FnOnce trait's arguments,
48+
// type resolution always gives a "maybe" here.
49+
if self.autoderef(span, ty).any(|(ty, _)| {
50+
info!("check deref {:?} error", ty);
51+
matches!(ty.kind(), ty::Error(_) | ty::Infer(_))
52+
}) {
53+
return false;
54+
}
55+
4556
self.autoderef(span, ty).any(|(ty, _)| {
57+
info!("check deref {:?} impl FnOnce", ty);
4658
self.probe(|_| {
4759
let fn_once_substs = tcx.mk_substs_trait(
4860
ty,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
struct Struct<T>(T);
2+
impl Struct<T>
3+
//~^ ERROR cannot find type `T` in this scope
4+
//~| NOTE not found in this scope
5+
//~| HELP you might be missing a type parameter
6+
where
7+
T: Copy,
8+
//~^ ERROR cannot find type `T` in this scope
9+
//~| NOTE not found in this scope
10+
{
11+
// The part where it claims that there is no method named `len` is a bug. Feel free to fix it.
12+
// This test is intended to ensure that a different bug, where it claimed
13+
// that `v` was a function, does not regress.
14+
fn method(v: Vec<u8>) { v.len(); }
15+
//~^ ERROR type annotations needed
16+
//~| NOTE cannot infer type
17+
//~| NOTE type must be known at this point
18+
//~| ERROR no method named `len`
19+
//~| NOTE private field, not a method
20+
}
21+
22+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error[E0412]: cannot find type `T` in this scope
2+
--> $DIR/fn-help-with-err-generic-is-not-function.rs:2:13
3+
|
4+
LL | impl Struct<T>
5+
| - ^ not found in this scope
6+
| |
7+
| help: you might be missing a type parameter: `<T>`
8+
9+
error[E0412]: cannot find type `T` in this scope
10+
--> $DIR/fn-help-with-err-generic-is-not-function.rs:7:5
11+
|
12+
LL | T: Copy,
13+
| ^ not found in this scope
14+
15+
error[E0282]: type annotations needed
16+
--> $DIR/fn-help-with-err-generic-is-not-function.rs:14:31
17+
|
18+
LL | fn method(v: Vec<u8>) { v.len(); }
19+
| ^^^ cannot infer type
20+
|
21+
= note: type must be known at this point
22+
23+
error[E0599]: no method named `len` found for struct `Vec<u8>` in the current scope
24+
--> $DIR/fn-help-with-err-generic-is-not-function.rs:14:31
25+
|
26+
LL | fn method(v: Vec<u8>) { v.len(); }
27+
| ^^^ private field, not a method
28+
29+
error: aborting due to 4 previous errors
30+
31+
Some errors have detailed explanations: E0282, E0412, E0599.
32+
For more information about an error, try `rustc --explain E0282`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This test case checks the behavior of typeck::check::method::suggest::is_fn on Ty::Error.
2+
fn main() {
3+
let arc = std::sync::Arc::new(oops);
4+
//~^ ERROR cannot find value `oops` in this scope
5+
//~| NOTE not found
6+
// The error "note: `arc` is a function, perhaps you wish to call it" MUST NOT appear.
7+
arc.blablabla();
8+
//~^ ERROR no method named `blablabla`
9+
//~| NOTE method not found
10+
let arc2 = std::sync::Arc::new(|| 1);
11+
// The error "note: `arc2` is a function, perhaps you wish to call it" SHOULD appear
12+
arc2.blablabla();
13+
//~^ ERROR no method named `blablabla`
14+
//~| NOTE method not found
15+
//~| NOTE `arc2` is a function, perhaps you wish to call it
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0425]: cannot find value `oops` in this scope
2+
--> $DIR/fn-help-with-err.rs:3:35
3+
|
4+
LL | let arc = std::sync::Arc::new(oops);
5+
| ^^^^ not found in this scope
6+
7+
error[E0599]: no method named `blablabla` found for struct `Arc<_>` in the current scope
8+
--> $DIR/fn-help-with-err.rs:7:9
9+
|
10+
LL | arc.blablabla();
11+
| ^^^^^^^^^ method not found in `Arc<_>`
12+
13+
error[E0599]: no method named `blablabla` found for struct `Arc<[closure@$DIR/fn-help-with-err.rs:10:36: 10:40]>` in the current scope
14+
--> $DIR/fn-help-with-err.rs:12:10
15+
|
16+
LL | arc2.blablabla();
17+
| ^^^^^^^^^ method not found in `Arc<[closure@$DIR/fn-help-with-err.rs:10:36: 10:40]>`
18+
|
19+
= note: `arc2` is a function, perhaps you wish to call it
20+
21+
error: aborting due to 3 previous errors
22+
23+
Some errors have detailed explanations: E0425, E0599.
24+
For more information about an error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)