about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOliver Scherer <github35764891676564198441@oli-obk.de>2018-10-11 18:02:00 +0200
committerOliver Scherer <github35764891676564198441@oli-obk.de>2018-10-16 17:01:24 +0200
commit8180e1b54c1f7457c258701287bcbbf32170a743 (patch)
tree7d2b1438959aae248fc4445cbe88933abf99442f
parent5ea8eb55cd9f4547b332f43c9f723de30187c223 (diff)
downloadrust-8180e1b54c1f7457c258701287bcbbf32170a743.tar.gz
rust-8180e1b54c1f7457c258701287bcbbf32170a743.zip
Check the type of statics and constants for `Sized`ness
-rw-r--r--src/librustc_typeck/check/wfcheck.rs38
-rw-r--r--src/test/ui/consts/const-array-oob.stderr4
-rw-r--r--src/test/ui/consts/const-eval/const-eval-overflow-4.rs2
-rw-r--r--src/test/ui/consts/const-eval/const-eval-overflow-4.stderr15
-rw-r--r--src/test/ui/consts/const-unsized.stderr20
-rw-r--r--src/test/ui/infinite/infinite-recursion-const-fn.stderr4
-rw-r--r--src/test/ui/issues/issue-24446.rs3
-rw-r--r--src/test/ui/issues/issue-24446.stderr31
-rw-r--r--src/test/ui/issues/issue-54410.rs9
-rw-r--r--src/test/ui/issues/issue-54410.stderr12
-rw-r--r--src/test/ui/traits/trait-bounds-on-structs-and-enums-static.stderr9
-rw-r--r--src/test/ui/wf/wf-const-type.stderr4
-rw-r--r--src/test/ui/wf/wf-static-type.stderr4
13 files changed, 79 insertions, 76 deletions
diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs
index cc1906d91d4..016ea5cba66 100644
--- a/src/librustc_typeck/check/wfcheck.rs
+++ b/src/librustc_typeck/check/wfcheck.rs
@@ -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);
-
-    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);
-
-        fcx.register_wf_obligation(item_ty, item.span, ObligationCauseCode::MiscObligation);
+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_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, 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
     });
diff --git a/src/test/ui/consts/const-array-oob.stderr b/src/test/ui/consts/const-array-oob.stderr
index 09e4918bf6e..19ec9a4aa17 100644
--- a/src/test/ui/consts/const-array-oob.stderr
+++ b/src/test/ui/consts/const-array-oob.stderr
@@ -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
 
diff --git a/src/test/ui/consts/const-eval/const-eval-overflow-4.rs b/src/test/ui/consts/const-eval/const-eval-overflow-4.rs
index 9fc31b7c727..e9fc8ec0c37 100644
--- a/src/test/ui/consts/const-eval/const-eval-overflow-4.rs
+++ b/src/test/ui/consts/const-eval/const-eval-overflow-4.rs
@@ -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() {
diff --git a/src/test/ui/consts/const-eval/const-eval-overflow-4.stderr b/src/test/ui/consts/const-eval/const-eval-overflow-4.stderr
index 058c8730d7c..b12aa032b60 100644
--- a/src/test/ui/consts/const-eval/const-eval-overflow-4.stderr
+++ b/src/test/ui/consts/const-eval/const-eval-overflow-4.stderr
@@ -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
 
diff --git a/src/test/ui/consts/const-unsized.stderr b/src/test/ui/consts/const-unsized.stderr
index 83d23bcba04..8671e609ac8 100644
--- a/src/test/ui/consts/const-unsized.stderr
+++ b/src/test/ui/consts/const-unsized.stderr
@@ -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
 
diff --git a/src/test/ui/infinite/infinite-recursion-const-fn.stderr b/src/test/ui/infinite/infinite-recursion-const-fn.stderr
index 579cc6bd548..c77452d9abe 100644
--- a/src/test/ui/infinite/infinite-recursion-const-fn.stderr
+++ b/src/test/ui/infinite/infinite-recursion-const-fn.stderr
@@ -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`
 
diff --git a/src/test/ui/issues/issue-24446.rs b/src/test/ui/issues/issue-24446.rs
index a9c7978642d..7c44319db0d 100644
--- a/src/test/ui/issues/issue-24446.rs
+++ b/src/test/ui/issues/issue-24446.rs
@@ -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
     };
 }
diff --git a/src/test/ui/issues/issue-24446.stderr b/src/test/ui/issues/issue-24446.stderr
index 3c424627437..d4921443cad 100644
--- a/src/test/ui/issues/issue-24446.stderr
+++ b/src/test/ui/issues/issue-24446.stderr
@@ -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`.
diff --git a/src/test/ui/issues/issue-54410.rs b/src/test/ui/issues/issue-54410.rs
new file mode 100644
index 00000000000..192b97a1677
--- /dev/null
+++ b/src/test/ui/issues/issue-54410.rs
@@ -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 });
+}
diff --git a/src/test/ui/issues/issue-54410.stderr b/src/test/ui/issues/issue-54410.stderr
new file mode 100644
index 00000000000..0c40c6384a4
--- /dev/null
+++ b/src/test/ui/issues/issue-54410.stderr
@@ -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`.
diff --git a/src/test/ui/traits/trait-bounds-on-structs-and-enums-static.stderr b/src/test/ui/traits/trait-bounds-on-structs-and-enums-static.stderr
index 28d4257a9fb..5145ba1881d 100644
--- a/src/test/ui/traits/trait-bounds-on-structs-and-enums-static.stderr
+++ b/src/test/ui/traits/trait-bounds-on-structs-and-enums-static.stderr
@@ -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
diff --git a/src/test/ui/wf/wf-const-type.stderr b/src/test/ui/wf/wf-const-type.stderr
index 1c07824ef77..78e831ef880 100644
--- a/src/test/ui/wf/wf-const-type.stderr
+++ b/src/test/ui/wf/wf-const-type.stderr
@@ -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`
diff --git a/src/test/ui/wf/wf-static-type.stderr b/src/test/ui/wf/wf-static-type.stderr
index f76444cfe1f..c9656d86546 100644
--- a/src/test/ui/wf/wf-static-type.stderr
+++ b/src/test/ui/wf/wf-static-type.stderr
@@ -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`