about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGurinder Singh <frederick.the.fool@gmail.com>2024-02-25 17:51:56 +0530
committerGurinder Singh <frederick.the.fool@gmail.com>2024-02-25 17:51:56 +0530
commitfa7557181f371d88fb67f17b85827954f79cdf94 (patch)
treefd973d739dd7f385e706d38eb7de77de468e8669
parent710048f790ebc9ebfac6039c6733570f8084c9de (diff)
downloadrust-fa7557181f371d88fb67f17b85827954f79cdf94.tar.gz
rust-fa7557181f371d88fb67f17b85827954f79cdf94.zip
Don't use `unwrap()` in `ArrayIntoIter` lint when typeck fails
-rw-r--r--compiler/rustc_lint/src/array_into_iter.rs14
-rw-r--r--tests/ui/lint/ice-array-into-iter-lint-issue-121532.rs11
-rw-r--r--tests/ui/lint/ice-array-into-iter-lint-issue-121532.stderr9
3 files changed, 29 insertions, 5 deletions
diff --git a/compiler/rustc_lint/src/array_into_iter.rs b/compiler/rustc_lint/src/array_into_iter.rs
index 3a5c585366a..993b1d739a1 100644
--- a/compiler/rustc_lint/src/array_into_iter.rs
+++ b/compiler/rustc_lint/src/array_into_iter.rs
@@ -70,11 +70,15 @@ impl<'tcx> LateLintPass<'tcx> for ArrayIntoIter {
 
             // Check if the method call actually calls the libcore
             // `IntoIterator::into_iter`.
-            let def_id = cx.typeck_results().type_dependent_def_id(expr.hir_id).unwrap();
-            match cx.tcx.trait_of_item(def_id) {
-                Some(trait_id) if cx.tcx.is_diagnostic_item(sym::IntoIterator, trait_id) => {}
-                _ => return,
-            };
+            let trait_id = cx
+                .typeck_results()
+                .type_dependent_def_id(expr.hir_id)
+                .and_then(|did| cx.tcx.trait_of_item(did));
+            if trait_id.is_none()
+                || !cx.tcx.is_diagnostic_item(sym::IntoIterator, trait_id.unwrap())
+            {
+                return;
+            }
 
             // As this is a method call expression, we have at least one argument.
             let receiver_ty = cx.typeck_results().expr_ty(receiver_arg);
diff --git a/tests/ui/lint/ice-array-into-iter-lint-issue-121532.rs b/tests/ui/lint/ice-array-into-iter-lint-issue-121532.rs
new file mode 100644
index 00000000000..92cab01fe48
--- /dev/null
+++ b/tests/ui/lint/ice-array-into-iter-lint-issue-121532.rs
@@ -0,0 +1,11 @@
+// Regression test for #121532
+// Checks the we don't ICE in ArrayIntoIter
+// lint when typeck has failed
+
+ // Typeck fails for the arg type as
+ // `Self` makes no sense here
+fn func(a: Self::ItemsIterator) { //~ ERROR failed to resolve: `Self` is only available in impls, traits, and type definitions
+    a.into_iter();
+}
+
+fn main() {}
diff --git a/tests/ui/lint/ice-array-into-iter-lint-issue-121532.stderr b/tests/ui/lint/ice-array-into-iter-lint-issue-121532.stderr
new file mode 100644
index 00000000000..73ceddae940
--- /dev/null
+++ b/tests/ui/lint/ice-array-into-iter-lint-issue-121532.stderr
@@ -0,0 +1,9 @@
+error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
+  --> $DIR/ice-array-into-iter-lint-issue-121532.rs:7:12
+   |
+LL | fn func(a: Self::ItemsIterator) {
+   |            ^^^^ `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`.