Skip to content

Commit 8f92102

Browse files
matthewjasperMark-Simulacrum
authored andcommitted
Check types of statics in MIR typeck
1 parent 6195b60 commit 8f92102

File tree

5 files changed

+99
-3
lines changed

5 files changed

+99
-3
lines changed

src/librustc_mir/borrow_check/type_check/mod.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,15 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
326326
);
327327
}
328328
} else {
329+
let tcx = self.tcx();
329330
if let ty::ConstKind::Unevaluated(def_id, substs) = constant.literal.val {
330331
if let Err(terr) = self.cx.fully_perform_op(
331332
location.to_locations(),
332333
ConstraintCategory::Boring,
333334
self.cx.param_env.and(type_op::ascribe_user_type::AscribeUserType::new(
334-
constant.literal.ty, def_id, UserSubsts { substs, user_self_ty: None },
335+
constant.literal.ty,
336+
def_id,
337+
UserSubsts { substs, user_self_ty: None },
335338
)),
336339
) {
337340
span_mirbug!(
@@ -342,10 +345,22 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
342345
terr
343346
);
344347
}
348+
} else if let Some(static_def_id) = constant.check_static_ptr(tcx) {
349+
let unnormalized_ty = tcx.type_of(static_def_id);
350+
let locations = location.to_locations();
351+
let normalized_ty = self.cx.normalize(unnormalized_ty, locations);
352+
let literal_ty = constant.literal.ty.builtin_deref(true).unwrap().ty;
353+
354+
if let Err(terr) = self.cx.eq_types(
355+
normalized_ty,
356+
literal_ty,
357+
locations,
358+
ConstraintCategory::Boring,
359+
) {
360+
span_mirbug!(self, constant, "bad static type {:?} ({:?})", constant, terr);
361+
}
345362
}
346363
if let ty::FnDef(def_id, substs) = constant.literal.ty.kind {
347-
let tcx = self.tcx();
348-
349364
let instantiated_predicates = tcx
350365
.predicates_of(def_id)
351366
.instantiate(tcx, substs);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Check that borrowck ensures that `static mut` items have the expected type.
2+
3+
static FOO: u8 = 42;
4+
static mut BAR: &'static u8 = &FOO;
5+
static mut BAR_ELIDED: &u8 = &FOO;
6+
7+
fn main() {
8+
unsafe {
9+
println!("{} {}", BAR, BAR_ELIDED);
10+
set_bar();
11+
set_bar_elided();
12+
println!("{} {}", BAR, BAR_ELIDED);
13+
}
14+
}
15+
16+
fn set_bar() {
17+
let n = 42;
18+
unsafe {
19+
BAR = &n;
20+
//~^ ERROR does not live long enough
21+
}
22+
}
23+
24+
fn set_bar_elided() {
25+
let n = 42;
26+
unsafe {
27+
BAR_ELIDED = &n;
28+
//~^ ERROR does not live long enough
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0597]: `n` does not live long enough
2+
--> $DIR/issue-69114-static-mut-ty.rs:19:15
3+
|
4+
LL | BAR = &n;
5+
| ------^^
6+
| | |
7+
| | borrowed value does not live long enough
8+
| assignment requires that `n` is borrowed for `'static`
9+
...
10+
LL | }
11+
| - `n` dropped here while still borrowed
12+
13+
error[E0597]: `n` does not live long enough
14+
--> $DIR/issue-69114-static-mut-ty.rs:27:22
15+
|
16+
LL | BAR_ELIDED = &n;
17+
| -------------^^
18+
| | |
19+
| | borrowed value does not live long enough
20+
| assignment requires that `n` is borrowed for `'static`
21+
...
22+
LL | }
23+
| - `n` dropped here while still borrowed
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0597`.
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Check that borrowck ensures that `static` items have the expected type.
2+
3+
static FOO: &'static (dyn Fn(&'static u8) + Send + Sync) = &drop;
4+
5+
fn main() {
6+
let n = 42;
7+
FOO(&n);
8+
//~^ ERROR does not live long enough
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0597]: `n` does not live long enough
2+
--> $DIR/issue-69114-static-ty.rs:7:9
3+
|
4+
LL | FOO(&n);
5+
| ----^^-
6+
| | |
7+
| | borrowed value does not live long enough
8+
| argument requires that `n` is borrowed for `'static`
9+
LL |
10+
LL | }
11+
| - `n` dropped here while still borrowed
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)