diff options
| author | Eduard Burtescu <edy.burt@gmail.com> | 2016-06-24 03:20:37 +0300 |
|---|---|---|
| committer | Eduard Burtescu <edy.burt@gmail.com> | 2016-06-26 18:28:17 +0300 |
| commit | fe7207f6afed3b4727d5228d573803c165a80477 (patch) | |
| tree | a4611bce6c4d2992bf5a4179b926250e2e2066fb | |
| parent | 15e8a67c47dc624cc7de67dfa8020a1daefc5067 (diff) | |
| download | rust-fe7207f6afed3b4727d5228d573803c165a80477.tar.gz rust-fe7207f6afed3b4727d5228d573803c165a80477.zip | |
Disallow constants and statics from having unsized types.
| -rw-r--r-- | src/librustc/traits/error_reporting.rs | 3 | ||||
| -rw-r--r-- | src/librustc/traits/mod.rs | 3 | ||||
| -rw-r--r-- | src/librustc_typeck/check/mod.rs | 1 | ||||
| -rw-r--r-- | src/test/compile-fail/const-unsized.rs | 35 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-24446.rs | 2 |
5 files changed, 44 insertions, 0 deletions
diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index 9a69958fea0..9de57656af4 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -908,6 +908,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { err.note("only the last field of a struct or enum variant \ may have a dynamically sized type"); } + ObligationCauseCode::ConstSized => { + err.note("constant expressions must have a statically known size"); + } ObligationCauseCode::SharedStatic => { err.note("shared static variables must have a type that implements `Sync`"); } diff --git a/src/librustc/traits/mod.rs b/src/librustc/traits/mod.rs index 5b363d90578..ad16bca3a9d 100644 --- a/src/librustc/traits/mod.rs +++ b/src/librustc/traits/mod.rs @@ -127,6 +127,9 @@ pub enum ObligationCauseCode<'tcx> { // Types of fields (other than the last) in a struct must be sized. FieldSized, + // Constant expressions must be sized. + ConstSized, + // static items must have `Sync` type SharedStatic, diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 6f7f33fe24d..3e17b5937bc 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1156,6 +1156,7 @@ fn check_const<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, let rty = ccx.tcx.node_id_to_type(id); let fcx = FnCtxt::new(&inh, ty::FnConverging(rty), e.id); let declty = fcx.tcx.lookup_item_type(ccx.tcx.map.local_def_id(id)).ty; + fcx.require_type_is_sized(declty, e.span, traits::ConstSized); fcx.check_const_with_ty(sp, e, declty); }); } diff --git a/src/test/compile-fail/const-unsized.rs b/src/test/compile-fail/const-unsized.rs new file mode 100644 index 00000000000..72a5c5fff60 --- /dev/null +++ b/src/test/compile-fail/const-unsized.rs @@ -0,0 +1,35 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::fmt::Debug; + +const CONST_0: Debug+Sync = *(&0 as &(Debug+Sync)); +//~^ ERROR `std::fmt::Debug + Sync + 'static: std::marker::Sized` is not satisfied +//~| NOTE does not have a constant size known at compile-time +//~| NOTE constant expressions must have a statically known size + +const CONST_FOO: str = *"foo"; +//~^ ERROR `str: std::marker::Sized` is not satisfied +//~| NOTE does not have a constant size known at compile-time +//~| NOTE constant expressions must have a statically known size + +static STATIC_1: Debug+Sync = *(&1 as &(Debug+Sync)); +//~^ ERROR `std::fmt::Debug + Sync + 'static: std::marker::Sized` is not satisfied +//~| NOTE does not have a constant size known at compile-time +//~| NOTE constant expressions must have a statically known size + +static STATIC_BAR: str = *"bar"; +//~^ ERROR `str: std::marker::Sized` is not satisfied +//~| NOTE does not have a constant size known at compile-time +//~| NOTE constant expressions must have a statically known size + +fn main() { + println!("{:?} {:?} {:?} {:?}", &CONST_0, &CONST_FOO, &STATIC_1, &STATIC_BAR); +} diff --git a/src/test/compile-fail/issue-24446.rs b/src/test/compile-fail/issue-24446.rs index cbeac774798..b9382520cf9 100644 --- a/src/test/compile-fail/issue-24446.rs +++ b/src/test/compile-fail/issue-24446.rs @@ -11,6 +11,8 @@ fn main() { static foo: Fn() -> u32 = || -> u32 { //~^ ERROR: mismatched types + //~| ERROR: `std::ops::Fn() -> u32 + 'static: std::marker::Sized` is not satisfied + 0 }; } |
