Skip to content

Bogus extra error about unsized variable (the trait bound str: std::marker::Sized is not satisfied) #44504

Description

@bluss

Given code like this (not complete):

extern crate serde_json;
let g1 = vec![0; 16];
let json1 = serde_json::to_pretty_string(&g1).unwrap();
let foo: Foo = serde_json::from_str(&json1).unwrap();

The first error is correct but the second is nonsense:

error[E0425]: cannot find function `to_pretty_string` in module `serde_json`
   --> serialization-tests/tests/serialization.rs:498:35
    |
498 |         let json1 = serde_json::to_pretty_string(&g1).unwrap();
    |                                 ^^^^^^^^^^^^^^^^ not found in `serde_json`

error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied
   --> serialization-tests/tests/serialization.rs:498:13
    |
498 |         let json1 = serde_json::to_pretty_string(&g1).unwrap();
    |             ^^^^^ `str` does not have a constant size known at compile-time
    |
    = help: the trait `std::marker::Sized` is not implemented for `str`
    = note: all local variables must have a statically known size

There's an extra error here. json1 is used in a way (with the from_str function) that it should be a str or something that derefs to str. The second error message supposes it is a str, which is nonsense that the user doesn't intend at all. (If the user would only find the right name of the function, they would get a String in the json variable and the next line compiles fine.)

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions