Skip to content

Commit 960acb0

Browse files
committed
Show scrutinee expr type for struct fields.
TODO: The type is wrong and will be fixed in later commits.
1 parent ab050d6 commit 960acb0

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/librustc_typeck/check/pat.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
577577
self.demand_eqtype_pat(pat.span, expected, pat_ty, discrim_span);
578578

579579
// Type-check subpatterns.
580-
if self.check_struct_pat_fields(pat_ty, pat.hir_id, pat.span, variant, fields, etc, def_bm)
581-
{
580+
if self.check_struct_pat_fields(
581+
pat_ty,
582+
pat.hir_id,
583+
pat.span,
584+
variant,
585+
fields,
586+
etc,
587+
def_bm,
588+
discrim_span,
589+
) {
582590
pat_ty
583591
} else {
584592
self.tcx.types.err
@@ -859,6 +867,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
859867
fields: &'tcx [hir::FieldPat<'tcx>],
860868
etc: bool,
861869
def_bm: BindingMode,
870+
discrim_span: Option<Span>,
862871
) -> bool {
863872
let tcx = self.tcx;
864873

@@ -908,7 +917,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
908917
}
909918
};
910919

911-
self.check_pat(&field.pat, field_ty, def_bm, None);
920+
self.check_pat(&field.pat, field_ty, def_bm, discrim_span);
912921
}
913922

914923
let mut unmentioned_fields = variant
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
struct S {
2+
f: u8,
3+
}
4+
5+
fn main() {
6+
match (S { f: 42 }) {
7+
S { f: Ok(_) } => {} //~ ERROR mismatched types
8+
}
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/pat-struct-field-expr-has-type.rs:7:16
3+
|
4+
LL | match (S { f: 42 }) {
5+
| ------------- this expression has type `u8`
6+
LL | S { f: Ok(_) } => {}
7+
| ^^^^^ expected `u8`, found enum `std::result::Result`
8+
|
9+
= note: expected type `u8`
10+
found enum `std::result::Result<_, _>`
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)