Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use unwrap() in ArrayIntoIter lint when typeck fails #121586

Merged
merged 1 commit into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions compiler/rustc_lint/src/array_into_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/lint/ice-array-into-iter-lint-issue-121532.rs
Original file line number Diff line number Diff line change
@@ -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() {}
9 changes: 9 additions & 0 deletions tests/ui/lint/ice-array-into-iter-lint-issue-121532.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Loading