]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #50460 - F001:const_string, r=kennytm
authorkennytm <kennytm@gmail.com>
Wed, 9 May 2018 09:25:53 +0000 (17:25 +0800)
committerkennytm <kennytm@gmail.com>
Wed, 9 May 2018 12:29:42 +0000 (20:29 +0800)
Make `String::new()` const

Following the steps of https://github.com/rust-lang/rust/pull/50233 , make `String::new()` a `const fn`.

src/liballoc/lib.rs
src/liballoc/string.rs
src/test/run-pass/collections-const-new.rs [new file with mode: 0644]
src/test/run-pass/vec-const-new.rs [deleted file]

index da4b76a4d527d41ee755812ffae464e780e2e4c4..bb78c14b90586fcb65a93559491662489b83813e 100644 (file)
 #![feature(inclusive_range_methods)]
 #![cfg_attr(stage0, feature(generic_param_attrs))]
 #![feature(rustc_const_unstable)]
+#![feature(const_vec_new)]
 
 #![cfg_attr(not(test), feature(fn_traits, i128))]
 #![cfg_attr(test, feature(test))]
index 2f84d5f7f8676877328dc57aa79155374886f6e6..da9afdd2ca37bcb410b70bcbab444bb06715eaa1 100644 (file)
@@ -380,7 +380,8 @@ impl String {
     /// ```
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
-    pub fn new() -> String {
+    #[rustc_const_unstable(feature = "const_string_new")]
+    pub const fn new() -> String {
         String { vec: Vec::new() }
     }
 
diff --git a/src/test/run-pass/collections-const-new.rs b/src/test/run-pass/collections-const-new.rs
new file mode 100644 (file)
index 0000000..4003c2e
--- /dev/null
@@ -0,0 +1,22 @@
+// Copyright 2012 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.
+
+// Test several functions can be used for constants
+// 1. Vec::new()
+// 2. String::new()
+
+#![feature(const_vec_new)]
+#![feature(const_string_new)]
+
+const MY_VEC: Vec<usize> = Vec::new();
+
+const MY_STRING: String = String::new();
+
+pub fn main() {}
diff --git a/src/test/run-pass/vec-const-new.rs b/src/test/run-pass/vec-const-new.rs
deleted file mode 100644 (file)
index 62e2a36..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-// Copyright 2012 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.
-
-// Test that Vec::new() can be used for constants
-
-#![feature(const_vec_new)]
-
-const MY_VEC: Vec<usize> = Vec::new();
-
-pub fn main() {}