Fix diagnostics ordering in liveness analysis

This commit is contained in:
oxalica 2023-07-12 07:58:58 +08:00
parent 1abcaf1eac
commit 9512111505
2 changed files with 16 additions and 2 deletions

View file

@ -20,7 +20,7 @@
use super::{BindingValue, DefDatabase, Expr, ExprId, NameId, ResolveResult};
use crate::{Diagnostic, DiagnosticKind, FileId, ModuleKind};
use la_arena::ArenaMap;
use std::collections::HashMap;
use std::collections::BTreeMap;
use std::sync::Arc;
use syntax::ast::{self, AstNode};
use syntax::TextRange;
@ -99,11 +99,12 @@ pub(crate) fn liveness_check_query(
let mut visited_def_rhs = ArenaMap::with_capacity(expr_cnt);
let mut visited_withs = ArenaMap::with_capacity(expr_cnt);
let mut stack = vec![module.entry_expr];
let mut discovered_let_rhs: BTreeMap<NameId, ExprId> = BTreeMap::new();
while !stack.is_empty() {
// N.B. This should be dropped in every loop.
// Or it will make this whole check cost quadratic time!
let mut discovered_let_rhs: HashMap<NameId, ExprId> = HashMap::new();
discovered_let_rhs.clear();
// Traverse all reachable Exprs from roots.
while let Some(expr) = stack.pop() {

View file

@ -73,4 +73,17 @@ mod tests {
"#]],
);
}
#[test]
fn deterministic_order() {
check(
"let a = 1; b = 2; c = 3; d = 4; in 0",
expect![[r#"
4..5: UnusedBinding
11..12: UnusedBinding
18..19: UnusedBinding
25..26: UnusedBinding
"#]],
);
}
}