Type inference breaks when a loop is added between inference points
Hello,
I just noticed that the compiler / Rust Analyzer seems to behave strangely. It can no longer correctly infer types when there is a loop between the expression and the target type, as if the presence of loops causes it to lose track of the type inference context.
I will provide the steps to reproduce the bug. (The playground link can be found at the bottom)
Test struct :
#[derive(Clone, Debug)]
pub struct NodeMoonStatsDTO {
pub node_id: String,
pub uptime: u64,
//.....
}
type SharedState = Arc<DashMap<String, DashMap<String, NodeMoonStatsDTO>>>;
pub struct SystemMonitorService {
pub live_nodes_moon_stats: SharedState,
//.....
}
Step 1: Type inference works correctly; result is properly inferred as Option<Vec<NodeMoonStatsDTO>>.
Here are the details of the variable types :
Step 2: Add the first building block before the bug appears.
So far, everything works correctly; no bug appears.
Step 3: Introduce a loop.
However, it correctly knows that n is of type NodeMoonStatsDTO, yet at the same time it seems to ignore it.
This is only one example; I have encountered the same issue in several parts of the code.
Here is an example code with the same bug (I replacedDashMap with HashMap to simplify it).
Here is the playground link.
Here is the GitHub issue link
I use :
rustup: 1.29.0
rustc : 1.95.0
rust-analyzer: 0.3.2887
Thank you in advance for your help and feedback.
Discussion in the ATmosphere