Compiler can't infer &str type when &String is inserted first in a collection with tuples
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Description: compiler can't infer &str type when String is inserted first in a collection with tuples.
Steps to reproduce: create a new array and fill it with tuples, where one of the elements is a string literal and one is a variable of type &String. Depending one the order, it might or might not compile because type can't be inferred.
let s = &String::from("foo");
let arr1 = [(1, s), (2, "bar")]; // This line contains a compilation error
let arr2 = [(2, "bar"), (1, s)]; // Does compile; inferred type is [(i32, &str); 2]
Error message:
error[E0308]: mismatched types
|
| let arr1 = [(1, s), (2, "bar")];
| ^^^^^ expected `&String`, found `&str`
|
= note: expected reference `&String`
found reference `&'static str`
Rust compiler version: rustc 1.80.1 (3f5fd8dd4 2024-08-06)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by compiling the provided arr1 and arr2 reproductions with rustc 1.80.1 and compare their inferred types and diagnostics. Investigate the compiler's type inference for arrays containing tuples with &String and string literals; done means both orderings compile consistently with the intended common element type and have regression coverage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100