Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In Rust 1.78+, too many errors get printed when building with neither "std" nor "alloc" #1125

Closed
dtolnay opened this issue Apr 16, 2024 · 2 comments

Comments

@dtolnay
Copy link
Member

dtolnay commented Apr 16, 2024

This bisects to a regression in nightly-2024-02-09, and specifically rust-lang/rust#120550. I noticed this while testing #1124.

For the following dependency:

[dependencies]
serde_json = { version = "1", default-features = false }

in Rust 1.77 or nightly-2024-02-08 and older, the error you get is:

error: expected item, found `"serde_json requires that either `std` (default) or `alloc` feature is enabled"`
 --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde_json-1.0.115/src/features_check/error.rs:1:1
  |
1 | "serde_json requires that either `std` (default) or `alloc` feature is enabled"
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected item
  |
  = note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>

Whereas with Rust 1.78-beta, nightly-2024-02-09 or newer:

error: expected item, found `"serde_json requires that either `std` (default) or `alloc` feature is enabled"`
 --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde_json-1.0.115/src/features_check/error.rs:1:1
  |
1 | "serde_json requires that either `std` (default) or `alloc` feature is enabled"
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected item
  |
  = note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>

error[E0004]: non-exhaustive patterns: `Value::String(_)` not covered
   --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde_json-1.0.115/src/value/de.rs:216:15
    |
216 |         match self {
    |               ^^^^ pattern `Value::String(_)` not covered
    |
note: `Value` defined here
   --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde_json-1.0.115/src/value/mod.rs:116:10
    |
116 | pub enum Value {
    |          ^^^^^
...
151 |     String(String),
    |     ------ not covered
    = note: the matched value is of type `Value`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
    |
223 ~             Value::Object(v) => visit_object(v, visitor),
224 ~             Value::String(_) => todo!(),
    |

error[E0004]: non-exhaustive patterns: `Cow::Owned(_)` not covered
    --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde_json-1.0.115/src/value/de.rs:1338:15
     |
1338 |         match self.value {
     |               ^^^^^^^^^^ pattern `Cow::Owned(_)` not covered
     |
note: `Cow<'_, str>` defined here
    --> /rustc/98aa3624be70462d6a25ed5544333e3df62f4c66/library/alloc/src/borrow.rs:180:1
    ::: /rustc/98aa3624be70462d6a25ed5544333e3df62f4c66/library/alloc/src/borrow.rs:190:5
     |
     = note: not covered
     = note: the matched value is of type `Cow<'_, str>`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
     |
1339 ~             Cow::Borrowed(string) => visitor.visit_borrowed_str(string),
1340 ~             Cow::Owned(_) => todo!(),
     |

error[E0004]: non-exhaustive patterns: `&Value::Object(_)` not covered
   --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde_json-1.0.115/src/value/ser.rs:17:15
    |
17  |         match self {
    |               ^^^^ pattern `&Value::Object(_)` not covered
    |
note: `Value` defined here
   --> /home/david/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde_json-1.0.115/src/value/mod.rs:116:10
    |
116 | pub enum Value {
    |          ^^^^^
...
175 |     Object(Map<String, Value>),
    |     ------ not covered
    = note: the matched value is of type `&Value`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
    |
22  ~             Value::Array(v) => v.serialize(serializer),
23  ~             &Value::Object(_) => todo!(),
    |
@dtolnay
Copy link
Member Author

dtolnay commented Apr 16, 2024

The current approach was put in place before serde_json had a build.rs. But now we have a build.rs anyway for a different reason, that might be a better place to do this check, if it can produce a good enough error.

With #1124:

error: serde_json requires that either `std` (default) or `alloc` feature is enabled
   --> serde-json/src/lib.rs:367:1
    |
367 | / compile_error! {
368 | |     "serde_json requires that either `std` (default) or `alloc` feature is enabled"
369 | | }
    | |_^

error: could not compile `serde_json` (lib) due to 1 previous error

Instead with this code in build.rs:

if cfg!(not(any(feature = "std", feature = "alloc"))) {
    eprintln!("serde_json requires that either `std` (default) or `alloc` feature is enabled");
    process::exit(1);
}
error: failed to run custom build command for `serde_json v1.0.115`

Caused by:
  process didn't exit successfully: `target/debug/build/serde_json-9e37cf325e9030a2/build-script-build` (exit status: 1)
  --- stderr
  serde_json requires that either `std` (default) or `alloc` feature is enabled

@dtolnay
Copy link
Member Author

dtolnay commented Apr 16, 2024

Some old workarounds that we could drop if build.rs does the check, meaning the library code would never even begin to build with an invalid combination of features: #643.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant