Skip to content

Commit dd27f85

Browse files
authored
Rollup merge of #152727 - lcnr:method-ambig-err-taint, r=jackh726
`probe_op` silence ambiguity errors if tainted see the `proc-macro/quote/not-repeatable.rs` test for a case where this is useful r? types
2 parents dc77672 + 7e14260 commit dd27f85

9 files changed

Lines changed: 52 additions & 48 deletions

File tree

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
490490
.unwrap_or_else(|_| span_bug!(span, "instantiating {:?} failed?", ty));
491491
let ty = self.resolve_vars_if_possible(ty.value);
492492
let guar = match *ty.kind() {
493+
_ if let Some(guar) = self.tainted_by_errors() => guar,
493494
ty::Infer(ty::TyVar(_)) => {
494495
// We want to get the variable name that the method
495496
// is being called on. If it is a method call.

tests/ui/methods/call_method_unknown_pointee.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,39 @@
33
// tests that the pointee type of a raw pointer must be known to call methods on it
44
// see also: `tests/ui/editions/edition-raw-pointer-method-2018.rs`
55

6-
fn main() {
7-
let val = 1_u32;
8-
let ptr = &val as *const u32;
6+
fn a() {
7+
let ptr = &1u32 as *const u32;
98
unsafe {
109
let _a: i32 = (ptr as *const _).read();
1110
//~^ ERROR type annotations needed
11+
}
12+
}
13+
14+
fn b() {
15+
let ptr = &1u32 as *const u32;
16+
unsafe {
1217
let b = ptr as *const _;
1318
//~^ ERROR type annotations needed
1419
let _b: u8 = b.read();
15-
let _c = (ptr as *const u8).read(); // we know the type here
1620
}
21+
}
22+
1723

18-
let mut val = 2_u32;
19-
let ptr = &mut val as *mut u32;
24+
fn c() {
25+
let ptr = &mut 2u32 as *mut u32;
2026
unsafe {
21-
let _a: i32 = (ptr as *mut _).read();
27+
let _c: i32 = (ptr as *mut _).read();
2228
//~^ ERROR type annotations needed
23-
let b = ptr as *mut _;
29+
}
30+
}
31+
32+
fn d() {
33+
let ptr = &mut 2u32 as *mut u32;
34+
unsafe {
35+
let d = ptr as *mut _;
2436
//~^ ERROR type annotations needed
25-
b.write(10);
26-
(ptr as *mut i32).write(1000); // we know the type here
37+
let _d: u8 = d.read();
2738
}
2839
}
40+
41+
fn main() {}

tests/ui/methods/call_method_unknown_pointee.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/call_method_unknown_pointee.rs:10:23
2+
--> $DIR/call_method_unknown_pointee.rs:9:23
33
|
44
LL | let _a: i32 = (ptr as *const _).read();
55
| ^^^^^^^^^^^^^^^^^ ---- cannot call a method on a raw pointer with an unknown pointee type
66
| |
77
| cannot infer type
88

99
error[E0282]: type annotations needed for `*const _`
10-
--> $DIR/call_method_unknown_pointee.rs:12:13
10+
--> $DIR/call_method_unknown_pointee.rs:17:13
1111
|
1212
LL | let b = ptr as *const _;
1313
| ^
@@ -21,25 +21,25 @@ LL | let b: *const _ = ptr as *const _;
2121
| ++++++++++
2222

2323
error[E0282]: type annotations needed
24-
--> $DIR/call_method_unknown_pointee.rs:21:23
24+
--> $DIR/call_method_unknown_pointee.rs:27:23
2525
|
26-
LL | let _a: i32 = (ptr as *mut _).read();
26+
LL | let _c: i32 = (ptr as *mut _).read();
2727
| ^^^^^^^^^^^^^^^ ---- cannot call a method on a raw pointer with an unknown pointee type
2828
| |
2929
| cannot infer type
3030

3131
error[E0282]: type annotations needed for `*mut _`
32-
--> $DIR/call_method_unknown_pointee.rs:23:13
32+
--> $DIR/call_method_unknown_pointee.rs:35:13
3333
|
34-
LL | let b = ptr as *mut _;
34+
LL | let d = ptr as *mut _;
3535
| ^
3636
LL |
37-
LL | b.write(10);
38-
| ----- cannot call a method on a raw pointer with an unknown pointee type
37+
LL | let _d: u8 = d.read();
38+
| ---- cannot call a method on a raw pointer with an unknown pointee type
3939
|
40-
help: consider giving `b` an explicit type, where the placeholders `_` are specified
40+
help: consider giving `d` an explicit type, where the placeholders `_` are specified
4141
|
42-
LL | let b: *mut _ = ptr as *mut _;
42+
LL | let d: *mut _ = ptr as *mut _;
4343
| ++++++++
4444

4545
error: aborting due to 4 previous errors

tests/ui/methods/call_method_unknown_referent.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@ impl<T> SmartPtr<T> {
1414
fn foo(&self) {}
1515
}
1616

17-
fn main() {
18-
let val = 1_u32;
19-
let ptr = &val;
17+
fn a() {
18+
let ptr = &1u32;
2019
let _a: i32 = (ptr as &_).read();
2120
//~^ ERROR type annotations needed
21+
}
2222

23+
fn b() {
2324
// Same again, but with a smart pointer type
24-
let val2 = 1_u32;
25-
let rc = std::rc::Rc::new(val2);
25+
let rc = std::rc::Rc::new(1u32);
2626
let _b = (rc as std::rc::Rc<_>).read();
2727
//~^ ERROR type annotations needed
28+
}
2829

30+
fn c() {
2931
// Same again, but with a smart pointer type
30-
let ptr = SmartPtr(val);
32+
let ptr = SmartPtr(1u32);
3133

3234
// We can call unambiguous outer-type methods on this
3335
(ptr as SmartPtr<_>).foo();
@@ -46,3 +48,5 @@ fn main() {
4648
let _c = (ptr as SmartPtr<_>).read();
4749
//~^ ERROR no method named `read` found for struct `SmartPtr<T>`
4850
}
51+
52+
fn main() {}

tests/ui/methods/call_method_unknown_referent.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/call_method_unknown_referent.rs:20:19
2+
--> $DIR/call_method_unknown_referent.rs:19:19
33
|
44
LL | let _a: i32 = (ptr as &_).read();
55
| ^^^^^^^^^^^ cannot infer type
@@ -11,7 +11,7 @@ LL | let _b = (rc as std::rc::Rc<_>).read();
1111
| ^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
1212

1313
error[E0599]: no method named `read` found for struct `SmartPtr<T>` in the current scope
14-
--> $DIR/call_method_unknown_referent.rs:46:35
14+
--> $DIR/call_method_unknown_referent.rs:48:35
1515
|
1616
LL | struct SmartPtr<T>(T);
1717
| ------------------ method `read` not found for this struct

tests/ui/proc-macro/quote/not-repeatable.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ fn main() {
1010
let ip = Ipv4Addr;
1111
let _ = quote! { $($ip)* };
1212
//~^ ERROR the method `quote_into_iter` exists for struct `Ipv4Addr`, but its trait bounds were not satisfied
13-
//~| ERROR type annotations needed
1413
}

tests/ui/proc-macro/quote/not-repeatable.stderr

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ note: the traits `Iterator` and `ToTokens` must be implemented
2020
--> $SRC_DIR/proc_macro/src/to_tokens.rs:LL:COL
2121
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
2222

23-
error[E0282]: type annotations needed
24-
--> $DIR/not-repeatable.rs:11:25
25-
|
26-
LL | let _ = quote! { $($ip)* };
27-
| ^^ cannot infer type
28-
29-
error: aborting due to 2 previous errors
23+
error: aborting due to 1 previous error
3024

31-
Some errors have detailed explanations: E0282, E0599.
32-
For more information about an error, try `rustc --explain E0282`.
25+
For more information about this error, try `rustc --explain E0599`.

tests/ui/typeck/issue-13853.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl Node for Stuff {
2525

2626
fn iterate<N: Node, G: Graph<N>>(graph: &G) {
2727
for node in graph.iter() { //~ ERROR no method named `iter` found
28-
node.zomg(); //~ ERROR type annotations needed
28+
node.zomg();
2929
}
3030
}
3131

tests/ui/typeck/issue-13853.stderr

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ error[E0599]: no method named `iter` found for reference `&G` in the current sco
1717
LL | for node in graph.iter() {
1818
| ^^^^ method not found in `&G`
1919

20-
error[E0282]: type annotations needed
21-
--> $DIR/issue-13853.rs:28:9
22-
|
23-
LL | node.zomg();
24-
| ^^^^ cannot infer type
25-
2620
error[E0308]: mismatched types
2721
--> $DIR/issue-13853.rs:37:13
2822
|
@@ -43,7 +37,7 @@ help: consider borrowing here
4337
LL | iterate(&graph);
4438
| +
4539

46-
error: aborting due to 4 previous errors
40+
error: aborting due to 3 previous errors
4741

48-
Some errors have detailed explanations: E0282, E0308, E0599.
49-
For more information about an error, try `rustc --explain E0282`.
42+
Some errors have detailed explanations: E0308, E0599.
43+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)