Skip to content

Commit 9cce7bb

Browse files
committed
Account for type obligation coming from const and static
1 parent 37a11a9 commit 9cce7bb

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

compiler/rustc_typeck/src/check/demand.rs

+46-22
Original file line numberDiff line numberDiff line change
@@ -179,31 +179,55 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
179179
let mut primary_span = lhs.span;
180180
let mut secondary_span = lhs.span;
181181
let mut post_message = "";
182-
if let hir::ExprKind::Path(hir::QPath::Resolved(
183-
None,
184-
hir::Path { res: hir::def::Res::Local(hir_id), .. },
185-
)) = lhs.kind
186-
{
187-
if let Some(hir::Node::Binding(pat)) = self.tcx.hir().find(*hir_id) {
188-
let parent = self.tcx.hir().get_parent_node(pat.hir_id);
189-
primary_span = pat.span;
190-
secondary_span = pat.span;
191-
match self.tcx.hir().find(parent) {
192-
Some(hir::Node::Local(hir::Local { ty: Some(ty), .. })) => {
193-
primary_span = ty.span;
194-
post_message = " type";
195-
}
196-
Some(hir::Node::Local(hir::Local { init: Some(init), .. })) => {
197-
primary_span = init.span;
198-
post_message = " value";
199-
}
200-
Some(hir::Node::Param(hir::Param { ty_span, .. })) => {
201-
primary_span = *ty_span;
202-
post_message = " parameter type";
182+
match lhs.kind {
183+
hir::ExprKind::Path(hir::QPath::Resolved(
184+
None,
185+
hir::Path {
186+
res:
187+
hir::def::Res::Def(
188+
hir::def::DefKind::Static | hir::def::DefKind::Const,
189+
def_id,
190+
),
191+
..
192+
},
193+
)) => {
194+
if let Some(hir::Node::Item(hir::Item {
195+
ident,
196+
kind: hir::ItemKind::Static(ty, ..) | hir::ItemKind::Const(ty, ..),
197+
..
198+
})) = self.tcx.hir().get_if_local(*def_id)
199+
{
200+
primary_span = ty.span;
201+
secondary_span = ident.span;
202+
post_message = " type";
203+
}
204+
}
205+
hir::ExprKind::Path(hir::QPath::Resolved(
206+
None,
207+
hir::Path { res: hir::def::Res::Local(hir_id), .. },
208+
)) => {
209+
if let Some(hir::Node::Binding(pat)) = self.tcx.hir().find(*hir_id) {
210+
let parent = self.tcx.hir().get_parent_node(pat.hir_id);
211+
primary_span = pat.span;
212+
secondary_span = pat.span;
213+
match self.tcx.hir().find(parent) {
214+
Some(hir::Node::Local(hir::Local { ty: Some(ty), .. })) => {
215+
primary_span = ty.span;
216+
post_message = " type";
217+
}
218+
Some(hir::Node::Local(hir::Local { init: Some(init), .. })) => {
219+
primary_span = init.span;
220+
post_message = " value";
221+
}
222+
Some(hir::Node::Param(hir::Param { ty_span, .. })) => {
223+
primary_span = *ty_span;
224+
post_message = " parameter type";
225+
}
226+
_ => {}
203227
}
204-
_ => {}
205228
}
206229
}
230+
_ => {}
207231
}
208232

209233
if primary_span != secondary_span

src/test/ui/static/static-mut-bad-types.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
error[E0308]: mismatched types
22
--> $DIR/static-mut-bad-types.rs:5:13
33
|
4+
LL | static mut a: isize = 3;
5+
| ----- expected due to this type
6+
...
47
LL | a = true;
5-
| - ^^^^ expected `isize`, found `bool`
6-
| |
7-
| expected due to the type of this binding
8+
| ^^^^ expected `isize`, found `bool`
89

910
error: aborting due to previous error
1011

0 commit comments

Comments
 (0)