Skip to content

Commit fe7207f

Browse files
committed
Disallow constants and statics from having unsized types.
1 parent 15e8a67 commit fe7207f

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

src/librustc/traits/error_reporting.rs

+3
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
908908
err.note("only the last field of a struct or enum variant \
909909
may have a dynamically sized type");
910910
}
911+
ObligationCauseCode::ConstSized => {
912+
err.note("constant expressions must have a statically known size");
913+
}
911914
ObligationCauseCode::SharedStatic => {
912915
err.note("shared static variables must have a type that implements `Sync`");
913916
}

src/librustc/traits/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ pub enum ObligationCauseCode<'tcx> {
127127
// Types of fields (other than the last) in a struct must be sized.
128128
FieldSized,
129129

130+
// Constant expressions must be sized.
131+
ConstSized,
132+
130133
// static items must have `Sync` type
131134
SharedStatic,
132135

src/librustc_typeck/check/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,7 @@ fn check_const<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
11561156
let rty = ccx.tcx.node_id_to_type(id);
11571157
let fcx = FnCtxt::new(&inh, ty::FnConverging(rty), e.id);
11581158
let declty = fcx.tcx.lookup_item_type(ccx.tcx.map.local_def_id(id)).ty;
1159+
fcx.require_type_is_sized(declty, e.span, traits::ConstSized);
11591160
fcx.check_const_with_ty(sp, e, declty);
11601161
});
11611162
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::fmt::Debug;
12+
13+
const CONST_0: Debug+Sync = *(&0 as &(Debug+Sync));
14+
//~^ ERROR `std::fmt::Debug + Sync + 'static: std::marker::Sized` is not satisfied
15+
//~| NOTE does not have a constant size known at compile-time
16+
//~| NOTE constant expressions must have a statically known size
17+
18+
const CONST_FOO: str = *"foo";
19+
//~^ ERROR `str: std::marker::Sized` is not satisfied
20+
//~| NOTE does not have a constant size known at compile-time
21+
//~| NOTE constant expressions must have a statically known size
22+
23+
static STATIC_1: Debug+Sync = *(&1 as &(Debug+Sync));
24+
//~^ ERROR `std::fmt::Debug + Sync + 'static: std::marker::Sized` is not satisfied
25+
//~| NOTE does not have a constant size known at compile-time
26+
//~| NOTE constant expressions must have a statically known size
27+
28+
static STATIC_BAR: str = *"bar";
29+
//~^ ERROR `str: std::marker::Sized` is not satisfied
30+
//~| NOTE does not have a constant size known at compile-time
31+
//~| NOTE constant expressions must have a statically known size
32+
33+
fn main() {
34+
println!("{:?} {:?} {:?} {:?}", &CONST_0, &CONST_FOO, &STATIC_1, &STATIC_BAR);
35+
}

src/test/compile-fail/issue-24446.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
fn main() {
1212
static foo: Fn() -> u32 = || -> u32 {
1313
//~^ ERROR: mismatched types
14+
//~| ERROR: `std::ops::Fn() -> u32 + 'static: std::marker::Sized` is not satisfied
15+
1416
0
1517
};
1618
}

0 commit comments

Comments
 (0)