summary refs log tree commit diff
path: root/library/std/src/sys/hermit
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2021-03-24 01:52:29 +0100
committerGitHub <noreply@github.com>2021-03-24 01:52:29 +0100
commita42e62fa0a59d0ba620889f97513929a113a6fbd (patch)
treec8dabc69676343818b44914550e763ddd18d214d /library/std/src/sys/hermit
parent2f611da1d66ae98b53358bcb7739884524b7e18d (diff)
parent6bbcc5bfbbfd9ba5a6d584a753fa32d80e3a7a17 (diff)
downloadrust-a42e62fa0a59d0ba620889f97513929a113a6fbd.tar.gz
rust-a42e62fa0a59d0ba620889f97513929a113a6fbd.zip
Rollup merge of #83353 - m-ou-se:io-error-avoid-alloc, r=nagisa
Add internal io::Error::new_const to avoid allocations.

This makes it possible to have a io::Error containing a message with zero allocations, and uses that everywhere to avoid the *three* allocations involved in `io::Error::new(kind, "message")`.

The function signature isn't perfect, because it needs a reference to the `&str`. So for now, this is just a `pub(crate)` function. Later, we'll be able to use `fn new_const<MSG: &'static str>(kind: ErrorKind)` to make that a bit better. (Then we'll also be able to use some ZST trickery if that would result in more efficient code.)

See https://github.com/rust-lang/rust/issues/83352
Diffstat (limited to 'library/std/src/sys/hermit')
-rw-r--r--library/std/src/sys/hermit/fd.rs6
-rw-r--r--library/std/src/sys/hermit/fs.rs12
-rw-r--r--library/std/src/sys/hermit/mod.rs5
-rw-r--r--library/std/src/sys/hermit/net.rs140
-rw-r--r--library/std/src/sys/hermit/stdio.rs8
-rw-r--r--library/std/src/sys/hermit/thread.rs2
6 files changed, 95 insertions, 78 deletions
diff --git a/library/std/src/sys/hermit/fd.rs b/library/std/src/sys/hermit/fd.rs
index 97d1a38b41a..1c0515a1503 100644
--- a/library/std/src/sys/hermit/fd.rs
+++ b/library/std/src/sys/hermit/fd.rs
@@ -46,7 +46,7 @@ impl FileDesc {
         self.duplicate_path(&[])
     }
     pub fn duplicate_path(&self, _path: &[u8]) -> io::Result<FileDesc> {
-        Err(io::Error::new(ErrorKind::Other, "duplicate isn't supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"duplicate isn't supported"))
     }
 
     pub fn nonblocking(&self) -> io::Result<bool> {
@@ -54,11 +54,11 @@ impl FileDesc {
     }
 
     pub fn set_cloexec(&self) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "cloexec isn't supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"cloexec isn't supported"))
     }
 
     pub fn set_nonblocking(&self, _nonblocking: bool) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "nonblocking isn't supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"nonblocking isn't supported"))
     }
 }
 
diff --git a/library/std/src/sys/hermit/fs.rs b/library/std/src/sys/hermit/fs.rs
index 1807655e971..0f33282fa83 100644
--- a/library/std/src/sys/hermit/fs.rs
+++ b/library/std/src/sys/hermit/fs.rs
@@ -226,7 +226,7 @@ impl OpenOptions {
             (false, _, true) => Ok(O_WRONLY | O_APPEND),
             (true, _, true) => Ok(O_RDWR | O_APPEND),
             (false, false, false) => {
-                Err(io::Error::new(ErrorKind::InvalidInput, "invalid access mode"))
+                Err(io::Error::new_const(ErrorKind::InvalidInput, &"invalid access mode"))
             }
         }
     }
@@ -236,12 +236,18 @@ impl OpenOptions {
             (true, false) => {}
             (false, false) => {
                 if self.truncate || self.create || self.create_new {
-                    return Err(io::Error::new(ErrorKind::InvalidInput, "invalid creation mode"));
+                    return Err(io::Error::new_const(
+                        ErrorKind::InvalidInput,
+                        &"invalid creation mode",
+                    ));
                 }
             }
             (_, true) => {
                 if self.truncate && !self.create_new {
-                    return Err(io::Error::new(ErrorKind::InvalidInput, "invalid creation mode"));
+                    return Err(io::Error::new_const(
+                        ErrorKind::InvalidInput,
+                        &"invalid creation mode",
+                    ));
                 }
             }
         }
diff --git a/library/std/src/sys/hermit/mod.rs b/library/std/src/sys/hermit/mod.rs
index 17a51abeb0e..1ecda25c03d 100644
--- a/library/std/src/sys/hermit/mod.rs
+++ b/library/std/src/sys/hermit/mod.rs
@@ -55,7 +55,10 @@ pub fn unsupported<T>() -> crate::io::Result<T> {
 }
 
 pub fn unsupported_err() -> crate::io::Error {
-    crate::io::Error::new(crate::io::ErrorKind::Other, "operation not supported on HermitCore yet")
+    crate::io::Error::new_const(
+        crate::io::ErrorKind::Other,
+        &"operation not supported on HermitCore yet",
+    )
 }
 
 // This enum is used as the storage for a bunch of types which can't actually
diff --git a/library/std/src/sys/hermit/net.rs b/library/std/src/sys/hermit/net.rs
index 8a788a9265f..7053487ccfb 100644
--- a/library/std/src/sys/hermit/net.rs
+++ b/library/std/src/sys/hermit/net.rs
@@ -14,7 +14,10 @@ use crate::time::Duration;
 /// if not, starts it.
 pub fn init() -> io::Result<()> {
     if abi::network_init() < 0 {
-        return Err(io::Error::new(ErrorKind::Other, "Unable to initialize network interface"));
+        return Err(io::Error::new_const(
+            ErrorKind::Other,
+            &"Unable to initialize network interface",
+        ));
     }
 
     Ok(())
@@ -47,9 +50,10 @@ impl TcpStream {
 
         match abi::tcpstream::connect(addr.ip().to_string().as_bytes(), addr.port(), None) {
             Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
-            _ => {
-                Err(io::Error::new(ErrorKind::Other, "Unable to initiate a connection on a socket"))
-            }
+            _ => Err(io::Error::new_const(
+                ErrorKind::Other,
+                &"Unable to initiate a connection on a socket",
+            )),
         }
     }
 
@@ -60,15 +64,16 @@ impl TcpStream {
             Some(duration.as_millis() as u64),
         ) {
             Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
-            _ => {
-                Err(io::Error::new(ErrorKind::Other, "Unable to initiate a connection on a socket"))
-            }
+            _ => Err(io::Error::new_const(
+                ErrorKind::Other,
+                &"Unable to initiate a connection on a socket",
+            )),
         }
     }
 
     pub fn set_read_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
         abi::tcpstream::set_read_timeout(*self.0.as_inner(), duration.map(|d| d.as_millis() as u64))
-            .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to set timeout value"))
+            .map_err(|_| io::Error::new_const(ErrorKind::Other, &"Unable to set timeout value"))
     }
 
     pub fn set_write_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
@@ -76,26 +81,28 @@ impl TcpStream {
             *self.0.as_inner(),
             duration.map(|d| d.as_millis() as u64),
         )
-        .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to set timeout value"))
+        .map_err(|_| io::Error::new_const(ErrorKind::Other, &"Unable to set timeout value"))
     }
 
     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
-        let duration = abi::tcpstream::get_read_timeout(*self.0.as_inner())
-            .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to determine timeout value"))?;
+        let duration = abi::tcpstream::get_read_timeout(*self.0.as_inner()).map_err(|_| {
+            io::Error::new_const(ErrorKind::Other, &"Unable to determine timeout value")
+        })?;
 
         Ok(duration.map(|d| Duration::from_millis(d)))
     }
 
     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
-        let duration = abi::tcpstream::get_write_timeout(*self.0.as_inner())
-            .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to determine timeout value"))?;
+        let duration = abi::tcpstream::get_write_timeout(*self.0.as_inner()).map_err(|_| {
+            io::Error::new_const(ErrorKind::Other, &"Unable to determine timeout value")
+        })?;
 
         Ok(duration.map(|d| Duration::from_millis(d)))
     }
 
     pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
         abi::tcpstream::peek(*self.0.as_inner(), buf)
-            .map_err(|_| io::Error::new(ErrorKind::Other, "set_nodelay failed"))
+            .map_err(|_| io::Error::new_const(ErrorKind::Other, &"set_nodelay failed"))
     }
 
     pub fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
@@ -107,7 +114,7 @@ impl TcpStream {
 
         for i in ioslice.iter_mut() {
             let ret = abi::tcpstream::read(*self.0.as_inner(), &mut i[0..])
-                .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to read on socket"))?;
+                .map_err(|_| io::Error::new_const(ErrorKind::Other, &"Unable to read on socket"))?;
 
             if ret != 0 {
                 size += ret;
@@ -130,8 +137,9 @@ impl TcpStream {
         let mut size: usize = 0;
 
         for i in ioslice.iter() {
-            size += abi::tcpstream::write(*self.0.as_inner(), i)
-                .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to write on socket"))?;
+            size += abi::tcpstream::write(*self.0.as_inner(), i).map_err(|_| {
+                io::Error::new_const(ErrorKind::Other, &"Unable to write on socket")
+            })?;
         }
 
         Ok(size)
@@ -144,13 +152,13 @@ impl TcpStream {
 
     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
         let (ipaddr, port) = abi::tcpstream::peer_addr(*self.0.as_inner())
-            .map_err(|_| io::Error::new(ErrorKind::Other, "peer_addr failed"))?;
+            .map_err(|_| io::Error::new_const(ErrorKind::Other, &"peer_addr failed"))?;
 
         let saddr = match ipaddr {
             Ipv4(ref addr) => SocketAddr::new(IpAddr::V4(Ipv4Addr::from(addr.0)), port),
             Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
             _ => {
-                return Err(io::Error::new(ErrorKind::Other, "peer_addr failed"));
+                return Err(io::Error::new_const(ErrorKind::Other, &"peer_addr failed"));
             }
         };
 
@@ -158,12 +166,12 @@ impl TcpStream {
     }
 
     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
-        Err(io::Error::new(ErrorKind::Other, "socket_addr isn't supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"socket_addr isn't supported"))
     }
 
     pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
         abi::tcpstream::shutdown(*self.0.as_inner(), how as i32)
-            .map_err(|_| io::Error::new(ErrorKind::Other, "unable to shutdown socket"))
+            .map_err(|_| io::Error::new_const(ErrorKind::Other, &"unable to shutdown socket"))
     }
 
     pub fn duplicate(&self) -> io::Result<TcpStream> {
@@ -172,31 +180,31 @@ impl TcpStream {
 
     pub fn set_nodelay(&self, mode: bool) -> io::Result<()> {
         abi::tcpstream::set_nodelay(*self.0.as_inner(), mode)
-            .map_err(|_| io::Error::new(ErrorKind::Other, "set_nodelay failed"))
+            .map_err(|_| io::Error::new_const(ErrorKind::Other, &"set_nodelay failed"))
     }
 
     pub fn nodelay(&self) -> io::Result<bool> {
         abi::tcpstream::nodelay(*self.0.as_inner())
-            .map_err(|_| io::Error::new(ErrorKind::Other, "nodelay failed"))
+            .map_err(|_| io::Error::new_const(ErrorKind::Other, &"nodelay failed"))
     }
 
     pub fn set_ttl(&self, tll: u32) -> io::Result<()> {
         abi::tcpstream::set_tll(*self.0.as_inner(), tll)
-            .map_err(|_| io::Error::new(ErrorKind::Other, "unable to set TTL"))
+            .map_err(|_| io::Error::new_const(ErrorKind::Other, &"unable to set TTL"))
     }
 
     pub fn ttl(&self) -> io::Result<u32> {
         abi::tcpstream::get_tll(*self.0.as_inner())
-            .map_err(|_| io::Error::new(ErrorKind::Other, "unable to get TTL"))
+            .map_err(|_| io::Error::new_const(ErrorKind::Other, &"unable to get TTL"))
     }
 
     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
-        Err(io::Error::new(ErrorKind::Other, "take_error isn't supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"take_error isn't supported"))
     }
 
     pub fn set_nonblocking(&self, mode: bool) -> io::Result<()> {
         abi::tcpstream::set_nonblocking(*self.0.as_inner(), mode)
-            .map_err(|_| io::Error::new(ErrorKind::Other, "unable to set blocking mode"))
+            .map_err(|_| io::Error::new_const(ErrorKind::Other, &"unable to set blocking mode"))
     }
 }
 
@@ -222,12 +230,12 @@ impl TcpListener {
 
     pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
         let (handle, ipaddr, port) = abi::tcplistener::accept(self.0.port())
-            .map_err(|_| io::Error::new(ErrorKind::Other, "accept failed"))?;
+            .map_err(|_| io::Error::new_const(ErrorKind::Other, &"accept failed"))?;
         let saddr = match ipaddr {
             Ipv4(ref addr) => SocketAddr::new(IpAddr::V4(Ipv4Addr::from(addr.0)), port),
             Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
             _ => {
-                return Err(io::Error::new(ErrorKind::Other, "accept failed"));
+                return Err(io::Error::new_const(ErrorKind::Other, &"accept failed"));
             }
         };
 
@@ -239,27 +247,27 @@ impl TcpListener {
     }
 
     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn ttl(&self) -> io::Result<u32> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn only_v6(&self) -> io::Result<bool> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 }
 
@@ -273,127 +281,127 @@ pub struct UdpSocket(abi::Handle);
 
 impl UdpSocket {
     pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn duplicate(&self) -> io::Result<UdpSocket> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn broadcast(&self) -> io::Result<bool> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn multicast_loop_v4(&self) -> io::Result<bool> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn multicast_loop_v6(&self) -> io::Result<bool> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn ttl(&self) -> io::Result<u32> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn send(&self, _: &[u8]) -> io::Result<usize> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 
     pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
-        Err(io::Error::new(ErrorKind::Other, "not supported"))
+        Err(io::Error::new_const(ErrorKind::Other, &"not supported"))
     }
 }
 
diff --git a/library/std/src/sys/hermit/stdio.rs b/library/std/src/sys/hermit/stdio.rs
index 82304dd6dc2..6bff13ca92c 100644
--- a/library/std/src/sys/hermit/stdio.rs
+++ b/library/std/src/sys/hermit/stdio.rs
@@ -40,7 +40,7 @@ impl io::Write for Stdout {
         unsafe { len = abi::write(1, data.as_ptr() as *const u8, data.len()) }
 
         if len < 0 {
-            Err(io::Error::new(io::ErrorKind::Other, "Stdout is not able to print"))
+            Err(io::Error::new_const(io::ErrorKind::Other, &"Stdout is not able to print"))
         } else {
             Ok(len as usize)
         }
@@ -52,7 +52,7 @@ impl io::Write for Stdout {
         unsafe { len = abi::write(1, data.as_ptr() as *const u8, data.len()) }
 
         if len < 0 {
-            Err(io::Error::new(io::ErrorKind::Other, "Stdout is not able to print"))
+            Err(io::Error::new_const(io::ErrorKind::Other, &"Stdout is not able to print"))
         } else {
             Ok(len as usize)
         }
@@ -81,7 +81,7 @@ impl io::Write for Stderr {
         unsafe { len = abi::write(2, data.as_ptr() as *const u8, data.len()) }
 
         if len < 0 {
-            Err(io::Error::new(io::ErrorKind::Other, "Stderr is not able to print"))
+            Err(io::Error::new_const(io::ErrorKind::Other, &"Stderr is not able to print"))
         } else {
             Ok(len as usize)
         }
@@ -93,7 +93,7 @@ impl io::Write for Stderr {
         unsafe { len = abi::write(2, data.as_ptr() as *const u8, data.len()) }
 
         if len < 0 {
-            Err(io::Error::new(io::ErrorKind::Other, "Stderr is not able to print"))
+            Err(io::Error::new_const(io::ErrorKind::Other, &"Stderr is not able to print"))
         } else {
             Ok(len as usize)
         }
diff --git a/library/std/src/sys/hermit/thread.rs b/library/std/src/sys/hermit/thread.rs
index 7bd71e120de..f35a3a8a80f 100644
--- a/library/std/src/sys/hermit/thread.rs
+++ b/library/std/src/sys/hermit/thread.rs
@@ -37,7 +37,7 @@ impl Thread {
             // The thread failed to start and as a result p was not consumed. Therefore, it is
             // safe to reconstruct the box so that it gets deallocated.
             drop(Box::from_raw(p));
-            Err(io::Error::new(io::ErrorKind::Other, "Unable to create thread!"))
+            Err(io::Error::new_const(io::ErrorKind::Other, &"Unable to create thread!"))
         } else {
             Ok(Thread { tid: tid })
         };