Pass the span of <init> in let <pat> = <init>;

when type checking `<pat>`.
This commit is contained in:
Mazdak Farrokhzad 2019-12-30 08:17:32 +01:00
parent 6137ad49e4
commit f35840f77c
6 changed files with 19 additions and 16 deletions

View File

@ -4275,17 +4275,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} }
pub fn check_decl_local(&self, local: &'tcx hir::Local<'tcx>) { pub fn check_decl_local(&self, local: &'tcx hir::Local<'tcx>) {
let t = self.local_ty(local.span, local.hir_id).decl_ty; let ty = self.local_ty(local.span, local.hir_id).decl_ty;
self.write_ty(local.hir_id, t); self.write_ty(local.hir_id, ty);
if let Some(ref init) = local.init { if let Some(ref init) = local.init {
let init_ty = self.check_decl_initializer(local, &init); let init_ty = self.check_decl_initializer(local, &init);
self.overwrite_local_ty_if_err(local, t, init_ty); self.overwrite_local_ty_if_err(local, ty, init_ty);
} }
self.check_pat_top(&local.pat, t, None); self.check_pat_top(&local.pat, ty, local.init.map(|init| init.span));
let pat_ty = self.node_ty(local.pat.hir_id); let pat_ty = self.node_ty(local.pat.hir_id);
self.overwrite_local_ty_if_err(local, t, pat_ty); self.overwrite_local_ty_if_err(local, ty, pat_ty);
} }
fn overwrite_local_ty_if_err( fn overwrite_local_ty_if_err(

View File

@ -59,14 +59,9 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
} }
impl<'a, 'tcx> FnCtxt<'a, 'tcx> { impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub fn check_pat_top( pub fn check_pat_top(&self, pat: &'tcx Pat<'tcx>, expected: Ty<'tcx>, span: Option<Span>) {
&self,
pat: &'tcx Pat<'tcx>,
expected: Ty<'tcx>,
discrim_span: Option<Span>,
) {
let def_bm = BindingMode::BindByValue(hir::Mutability::Not); let def_bm = BindingMode::BindByValue(hir::Mutability::Not);
self.check_pat(pat, expected, def_bm, discrim_span); self.check_pat(pat, expected, def_bm, span);
} }
/// `discrim_span` argument having a `Span` indicates that this pattern is part of a match /// `discrim_span` argument having a `Span` indicates that this pattern is part of a match

View File

@ -2,7 +2,9 @@ error[E0308]: mismatched types
--> $DIR/issue-14541.rs:5:9 --> $DIR/issue-14541.rs:5:9
| |
LL | let Vec3 { y: _, z: _ } = v; LL | let Vec3 { y: _, z: _ } = v;
| ^^^^^^^^^^^^^^^^^^^ expected struct `Vec2`, found struct `Vec3` | ^^^^^^^^^^^^^^^^^^^ - this expression has type `Vec2`
| |
| expected struct `Vec2`, found struct `Vec3`
error: aborting due to previous error error: aborting due to previous error

View File

@ -2,7 +2,9 @@ error[E0308]: mismatched types
--> $DIR/issue-16338.rs:7:9 --> $DIR/issue-16338.rs:7:9
| |
LL | let Slice { data: data, len: len } = "foo"; LL | let Slice { data: data, len: len } = "foo";
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `str`, found struct `Slice` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ----- this expression has type `str`
| |
| expected `str`, found struct `Slice`
| |
= note: expected type `str` = note: expected type `str`
found struct `Slice<_>` found struct `Slice<_>`

View File

@ -8,7 +8,9 @@ error[E0308]: mismatched types
--> $DIR/issue-37026.rs:7:9 --> $DIR/issue-37026.rs:7:9
| |
LL | let empty_struct::XEmpty6(..) = (); LL | let empty_struct::XEmpty6(..) = ();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found struct `empty_struct::XEmpty6` | ^^^^^^^^^^^^^^^^^^^^^^^^^ -- this expression has type `()`
| |
| expected `()`, found struct `empty_struct::XEmpty6`
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View File

@ -2,7 +2,9 @@ error[E0308]: mismatched types
--> $DIR/issue-67037-pat-tup-scrut-ty-diff-less-fields.rs:19:9 --> $DIR/issue-67037-pat-tup-scrut-ty-diff-less-fields.rs:19:9
| |
LL | let P() = U {}; LL | let P() = U {};
| ^^^ expected struct `U`, found struct `P` | ^^^ ---- this expression has type `U`
| |
| expected struct `U`, found struct `P`
| |
= note: expected struct `U` = note: expected struct `U`
found struct `P<_>` found struct `P<_>`