diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..e5ccb61 --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,60 @@ +name: Rust + +on: [push, pull_request] + +env: + CARGO_TERM_COLOR: always + +permissions: + contents: read + +jobs: + check: + name: "Tests" + runs-on: ubuntu-latest + strategy: + matrix: + channel: + - stable + - beta + - nightly + + steps: + - name: Checkout repository + uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab + + - name: Install Rust Toolchain + run: rustup default ${{ matrix.channel }} + + - name: Install cargo-hack + run: cargo install cargo-hack + + - name: Powerset + run: cargo hack test --feature-powerset --lib + + miri: + name: "Miri" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Install Miri + run: | + rustup toolchain install nightly --component miri + cargo +nightly miri setup + - name: Default features + run: cargo +nightly miri test + + embedded: + name: Build (embedded) + runs-on: ubuntu-latest + steps: + - name: Checkout sources + uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab + + - name: Install Rust toolchain + run: | + rustup default nightly + rustup target add thumbv7em-none-eabi + + - name: Default features + run: cargo build -Z avoid-dev-deps --features spin_no_std --target thumbv7em-none-eabi diff --git a/.gitignore b/.gitignore index bde55ce..23b8f5e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ target doc Cargo.lock .cargo +wip diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index e4a0da3..0000000 --- a/.travis.yml +++ /dev/null @@ -1,34 +0,0 @@ -language: rust -matrix: - include: - - rust: 1.27.2 - - rust: stable - script: - - cargo test - - cargo test --features spin_no_std - - os: osx - - rust: beta - - rust: nightly - script: - - cargo test - - cargo bench - - cargo test --features spin_no_std - - cargo bench --features spin_no_std - - cd compiletest - - cargo clean - - cargo test - - cd ../ - - - rust: nightly - before_script: - - pip install 'travis-cargo<0.2' --user && export PATH=$HOME/.local/bin:$PATH - script: - - cargo doc --no-deps --all-features - after_success: - - travis-cargo --only nightly doc-upload -script: - - cargo test - -env: - global: - - secure: YXu24LptjeYirjWYjWGsMT2m3mB7LvQATE6TVo7VEUXv8GYoy2ORIHD83PeImxC93MmZ01QeUezRzuCW51ZcK92VnNSBttlF60SvIX18VsJrV92tsAhievFstqYQ+fB8DIuQ8noU0jPz7GpI+R9dlTRSImAqWOnVIghA+Wzz7Js= diff --git a/Cargo.toml b/Cargo.toml index 9c0a177..3de0e4b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [package] name = "lazy_static" # NB: When modifying, also modify html_root_url in lib.rs -version = "1.4.0" +version = "1.5.0" authors = ["Marvin Löbel "] -license = "MIT/Apache-2.0" +license = "MIT OR Apache-2.0" description = "A macro for declaring lazily evaluated statics in Rust." readme = "README.md" @@ -12,10 +12,12 @@ documentation = "https://docs.rs/lazy_static" repository = "https://github.com/rust-lang-nursery/lazy-static.rs" keywords = ["macro", "lazy", "static"] categories = [ "no-std", "rust-patterns", "memory-management" ] -exclude = ["/.travis.yml", "/appveyor.yml"] +exclude = [".github"] [dependencies.spin] -version = "0.5.0" +version = "0.9.8" +default-features = false +features = ["once"] optional = true [features] @@ -23,12 +25,4 @@ spin_no_std = ["spin"] [dev-dependencies] doc-comment = "0.3.1" - -[badges] -appveyor = { repository = "rust-lang-nursery/lazy-static.rs" } -travis-ci = { repository = "rust-lang-nursery/lazy-static.rs" } - -is-it-maintained-issue-resolution = { repository = "rust-lang-nursery/lazy-static.rs" } -is-it-maintained-open-issues = { repository = "rust-lang-nursery/lazy-static.rs" } - -maintenance = { status = "passively-maintained" } +trybuild = "1" diff --git a/README.md b/README.md index aa9f828..93edbe8 100644 --- a/README.md +++ b/README.md @@ -8,14 +8,14 @@ executed at runtime in order to be initialized. This includes anything requiring heap allocations, like vectors or hash maps, as well as anything that requires non-const function calls to be computed. -[![Travis-CI Status](https://travis-ci.com/rust-lang-nursery/lazy-static.rs.svg?branch=master)](https://travis-ci.com/rust-lang-nursery/lazy-static.rs) +[![Rust](https://github.com/rust-lang-nursery/lazy-static.rs/actions/workflows/rust.yml/badge.svg)](https://github.com/rust-lang-nursery/lazy-static.rs/actions/workflows/rust.yml) [![Latest version](https://img.shields.io/crates/v/lazy_static.svg)](https://crates.io/crates/lazy_static) [![Documentation](https://docs.rs/lazy_static/badge.svg)](https://docs.rs/lazy_static) [![License](https://img.shields.io/crates/l/lazy_static.svg)](https://github.com/rust-lang-nursery/lazy-static.rs#license) ## Minimum supported `rustc` -`1.27.2+` +`1.40.0+` This version is explicitly tested in CI and may only be bumped in new minor versions. Any changes to the supported minimum version will be called out in the release notes. @@ -31,7 +31,7 @@ Add the following dependency to your Cargo manifest... ```toml [dependencies] -lazy_static = "1.4.0" +lazy_static = "1.5.0" ``` ...and see the [docs](https://docs.rs/lazy_static) for how to use it. @@ -39,9 +39,7 @@ lazy_static = "1.4.0" # Example ```rust -#[macro_use] -extern crate lazy_static; - +use lazy_static::lazy_static; use std::collections::HashMap; lazy_static! { @@ -63,12 +61,40 @@ fn main() { } ``` +# Standard library + +It is now possible to easily replicate this crate's functionality in Rust's standard library with [`std::sync::OnceLock`](https://doc.rust-lang.org/std/sync/struct.OnceLock.html). The example above could be also be written as: + +```rust +use std::collections::HashMap; +use std::sync::OnceLock; + +fn hashmap() -> &'static HashMap { + static HASHMAP: OnceLock> = OnceLock::new(); + HASHMAP.get_or_init(|| { + let mut m = HashMap::new(); + m.insert(0, "foo"); + m.insert(1, "bar"); + m.insert(2, "baz"); + m + }) +} + +fn main() { + // First access to `HASHMAP` initializes it + println!("The entry for `0` is \"{}\".", hashmap().get(&0).unwrap()); + + // Any further access to `HASHMAP` just returns the computed value + println!("The entry for `1` is \"{}\".", hashmap().get(&1).unwrap()); +} +``` + ## License Licensed under either of - * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) - * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or https://www.apache.org/licenses/LICENSE-2.0) + * MIT license ([LICENSE-MIT](LICENSE-MIT) or https://opensource.org/licenses/MIT) at your option. diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index b138452..0000000 --- a/appveyor.yml +++ /dev/null @@ -1,61 +0,0 @@ -environment: - global: - PROJECT_NAME: lazy_static - # When this was added there were revocation check failures when using the - # libcurl backend as libcurl checks by default, but rustup doesn't provide the - # switch to turn this off. Switch to Hyper which looks to not check for - # revocation by default like libcurl does. - RUSTUP_USE_REQWEST: 1 - CARGO_HTTP_CHECK_REVOKE: false - matrix: - # Stable channel - - TARGET: i686-pc-windows-gnu - CHANNEL: stable - - TARGET: i686-pc-windows-msvc - CHANNEL: stable - - TARGET: x86_64-pc-windows-gnu - CHANNEL: stable - - TARGET: x86_64-pc-windows-msvc - CHANNEL: stable - # Beta channel - - TARGET: i686-pc-windows-gnu - CHANNEL: beta - - TARGET: i686-pc-windows-msvc - CHANNEL: beta - - TARGET: x86_64-pc-windows-gnu - CHANNEL: beta - - TARGET: x86_64-pc-windows-msvc - CHANNEL: beta - # Nightly channel - - TARGET: i686-pc-windows-gnu - CHANNEL: nightly - - TARGET: i686-pc-windows-msvc - CHANNEL: nightly - - TARGET: x86_64-pc-windows-gnu - CHANNEL: nightly - - TARGET: x86_64-pc-windows-msvc - CHANNEL: nightly - -# Install Rust and Cargo -# (Based on from https://github.com/rust-lang/libc/blob/master/appveyor.yml) -install: - - appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe - - rustup-init.exe -y --default-toolchain %CHANNEL% --default-host %TARGET% - - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin - - if "%TARGET%" == "i686-pc-windows-gnu" set PATH=%PATH%;C:\msys64\mingw32\bin - - if "%TARGET%" == "x86_64-pc-windows-gnu" set PATH=%PATH%;C:\msys64\mingw64\bin - - rustc -V - - cargo -V - -build: false - -test_script: - - cargo build --verbose - - cargo test - - if [%CHANNEL%]==[nightly] ( - cd compiletest && - cargo clean && - cargo build --verbose && - cargo test && - cd ../ - ) diff --git a/compiletest/Cargo.toml b/compiletest/Cargo.toml deleted file mode 100644 index e25d5a7..0000000 --- a/compiletest/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "lazy_static_compiletest" -version = "0.0.1" -publish = false -authors = ["lazy_static contributors"] - -[dependencies.lazy_static] -path = "../" - -[dependencies.compiletest_rs] -version = "0.3" diff --git a/compiletest/src/lib.rs b/compiletest/src/lib.rs deleted file mode 100644 index 0452765..0000000 --- a/compiletest/src/lib.rs +++ /dev/null @@ -1,11 +0,0 @@ -/* -This library is a shim around `lazy_static` that disambiguates it with the `lazy_static` -that's shipped with the Rust toolchain. We re-export the entire public API of `lazy_static` -under a different crate name so that can be imported in the compile tests. - -This currently appears to use the right local build of `lazy_static`. -*/ - -extern crate lazy_static; - -pub use self::lazy_static::*; diff --git a/compiletest/tests/compile-fail/README.md b/compiletest/tests/compile-fail/README.md deleted file mode 100644 index 58dbc3b..0000000 --- a/compiletest/tests/compile-fail/README.md +++ /dev/null @@ -1,22 +0,0 @@ -This directory contains snippets of code that should yield a -warning/note/help/error at compilation. Syntax of annotations is described in -[rust documentation](https://github.com/rust-lang/rust/blob/master/src/test/COMPILER_TESTS.md). -For more information check out [`compiletest` crate](https://github.com/laumann/compiletest-rs). - -To run compile tests issue `cargo +nightly --test`. - -## Notes on working with `compiletest` crate - -* Currently code that is inside macro should not be annotated, as `compiletest` - crate cannot deal with the fact that macro invocations effectively changes - line numbering. To prevent this add a `// error-pattern:` - on the top of the file and make sure that you set `deny` lint level - if you want to test compiler message different than error. -* `compiletest` crate by default sets `allow(dead_code)` lint level so make sure - that you change it to something suiting your needs even if the warning is - issued prior to any macro invocation. -* If you get a message `error: 0 unexpected errors found, 1 expected errors not found` - despite the fact that some error was bound to occur don't worry - it's a known - issue in the `compiletest` crate and your error was probably not registered - - make sure that your annotations are correct and that you are setting correct - lint levels. diff --git a/compiletest/tests/compile-fail/incorrect_visibility_restriction.rs b/compiletest/tests/compile-fail/incorrect_visibility_restriction.rs deleted file mode 100644 index 360e23d..0000000 --- a/compiletest/tests/compile-fail/incorrect_visibility_restriction.rs +++ /dev/null @@ -1,10 +0,0 @@ -// incorrect visibility restriction -#[macro_use] -extern crate lazy_static_compiletest as lazy_static; - -lazy_static! { - pub(nonsense) static ref WRONG: () = (); - //~^ ERROR incorrect visibility restriction -} - -fn main() { } diff --git a/compiletest/tests/compile-fail/static_is_sized.rs b/compiletest/tests/compile-fail/static_is_sized.rs deleted file mode 100644 index ac1cad4..0000000 --- a/compiletest/tests/compile-fail/static_is_sized.rs +++ /dev/null @@ -1,11 +0,0 @@ -// error-pattern:the size for values of type `str` cannot be known at compilation time -#[macro_use] -extern crate lazy_static_compiletest as lazy_static; - -lazy_static! { - pub static ref FOO: str = panic!(); -} - - -fn main() { -} diff --git a/compiletest/tests/compile_tests.rs b/compiletest/tests/compile_tests.rs deleted file mode 100644 index d908077..0000000 --- a/compiletest/tests/compile_tests.rs +++ /dev/null @@ -1,19 +0,0 @@ -extern crate compiletest_rs as compiletest; - -fn run_mode(mode: &'static str) { - let mut config = compiletest::Config::default(); - config.mode = mode.parse().expect("Invalid mode"); - config.src_base = ["tests", mode].iter().collect(); - - config.verbose = true; - - config.target_rustcflags = Some("-L target/debug/ -L target/debug/deps/".to_owned()); - config.clean_rmeta(); - - compiletest::run_tests(&config); -} - -#[test] -fn compile_test() { - run_mode("compile-fail"); -} diff --git a/examples/mutex_map.rs b/examples/mutex_map.rs new file mode 100644 index 0000000..92b07ab --- /dev/null +++ b/examples/mutex_map.rs @@ -0,0 +1,23 @@ +//! This example shows how to wrap a data structure in a mutex to achieve safe mutability. +extern crate lazy_static; +use lazy_static::lazy_static; +use std::collections::HashMap; +use std::sync::Mutex; + +lazy_static! { + static ref MUTEX_MAP: Mutex> = { + let mut m = HashMap::new(); + m.insert(0, "foo"); + m.insert(1, "bar"); + m.insert(2, "baz"); + Mutex::new(m) + }; +} + +fn main() { + MUTEX_MAP.lock().unwrap().insert(0, "boo"); + println!( + "The entry for `0` is \"{}\".", + MUTEX_MAP.lock().unwrap().get(&0).unwrap() + ); +} diff --git a/src/core_lazy.rs b/src/core_lazy.rs index b66c3e0..e2f16bc 100644 --- a/src/core_lazy.rs +++ b/src/core_lazy.rs @@ -1,8 +1,8 @@ // Copyright 2016 lazy-static.rs Developers // // Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be +// https://apache.org/licenses/LICENSE-2.0> or the MIT license , at your option. This file may not be // copied, modified, or distributed except according to those terms. extern crate spin; @@ -16,7 +16,8 @@ impl Lazy { #[inline(always)] pub fn get(&'static self, builder: F) -> &T - where F: FnOnce() -> T + where + F: FnOnce() -> T, { self.0.call_once(builder) } @@ -27,5 +28,5 @@ impl Lazy { macro_rules! __lazy_static_create { ($NAME:ident, $T:ty) => { static $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::INIT; - } + }; } diff --git a/src/inline_lazy.rs b/src/inline_lazy.rs index 219ce9c..36d52d3 100644 --- a/src/inline_lazy.rs +++ b/src/inline_lazy.rs @@ -1,26 +1,26 @@ // Copyright 2016 lazy-static.rs Developers // // Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be +// https://apache.org/licenses/LICENSE-2.0> or the MIT license , at your option. This file may not be // copied, modified, or distributed except according to those terms. extern crate core; extern crate std; -use self::std::prelude::v1::*; use self::std::cell::Cell; -use self::std::hint::unreachable_unchecked; +use self::std::mem::MaybeUninit; +use self::std::prelude::v1::*; use self::std::sync::Once; #[allow(deprecated)] pub use self::std::sync::ONCE_INIT; -// FIXME: Replace Option with MaybeUninit (stable since 1.36.0) -pub struct Lazy(Cell>, Once); +#[allow(dead_code)] // Used in macros +pub struct Lazy(Cell>, Once); impl Lazy { #[allow(deprecated)] - pub const INIT: Self = Lazy(Cell::new(None), ONCE_INIT); + pub const INIT: Self = Lazy(Cell::new(MaybeUninit::uninit()), ONCE_INIT); #[inline(always)] pub fn get(&'static self, f: F) -> &T @@ -28,21 +28,12 @@ impl Lazy { F: FnOnce() -> T, { self.1.call_once(|| { - self.0.set(Some(f())); + self.0.set(MaybeUninit::new(f())); }); - // `self.0` is guaranteed to be `Some` by this point + // `self.0` is guaranteed to be initialized by this point // The `Once` will catch and propagate panics - unsafe { - match *self.0.as_ptr() { - Some(ref x) => x, - None => { - debug_assert!(false, "attempted to derefence an uninitialized lazy static. This is a bug"); - - unreachable_unchecked() - }, - } - } + unsafe { &*(*self.0.as_ptr()).as_ptr() } } } diff --git a/src/lib.rs b/src/lib.rs index cada0dc..5003fa2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,8 @@ // Copyright 2016 lazy-static.rs Developers // // Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be +// https://apache.org/licenses/LICENSE-2.0> or the MIT license , at your option. This file may not be // copied, modified, or distributed except according to those terms. /*! @@ -27,8 +27,8 @@ lazy_static! { Attributes (including doc comments) are supported as well: ```rust -# #[macro_use] -# extern crate lazy_static; +use lazy_static::lazy_static; + # fn main() { lazy_static! { /// This is an example for using doc comment attributes @@ -58,9 +58,7 @@ have generally the same properties as regular "static" variables: Using the macro: ```rust -#[macro_use] -extern crate lazy_static; - +use lazy_static::lazy_static; use std::collections::HashMap; lazy_static! { @@ -96,23 +94,18 @@ This crate provides one cargo feature: */ -#![doc(html_root_url = "https://docs.rs/lazy_static/1.4.0")] +#![doc(html_root_url = "https://docs.rs/lazy_static/1.5.0")] #![no_std] -#[cfg(not(feature = "spin_no_std"))] -#[path="inline_lazy.rs"] -#[doc(hidden)] -pub mod lazy; - -#[cfg(test)] +#[cfg(doctest)] #[macro_use] extern crate doc_comment; -#[cfg(test)] +#[cfg(doctest)] doctest!("../README.md"); -#[cfg(feature = "spin_no_std")] -#[path="core_lazy.rs"] +#[cfg_attr(feature = "spin_no_std", path = "core_lazy.rs")] +#[cfg_attr(not(feature = "spin_no_std"), path = "inline_lazy.rs")] #[doc(hidden)] pub mod lazy; @@ -158,6 +151,7 @@ macro_rules! __lazy_static_internal { $(#[$attr])* $($vis)* struct $N {__private_field: ()} #[doc(hidden)] + #[allow(non_upper_case_globals)] $($vis)* static $N: $N = $N {__private_field: ()}; }; () => () @@ -195,8 +189,7 @@ pub trait LazyStatic { /// Example: /// /// ```rust -/// #[macro_use] -/// extern crate lazy_static; +/// use lazy_static::lazy_static; /// /// lazy_static! { /// static ref BUFFER: Vec = (0..255).collect(); diff --git a/tests/compile_fail/incorrect_visibility_restriction.rs b/tests/compile_fail/incorrect_visibility_restriction.rs new file mode 100644 index 0000000..5d44f02 --- /dev/null +++ b/tests/compile_fail/incorrect_visibility_restriction.rs @@ -0,0 +1,8 @@ +#[macro_use] +extern crate lazy_static; + +lazy_static! { + pub(nonsense) static ref WRONG: () = (); +} + +fn main() { } diff --git a/tests/compile_fail/incorrect_visibility_restriction.stderr b/tests/compile_fail/incorrect_visibility_restriction.stderr new file mode 100644 index 0000000..045ce14 --- /dev/null +++ b/tests/compile_fail/incorrect_visibility_restriction.stderr @@ -0,0 +1,10 @@ +error[E0704]: incorrect visibility restriction + --> tests/compile_fail/incorrect_visibility_restriction.rs:5:9 + | +5 | pub(nonsense) static ref WRONG: () = (); + | ^^^^^^^^ help: make this visible only to module `nonsense` with `in`: `in nonsense` + | + = help: some possible visibility restrictions are: + `pub(crate)`: visible only on the current crate + `pub(super)`: visible only in the current module's parent + `pub(in path::to::module)`: visible only on the specified path diff --git a/compiletest/tests/compile-fail/static_is_private.rs b/tests/compile_fail/static_is_private.rs similarity index 54% rename from compiletest/tests/compile-fail/static_is_private.rs rename to tests/compile_fail/static_is_private.rs index 6ebc8f5..4486f01 100644 --- a/compiletest/tests/compile-fail/static_is_private.rs +++ b/tests/compile_fail/static_is_private.rs @@ -1,5 +1,5 @@ #[macro_use] -extern crate lazy_static_compiletest as lazy_static; +extern crate lazy_static; mod outer { pub mod inner { @@ -10,5 +10,5 @@ mod outer { } fn main() { - assert_eq!(*outer::inner::FOO, ()); //~ ERROR static `FOO` is private + assert_eq!(*outer::inner::FOO, ()); } diff --git a/tests/compile_fail/static_is_private.stderr b/tests/compile_fail/static_is_private.stderr new file mode 100644 index 0000000..38007d4 --- /dev/null +++ b/tests/compile_fail/static_is_private.stderr @@ -0,0 +1,14 @@ +error[E0603]: static `FOO` is private + --> tests/compile_fail/static_is_private.rs:13:31 + | +13 | assert_eq!(*outer::inner::FOO, ()); + | ^^^ private static + | +note: the static `FOO` is defined here + --> tests/compile_fail/static_is_private.rs:6:9 + | +6 | / lazy_static! { +7 | | pub(in outer) static ref FOO: () = (); +8 | | } + | |_________^ + = note: this error originates in the macro `lazy_static` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/compile_fail/static_is_sized.rs b/tests/compile_fail/static_is_sized.rs new file mode 100644 index 0000000..69a9a06 --- /dev/null +++ b/tests/compile_fail/static_is_sized.rs @@ -0,0 +1,9 @@ +#[macro_use] +extern crate lazy_static; + +lazy_static! { + pub static ref FOO: str = panic!(); +} + + +fn main() { } diff --git a/tests/compile_fail/static_is_sized.stderr b/tests/compile_fail/static_is_sized.stderr new file mode 100644 index 0000000..49407ab --- /dev/null +++ b/tests/compile_fail/static_is_sized.stderr @@ -0,0 +1,64 @@ +error[E0277]: the size for values of type `str` cannot be known at compilation time + --> tests/compile_fail/static_is_sized.rs:4:1 + | +4 | / lazy_static! { +5 | | pub static ref FOO: str = panic!(); +6 | | } + | |_^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `str` +note: required by an implicit `Sized` bound in `Lazy` + --> src/inline_lazy.rs + | + | pub struct Lazy(Cell>, Once); + | ^ required by the implicit `Sized` requirement on this type parameter in `Lazy` + = note: this error originates in the macro `__lazy_static_create` which comes from the expansion of the macro `lazy_static` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `str` cannot be known at compilation time + --> tests/compile_fail/static_is_sized.rs:4:1 + | +4 | / lazy_static! { +5 | | pub static ref FOO: str = panic!(); +6 | | } + | |_^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `str` +note: required by an implicit `Sized` bound in `Lazy` + --> src/inline_lazy.rs + | + | pub struct Lazy(Cell>, Once); + | ^ required by the implicit `Sized` requirement on this type parameter in `Lazy` + = note: this error originates in the macro `__lazy_static_create` which comes from the expansion of the macro `lazy_static` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `str` cannot be known at compilation time + --> tests/compile_fail/static_is_sized.rs:5:25 + | +5 | pub static ref FOO: str = panic!(); + | ^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `str` + = note: the return type of a function must have a statically known size + +error[E0599]: the method `get` exists for struct `Lazy`, but its trait bounds were not satisfied + --> tests/compile_fail/static_is_sized.rs:4:1 + | +4 | / lazy_static! { +5 | | pub static ref FOO: str = panic!(); +6 | | } + | |_^ method cannot be called on `Lazy` due to unsatisfied trait bounds + | + = note: the following trait bounds were not satisfied: + `str: Sized` + = note: this error originates in the macro `__lazy_static_internal` which comes from the expansion of the macro `lazy_static` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the size for values of type `str` cannot be known at compilation time + --> tests/compile_fail/static_is_sized.rs:4:1 + | +4 | / lazy_static! { +5 | | pub static ref FOO: str = panic!(); +6 | | } + | |_^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `str` + = note: the return type of a function must have a statically known size + = note: this error originates in the macro `__lazy_static_internal` which comes from the expansion of the macro `lazy_static` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/no_std.rs b/tests/no_std.rs index f94a1aa..e332494 100644 --- a/tests/no_std.rs +++ b/tests/no_std.rs @@ -1,5 +1,4 @@ -#![cfg(feature="spin_no_std")] - +#![cfg(feature = "spin_no_std")] #![no_std] #[macro_use] diff --git a/tests/test.rs b/tests/test.rs index 03d0ab6..eb659d4 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,3 +1,5 @@ +#![deny(warnings)] + #[macro_use] extern crate lazy_static; use std::collections::HashMap; @@ -35,6 +37,11 @@ lazy_static! { static ref S3: String = [*S1, *S2].join(""); } +lazy_static! { + #[allow(non_upper_case_globals)] + pub static ref string: String = "hello".to_string(); +} + #[test] fn s3() { assert_eq!(&*S3, "ab"); @@ -69,7 +76,10 @@ fn test_meta() { assert!(&STRING as *const _ != ©_of_string as *const _); // this would not compile if STRING were not marked #[derive(Debug)] - assert_eq!(format!("{:?}", STRING), "STRING { __private_field: () }".to_string()); + assert_eq!( + format!("{:?}", STRING), + "STRING { __private_field: () }".to_string() + ); } mod visibility { @@ -102,7 +112,7 @@ fn test_visibility() { // This should not cause a warning about a missing Copy implementation lazy_static! { - pub static ref VAR: i32 = { 0 }; + pub static ref VAR: i32 = 0; } #[derive(Copy, Clone, Debug, PartialEq)] @@ -111,10 +121,18 @@ struct Once(X); const ONCE_INIT: Once = Once(X); static DATA: X = X; static ONCE: X = X; -fn require_sync() -> X { X } -fn transmute() -> X { X } -fn __static_ref_initialize() -> X { X } -fn test(_: Vec) -> X { X } +fn require_sync() -> X { + X +} +fn transmute() -> X { + X +} +fn __static_ref_initialize() -> X { + X +} +fn test(_: Vec) -> X { + X +} // All these names should not be shadowed lazy_static! { @@ -133,9 +151,9 @@ fn item_name_shadowing() { } use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering::SeqCst; #[allow(deprecated)] use std::sync::atomic::ATOMIC_BOOL_INIT; -use std::sync::atomic::Ordering::SeqCst; #[allow(deprecated)] static PRE_INIT_FLAG: AtomicBool = ATOMIC_BOOL_INIT; @@ -155,7 +173,10 @@ fn pre_init() { } lazy_static! { - static ref LIFETIME_NAME: for<'a> fn(&'a u8) = { fn f(_: &u8) {} f }; + static ref LIFETIME_NAME: for<'a> fn(&'a u8) = { + fn f(_: &u8) {} + f + }; } #[test] diff --git a/tests/ui.rs b/tests/ui.rs new file mode 100644 index 0000000..af007cd --- /dev/null +++ b/tests/ui.rs @@ -0,0 +1,6 @@ +#[test] +#[cfg(not(miri))] +fn ui() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/compile_fail/*.rs"); +}