Skip to content

Commit

Permalink
Account for type obligation coming from const and static
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Nov 25, 2021
1 parent 37a11a9 commit 9cce7bb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
68 changes: 46 additions & 22 deletions compiler/rustc_typeck/src/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/test/ui/static/static-mut-bad-types.stderr
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 9cce7bb

Please sign in to comment.