Skip to content

Commit 02b4c1f

Browse files
committed
Rust: downgrade uncompiled source files from warning to info
1 parent 80031e5 commit 02b4c1f

File tree

3 files changed

+78
-30
lines changed

3 files changed

+78
-30
lines changed

rust/extractor/src/main.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::diagnostics::{ExtractionStep, emit_extraction_diagnostics};
2-
use crate::rust_analyzer::path_to_file_id;
2+
use crate::rust_analyzer::{RustAnalyzerNoSemantics, path_to_file_id};
33
use crate::translate::{ResolvePaths, SourceKind};
44
use crate::trap::TrapId;
55
use anyhow::Context;
@@ -87,14 +87,12 @@ impl<'a> Extractor<'a> {
8787
translator.emit_parse_error(&ast, &err);
8888
}
8989
let no_location = (LineCol { line: 0, col: 0 }, LineCol { line: 0, col: 0 });
90-
if let Err(reason) = semantics_info {
90+
if let Err(RustAnalyzerNoSemantics { severity, reason }) = semantics_info {
9191
if !reason.is_empty() {
9292
let message = format!("semantic analyzer unavailable ({reason})");
93-
let full_message = format!(
94-
"{message}: macro expansion, call graph, and type inference will be skipped."
95-
);
93+
let full_message = format!("{message}: macro expansion will be skipped.");
9694
translator.emit_diagnostic(
97-
trap::DiagnosticSeverity::Warning,
95+
severity,
9896
"semantics".to_owned(),
9997
message,
10098
full_message,
@@ -135,10 +133,10 @@ impl<'a> Extractor<'a> {
135133
&mut self,
136134
file: &Path,
137135
source_kind: SourceKind,
138-
reason: &str,
136+
err: RustAnalyzerNoSemantics,
139137
) {
140138
self.extract(
141-
&RustAnalyzer::WithoutSemantics { reason },
139+
&RustAnalyzer::from(err),
142140
file,
143141
ResolvePaths::No,
144142
source_kind,
@@ -163,21 +161,25 @@ impl<'a> Extractor<'a> {
163161
file: &Path,
164162
semantics: &Semantics<'_, RootDatabase>,
165163
vfs: &Vfs,
166-
) -> Result<(), String> {
164+
) -> Result<(), RustAnalyzerNoSemantics> {
167165
let before = Instant::now();
168166
let Some(id) = path_to_file_id(file, vfs) else {
169-
return Err("not included in files loaded from manifest".to_string());
167+
return Err(RustAnalyzerNoSemantics::warning(
168+
"not included in files loaded from manifest",
169+
));
170170
};
171171
match semantics.file_to_module_def(id) {
172-
None => return Err("not included as a module".to_string()),
172+
None => {
173+
return Err(RustAnalyzerNoSemantics::info("not included as a module"));
174+
}
173175
Some(module)
174176
if module
175177
.as_source_file_id(semantics.db)
176178
.is_none_or(|mod_file_id| mod_file_id.file_id(semantics.db) != id) =>
177179
{
178-
return Err(
179-
"not loaded as its own module, probably included by `!include`".to_string(),
180-
);
180+
return Err(RustAnalyzerNoSemantics::info(
181+
"not loaded as its own module, probably included by `!include`",
182+
));
181183
}
182184
_ => {}
183185
};
@@ -279,7 +281,11 @@ fn main() -> anyhow::Result<()> {
279281
continue 'outer;
280282
}
281283
}
282-
extractor.extract_without_semantics(file, SourceKind::Source, "no manifest found");
284+
extractor.extract_without_semantics(
285+
file,
286+
SourceKind::Source,
287+
RustAnalyzerNoSemantics::warning("no manifest found"),
288+
);
283289
}
284290
let cwd = cwd()?;
285291
let (cargo_config, load_cargo_config) = cfg.to_cargo_config(&cwd);
@@ -319,7 +325,7 @@ fn main() -> anyhow::Result<()> {
319325
source_resolve_paths,
320326
source_mode,
321327
),
322-
Err(reason) => extractor.extract_without_semantics(file, source_mode, &reason),
328+
Err(e) => extractor.extract_without_semantics(file, source_mode, e),
323329
};
324330
}
325331
for (file_id, file) in vfs.iter() {
@@ -347,7 +353,7 @@ fn main() -> anyhow::Result<()> {
347353
extractor.extract_without_semantics(
348354
file,
349355
SourceKind::Source,
350-
"unable to load manifest",
356+
RustAnalyzerNoSemantics::warning("unable to load manifest"),
351357
);
352358
}
353359
}
@@ -359,7 +365,7 @@ fn main() -> anyhow::Result<()> {
359365
let entry = entry.context("failed to read builtins directory")?;
360366
let path = entry.path();
361367
if path.extension().is_some_and(|ext| ext == "rs") {
362-
extractor.extract_without_semantics(&path, SourceKind::Library, "");
368+
extractor.extract_without_semantics(&path, SourceKind::Library, Default::default());
363369
}
364370
}
365371

rust/extractor/src/rust_analyzer.rs

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::trap;
12
use itertools::Itertools;
23
use ra_ap_base_db::{EditionedFileId, FileText, RootQueryDb, SourceDatabase};
34
use ra_ap_hir::Semantics;
@@ -23,16 +24,47 @@ use std::rc::Rc;
2324
use tracing::{debug, error, info, trace, warn};
2425
use triomphe::Arc;
2526

27+
#[derive(Clone, Default)]
28+
pub struct RustAnalyzerNoSemantics {
29+
pub severity: trap::DiagnosticSeverity,
30+
pub reason: &'static str,
31+
}
32+
33+
impl RustAnalyzerNoSemantics {
34+
pub fn warning(reason: &'static str) -> Self {
35+
RustAnalyzerNoSemantics {
36+
severity: trap::DiagnosticSeverity::Warning,
37+
reason,
38+
}
39+
}
40+
pub fn info(reason: &'static str) -> Self {
41+
RustAnalyzerNoSemantics {
42+
severity: trap::DiagnosticSeverity::Info,
43+
reason,
44+
}
45+
}
46+
}
47+
2648
pub enum RustAnalyzer<'a> {
2749
WithSemantics {
2850
vfs: &'a Vfs,
2951
semantics: &'a Semantics<'a, RootDatabase>,
3052
},
3153
WithoutSemantics {
32-
reason: &'a str,
54+
severity: trap::DiagnosticSeverity,
55+
reason: &'static str,
3356
},
3457
}
3558

59+
impl From<RustAnalyzerNoSemantics> for RustAnalyzer<'static> {
60+
fn from(value: RustAnalyzerNoSemantics) -> Self {
61+
RustAnalyzer::WithoutSemantics {
62+
severity: value.severity,
63+
reason: value.reason,
64+
}
65+
}
66+
}
67+
3668
pub struct FileSemanticInformation<'a> {
3769
pub file_id: EditionedFileId,
3870
pub semantics: &'a Semantics<'a, RootDatabase>,
@@ -42,7 +74,7 @@ pub struct ParseResult<'a> {
4274
pub ast: SourceFile,
4375
pub text: Arc<str>,
4476
pub errors: Vec<SyntaxError>,
45-
pub semantics_info: Result<FileSemanticInformation<'a>, &'a str>,
77+
pub semantics_info: Result<FileSemanticInformation<'a>, RustAnalyzerNoSemantics>,
4678
}
4779

4880
impl<'a> RustAnalyzer<'a> {
@@ -52,7 +84,7 @@ impl<'a> RustAnalyzer<'a> {
5284
load_config: &LoadCargoConfig,
5385
) -> Option<(RootDatabase, Vfs)> {
5486
let progress = |t| trace!("progress: {t}");
55-
let manifest = project.manifest_path();
87+
let manifest: &ManifestPath = project.manifest_path();
5688
match load_workspace_at(manifest.as_ref(), config, load_config, &progress) {
5789
Ok((db, vfs, _macro_server)) => Some((db, vfs)),
5890
Err(err) => {
@@ -67,16 +99,25 @@ impl<'a> RustAnalyzer<'a> {
6799
fn get_file_data(
68100
&self,
69101
path: &Path,
70-
) -> Result<(&Semantics<'_, RootDatabase>, EditionedFileId, FileText), &str> {
102+
) -> Result<(&Semantics<'_, RootDatabase>, EditionedFileId, FileText), RustAnalyzerNoSemantics>
103+
{
71104
match self {
72-
RustAnalyzer::WithoutSemantics { reason } => Err(reason),
105+
RustAnalyzer::WithoutSemantics { severity, reason } => Err(RustAnalyzerNoSemantics {
106+
severity: *severity,
107+
reason,
108+
}),
73109
RustAnalyzer::WithSemantics { vfs, semantics } => {
74-
let file_id = path_to_file_id(path, vfs).ok_or("file not found in project")?;
75-
let input = std::panic::catch_unwind(|| semantics.db.file_text(file_id))
76-
.or(Err("no text available for the file in the project"))?;
77-
let editioned_file_id = semantics
78-
.attach_first_edition(file_id)
79-
.ok_or("failed to determine rust edition")?;
110+
let file_id = path_to_file_id(path, vfs).ok_or(
111+
RustAnalyzerNoSemantics::warning("file not found in project"),
112+
)?;
113+
let input = std::panic::catch_unwind(|| semantics.db.file_text(file_id)).or(
114+
Err(RustAnalyzerNoSemantics::warning(
115+
"no text available for the file in the project",
116+
)),
117+
)?;
118+
let editioned_file_id = semantics.attach_first_edition(file_id).ok_or(
119+
RustAnalyzerNoSemantics::warning("failed to determine rust edition"),
120+
)?;
80121
Ok((semantics, editioned_file_id, input))
81122
}
82123
}

rust/extractor/src/trap.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ pub struct TrapFile {
127127
compression: Compression,
128128
}
129129

130-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
130+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
131131
pub enum DiagnosticSeverity {
132132
Debug = 10,
133+
#[default]
133134
Info = 20,
134135
Warning = 30,
135136
Error = 40,

0 commit comments

Comments
 (0)