Check the type of statics and constants for Sizedness

This commit is contained in:
Oliver Scherer 2018-10-11 18:02:00 +02:00
parent 5ea8eb55cd
commit 8180e1b54c
13 changed files with 77 additions and 74 deletions

View File

@ -118,12 +118,17 @@ pub fn check_item_well_formed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: Def
hir::ItemKind::Fn(..) => {
check_item_fn(tcx, item);
}
hir::ItemKind::Static(..) => {
check_item_type(tcx, item);
hir::ItemKind::Static(ref ty, ..) => {
check_item_type(tcx, item.id, ty.span);
}
hir::ItemKind::Const(..) => {
check_item_type(tcx, item);
hir::ItemKind::Const(ref ty, ..) => {
check_item_type(tcx, item.id, ty.span);
}
hir::ItemKind::ForeignMod(ref module) => for it in module.items.iter() {
if let hir::ForeignItemKind::Static(ref ty, ..) = it.node {
check_item_type(tcx, it.id, ty.span);
}
},
hir::ItemKind::Struct(ref struct_def, ref ast_generics) => {
check_type_defn(tcx, item, false, |fcx| {
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) {
})
}
fn check_item_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &hir::Item) {
debug!("check_item_type: {:?}", item);
fn check_item_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_id: ast::NodeId, ty_span: Span) {
debug!("check_item_type: {:?}", item_id);
for_item(tcx, item).with_fcx(|fcx, _this| {
let ty = fcx.tcx.type_of(fcx.tcx.hir.local_def_id(item.id));
let item_ty = fcx.normalize_associated_types_in(item.span, &ty);
for_id(tcx, item_id, ty_span).with_fcx(|fcx, _this| {
let ty = fcx.tcx.type_of(fcx.tcx.hir.local_def_id(item_id));
let item_ty = fcx.normalize_associated_types_in(ty_span, &ty);
fcx.register_wf_obligation(item_ty, item.span, ObligationCauseCode::MiscObligation);
fcx.register_wf_obligation(item_ty, ty_span, ObligationCauseCode::MiscObligation);
fcx.register_bound(
item_ty,
fcx.tcx.require_lang_item(lang_items::SizedTraitLangItem),
traits::ObligationCause::new(
ty_span,
fcx.body_id,
traits::MiscObligation,
),
);
vec![] // no implied bounds in a const etc
});

View File

@ -7,10 +7,10 @@ LL | const BLUB: [u32; FOO[4]] = [5, 6];
= note: #[deny(const_err)] on by default
error[E0080]: could not evaluate constant expression
--> $DIR/const-array-oob.rs:18:1
--> $DIR/const-array-oob.rs:18:13
|
LL | const BLUB: [u32; FOO[4]] = [5, 6];
| ^^^^^^^^^^^^^^^^^^------^^^^^^^^^^^
| ^^^^^^------^
| |
| index out of bounds: the len is 3 but the index is 4

View File

@ -20,9 +20,9 @@ use std::{i8, i16, i32, i64, isize};
use std::{u8, u16, u32, u64, usize};
const A_I8_T
//~^ ERROR could not evaluate constant expression
: [u32; (i8::MAX as i8 + 1i8) as usize]
//~^ ERROR attempt to add with overflow
//~| ERROR could not evaluate constant expression
= [0; (i8::MAX as usize) + 1];
fn main() {

View File

@ -1,5 +1,5 @@
error: attempt to add with overflow
--> $DIR/const-eval-overflow-4.rs:24:13
--> $DIR/const-eval-overflow-4.rs:23:13
|
LL | : [u32; (i8::MAX as i8 + 1i8) as usize]
| ^^^^^^^^^^^^^^^^^^^^^
@ -7,15 +7,12 @@ LL | : [u32; (i8::MAX as i8 + 1i8) as usize]
= note: #[deny(const_err)] on by default
error[E0080]: could not evaluate constant expression
--> $DIR/const-eval-overflow-4.rs:22:1
--> $DIR/const-eval-overflow-4.rs:23:7
|
LL | / const A_I8_T
LL | | //~^ ERROR could not evaluate constant expression
LL | | : [u32; (i8::MAX as i8 + 1i8) as usize]
| | --------------------- attempt to add with overflow
LL | | //~^ ERROR attempt to add with overflow
LL | | = [0; (i8::MAX as usize) + 1];
| |__________________________________^
LL | : [u32; (i8::MAX as i8 + 1i8) as usize]
| ^^^^^^---------------------^^^^^^^^^^
| |
| attempt to add with overflow
error: aborting due to 2 previous errors

View File

@ -1,42 +1,38 @@
error[E0277]: the size for values of type `(dyn std::fmt::Debug + std::marker::Sync + 'static)` cannot be known at compilation time
--> $DIR/const-unsized.rs:13:29
--> $DIR/const-unsized.rs:13:16
|
LL | const CONST_0: Debug+Sync = *(&0 as &(Debug+Sync));
| ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
| ^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `(dyn std::fmt::Debug + std::marker::Sync + 'static)`
= 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>
= note: constant expressions must have a statically known size
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/const-unsized.rs:16:24
--> $DIR/const-unsized.rs:16:18
|
LL | const CONST_FOO: str = *"foo";
| ^^^^^^ doesn't have a size known at compile-time
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `str`
= 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>
= note: constant expressions must have a statically known size
error[E0277]: the size for values of type `(dyn std::fmt::Debug + std::marker::Sync + 'static)` cannot be known at compilation time
--> $DIR/const-unsized.rs:19:31
--> $DIR/const-unsized.rs:19:18
|
LL | static STATIC_1: Debug+Sync = *(&1 as &(Debug+Sync));
| ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
| ^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `(dyn std::fmt::Debug + std::marker::Sync + 'static)`
= 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>
= note: constant expressions must have a statically known size
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/const-unsized.rs:22:26
--> $DIR/const-unsized.rs:22:20
|
LL | static STATIC_BAR: str = *"bar";
| ^^^^^^ doesn't have a size known at compile-time
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `str`
= 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>
= note: constant expressions must have a statically known size
error: aborting due to 4 previous errors

View File

@ -1,5 +1,5 @@
error[E0080]: could not evaluate constant expression
--> $DIR/infinite-recursion-const-fn.rs:15:1
--> $DIR/infinite-recursion-const-fn.rs:15:12
|
LL | const fn a() -> usize { b() }
| ---
@ -59,7 +59,7 @@ LL | const fn b() -> usize { a() }
| inside call to `a`
| inside call to `a`
LL | const ARR: [i32; a()] = [5; 6]; //~ ERROR could not evaluate constant expression
| ^^^^^^^^^^^^^^^^^---^^^^^^^^^^^
| ^^^^^^---^
| |
| inside call to `a`

View File

@ -10,8 +10,7 @@
fn main() {
static foo: Fn() -> u32 = || -> u32 {
//~^ ERROR mismatched types
//~| ERROR the size for values of type
//~^ ERROR the size for values of type
0
};
}

View File

@ -1,33 +1,12 @@
error[E0308]: mismatched types
--> $DIR/issue-24446.rs:12:31
|
LL | static foo: Fn() -> u32 = || -> u32 {
| _______________________________^
LL | | //~^ ERROR mismatched types
LL | | //~| ERROR the size for values of type
LL | | 0
LL | | };
| |_____^ expected trait std::ops::Fn, found closure
|
= note: expected type `(dyn std::ops::Fn() -> u32 + 'static)`
found type `[closure@$DIR/issue-24446.rs:12:31: 16:6]`
error[E0277]: the size for values of type `(dyn std::ops::Fn() -> u32 + 'static)` cannot be known at compilation time
--> $DIR/issue-24446.rs:12:31
--> $DIR/issue-24446.rs:12:17
|
LL | static foo: Fn() -> u32 = || -> u32 {
| _______________________________^
LL | | //~^ ERROR mismatched types
LL | | //~| ERROR the size for values of type
LL | | 0
LL | | };
| |_____^ doesn't have a size known at compile-time
LL | static foo: Fn() -> u32 = || -> u32 {
| ^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `(dyn std::ops::Fn() -> u32 + 'static)`
= 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>
= note: constant expressions must have a statically known size
error: aborting due to 2 previous errors
error: aborting due to previous error
Some errors occurred: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
For more information about this error, try `rustc --explain E0277`.

View File

@ -0,0 +1,9 @@
use std::os::raw::c_char;
extern "C" {
pub static mut symbol: [c_char];
//~^ ERROR the size for values of type `[i8]` cannot be known at compilation time
}
fn main() {
println!("{:p}", unsafe { &symbol });
}

View File

@ -0,0 +1,12 @@
error[E0277]: the size for values of type `[i8]` cannot be known at compilation time
--> $DIR/issue-54410.rs:3:28
|
LL | pub static mut symbol: [c_char];
| ^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `[i8]`
= 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>
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.

View File

@ -1,11 +1,8 @@
error[E0277]: the trait bound `usize: Trait` is not satisfied
--> $DIR/trait-bounds-on-structs-and-enums-static.rs:19:1
--> $DIR/trait-bounds-on-structs-and-enums-static.rs:19:11
|
LL | / static X: Foo<usize> = Foo {
LL | | //~^ ERROR E0277
LL | | x: 1,
LL | | };
| |__^ the trait `Trait` is not implemented for `usize`
LL | static X: Foo<usize> = Foo {
| ^^^^^^^^^^ the trait `Trait` is not implemented for `usize`
|
note: required by `Foo`
--> $DIR/trait-bounds-on-structs-and-enums-static.rs:15:1

View File

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

View File

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