Skip to content

Commit df5eec7

Browse files
committed
consolidation
1 parent 4fea3e0 commit df5eec7

File tree

11 files changed

+70
-152
lines changed

11 files changed

+70
-152
lines changed

crates/fe/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::print_stdout, clippy::print_stderr)]
2+
13
mod check;
24
mod tree;
35

crates/hir-analysis/src/analysis_pass.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,6 @@ impl AnalysisPassManager {
4141
diags
4242
}
4343

44-
/// Stable alternative to run_on_module that uses File as the key.
45-
/// This prevents issues with stale TopLevelMod references during incremental recompilation.
46-
pub fn run_on_file<'db, DB>(
47-
&mut self,
48-
db: &'db DB,
49-
file: common::file::File,
50-
) -> Vec<Box<dyn DiagnosticVoucher + 'db>>
51-
where
52-
DB: HirAnalysisDb + hir::LowerHirDb,
53-
{
54-
// Convert File to fresh TopLevelMod using the stable API
55-
let top_mod = hir::lower::map_file_to_mod(db, file);
56-
// Use the existing analysis logic
57-
self.run_on_module(db, top_mod)
58-
}
59-
6044
pub fn run_on_module_tree<'db>(
6145
&mut self,
6246
db: &'db dyn HirAnalysisDb,

crates/hir-analysis/src/lookup.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{diagnostics::SpannedHirAnalysisDb, HirAnalysisDb};
88
/// Generic semantic identity at a source offset.
99
/// This is compiler-facing and independent of any IDE layer types.
1010
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11-
pub enum SymbolIdentity<'db> {
11+
pub enum SymbolKey<'db> {
1212
Scope(hir::hir_def::scope_graph::ScopeId<'db>),
1313
EnumVariant(hir::hir_def::EnumVariant<'db>),
1414
FuncParam(hir::hir_def::ItemKind<'db>, u16),
@@ -32,14 +32,14 @@ fn enclosing_func<'db>(
3232
None
3333
}
3434

35-
fn map_path_res<'db>(db: &'db dyn HirAnalysisDb, res: PathRes<'db>) -> Option<SymbolIdentity<'db>> {
35+
fn map_path_res<'db>(db: &'db dyn HirAnalysisDb, res: PathRes<'db>) -> Option<SymbolKey<'db>> {
3636
match res {
37-
PathRes::EnumVariant(v) => Some(SymbolIdentity::EnumVariant(v.variant)),
38-
PathRes::FuncParam(item, idx) => Some(SymbolIdentity::FuncParam(item, idx)),
37+
PathRes::EnumVariant(v) => Some(SymbolKey::EnumVariant(v.variant)),
38+
PathRes::FuncParam(item, idx) => Some(SymbolKey::FuncParam(item, idx)),
3939
PathRes::Method(..) => {
40-
crate::name_resolution::method_func_def_from_res(&res).map(SymbolIdentity::Method)
40+
crate::name_resolution::method_func_def_from_res(&res).map(SymbolKey::Method)
4141
}
42-
_ => res.as_scope(db).map(SymbolIdentity::Scope),
42+
_ => res.as_scope(db).map(SymbolKey::Scope),
4343
}
4444
}
4545

@@ -50,24 +50,24 @@ pub fn identity_for_occurrence<'db>(
5050
db: &'db dyn SpannedHirAnalysisDb,
5151
top_mod: TopLevelMod<'db>,
5252
occ: &hir::source_index::OccurrencePayload<'db>,
53-
) -> Vec<SymbolIdentity<'db>> {
53+
) -> Vec<SymbolKey<'db>> {
5454
use hir::source_index::OccurrencePayload as OP;
5555

5656
match *occ {
5757
OP::ItemHeaderName { scope, .. } => match scope {
5858
hir::hir_def::scope_graph::ScopeId::Item(ItemKind::Func(f)) => {
5959
if let Some(fd) = crate::ty::func_def::lower_func(db, f) {
6060
if fd.is_method(db) {
61-
return vec![SymbolIdentity::Method(fd)];
61+
return vec![SymbolKey::Method(fd)];
6262
}
6363
}
64-
vec![SymbolIdentity::Scope(scope)]
64+
vec![SymbolKey::Scope(scope)]
6565
}
6666
hir::hir_def::scope_graph::ScopeId::FuncParam(item, idx) => {
67-
vec![SymbolIdentity::FuncParam(item, idx)]
67+
vec![SymbolKey::FuncParam(item, idx)]
6868
}
69-
hir::hir_def::scope_graph::ScopeId::Variant(v) => vec![SymbolIdentity::EnumVariant(v)],
70-
other => vec![SymbolIdentity::Scope(other)],
69+
hir::hir_def::scope_graph::ScopeId::Variant(v) => vec![SymbolKey::EnumVariant(v)],
70+
other => vec![SymbolKey::Scope(other)],
7171
},
7272
OP::MethodName {
7373
scope,
@@ -100,19 +100,18 @@ pub fn identity_for_occurrence<'db>(
100100
MethodCandidate::TraitMethod(tm)
101101
| MethodCandidate::NeedsConfirmation(tm) => tm.method.0,
102102
};
103-
vec![SymbolIdentity::Method(fd)]
103+
vec![SymbolKey::Method(fd)]
104+
}
105+
Err(MethodSelectionError::AmbiguousInherentMethod(methods)) => {
106+
methods.iter().map(|fd| SymbolKey::Method(*fd)).collect()
104107
}
105-
Err(MethodSelectionError::AmbiguousInherentMethod(methods)) => methods
106-
.iter()
107-
.map(|fd| SymbolIdentity::Method(*fd))
108-
.collect(),
109108
Err(MethodSelectionError::AmbiguousTraitMethod(traits)) => traits
110109
.iter()
111110
.filter_map(|trait_def| {
112111
trait_def
113112
.methods(db)
114113
.get(&ident)
115-
.map(|tm| SymbolIdentity::Method(tm.0))
114+
.map(|tm| SymbolKey::Method(tm.0))
116115
})
117116
.collect(),
118117
Err(_) => vec![],
@@ -133,9 +132,9 @@ pub fn identity_for_occurrence<'db>(
133132
if let Some(bkey) = crate::ty::ty_check::expr_binding_key_for_expr(db, func, expr) {
134133
return vec![match bkey {
135134
crate::ty::ty_check::BindingKey::FuncParam(f, idx) => {
136-
SymbolIdentity::FuncParam(ItemKind::Func(f), idx)
135+
SymbolKey::FuncParam(ItemKind::Func(f), idx)
137136
}
138-
other => SymbolIdentity::Local(func, other),
137+
other => SymbolKey::Local(func, other),
139138
}];
140139
}
141140
}
@@ -160,7 +159,7 @@ pub fn identity_for_occurrence<'db>(
160159
}
161160
OP::PathPatSeg { body, pat, .. } => {
162161
if let Some(func) = enclosing_func(db, body.scope()) {
163-
vec![SymbolIdentity::Local(
162+
vec![SymbolKey::Local(
164163
func,
165164
crate::ty::ty_check::BindingKey::LocalPat(pat),
166165
)]
@@ -180,7 +179,7 @@ pub fn identity_for_occurrence<'db>(
180179
if let Some(sc) =
181180
crate::ty::ty_check::RecordLike::from_ty(recv_ty).record_field_scope(db, ident)
182181
{
183-
return vec![SymbolIdentity::Scope(sc)];
182+
return vec![SymbolKey::Scope(sc)];
184183
}
185184
}
186185
vec![]
@@ -210,7 +209,7 @@ pub fn identity_for_occurrence<'db>(
210209
_ => None,
211210
};
212211
if let Some(target) = target {
213-
return vec![SymbolIdentity::Scope(target)];
212+
return vec![SymbolKey::Scope(target)];
214213
}
215214
}
216215
}
@@ -230,7 +229,7 @@ pub fn identity_for_occurrence<'db>(
230229
{
231230
match nr.kind {
232231
crate::name_resolution::NameResKind::Scope(sc) => {
233-
return vec![SymbolIdentity::Scope(sc)];
232+
return vec![SymbolKey::Scope(sc)];
234233
}
235234
crate::name_resolution::NameResKind::Prim(_) => {}
236235
}
@@ -329,7 +328,7 @@ fn find_ambiguous_candidates_for_path_seg<'db>(
329328
scope: ScopeId<'db>,
330329
path: PathId<'db>,
331330
seg_idx: usize,
332-
) -> Vec<SymbolIdentity<'db>> {
331+
) -> Vec<SymbolKey<'db>> {
333332
use crate::name_resolution::NameDomain;
334333

335334
// Get the identifier from the path segment
@@ -367,14 +366,14 @@ fn find_ambiguous_candidates_for_path_seg<'db>(
367366
match bucket.pick(domain) {
368367
Ok(name_res) => {
369368
if let crate::name_resolution::NameResKind::Scope(sc) = name_res.kind {
370-
candidates.push(SymbolIdentity::Scope(sc));
369+
candidates.push(SymbolKey::Scope(sc));
371370
}
372371
}
373372
Err(crate::name_resolution::NameResolutionError::Ambiguous(ambiguous_candidates)) => {
374373
// This is exactly what we want for ambiguous imports!
375374
for name_res in ambiguous_candidates {
376375
if let crate::name_resolution::NameResKind::Scope(sc) = name_res.kind {
377-
candidates.push(SymbolIdentity::Scope(sc));
376+
candidates.push(SymbolKey::Scope(sc));
378377
}
379378
}
380379
}

crates/hir-analysis/src/ty/trait_resolution/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ use crate::{
1616
};
1717
use common::indexmap::IndexSet;
1818
use constraint::collect_constraints;
19-
use hir::{
20-
hir_def::HirIngot,
21-
Ingot,
22-
};
19+
use hir::{hir_def::HirIngot, Ingot};
2320
use salsa::Update;
2421

2522
pub(crate) mod constraint;

crates/language-server/src/lsp_diagnostics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ impl LspDiagnostics for DriverDataBase {
4141
// (to clear any previous diagnostics)
4242
result.entry(url.clone()).or_default();
4343

44-
// Use the stable file-based API to prevent stale TopLevelMod references
45-
let diagnostics = pass_manager.run_on_file(self, file);
44+
let top_mod = hir::lower::map_file_to_mod(self, file);
45+
let diagnostics = pass_manager.run_on_module(self, top_mod);
4646
let mut finalized_diags: Vec<CompleteDiagnostic> = diagnostics
4747
.iter()
4848
.map(|d| d.to_complete(self).clone())
@@ -166,7 +166,7 @@ mod tests {
166166
for (uri, diags) in map.iter() {
167167
by_uri
168168
.entry(uri.to_string())
169-
.or_insert_with(Vec::new)
169+
.or_default()
170170
.extend(diags.iter().cloned());
171171
}
172172

crates/language-server/src/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn load_ingot_from_directory(db: &mut DriverDataBase, ingot_dir: &Path) {
2121
}
2222
_ => {
2323
// Log other diagnostics but don't panic
24-
eprintln!("Test ingot diagnostic for {ingot_dir:?}: {diagnostic}");
24+
tracing::debug!("Test ingot diagnostic for {ingot_dir:?}: {diagnostic}");
2525
}
2626
}
2727
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
---
22
source: crates/language-server/src/lsp_diagnostics.rs
3-
assertion_line: 238
43
expression: snapshot
4+
input_file: test_projects/comprehensive/diagnostics.snap
55
---
6-
File: file:///home/micah/hacker-stuff-2023/fe-stuff/fe/crates/language-server/test_projects/comprehensive/src/lib.fe
6+
File: file:///home/micah/hacker-stuff-2023/fe-stuff/fe-B/crates/language-server/test_projects/comprehensive/src/lib.fe
77
- code:8-0000 severity:Error @ 38:3..38:8
88
type mismatch
99
expected `()`, but `i32` is given
10-

crates/semantic-query/src/hover.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use hir_analysis::diagnostics::SpannedHirAnalysisDb;
2+
use hir_analysis::lookup::SymbolKey;
23

34
use hir::hir_def::scope_graph::ScopeId;
45
use hir::source_index::OccurrencePayload;
@@ -18,8 +19,7 @@ pub(crate) fn hover_for_occurrence<'db>(
1819
top_mod: hir::hir_def::TopLevelMod<'db>,
1920
) -> Option<HoverSemantics<'db>> {
2021
// Use the canonical occurrence interpreter to get the symbol target
21-
let target = crate::identity::occurrence_symbol_target(db, top_mod, occ)?;
22-
let symbol_key = crate::occ_target_to_symbol_key(target);
22+
let symbol_key = crate::identity::occurrence_symbol_target(db, top_mod, occ)?;
2323

2424
// Get the span from the occurrence
2525
let span = get_span_from_occurrence(occ);
@@ -44,11 +44,11 @@ pub(crate) fn get_span_from_occurrence<'db>(occ: &OccurrencePayload<'db>) -> Dyn
4444

4545
fn hover_data_from_symbol_key<'db>(
4646
db: &'db dyn SpannedHirAnalysisDb,
47-
symbol_key: crate::SymbolKey<'db>,
47+
symbol_key: SymbolKey<'db>,
4848
span: DynLazySpan<'db>,
4949
) -> Option<HoverSemantics<'db>> {
5050
match symbol_key {
51-
crate::SymbolKey::Scope(sc) => {
51+
SymbolKey::Scope(sc) => {
5252
let signature = sc.pretty_path(db);
5353
let documentation = get_docstring(db, sc);
5454
let kind = sc.kind_name();
@@ -59,7 +59,7 @@ fn hover_data_from_symbol_key<'db>(
5959
kind,
6060
})
6161
}
62-
crate::SymbolKey::Method(fd) => {
62+
SymbolKey::Method(fd) => {
6363
let meth = fd.name(db).data(db).to_string();
6464
let signature = Some(format!("method: {}", meth));
6565
let documentation = get_docstring(db, fd.scope(db));
@@ -70,7 +70,7 @@ fn hover_data_from_symbol_key<'db>(
7070
kind: "method",
7171
})
7272
}
73-
crate::SymbolKey::Local(_func, bkey) => {
73+
SymbolKey::Local(_func, bkey) => {
7474
let signature = Some(format!("local binding: {:?}", bkey));
7575
Some(HoverSemantics {
7676
span,
@@ -79,7 +79,7 @@ fn hover_data_from_symbol_key<'db>(
7979
kind: "local",
8080
})
8181
}
82-
crate::SymbolKey::FuncParam(item, idx) => {
82+
SymbolKey::FuncParam(item, idx) => {
8383
let signature = Some(format!("parameter {} of {:?}", idx, item));
8484
Some(HoverSemantics {
8585
span,
@@ -88,7 +88,7 @@ fn hover_data_from_symbol_key<'db>(
8888
kind: "parameter",
8989
})
9090
}
91-
crate::SymbolKey::EnumVariant(v) => {
91+
SymbolKey::EnumVariant(v) => {
9292
let sc = v.scope();
9393
let signature = sc.pretty_path(db);
9494
let documentation = get_docstring(db, sc);

crates/semantic-query/src/identity.rs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,24 @@
1-
use hir::hir_def::{scope_graph::ScopeId, TopLevelMod};
2-
use hir::source_index::OccurrencePayload;
1+
use hir::{hir_def::TopLevelMod, source_index::OccurrencePayload};
32

4-
use hir_analysis::diagnostics::SpannedHirAnalysisDb;
5-
use hir_analysis::ty::{func_def::FuncDef, ty_check::BindingKey};
6-
7-
/// Analysis-side identity for a single occurrence. Mirrors `SymbolKey` mapping
8-
/// without pulling semantic-query’s public type into analysis.
9-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10-
pub enum OccTarget<'db> {
11-
Scope(ScopeId<'db>),
12-
EnumVariant(hir::hir_def::EnumVariant<'db>),
13-
FuncParam(hir::hir_def::ItemKind<'db>, u16),
14-
Method(FuncDef<'db>),
15-
Local(hir::hir_def::item::Func<'db>, BindingKey<'db>),
16-
}
3+
use hir_analysis::{diagnostics::SpannedHirAnalysisDb, lookup::SymbolKey};
174

185
/// Returns all possible symbol targets for an occurrence, including ambiguous cases.
6+
/// Now directly returns SymbolIdentity from hir-analysis without translation.
197
pub(crate) fn occurrence_symbol_targets<'db>(
208
db: &'db dyn SpannedHirAnalysisDb,
219
top_mod: TopLevelMod<'db>,
2210
occ: &OccurrencePayload<'db>,
23-
) -> Vec<OccTarget<'db>> {
11+
) -> Vec<SymbolKey<'db>> {
2412
// Use hir-analysis as the single source of truth for occurrence interpretation
25-
let identities = hir_analysis::lookup::identity_for_occurrence(db, top_mod, occ);
26-
identities
27-
.into_iter()
28-
.map(|identity| match identity {
29-
hir_analysis::lookup::SymbolIdentity::Scope(sc) => OccTarget::Scope(sc),
30-
hir_analysis::lookup::SymbolIdentity::EnumVariant(v) => OccTarget::EnumVariant(v),
31-
hir_analysis::lookup::SymbolIdentity::FuncParam(item, idx) => {
32-
OccTarget::FuncParam(item, idx)
33-
}
34-
hir_analysis::lookup::SymbolIdentity::Method(fd) => OccTarget::Method(fd),
35-
hir_analysis::lookup::SymbolIdentity::Local(func, bkey) => OccTarget::Local(func, bkey),
36-
})
37-
.collect()
13+
hir_analysis::lookup::identity_for_occurrence(db, top_mod, occ)
3814
}
3915

4016
/// Returns the first symbol target for an occurrence (backward compatibility).
4117
pub(crate) fn occurrence_symbol_target<'db>(
4218
db: &'db dyn SpannedHirAnalysisDb,
4319
top_mod: TopLevelMod<'db>,
4420
occ: &OccurrencePayload<'db>,
45-
) -> Option<OccTarget<'db>> {
21+
) -> Option<SymbolKey<'db>> {
4622
occurrence_symbol_targets(db, top_mod, occ)
4723
.into_iter()
4824
.next()

0 commit comments

Comments
 (0)