Skip to content

Rust: extract source files of dependencies #19506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
May 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
dd5c487
Rust: extract source files of depdendencies
aibaars May 15, 2025
f05bed6
Rust: remove module data from Crate elements
aibaars May 15, 2025
980cebe
Rust: fix QL code after removing Crate::getModule()
aibaars May 16, 2025
0bb0a70
Rust: add upgrade/downgrade scripts
aibaars May 16, 2025
8996f9e
Rust: Follow-up work to make path resolution and type inference tests…
hvitved May 19, 2025
1269a2e
Rust: fix extractor-tests
aibaars May 19, 2025
456a4b2
Rust: Make `dataflow/modeled` pass by not using `#[derive(Clone)]`
hvitved May 20, 2025
44a4045
Rust: fixes
aibaars May 19, 2025
643059e
Rust: fix type-interence file paths
aibaars May 20, 2025
67846f1
fixup TestUtils
aibaars May 20, 2025
3761099
Rust: drop Param::pat when extracting libraries
aibaars May 20, 2025
5ee7658
Rust: update DataFlowStep.expected
aibaars May 20, 2025
457632e
Rust: update UncontrolledAllocationSize.expected
aibaars May 20, 2025
e90ab7b
Rust: fix diagnostics tests
aibaars May 20, 2025
76da2e4
Rust: drop crate_graph/modules.ql test
aibaars May 20, 2025
81f0e42
Rust: improve ExtractionConsistency.ql
aibaars May 20, 2025
f093c49
Rust: normalize file paths for PathResolutionConsistency.ql
aibaars May 20, 2025
9ee0d2e
Rust: Exclude flow summary nodes from `DataFlowStep.ql`
hvitved May 21, 2025
c69aa22
Rust: restrict to library files
aibaars May 21, 2025
1eaa491
Rust: update integration tests
aibaars May 21, 2025
2a93b2a
Rust: integration-tests: update output
aibaars May 21, 2025
fa1a21b
Rust: reduce log-level of diagnostics when extracting library files
aibaars May 21, 2025
a6cd60f
Rust: address comments
aibaars May 21, 2025
28be208
Rust: drop too noisy log statements
aibaars May 21, 2025
76737cb
Rust: Follow-up changes after rebase
hvitved May 22, 2025
7e5f652
Rust: disable ResolvePaths when extracting library source files
aibaars May 22, 2025
a4788fd
Rust: update expected output
aibaars May 22, 2025
df99e06
Rust: temporarily disable attribute macro expansion in library mode
aibaars May 23, 2025
b62d52e
Rust: prevent source files from being extracted in both source and li…
aibaars May 23, 2025
23b4e50
Rust: update expected output
aibaars May 23, 2025
c8ff69a
Rust: Fix bad join
hvitved May 23, 2025
5b28ff1
Merge branch 'main' into aibaars/rust-extract-libs
aibaars May 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Rust: extract source files of depdendencies
  • Loading branch information
aibaars authored and hvitved committed May 22, 2025
commit dd5c48762879d170f7370a52bc6b53d259d970f1
69 changes: 58 additions & 11 deletions rust/extractor/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::diagnostics::{ExtractionStep, emit_extraction_diagnostics};
use crate::rust_analyzer::path_to_file_id;
use crate::translate::ResolvePaths;
use crate::translate::{ResolvePaths, SourceKind};
use crate::trap::TrapId;
use anyhow::Context;
use archive::Archiver;
Expand All @@ -12,6 +12,7 @@ use ra_ap_paths::{AbsPathBuf, Utf8PathBuf};
use ra_ap_project_model::{CargoConfig, ProjectManifest};
use ra_ap_vfs::Vfs;
use rust_analyzer::{ParseResult, RustAnalyzer};
use std::collections::HashSet;
use std::time::Instant;
use std::{
collections::HashMap,
Expand Down Expand Up @@ -47,9 +48,14 @@ impl<'a> Extractor<'a> {
}
}

fn extract(&mut self, rust_analyzer: &RustAnalyzer, file: &Path, resolve_paths: ResolvePaths) {
fn extract(
&mut self,
rust_analyzer: &RustAnalyzer,
file: &Path,
resolve_paths: ResolvePaths,
source_kind: SourceKind,
) {
self.archiver.archive(file);

let before_parse = Instant::now();
let ParseResult {
ast,
Expand All @@ -71,6 +77,7 @@ impl<'a> Extractor<'a> {
line_index,
semantics_info.as_ref().ok(),
resolve_paths,
source_kind,
);

for err in errors {
Expand Down Expand Up @@ -110,15 +117,27 @@ impl<'a> Extractor<'a> {
semantics: &Semantics<'_, RootDatabase>,
vfs: &Vfs,
resolve_paths: ResolvePaths,
source_kind: SourceKind,
) {
self.extract(&RustAnalyzer::new(vfs, semantics), file, resolve_paths);
self.extract(
&RustAnalyzer::new(vfs, semantics),
file,
resolve_paths,
source_kind,
);
}

pub fn extract_without_semantics(&mut self, file: &Path, reason: &str) {
pub fn extract_without_semantics(
&mut self,
file: &Path,
source_kind: SourceKind,
reason: &str,
) {
self.extract(
&RustAnalyzer::WithoutSemantics { reason },
file,
ResolvePaths::No,
source_kind,
);
}

Expand Down Expand Up @@ -246,7 +265,7 @@ fn main() -> anyhow::Result<()> {
continue 'outer;
}
}
extractor.extract_without_semantics(file, "no manifest found");
extractor.extract_without_semantics(file, SourceKind::Source, "no manifest found");
}
let cwd = cwd()?;
let (cargo_config, load_cargo_config) = cfg.to_cargo_config(&cwd);
Expand All @@ -255,6 +274,7 @@ fn main() -> anyhow::Result<()> {
} else {
ResolvePaths::Yes
};
let mut processed_files = HashSet::new();
for (manifest, files) in map.values().filter(|(_, files)| !files.is_empty()) {
if let Some((ref db, ref vfs)) =
extractor.load_manifest(manifest, &cargo_config, &load_cargo_config)
Expand All @@ -266,16 +286,43 @@ fn main() -> anyhow::Result<()> {
.push(ExtractionStep::crate_graph(before_crate_graph));
let semantics = Semantics::new(db);
for file in files {
processed_files.insert((*file).to_owned());
match extractor.load_source(file, &semantics, vfs) {
Ok(()) => {
extractor.extract_with_semantics(file, &semantics, vfs, resolve_paths)
Ok(()) => extractor.extract_with_semantics(
file,
&semantics,
vfs,
resolve_paths,
SourceKind::Source,
),
Err(reason) => {
extractor.extract_without_semantics(file, SourceKind::Source, &reason)
}
Err(reason) => extractor.extract_without_semantics(file, &reason),
};
}
for (_, file) in vfs.iter() {
if let Some(file) = file.as_path().map(<_ as AsRef<Path>>::as_ref) {
if file.extension().is_some_and(|ext| ext == "rs")
&& processed_files.insert(file.to_owned())
{
extractor.extract_with_semantics(
file,
&semantics,
vfs,
resolve_paths,
SourceKind::Library,
);
extractor.archiver.archive(file);
}
}
}
} else {
for file in files {
extractor.extract_without_semantics(file, "unable to load manifest");
extractor.extract_without_semantics(
file,
SourceKind::Source,
"unable to load manifest",
);
}
}
}
Expand All @@ -286,7 +333,7 @@ fn main() -> anyhow::Result<()> {
let entry = entry.context("failed to read builtins directory")?;
let path = entry.path();
if path.extension().is_some_and(|ext| ext == "rs") {
extractor.extract_without_semantics(&path, "");
extractor.extract_without_semantics(&path, SourceKind::Library, "");
}
}

Expand Down
2 changes: 1 addition & 1 deletion rust/extractor/src/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod base;
mod generated;
mod mappings;

pub use base::{ResolvePaths, Translator};
pub use base::{ResolvePaths, SourceKind, Translator};
35 changes: 34 additions & 1 deletion rust/extractor/src/translate/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ra_ap_ide_db::RootDatabase;
use ra_ap_ide_db::line_index::{LineCol, LineIndex};
use ra_ap_parser::SyntaxKind;
use ra_ap_span::TextSize;
use ra_ap_syntax::ast::HasName;
use ra_ap_syntax::ast::{Const, Fn, HasName, Static};
use ra_ap_syntax::{
AstNode, NodeOrToken, SyntaxElementChildren, SyntaxError, SyntaxNode, SyntaxToken, TextRange,
ast,
Expand Down Expand Up @@ -93,6 +93,11 @@ pub enum ResolvePaths {
Yes,
No,
}
#[derive(PartialEq, Eq)]
pub enum SourceKind {
Source,
Library,
}

pub struct Translator<'a> {
pub trap: TrapFile,
Expand All @@ -102,6 +107,7 @@ pub struct Translator<'a> {
file_id: Option<EditionedFileId>,
pub semantics: Option<&'a Semantics<'a, RootDatabase>>,
resolve_paths: ResolvePaths,
source_kind: SourceKind,
}

const UNKNOWN_LOCATION: (LineCol, LineCol) =
Expand All @@ -115,6 +121,7 @@ impl<'a> Translator<'a> {
line_index: LineIndex,
semantic_info: Option<&FileSemanticInformation<'a>>,
resolve_paths: ResolvePaths,
source_kind: SourceKind,
) -> Translator<'a> {
Translator {
trap,
Expand All @@ -124,6 +131,7 @@ impl<'a> Translator<'a> {
file_id: semantic_info.map(|i| i.file_id),
semantics: semantic_info.map(|i| i.semantics),
resolve_paths,
source_kind,
}
}
fn location(&self, range: TextRange) -> Option<(LineCol, LineCol)> {
Expand Down Expand Up @@ -612,6 +620,31 @@ impl<'a> Translator<'a> {
}

pub(crate) fn should_be_excluded(&self, item: &impl ast::HasAttrs) -> bool {
if self.source_kind == SourceKind::Library {
let syntax = item.syntax();
if let Some(body) = syntax.parent().and_then(Fn::cast).and_then(|x| x.body()) {
if body.syntax() == syntax {
tracing::debug!("Skipping Fn body");
return true;
}
}
if let Some(body) = syntax.parent().and_then(Const::cast).and_then(|x| x.body()) {
if body.syntax() == syntax {
tracing::debug!("Skipping Const body");
return true;
}
}
if let Some(body) = syntax
.parent()
.and_then(Static::cast)
.and_then(|x| x.body())
{
if body.syntax() == syntax {
tracing::debug!("Skipping Static body");
return true;
}
}
}
self.semantics.is_some_and(|sema| {
item.attrs().any(|attr| {
attr.as_simple_call().is_some_and(|(name, tokens)| {
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy