Skip to content

Commit dd68d68

Browse files
authored
Merge pull request #20288 from github/redsun82/rust-less-warnings
Rust: downgrade uncompiled source files from warning to info
2 parents 3527fca + 531955e commit dd68d68

File tree

12 files changed

+87
-46
lines changed

12 files changed

+87
-46
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/translate/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ impl<'a> Translator<'a> {
514514
mcall,
515515
&SyntaxError::new(
516516
format!(
517-
"macro expansion failed: could not resolve macro '{}'",
517+
"macro expansion failed for '{}'",
518518
mcall.path().map(|p| p.to_string()).unwrap_or_default()
519519
),
520520
range.unwrap_or_else(|| TextRange::empty(TextSize::from(0))),

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,
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
| src/directory_module/not_loaded.rs:1:1:1:1 | semantic analyzer unavailable (not included as a module) | Extraction warning in src/directory_module/not_loaded.rs with message semantic analyzer unavailable (not included as a module) | 1 |

rust/ql/integration-tests/hello-project/summary.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
| Extraction errors | 0 |
2-
| Extraction warnings | 1 |
2+
| Extraction warnings | 0 |
33
| Files extracted - total | 5 |
4-
| Files extracted - with errors | 1 |
5-
| Files extracted - without errors | 4 |
6-
| Files extracted - without errors % | 80 |
4+
| Files extracted - with errors | 0 |
5+
| Files extracted - without errors | 5 |
6+
| Files extracted - without errors % | 100 |
77
| Inconsistencies - AST | 0 |
88
| Inconsistencies - CFG | 0 |
99
| Inconsistencies - Path resolution | 0 |
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
extractionWarning
2-
| included/included.rs:1:1:1:1 | semantic analyzer unavailable (not loaded as its own module, probably included by `!include`) |
3-
| macro_expansion.rs:56:9:56:31 | macro expansion failed: could not resolve macro 'concat' |
2+
| macro_expansion.rs:56:9:56:31 | macro expansion failed for 'concat' |

rust/ql/test/extractor-tests/macro-expansion/test.expected

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,4 @@ unexpanded_macro_calls
5151
| macro_expansion.rs:56:9:56:31 | concat!... |
5252
| macro_expansion.rs:63:9:63:32 | include_str!... |
5353
warnings
54-
| included/included.rs:1:1:1:1 | semantic analyzer unavailable (not loaded as its own module, probably included by `!include`) |
55-
| macro_expansion.rs:56:9:56:31 | macro expansion failed: could not resolve macro 'concat' |
54+
| macro_expansion.rs:56:9:56:31 | macro expansion failed for 'concat' |

rust/ql/test/library-tests/type-inference/CONSISTENCY/ExtractionConsistency.expected

Lines changed: 0 additions & 2 deletions
This file was deleted.

rust/ql/test/query-tests/diagnostics/CONSISTENCY/ExtractionConsistency.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ extractionWarning
55
| does_not_compile.rs:2:21:2:20 | expected SEMICOLON |
66
| does_not_compile.rs:2:26:2:25 | expected SEMICOLON |
77
| error.rs:2:5:2:17 | An error! |
8-
| my_macro.rs:17:9:17:27 | macro expansion failed: could not resolve macro 'myUndefinedMacro' |
8+
| my_macro.rs:17:9:17:27 | macro expansion failed for 'myUndefinedMacro' |

0 commit comments

Comments
 (0)