Skip to content

Commit 4aa318c

Browse files
authored
Merge pull request rust-lang#19143 from Veykril/push-mkokotluzskw
Propogate error types in mir type projections
2 parents 8ddf82b + 3a041fc commit 4aa318c

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/mir.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
lang_items::is_box,
1111
mapping::ToChalk,
1212
CallableDefId, ClosureId, Const, ConstScalar, InferenceResult, Interner, MemoryMap,
13-
Substitution, TraitEnvironment, Ty, TyKind,
13+
Substitution, TraitEnvironment, Ty, TyExt, TyKind,
1414
};
1515
use base_db::CrateId;
1616
use chalk_ir::Mutability;
@@ -144,6 +144,13 @@ impl<V, T> ProjectionElem<V, T> {
144144
closure_field: impl FnOnce(ClosureId, &Substitution, usize) -> Ty,
145145
krate: CrateId,
146146
) -> Ty {
147+
// we only bail on mir building when there are type mismatches
148+
// but error types may pop up resulting in us still attempting to build the mir
149+
// so just propagate the error type
150+
if base.is_unknown() {
151+
return TyKind::Error.intern(Interner);
152+
}
153+
147154
if matches!(base.kind(Interner), TyKind::Alias(_) | TyKind::AssociatedType(..)) {
148155
base = normalize(
149156
db,
@@ -166,7 +173,7 @@ impl<V, T> ProjectionElem<V, T> {
166173
TyKind::Error.intern(Interner)
167174
}
168175
},
169-
ProjectionElem::Field(Either::Left(f)) => match &base.kind(Interner) {
176+
ProjectionElem::Field(Either::Left(f)) => match base.kind(Interner) {
170177
TyKind::Adt(_, subst) => {
171178
db.field_types(f.parent)[f.local_id].clone().substitute(Interner, subst)
172179
}

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unused_variables.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,19 @@ fn main() {
260260
let arr = [1, 2, 3, 4, 5];
261261
let [_x, _y @ ..] = arr;
262262
}
263+
"#,
264+
);
265+
}
266+
267+
// regression test as we used to panic in this scenario
268+
#[test]
269+
fn unknown_struct_pattern_param_type() {
270+
check_diagnostics(
271+
r#"
272+
struct S { field : u32 }
273+
fn f(S { field }: error) {
274+
// ^^^^^ 💡 warn: unused variable
275+
}
263276
"#,
264277
);
265278
}

0 commit comments

Comments
 (0)