Skip to content

Commit 048a42d

Browse files
committed
Fix ICE due to unwrap in probe_for_name_many
1 parent 76e7a08 commit 048a42d

File tree

5 files changed

+89
-29
lines changed

5 files changed

+89
-29
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+24-18
Original file line numberDiff line numberDiff line change
@@ -827,27 +827,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
827827
) else {
828828
return;
829829
};
830-
let in_scope_methods = self.probe_for_name_many(
831-
probe::Mode::MethodCall,
832-
path.ident,
833-
Some(expected),
834-
probe::IsSuggestion(true),
835-
self_ty,
836-
deref.hir_id,
837-
probe::ProbeScope::TraitsInScope,
838-
);
830+
let in_scope_methods = self
831+
.probe_for_name_many(
832+
probe::Mode::MethodCall,
833+
path.ident,
834+
Some(expected),
835+
probe::IsSuggestion(true),
836+
self_ty,
837+
deref.hir_id,
838+
probe::ProbeScope::TraitsInScope,
839+
)
840+
.unwrap_or(Vec::new());
841+
839842
let other_methods_in_scope: Vec<_> =
840843
in_scope_methods.iter().filter(|c| c.item.def_id != pick.item.def_id).collect();
841844

842-
let all_methods = self.probe_for_name_many(
843-
probe::Mode::MethodCall,
844-
path.ident,
845-
Some(expected),
846-
probe::IsSuggestion(true),
847-
self_ty,
848-
deref.hir_id,
849-
probe::ProbeScope::AllTraits,
850-
);
845+
let all_methods = self
846+
.probe_for_name_many(
847+
probe::Mode::MethodCall,
848+
path.ident,
849+
Some(expected),
850+
probe::IsSuggestion(true),
851+
self_ty,
852+
deref.hir_id,
853+
probe::ProbeScope::AllTraits,
854+
)
855+
.unwrap_or(Vec::new());
856+
851857
let suggestions: Vec<_> = all_methods
852858
.into_iter()
853859
.filter(|c| c.item.def_id != pick.item.def_id)

compiler/rustc_hir_typeck/src/method/probe.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
306306
self_ty: Ty<'tcx>,
307307
scope_expr_id: HirId,
308308
scope: ProbeScope,
309-
) -> Vec<Candidate<'tcx>> {
309+
) -> Result<Vec<Candidate<'tcx>>, MethodError<'tcx>> {
310310
self.probe_op(
311311
item_name.span,
312312
mode,
@@ -324,7 +324,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
324324
.collect())
325325
},
326326
)
327-
.unwrap()
328327
}
329328

330329
pub(crate) fn probe_op<OP, R>(

compiler/rustc_hir_typeck/src/method/suggest.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -1640,18 +1640,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16401640
.unwrap_or(Ty::new_misc_error(self.tcx)),
16411641
);
16421642

1643+
let candidates = self
1644+
.probe_for_name_many(
1645+
Mode::MethodCall,
1646+
item_name,
1647+
None,
1648+
IsSuggestion(true),
1649+
rcvr_ty,
1650+
source_expr.hir_id,
1651+
ProbeScope::TraitsInScope,
1652+
)
1653+
.unwrap_or(Vec::new());
1654+
16431655
// FIXME: `probe_for_name_many` searches for methods in inherent implementations,
16441656
// so it may return a candidate that doesn't belong to this `revr_ty`. We need to
16451657
// check whether the instantiated type matches the received one.
1646-
for _matched_method in self.probe_for_name_many(
1647-
Mode::MethodCall,
1648-
item_name,
1649-
None,
1650-
IsSuggestion(true),
1651-
rcvr_ty,
1652-
source_expr.hir_id,
1653-
ProbeScope::TraitsInScope,
1654-
) {
1658+
for _matched_method in candidates {
16551659
// found a match, push to stack
16561660
stack_methods.push(rcvr_ty);
16571661
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Regression test for ICE # 125876
2+
3+
fn main() {
4+
std::ptr::from_ref(num).cast_mut().as_deref();
5+
//~^ ERROR cannot find value `num` in this scope
6+
//~| ERROR no method named `as_deref` found for raw pointer `*mut _` in the current scope
7+
//~| WARN type annotations needed
8+
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018!
9+
//~| WARN type annotations needed
10+
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018!
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error[E0425]: cannot find value `num` in this scope
2+
--> $DIR/ice-unwrap-probe-many-result-125876.rs:4:24
3+
|
4+
LL | std::ptr::from_ref(num).cast_mut().as_deref();
5+
| ^^^ not found in this scope
6+
7+
warning: type annotations needed
8+
--> $DIR/ice-unwrap-probe-many-result-125876.rs:4:29
9+
|
10+
LL | std::ptr::from_ref(num).cast_mut().as_deref();
11+
| ^^^^^^^^
12+
|
13+
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018!
14+
= note: for more information, see issue #46906 <https://git.1-hub.cnrust-lang/rust/issues/46906>
15+
= note: `#[warn(tyvar_behind_raw_pointer)]` on by default
16+
17+
warning: type annotations needed
18+
--> $DIR/ice-unwrap-probe-many-result-125876.rs:4:40
19+
|
20+
LL | std::ptr::from_ref(num).cast_mut().as_deref();
21+
| ^^^^^^^^
22+
|
23+
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018!
24+
= note: for more information, see issue #46906 <https://git.1-hub.cnrust-lang/rust/issues/46906>
25+
26+
error[E0599]: no method named `as_deref` found for raw pointer `*mut _` in the current scope
27+
--> $DIR/ice-unwrap-probe-many-result-125876.rs:4:40
28+
|
29+
LL | std::ptr::from_ref(num).cast_mut().as_deref();
30+
| ^^^^^^^^
31+
|
32+
help: there is a method `as_ref` with a similar name
33+
|
34+
LL | std::ptr::from_ref(num).cast_mut().as_ref();
35+
| ~~~~~~
36+
37+
error: aborting due to 2 previous errors; 2 warnings emitted
38+
39+
Some errors have detailed explanations: E0425, E0599.
40+
For more information about an error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)