about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2024-02-23 10:47:53 +1100
committerNicholas Nethercote <n.nethercote@gmail.com>2024-02-23 10:57:11 +1100
commit21bb1a435907294e50a95359fedcf5ebd5de1959 (patch)
treef67d87134d65a752f531c8fe7e2b45c923cb24da
parentc5f69bdd5173a948e0131f934fa7c4cbf5e0b55f (diff)
downloadrust-21bb1a435907294e50a95359fedcf5ebd5de1959.tar.gz
rust-21bb1a435907294e50a95359fedcf5ebd5de1959.zip
Allow for a missing `adt_def` in `NamePrivacyVisitor`.
This was caused by 72b172bdf6 in #121206. That commit removed an early
return from `analysis` when there are stashed errors. As a result, it's
possible to reach privacy analysis when there are stashed errors, which
means more code paths can be reached. One such code path was handled in
that commit, where a `span_bug` was changed to a `span_delayed_bug`.

This commit handles another such code path uncovered by fuzzing, in much
the same way.

Fixes #121455.
-rw-r--r--compiler/rustc_privacy/src/lib.rs5
-rw-r--r--tests/ui/privacy/unreachable-issue-121455.rs6
-rw-r--r--tests/ui/privacy/unreachable-issue-121455.stderr9
3 files changed, 19 insertions, 1 deletions
diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs
index 9d8a9f5fce3..1c6bd887128 100644
--- a/compiler/rustc_privacy/src/lib.rs
+++ b/compiler/rustc_privacy/src/lib.rs
@@ -988,7 +988,10 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> {
     fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
         if let hir::ExprKind::Struct(qpath, fields, ref base) = expr.kind {
             let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
-            let adt = self.typeck_results().expr_ty(expr).ty_adt_def().unwrap();
+            let Some(adt) = self.typeck_results().expr_ty(expr).ty_adt_def() else {
+                self.tcx.dcx().span_delayed_bug(expr.span, "no adt_def for expression");
+                return;
+            };
             let variant = adt.variant_of_res(res);
             if let Some(base) = *base {
                 // If the expression uses FRU we need to make sure all the unmentioned fields
diff --git a/tests/ui/privacy/unreachable-issue-121455.rs b/tests/ui/privacy/unreachable-issue-121455.rs
new file mode 100644
index 00000000000..5da30d6ed63
--- /dev/null
+++ b/tests/ui/privacy/unreachable-issue-121455.rs
@@ -0,0 +1,6 @@
+fn test(s: &Self::Id) {
+//~^ ERROR failed to resolve: `Self` is only available in impls, traits, and type definitions
+    match &s[0..3] {}
+}
+
+fn main() {}
diff --git a/tests/ui/privacy/unreachable-issue-121455.stderr b/tests/ui/privacy/unreachable-issue-121455.stderr
new file mode 100644
index 00000000000..864e950a98e
--- /dev/null
+++ b/tests/ui/privacy/unreachable-issue-121455.stderr
@@ -0,0 +1,9 @@
+error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
+  --> $DIR/unreachable-issue-121455.rs:1:13
+   |
+LL | fn test(s: &Self::Id) {
+   |             ^^^^ `Self` is only available in impls, traits, and type definitions
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0433`.