diff options
| author | Esteban Kuber <esteban@kuber.com.ar> | 2021-11-21 01:49:51 +0000 |
|---|---|---|
| committer | Esteban Kuber <esteban@kuber.com.ar> | 2021-11-25 18:04:33 +0000 |
| commit | 9cce7bb92181c4e27dbff1fc18d7496c172768ec (patch) | |
| tree | 55ab0a233b5f9d32f053cea0a739fa9690306175 | |
| parent | 37a11a96a1b3ad68c40cc293270cf8ffbe7904de (diff) | |
| download | rust-9cce7bb92181c4e27dbff1fc18d7496c172768ec.tar.gz rust-9cce7bb92181c4e27dbff1fc18d7496c172768ec.zip | |
Account for type obligation coming from `const` and `static`
| -rw-r--r-- | compiler/rustc_typeck/src/check/demand.rs | 68 | ||||
| -rw-r--r-- | src/test/ui/static/static-mut-bad-types.stderr | 7 |
2 files changed, 50 insertions, 25 deletions
diff --git a/compiler/rustc_typeck/src/check/demand.rs b/compiler/rustc_typeck/src/check/demand.rs index 5d121913aac..12cd7ad1848 100644 --- a/compiler/rustc_typeck/src/check/demand.rs +++ b/compiler/rustc_typeck/src/check/demand.rs @@ -179,31 +179,55 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let mut primary_span = lhs.span; let mut secondary_span = lhs.span; let mut post_message = ""; - if let hir::ExprKind::Path(hir::QPath::Resolved( - None, - hir::Path { res: hir::def::Res::Local(hir_id), .. }, - )) = lhs.kind - { - if let Some(hir::Node::Binding(pat)) = self.tcx.hir().find(*hir_id) { - let parent = self.tcx.hir().get_parent_node(pat.hir_id); - primary_span = pat.span; - secondary_span = pat.span; - match self.tcx.hir().find(parent) { - Some(hir::Node::Local(hir::Local { ty: Some(ty), .. })) => { - primary_span = ty.span; - post_message = " type"; - } - Some(hir::Node::Local(hir::Local { init: Some(init), .. })) => { - primary_span = init.span; - post_message = " value"; - } - Some(hir::Node::Param(hir::Param { ty_span, .. })) => { - primary_span = *ty_span; - post_message = " parameter type"; + match lhs.kind { + hir::ExprKind::Path(hir::QPath::Resolved( + None, + hir::Path { + res: + hir::def::Res::Def( + hir::def::DefKind::Static | hir::def::DefKind::Const, + def_id, + ), + .. + }, + )) => { + if let Some(hir::Node::Item(hir::Item { + ident, + kind: hir::ItemKind::Static(ty, ..) | hir::ItemKind::Const(ty, ..), + .. + })) = self.tcx.hir().get_if_local(*def_id) + { + primary_span = ty.span; + secondary_span = ident.span; + post_message = " type"; + } + } + hir::ExprKind::Path(hir::QPath::Resolved( + None, + hir::Path { res: hir::def::Res::Local(hir_id), .. }, + )) => { + if let Some(hir::Node::Binding(pat)) = self.tcx.hir().find(*hir_id) { + let parent = self.tcx.hir().get_parent_node(pat.hir_id); + primary_span = pat.span; + secondary_span = pat.span; + match self.tcx.hir().find(parent) { + Some(hir::Node::Local(hir::Local { ty: Some(ty), .. })) => { + primary_span = ty.span; + post_message = " type"; + } + Some(hir::Node::Local(hir::Local { init: Some(init), .. })) => { + primary_span = init.span; + post_message = " value"; + } + Some(hir::Node::Param(hir::Param { ty_span, .. })) => { + primary_span = *ty_span; + post_message = " parameter type"; + } + _ => {} } - _ => {} } } + _ => {} } if primary_span != secondary_span diff --git a/src/test/ui/static/static-mut-bad-types.stderr b/src/test/ui/static/static-mut-bad-types.stderr index e5a59de6f14..983e1026f91 100644 --- a/src/test/ui/static/static-mut-bad-types.stderr +++ b/src/test/ui/static/static-mut-bad-types.stderr @@ -1,10 +1,11 @@ error[E0308]: mismatched types --> $DIR/static-mut-bad-types.rs:5:13 | +LL | static mut a: isize = 3; + | ----- expected due to this type +... LL | a = true; - | - ^^^^ expected `isize`, found `bool` - | | - | expected due to the type of this binding + | ^^^^ expected `isize`, found `bool` error: aborting due to previous error |
