Update no_std support for Rust 2018 Edition.

With Rust 2018 Edition, the `mod std` trick to alias `core` names to
`std` no longer works, so switch to just having the code use `core`
explicitly.

So instead, switch to just using `core::*` for things that in core.
This is more consistent with other Rust no_std code. And it allows
us to enable `no_std` mode unconditionally in the crates that support
it, which makes testing a little easier.

There actually three cases:

 - For things in std and also in core, like `cmp`: Just use them via
   `core::*`.

 - For things in std and also in alloc, like `Vec`: Import alloc as std, as
   use them from std. This allows them to work on both stable (which
   doesn't provide alloc, but we don't support no_std mode anyway) and
   nightly.

 - For HashMap and similar which are not in core or alloc, import them in
   the top-level lib.rs files from either std or the third-party hashmap_core
   crate, and then have the code use super::hashmap_core.

Also, no_std support continues to be "best effort" at this time and not
something most people need to be testing.
This commit is contained in:
Dan Gohman
2019-01-07 11:04:58 -08:00
parent 50a045363c
commit aeb9161e2c
118 changed files with 322 additions and 355 deletions

View File

@@ -608,7 +608,7 @@ impl<'a> FunctionBuilder<'a> {
"`size` is not a power of two"
);
assert!(
access_size >= u64::from(::std::cmp::min(src_align, dest_align)),
access_size >= u64::from(::core::cmp::min(src_align, dest_align)),
"`size` is smaller than `dest` and `src`'s alignment value."
);
@@ -777,7 +777,7 @@ impl<'a> FunctionBuilder<'a> {
"`size` is not a power of two"
);
assert!(
access_size >= u64::from(::std::cmp::min(src_align, dest_align)),
access_size >= u64::from(::core::cmp::min(src_align, dest_align)),
"`size` is smaller than `dest` and `src`'s alignment value."
);
let load_and_store_amount = size / access_size;
@@ -962,8 +962,8 @@ mod tests {
#[test]
fn memcpy() {
use core::str::FromStr;
use cranelift_codegen::{isa, settings};
use std::str::FromStr;
let shared_builder = settings::builder();
let shared_flags = settings::Flags::new(shared_builder);
@@ -1023,8 +1023,8 @@ ebb0:
#[test]
fn small_memcpy() {
use core::str::FromStr;
use cranelift_codegen::{isa, settings};
use std::str::FromStr;
let shared_builder = settings::builder();
let shared_flags = settings::Flags::new(shared_builder);

View File

@@ -174,12 +174,20 @@
clippy::use_self
)
)]
#![cfg_attr(not(feature = "std"), no_std)]
#![no_std]
#![cfg_attr(not(feature = "std"), feature(alloc))]
#[cfg(not(feature = "std"))]
#[macro_use]
extern crate alloc;
extern crate alloc as std;
#[cfg(feature = "std")]
#[macro_use]
extern crate std;
#[cfg(not(feature = "std"))]
use hashmap_core::HashMap;
#[cfg(feature = "std")]
use std::collections::HashMap;
pub use crate::frontend::{FunctionBuilder, FunctionBuilderContext};
pub use crate::switch::Switch;
@@ -190,19 +198,5 @@ mod ssa;
mod switch;
mod variable;
/// This replaces `std` in builds with `core`.
#[cfg(not(feature = "std"))]
mod std {
pub use alloc::{string, vec};
pub use core::*;
pub mod collections {
#[allow(unused_extern_crates)]
extern crate hashmap_core;
pub use self::hashmap_core::map as hash_map;
pub use self::hashmap_core::{HashMap, HashSet};
}
}
/// Version number of this crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

View File

@@ -6,6 +6,8 @@
//! Lecture Notes in Computer Science, vol 7791. Springer, Berlin, Heidelberg
use crate::Variable;
use core::mem;
use core::u32;
use cranelift_codegen::cursor::{Cursor, FuncCursor};
use cranelift_codegen::entity::{EntityRef, PrimaryMap, SecondaryMap};
use cranelift_codegen::ir::immediates::{Ieee32, Ieee64};
@@ -14,8 +16,6 @@ use cranelift_codegen::ir::types::{F32, F64};
use cranelift_codegen::ir::{Ebb, Function, Inst, InstBuilder, InstructionData, Type, Value};
use cranelift_codegen::packed_option::PackedOption;
use cranelift_codegen::packed_option::ReservedValue;
use std::mem;
use std::u32;
use std::vec::Vec;
/// Structure containing the data relevant the construction of SSA for a given function.

View File

@@ -1,8 +1,8 @@
use super::HashMap;
use crate::frontend::FunctionBuilder;
use cranelift_codegen::ir::condcodes::IntCC;
use cranelift_codegen::ir::*;
use log::debug;
use std::collections::HashMap;
use std::vec::Vec;
type EntryIndex = u64;
@@ -334,7 +334,7 @@ ebb10:
#[test]
fn switch_min_index_value() {
let func = setup!(0, [::std::i64::MIN as u64, 1,]);
let func = setup!(0, [::core::i64::MIN as u64, 1,]);
assert_eq!(
func,
"ebb0:
@@ -350,7 +350,7 @@ ebb10:
#[test]
fn switch_max_index_value() {
let func = setup!(0, [::std::i64::MAX as u64, 1,]);
let func = setup!(0, [::core::i64::MAX as u64, 1,]);
assert_eq!(
func,
"ebb0:

View File

@@ -5,8 +5,8 @@
//! their own index types to use them directly. Frontends which don't
//! can use the `Variable` defined here.
use core::u32;
use cranelift_codegen::entity::EntityRef;
use std::u32;
///! An opaque reference to a variable.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]