Skip to content

Commit cb5e1b9

Browse files
committed
Auto merge of #55004 - oli-obk:sized_static, r=cramertj
Check the type of statics and constants for `Sized`ness fixes #54410
2 parents 45088b1 + 10a01c1 commit cb5e1b9

15 files changed

+91
-77
lines changed

Diff for: src/librustc_typeck/check/wfcheck.rs

+26-12
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,17 @@ pub fn check_item_well_formed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: Def
118118
hir::ItemKind::Fn(..) => {
119119
check_item_fn(tcx, item);
120120
}
121-
hir::ItemKind::Static(..) => {
122-
check_item_type(tcx, item);
121+
hir::ItemKind::Static(ref ty, ..) => {
122+
check_item_type(tcx, item.id, ty.span);
123123
}
124-
hir::ItemKind::Const(..) => {
125-
check_item_type(tcx, item);
124+
hir::ItemKind::Const(ref ty, ..) => {
125+
check_item_type(tcx, item.id, ty.span);
126126
}
127+
hir::ItemKind::ForeignMod(ref module) => for it in module.items.iter() {
128+
if let hir::ForeignItemKind::Static(ref ty, ..) = it.node {
129+
check_item_type(tcx, it.id, ty.span);
130+
}
131+
},
127132
hir::ItemKind::Struct(ref struct_def, ref ast_generics) => {
128133
check_type_defn(tcx, item, false, |fcx| {
129134
vec![fcx.non_enum_variant(struct_def)]
@@ -335,14 +340,23 @@ fn check_item_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &hir::Item) {
335340
})
336341
}
337342

338-
fn check_item_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &hir::Item) {
339-
debug!("check_item_type: {:?}", item);
340-
341-
for_item(tcx, item).with_fcx(|fcx, _this| {
342-
let ty = fcx.tcx.type_of(fcx.tcx.hir.local_def_id(item.id));
343-
let item_ty = fcx.normalize_associated_types_in(item.span, &ty);
344-
345-
fcx.register_wf_obligation(item_ty, item.span, ObligationCauseCode::MiscObligation);
343+
fn check_item_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_id: ast::NodeId, ty_span: Span) {
344+
debug!("check_item_type: {:?}", item_id);
345+
346+
for_id(tcx, item_id, ty_span).with_fcx(|fcx, _this| {
347+
let ty = fcx.tcx.type_of(fcx.tcx.hir.local_def_id(item_id));
348+
let item_ty = fcx.normalize_associated_types_in(ty_span, &ty);
349+
350+
fcx.register_wf_obligation(item_ty, ty_span, ObligationCauseCode::MiscObligation);
351+
fcx.register_bound(
352+
item_ty,
353+
fcx.tcx.require_lang_item(lang_items::SizedTraitLangItem),
354+
traits::ObligationCause::new(
355+
ty_span,
356+
fcx.body_id,
357+
traits::MiscObligation,
358+
),
359+
);
346360

347361
vec![] // no implied bounds in a const etc
348362
});

Diff for: src/test/ui/consts/const-array-oob.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ LL | const BLUB: [u32; FOO[4]] = [5, 6];
77
= note: #[deny(const_err)] on by default
88

99
error[E0080]: could not evaluate constant expression
10-
--> $DIR/const-array-oob.rs:18:1
10+
--> $DIR/const-array-oob.rs:18:13
1111
|
1212
LL | const BLUB: [u32; FOO[4]] = [5, 6];
13-
| ^^^^^^^^^^^^^^^^^^------^^^^^^^^^^^
13+
| ^^^^^^------^
1414
| |
1515
| index out of bounds: the len is 3 but the index is 4
1616

Diff for: src/test/ui/consts/const-eval/const-eval-overflow-4.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ use std::{i8, i16, i32, i64, isize};
2020
use std::{u8, u16, u32, u64, usize};
2121

2222
const A_I8_T
23-
//~^ ERROR could not evaluate constant expression
2423
: [u32; (i8::MAX as i8 + 1i8) as usize]
2524
//~^ ERROR attempt to add with overflow
25+
//~| ERROR could not evaluate constant expression
2626
= [0; (i8::MAX as usize) + 1];
2727

2828
fn main() {

Diff for: src/test/ui/consts/const-eval/const-eval-overflow-4.stderr

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
error: attempt to add with overflow
2-
--> $DIR/const-eval-overflow-4.rs:24:13
2+
--> $DIR/const-eval-overflow-4.rs:23:13
33
|
44
LL | : [u32; (i8::MAX as i8 + 1i8) as usize]
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: #[deny(const_err)] on by default
88

99
error[E0080]: could not evaluate constant expression
10-
--> $DIR/const-eval-overflow-4.rs:22:1
10+
--> $DIR/const-eval-overflow-4.rs:23:7
1111
|
12-
LL | / const A_I8_T
13-
LL | | //~^ ERROR could not evaluate constant expression
14-
LL | | : [u32; (i8::MAX as i8 + 1i8) as usize]
15-
| | --------------------- attempt to add with overflow
16-
LL | | //~^ ERROR attempt to add with overflow
17-
LL | | = [0; (i8::MAX as usize) + 1];
18-
| |__________________________________^
12+
LL | : [u32; (i8::MAX as i8 + 1i8) as usize]
13+
| ^^^^^^---------------------^^^^^^^^^^
14+
| |
15+
| attempt to add with overflow
1916

2017
error: aborting due to 2 previous errors
2118

Diff for: src/test/ui/consts/const-unsized.stderr

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,38 @@
11
error[E0277]: the size for values of type `(dyn std::fmt::Debug + std::marker::Sync + 'static)` cannot be known at compilation time
2-
--> $DIR/const-unsized.rs:13:29
2+
--> $DIR/const-unsized.rs:13:16
33
|
44
LL | const CONST_0: Debug+Sync = *(&0 as &(Debug+Sync));
5-
| ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
5+
| ^^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `std::marker::Sized` is not implemented for `(dyn std::fmt::Debug + std::marker::Sync + 'static)`
88
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
9-
= note: constant expressions must have a statically known size
109

1110
error[E0277]: the size for values of type `str` cannot be known at compilation time
12-
--> $DIR/const-unsized.rs:16:24
11+
--> $DIR/const-unsized.rs:16:18
1312
|
1413
LL | const CONST_FOO: str = *"foo";
15-
| ^^^^^^ doesn't have a size known at compile-time
14+
| ^^^ doesn't have a size known at compile-time
1615
|
1716
= help: the trait `std::marker::Sized` is not implemented for `str`
1817
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
19-
= note: constant expressions must have a statically known size
2018

2119
error[E0277]: the size for values of type `(dyn std::fmt::Debug + std::marker::Sync + 'static)` cannot be known at compilation time
22-
--> $DIR/const-unsized.rs:19:31
20+
--> $DIR/const-unsized.rs:19:18
2321
|
2422
LL | static STATIC_1: Debug+Sync = *(&1 as &(Debug+Sync));
25-
| ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
23+
| ^^^^^^^^^^ doesn't have a size known at compile-time
2624
|
2725
= help: the trait `std::marker::Sized` is not implemented for `(dyn std::fmt::Debug + std::marker::Sync + 'static)`
2826
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
29-
= note: constant expressions must have a statically known size
3027

3128
error[E0277]: the size for values of type `str` cannot be known at compilation time
32-
--> $DIR/const-unsized.rs:22:26
29+
--> $DIR/const-unsized.rs:22:20
3330
|
3431
LL | static STATIC_BAR: str = *"bar";
35-
| ^^^^^^ doesn't have a size known at compile-time
32+
| ^^^ doesn't have a size known at compile-time
3633
|
3734
= help: the trait `std::marker::Sized` is not implemented for `str`
3835
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
39-
= note: constant expressions must have a statically known size
4036

4137
error: aborting due to 4 previous errors
4238

Diff for: src/test/ui/infinite/infinite-recursion-const-fn.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0080]: could not evaluate constant expression
2-
--> $DIR/infinite-recursion-const-fn.rs:15:1
2+
--> $DIR/infinite-recursion-const-fn.rs:15:12
33
|
44
LL | const fn a() -> usize { b() }
55
| ---
@@ -59,7 +59,7 @@ LL | const fn b() -> usize { a() }
5959
| inside call to `a`
6060
| inside call to `a`
6161
LL | const ARR: [i32; a()] = [5; 6]; //~ ERROR could not evaluate constant expression
62-
| ^^^^^^^^^^^^^^^^^---^^^^^^^^^^^
62+
| ^^^^^^---^
6363
| |
6464
| inside call to `a`
6565

Diff for: src/test/ui/issues/issue-24446.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
fn main() {
1212
static foo: Fn() -> u32 = || -> u32 {
13-
//~^ ERROR mismatched types
14-
//~| ERROR the size for values of type
13+
//~^ ERROR the size for values of type
1514
0
1615
};
1716
}

Diff for: src/test/ui/issues/issue-24446.stderr

+5-26
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,12 @@
1-
error[E0308]: mismatched types
2-
--> $DIR/issue-24446.rs:12:31
3-
|
4-
LL | static foo: Fn() -> u32 = || -> u32 {
5-
| _______________________________^
6-
LL | | //~^ ERROR mismatched types
7-
LL | | //~| ERROR the size for values of type
8-
LL | | 0
9-
LL | | };
10-
| |_____^ expected trait std::ops::Fn, found closure
11-
|
12-
= note: expected type `(dyn std::ops::Fn() -> u32 + 'static)`
13-
found type `[closure@$DIR/issue-24446.rs:12:31: 16:6]`
14-
151
error[E0277]: the size for values of type `(dyn std::ops::Fn() -> u32 + 'static)` cannot be known at compilation time
16-
--> $DIR/issue-24446.rs:12:31
2+
--> $DIR/issue-24446.rs:12:17
173
|
18-
LL | static foo: Fn() -> u32 = || -> u32 {
19-
| _______________________________^
20-
LL | | //~^ ERROR mismatched types
21-
LL | | //~| ERROR the size for values of type
22-
LL | | 0
23-
LL | | };
24-
| |_____^ doesn't have a size known at compile-time
4+
LL | static foo: Fn() -> u32 = || -> u32 {
5+
| ^^^^^^^^^^^ doesn't have a size known at compile-time
256
|
267
= help: the trait `std::marker::Sized` is not implemented for `(dyn std::ops::Fn() -> u32 + 'static)`
278
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
28-
= note: constant expressions must have a statically known size
299

30-
error: aborting due to 2 previous errors
10+
error: aborting due to previous error
3111

32-
Some errors occurred: E0277, E0308.
33-
For more information about an error, try `rustc --explain E0277`.
12+
For more information about this error, try `rustc --explain E0277`.

Diff for: src/test/ui/issues/issue-54410.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
extern "C" {
2+
pub static mut symbol: [i8];
3+
//~^ ERROR the size for values of type `[i8]` cannot be known at compilation time
4+
}
5+
6+
fn main() {
7+
println!("{:p}", unsafe { &symbol });
8+
}

Diff for: src/test/ui/issues/issue-54410.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0277]: the size for values of type `[i8]` cannot be known at compilation time
2+
--> $DIR/issue-54410.rs:2:28
3+
|
4+
LL | pub static mut symbol: [i8];
5+
| ^^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `std::marker::Sized` is not implemented for `[i8]`
8+
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0277`.

Diff for: src/test/ui/static_sized_requirement.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// compile-pass
2+
3+
#![feature(no_core, lang_items)]
4+
#![no_core]
5+
#![crate_type = "lib"]
6+
7+
#[lang = "sized"]
8+
trait Sized {}
9+
10+
extern {
11+
pub static A: u32;
12+
}

Diff for: src/test/ui/traits/trait-bounds-on-structs-and-enums-static.stderr

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
error[E0277]: the trait bound `usize: Trait` is not satisfied
2-
--> $DIR/trait-bounds-on-structs-and-enums-static.rs:19:1
2+
--> $DIR/trait-bounds-on-structs-and-enums-static.rs:19:11
33
|
4-
LL | / static X: Foo<usize> = Foo {
5-
LL | | //~^ ERROR E0277
6-
LL | | x: 1,
7-
LL | | };
8-
| |__^ the trait `Trait` is not implemented for `usize`
4+
LL | static X: Foo<usize> = Foo {
5+
| ^^^^^^^^^^ the trait `Trait` is not implemented for `usize`
96
|
107
note: required by `Foo`
118
--> $DIR/trait-bounds-on-structs-and-enums-static.rs:15:1

Diff for: src/test/ui/wf/wf-const-type.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the trait bound `NotCopy: std::marker::Copy` is not satisfied
2-
--> $DIR/wf-const-type.rs:20:1
2+
--> $DIR/wf-const-type.rs:20:12
33
|
44
LL | const FOO: IsCopy<Option<NotCopy>> = IsCopy { t: None };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `NotCopy`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `NotCopy`
66
|
77
= note: required because of the requirements on the impl of `std::marker::Copy` for `std::option::Option<NotCopy>`
88
note: required by `IsCopy`

Diff for: src/test/ui/wf/wf-static-type.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the trait bound `NotCopy: std::marker::Copy` is not satisfied
2-
--> $DIR/wf-static-type.rs:20:1
2+
--> $DIR/wf-static-type.rs:20:13
33
|
44
LL | static FOO: IsCopy<Option<NotCopy>> = IsCopy { t: None };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `NotCopy`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `NotCopy`
66
|
77
= note: required because of the requirements on the impl of `std::marker::Copy` for `std::option::Option<NotCopy>`
88
note: required by `IsCopy`

0 commit comments

Comments
 (0)