Skip to content

Incorrect suggestion for function parameter of type str #82820

Closed
@camelid

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.

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