Account for type obligation coming from const and static

This commit is contained in:
Esteban Kuber 2021-11-21 01:49:51 +00:00
parent 37a11a96a1
commit 9cce7bb921
2 changed files with 51 additions and 26 deletions

View file

@ -179,31 +179,55 @@ fn annotate_expected_due_to_let_ty(
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

View file

@ -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