]> git.lizzy.rs Git - rust.git/commitdiff
Show scrutinee expr type for struct fields.
authorMazdak Farrokhzad <twingoow@gmail.com>
Mon, 30 Dec 2019 06:27:56 +0000 (07:27 +0100)
committerMazdak Farrokhzad <twingoow@gmail.com>
Mon, 30 Dec 2019 12:50:20 +0000 (13:50 +0100)
TODO: The type is wrong and will be fixed in later commits.

src/librustc_typeck/check/pat.rs
src/test/ui/pattern/pat-struct-field-expr-has-type.rs [new file with mode: 0644]
src/test/ui/pattern/pat-struct-field-expr-has-type.stderr [new file with mode: 0644]

index 397f8682ede7369b1ad6ea714c12c66132e8f306..1f374c3fe2089692d733ea7311363da97ab60d96 100644 (file)
@@ -577,8 +577,16 @@ fn check_pat_struct(
         self.demand_eqtype_pat(pat.span, expected, pat_ty, discrim_span);
 
         // Type-check subpatterns.
-        if self.check_struct_pat_fields(pat_ty, pat.hir_id, pat.span, variant, fields, etc, def_bm)
-        {
+        if self.check_struct_pat_fields(
+            pat_ty,
+            pat.hir_id,
+            pat.span,
+            variant,
+            fields,
+            etc,
+            def_bm,
+            discrim_span,
+        ) {
             pat_ty
         } else {
             self.tcx.types.err
@@ -859,6 +867,7 @@ fn check_struct_pat_fields(
         fields: &'tcx [hir::FieldPat<'tcx>],
         etc: bool,
         def_bm: BindingMode,
+        discrim_span: Option<Span>,
     ) -> bool {
         let tcx = self.tcx;
 
@@ -908,7 +917,7 @@ fn check_struct_pat_fields(
                 }
             };
 
-            self.check_pat(&field.pat, field_ty, def_bm, None);
+            self.check_pat(&field.pat, field_ty, def_bm, discrim_span);
         }
 
         let mut unmentioned_fields = variant
diff --git a/src/test/ui/pattern/pat-struct-field-expr-has-type.rs b/src/test/ui/pattern/pat-struct-field-expr-has-type.rs
new file mode 100644 (file)
index 0000000..1d18214
--- /dev/null
@@ -0,0 +1,9 @@
+struct S {
+    f: u8,
+}
+
+fn main() {
+    match (S { f: 42 }) {
+        S { f: Ok(_) } => {} //~ ERROR mismatched types
+    }
+}
diff --git a/src/test/ui/pattern/pat-struct-field-expr-has-type.stderr b/src/test/ui/pattern/pat-struct-field-expr-has-type.stderr
new file mode 100644 (file)
index 0000000..7962c37
--- /dev/null
@@ -0,0 +1,14 @@
+error[E0308]: mismatched types
+  --> $DIR/pat-struct-field-expr-has-type.rs:7:16
+   |
+LL |     match (S { f: 42 }) {
+   |           ------------- this expression has type `u8`
+LL |         S { f: Ok(_) } => {}
+   |                ^^^^^ expected `u8`, found enum `std::result::Result`
+   |
+   = note: expected type `u8`
+              found enum `std::result::Result<_, _>`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.