Skip to content

Incorrect suggestion for function parameter of type str #82820

Closed

Description

@camelid
fn foo(bar: str) {}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0277]: the size for values of type `str` cannot be known at compilation time
 --> src/lib.rs:1:8
  |
1 | fn foo(bar: str) {}
  |        ^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `str`
help: function arguments must have a statically known size, borrowed types always have a known size
  |
1 | fn foo(&bar: str) {}
  |        ^

error: aborting due to previous error

Applying the suggestion leads to yet more errors. The new errors do suggest using &str, but they still suggest invalid code:

error[E0308]: mismatched types
 --> src/lib.rs:1:8
  |
1 | fn foo(&bar: str) {}
  |        ^^^^-----
  |        |     |
  |        |     expected due to this
  |        expected `str`, found reference
  |        help: did you mean `bar`: `&str`
  |
  = note:   expected type `str`
          found reference `&_`

error[E0277]: the size for values of type `str` cannot be known at compilation time
 --> src/lib.rs:1:8
  |
1 | fn foo(&bar: str) {}
  |        ^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `str`
help: function arguments must have a statically known size, borrowed types always have a known size
  |
1 | fn foo(&bar: &str) {}
  |              ^

error: aborting due to 2 previous errors

After applying the new suggestions, I get:

error[E0277]: the size for values of type `str` cannot be known at compilation time
 --> src/lib.rs:1:9
  |
1 | fn foo(&bar: &str) {}
  |         ^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `str`
  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature

error: aborting due to previous error

This seems likely to send new users in circles.

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
    D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
    D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Mar 6, 2021
  2. camelid commented on Mar 6, 2021

    @camelid
    MemberAuthor

    Assigning P-medium and removing I-prioritize as discussed in the prioritization working group.

  3. added and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Mar 6, 2021
  4. apiraino commented on Mar 6, 2021

    @apiraino
    Contributor

    I've bisected to this nightly range:

    +nightly-2020-07-12-x86_64-unknown-linux-gnu 346aec9b0 => old error msg
    +nightly-2020-07-26-x86_64-unknown-linux-gnu d6953df14 => new error message

    (I cannot bisect further because in between of that range I cannot install nightlies 🤷‍♂️ )

  5. hellow554 commented on Apr 28, 2021

    @hellow554
    Contributor

    Funny enough, when using _ as variable name, the suggestion will get displayed correctly:

    fn f(a: str) {}
    fn g(_: str) {}
    
       Compiling playground v0.0.1 (/playground)
    error[E0277]: the size for values of type `str` cannot be known at compilation time
     --> src/lib.rs:1:6
      |
    1 | fn f(a: str) {}
      |      ^ doesn't have a size known at compile-time
      |
      = help: the trait `Sized` is not implemented for `str`
    help: function arguments must have a statically known size, borrowed types always have a known size
      |
    1 | fn f(&a: str) {}
      |      ^
    
    error[E0277]: the size for values of type `str` cannot be known at compilation time
     --> src/lib.rs:2:6
      |
    2 | fn g(_: str) {}
      |      ^ doesn't have a size known at compile-time
      |
      = help: the trait `Sized` is not implemented for `str`
    help: function arguments must have a statically known size, borrowed types always have a known size
      |
    2 | fn g(_: &str) {}
    
  6. hellow554 commented on Apr 28, 2021

    @hellow554
    Contributor

    This seems to be fixed on nighlty, but not on beta...

    somewhere between 5a4ab26...9d9c2c9

    Fixed in 9d9c2c9

    Fixed by #84313

    So this can be closed @camelid 🎉

  7. hellow554 commented on Apr 28, 2021

    @hellow554
    Contributor

    But, as a sidenote: How could this ever be commited to master? I mean, it's obviosly wrong so say &a: str :/

    I hope, that in the future the review process can be improved to detect such errors early on.

    The error was introduced in #78152, where the test has been adapted to match the wrong syntax, but nobody has seen this :( Let's hope in the future it will get noticed right away 😊

  8. camelid commented on Apr 29, 2021

    @camelid
    MemberAuthor

    The error was introduced in #78152, where the test has been adapted to match the wrong syntax, but nobody has seen this :( Let's hope in the future it will get noticed right away 😊

    It looks like that PR caused a bunch of small diagnostics changes, so the reviewers probably just missed that bug.

  9. camelid commented on Apr 29, 2021

    @camelid
    MemberAuthor

    Assigning myself for adding a test. @rustbot claim

  10. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Apr 29, 2021
  11. added a commit that references this issue on May 6, 2021
    3a0d6be
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.P-mediumMedium priorityT-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