Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 82f8b8f

Browse files
committedNov 2, 2024
Use opt functions to not ICE in fallback suggestion
1 parent 588a420 commit 82f8b8f

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed
 

‎compiler/rustc_hir_typeck/src/fallback.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
643643
fn visit_ty(&mut self, hir_ty: &'tcx hir::Ty<'tcx>) -> Self::Result {
644644
// Try to replace `_` with `()`.
645645
if let hir::TyKind::Infer = hir_ty.kind
646-
&& let ty = self.fcx.typeck_results.borrow().node_type(hir_ty.hir_id)
646+
&& let Some(ty) = self.fcx.typeck_results.borrow().node_type_opt(hir_ty.hir_id)
647647
&& let Some(vid) = self.fcx.root_vid(ty)
648648
&& self.reachable_vids.contains(&vid)
649649
{
@@ -680,7 +680,8 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
680680
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
681681
&& let Res::Def(DefKind::AssocFn, def_id) = path.res
682682
&& self.fcx.tcx.trait_of_item(def_id).is_some()
683-
&& let self_ty = self.fcx.typeck_results.borrow().node_args(expr.hir_id).type_at(0)
683+
&& let Some(args) = self.fcx.typeck_results.borrow().node_args_opt(expr.hir_id)
684+
&& let self_ty = args.type_at(0)
684685
&& let Some(vid) = self.fcx.root_vid(self_ty)
685686
&& self.reachable_vids.contains(&vid)
686687
&& let [.., trait_segment, _method_segment] = path.segments
@@ -701,7 +702,7 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
701702
fn visit_local(&mut self, local: &'tcx hir::LetStmt<'tcx>) -> Self::Result {
702703
// For a local, try suggest annotating the type if it's missing.
703704
if let None = local.ty
704-
&& let ty = self.fcx.typeck_results.borrow().node_type(local.hir_id)
705+
&& let Some(ty) = self.fcx.typeck_results.borrow().node_type_opt(local.hir_id)
705706
&& let Some(vid) = self.fcx.root_vid(ty)
706707
&& self.reachable_vids.contains(&vid)
707708
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
x::<_>(|_| panic!())
3+
//~^ ERROR cannot find function `x` in this scope
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0425]: cannot find function `x` in this scope
2+
--> $DIR/suggestion-ice-132517.rs:2:5
3+
|
4+
LL | x::<_>(|_| panic!())
5+
| ^ not found in this scope
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)
Failed to load comments.