Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #20905 from ShoyuVanilla/array-inhabit
fix: Fix a bug on inhabitedness checks for arrays
| -rw-r--r-- | crates/hir-ty/src/inhabitedness.rs | 2 | ||||
| -rw-r--r-- | crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs | 52 |
2 files changed, 53 insertions, 1 deletions
diff --git a/crates/hir-ty/src/inhabitedness.rs b/crates/hir-ty/src/inhabitedness.rs index 8aed2608d6..5e742bba3e 100644 --- a/crates/hir-ty/src/inhabitedness.rs +++ b/crates/hir-ty/src/inhabitedness.rs @@ -90,7 +90,7 @@ impl<'db> TypeVisitor<DbInterner<'db>> for UninhabitedFrom<'_, 'db> { TyKind::Tuple(..) => ty.super_visit_with(self), TyKind::Array(item_ty, len) => match try_const_usize(self.infcx.interner.db, len) { Some(0) | None => CONTINUE_OPAQUELY_INHABITED, - Some(1..) => item_ty.super_visit_with(self), + Some(1..) => item_ty.visit_with(self), }, _ => CONTINUE_OPAQUELY_INHABITED, }; diff --git a/crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs b/crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs index e31367f3b1..c86ecd2f03 100644 --- a/crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs +++ b/crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs @@ -155,4 +155,56 @@ fn test(x: Foo<(i32, bool)>) { "#, ); } + + #[test] + fn uninhabited_variants() { + check_diagnostics( + r#" +//- minicore: result +enum Infallible {} + +trait Foo { + type Bar; +} + +struct Wrapper<T> { + error: T, +} + +struct FooWrapper<T: Foo> { + error: T::Bar, +} + +fn foo<T: Foo<Bar = Infallible>>(result: Result<T, T::Bar>) -> T { + let Ok(ok) = result; + ok +} + +fn bar<T: Foo<Bar = Infallible>>(result: Result<T, (T::Bar,)>) -> T { + let Ok(ok) = result; + ok +} + +fn baz<T: Foo<Bar = Infallible>>(result: Result<T, Wrapper<T::Bar>>) -> T { + let Ok(ok) = result; + ok +} + +fn qux<T: Foo<Bar = Infallible>>(result: Result<T, FooWrapper<T>>) -> T { + let Ok(ok) = result; + ok +} + +fn quux<T: Foo<Bar = Infallible>>(result: Result<T, [T::Bar; 1]>) -> T { + let Ok(ok) = result; + ok +} + +fn corge<T: Foo<Bar = Infallible>>(result: Result<T, (i32, T::Bar)>) -> T { + let Ok(ok) = result; + ok +} +"#, + ); + } } |