From 4f773af1cc17621e7c52afba6be2db21b2f3aba5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= <me@fmease.dev>
Date: Sat, 3 Feb 2024 17:34:25 +0100
Subject: [PATCH] Check for presence of field in typeck results before visiting
 it

Co-authored-by: Michael Goulet <michael@errs.io>
---
 compiler/rustc_passes/src/dead.rs               |  6 +++++-
 .../field-access-after-const-eval-fail-in-ty.rs |  5 +++++
 ...ld-access-after-const-eval-fail-in-ty.stderr | 17 +++++++++++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)
 create mode 100644 tests/ui/consts/const-eval/field-access-after-const-eval-fail-in-ty.rs
 create mode 100644 tests/ui/consts/const-eval/field-access-after-const-eval-fail-in-ty.stderr

diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs
index 9be286744351f..d19987cb33cf2 100644
--- a/compiler/rustc_passes/src/dead.rs
+++ b/compiler/rustc_passes/src/dead.rs
@@ -464,7 +464,11 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
                 self.lookup_and_handle_method(expr.hir_id);
             }
             hir::ExprKind::Field(ref lhs, ..) => {
-                self.handle_field_access(lhs, expr.hir_id);
+                if self.typeck_results().opt_field_index(expr.hir_id).is_some() {
+                    self.handle_field_access(lhs, expr.hir_id);
+                } else {
+                    self.tcx.dcx().span_delayed_bug(expr.span, "couldn't resolve index for field");
+                }
             }
             hir::ExprKind::Struct(qpath, fields, _) => {
                 let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
diff --git a/tests/ui/consts/const-eval/field-access-after-const-eval-fail-in-ty.rs b/tests/ui/consts/const-eval/field-access-after-const-eval-fail-in-ty.rs
new file mode 100644
index 0000000000000..3f1f208459d17
--- /dev/null
+++ b/tests/ui/consts/const-eval/field-access-after-const-eval-fail-in-ty.rs
@@ -0,0 +1,5 @@
+// Regression test for issue #120615.
+
+fn main() {
+    [(); loop {}].field; //~ ERROR constant evaluation is taking a long time
+}
diff --git a/tests/ui/consts/const-eval/field-access-after-const-eval-fail-in-ty.stderr b/tests/ui/consts/const-eval/field-access-after-const-eval-fail-in-ty.stderr
new file mode 100644
index 0000000000000..9d62bbc2187f7
--- /dev/null
+++ b/tests/ui/consts/const-eval/field-access-after-const-eval-fail-in-ty.stderr
@@ -0,0 +1,17 @@
+error: constant evaluation is taking a long time
+  --> $DIR/field-access-after-const-eval-fail-in-ty.rs:4:10
+   |
+LL |     [(); loop {}].field;
+   |          ^^^^^^^
+   |
+   = note: this lint makes sure the compiler doesn't get stuck due to infinite loops in const eval.
+           If your compilation actually takes a long time, you can safely allow the lint.
+help: the constant being evaluated
+  --> $DIR/field-access-after-const-eval-fail-in-ty.rs:4:10
+   |
+LL |     [(); loop {}].field;
+   |          ^^^^^^^
+   = note: `#[deny(long_running_const_eval)]` on by default
+
+error: aborting due to 1 previous error
+