Skip to content
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

Rust: crate_graph: generate 'use' statements for re-exported items #19113

Merged
merged 3 commits into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
122 changes: 118 additions & 4 deletions rust/extractor/src/crate_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ use chalk_ir::{FloatTy, Safety};
use itertools::Itertools;
use ra_ap_base_db::{Crate, RootQueryDb};
use ra_ap_cfg::CfgAtom;
use ra_ap_hir::{DefMap, ModuleDefId, db::HirDatabase};
use ra_ap_hir::{DefMap, ModuleDefId, PathKind, db::HirDatabase};
use ra_ap_hir::{VariantId, Visibility, db::DefDatabase};
use ra_ap_hir_def::{AssocItemId, LocalModuleId, data::adt::VariantData, nameres::ModuleData};
use ra_ap_hir_def::Lookup;
use ra_ap_hir_def::{
AssocItemId, LocalModuleId, data::adt::VariantData, item_scope::ImportOrGlob,
item_tree::ImportKind, nameres::ModuleData, path::ImportAlias,
};
use ra_ap_hir_def::{HasModule, visibility::VisibilityExplicitness};
use ra_ap_hir_def::{ModuleId, resolver::HasResolver};
use ra_ap_hir_ty::TraitRefExt;
Expand All @@ -22,6 +26,7 @@ use ra_ap_hir_ty::{Binders, FnPointer};
use ra_ap_hir_ty::{Interner, ProjectionTy};
use ra_ap_ide_db::RootDatabase;
use ra_ap_vfs::{Vfs, VfsPath};

use std::hash::Hasher;
use std::{cmp::Ordering, collections::HashMap, path::PathBuf};
use std::{hash::Hash, vec};
Expand Down Expand Up @@ -172,19 +177,116 @@ fn emit_module_children(
.collect()
}

fn emit_reexport(
db: &dyn HirDatabase,
trap: &mut TrapFile,
uses: &mut HashMap<String, trap::Label<generated::Item>>,
import: ImportOrGlob,
name: &str,
) {
let (use_, idx) = match import {
ImportOrGlob::Glob(import) => (import.use_, import.idx),
ImportOrGlob::Import(import) => (import.use_, import.idx),
};
let def_db = db.upcast();
let loc = use_.lookup(def_db);
let use_ = &loc.id.item_tree(def_db)[loc.id.value];

use_.use_tree.expand(|id, path, kind, alias| {
if id == idx {
let mut path_components = Vec::new();
match path.kind {
PathKind::Plain => (),
PathKind::Super(0) => path_components.push("self".to_owned()),
PathKind::Super(n) => {
path_components.extend(std::iter::repeat_n("super".to_owned(), n.into()));
}
PathKind::Crate => path_components.push("crate".to_owned()),
PathKind::Abs => path_components.push("".to_owned()),
PathKind::DollarCrate(crate_id) => {
let crate_extra = crate_id.extra_data(db);
let crate_name = crate_extra
.display_name
.as_ref()
.map(|x| x.canonical_name().to_string());
path_components.push(crate_name.unwrap_or("crate".to_owned()));
}
}
path_components.extend(path.segments().iter().map(|x| x.as_str().to_owned()));
match kind {
ImportKind::Plain => (),
ImportKind::Glob => path_components.push(name.to_owned()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate on why we push name and not make a glob import instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think explicit imports are more clear and easier to interpret. However, if you prefer * imports then it is easy to change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicit imports are easier, I just wondered how a single name can make it up for a full glob import?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We iterate over all types and values that are in the "item scope", for the types/values that are imported (possibly as part of a glob import) we generate a use statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just see the pub use statements in the test case and how they end up in the output of modules.ql.

ImportKind::TypeOnly => path_components.push("self".to_owned()),
};

let alias = alias.map(|alias| match alias {
ImportAlias::Underscore => "_".to_owned(),
ImportAlias::Alias(name) => name.as_str().to_owned(),
});
let key = format!(
"{} as {}",
path_components.join("::"),
alias.as_ref().unwrap_or(&"".to_owned())
);
// prevent duplicate imports
if uses.contains_key(&key) {
return;
}
let rename = alias.map(|name| {
let name = Some(trap.emit(generated::Name {
id: trap::TrapId::Star,
text: Some(name),
}));
trap.emit(generated::Rename {
id: trap::TrapId::Star,
name,
})
});
let path = make_qualified_path(trap, path_components);
let use_tree = trap.emit(generated::UseTree {
id: trap::TrapId::Star,
is_glob: false,
path,
rename,
use_tree_list: None,
});
let visibility = emit_visibility(db, trap, Visibility::Public);
uses.insert(
key,
trap.emit(generated::Use {
id: trap::TrapId::Star,
attrs: vec![],
use_tree: Some(use_tree),
visibility,
})
.into(),
);
}
});
}

fn emit_module_items(
db: &dyn HirDatabase,
module: &ModuleData,
trap: &mut TrapFile,
) -> Vec<trap::Label<generated::Item>> {
let mut items = Vec::new();
let mut uses = HashMap::new();
let item_scope = &module.scope;
for (name, item) in item_scope.entries() {
let def = item.filter_visibility(|x| matches!(x, ra_ap_hir::Visibility::Public));
if let Some(ra_ap_hir_def::per_ns::Item {
def: _,
vis: _,
import: Some(import),
}) = def.values
{
emit_reexport(db, trap, &mut uses, import, name.as_str());
}
if let Some(ra_ap_hir_def::per_ns::Item {
def: value,
vis,
import: _,
import: None,
}) = def.values
{
match value {
Expand All @@ -203,10 +305,21 @@ fn emit_module_items(
_ => (),
}
}
if let Some(ra_ap_hir_def::per_ns::Item {
def: _,
vis: _,
import: Some(import),
}) = def.types
{
// TODO: handle ExternCrate as well?
if let Some(import) = import.import_or_glob() {
emit_reexport(db, trap, &mut uses, import, name.as_str());
}
}
if let Some(ra_ap_hir_def::per_ns::Item {
def: type_id,
vis,
import: _,
import: None,
}) = def.types
{
match type_id {
Expand All @@ -220,6 +333,7 @@ fn emit_module_items(
}
}
}
items.extend(uses.values());
items
}

Expand Down
2 changes: 0 additions & 2 deletions rust/ql/.generated.list

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions rust/ql/.gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions rust/ql/lib/codeql/rust/elements/internal/UseImpl.qll
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// generated by codegen, remove this comment if you wish to edit this file
/**
* This module provides a hand-modifiable wrapper around the generated class `Use`.
*
Expand All @@ -12,11 +11,14 @@ private import codeql.rust.elements.internal.generated.Use
* be referenced directly.
*/
module Impl {
// the following QLdoc is generated: if you need to edit it, do it in the schema file
/**
* A Use. For example:
* ```rust
* todo!()
* ```
*/
class Use extends Generated::Use { }
class Use extends Generated::Use {
override string toStringImpl() { result = "use " + this.getUseTree() }
}
}
18 changes: 16 additions & 2 deletions rust/ql/lib/codeql/rust/elements/internal/UseTreeImpl.qll
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// generated by codegen, remove this comment if you wish to edit this file
/**
* This module provides a hand-modifiable wrapper around the generated class `UseTree`.
*
Expand All @@ -12,6 +11,7 @@ private import codeql.rust.elements.internal.generated.UseTree
* be referenced directly.
*/
module Impl {
// the following QLdoc is generated: if you need to edit it, do it in the schema file
/**
* A UseTree. For example:
* ```rust
Expand All @@ -21,5 +21,19 @@ module Impl {
* use std::collections::{self, HashMap, HashSet};
* ```
*/
class UseTree extends Generated::UseTree { }
class UseTree extends Generated::UseTree {
override string toStringImpl() {
result = strictconcat(int i | | this.toStringPart(i) order by i)
}

private string toStringPart(int index) {
result = this.getPath().toStringImpl() and index = 0
or
result = "::{...}" and this.hasUseTreeList() and index = 1
or
result = "::*" and this.isGlob() and index = 2
or
result = " as " + this.getRename().getName().getText() and index = 3
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
canonicalPaths
| anonymous.rs:1:1:1:26 | Use | None | None |
| anonymous.rs:1:1:1:26 | use ...::Trait | None | None |
| anonymous.rs:3:1:32:1 | fn canonicals | repo::test | crate::anonymous::canonicals |
| anonymous.rs:4:5:4:23 | struct OtherStruct | None | None |
| anonymous.rs:6:5:8:5 | trait OtherTrait | None | None |
Expand Down Expand Up @@ -33,7 +33,7 @@ canonicalPaths
| regular.rs:34:1:38:1 | enum MyEnum | repo::test | crate::regular::MyEnum |
| regular.rs:40:1:46:1 | fn enum_qualified_usage | repo::test | crate::regular::enum_qualified_usage |
| regular.rs:48:1:55:1 | fn enum_unqualified_usage | repo::test | crate::regular::enum_unqualified_usage |
| regular.rs:51:5:51:18 | Use | None | None |
| regular.rs:51:5:51:18 | use MyEnum::* | None | None |
| regular.rs:57:1:63:1 | fn enum_match | repo::test | crate::regular::enum_match |
resolvedPaths
| anonymous.rs:27:17:27:30 | OtherStruct {...} | None | None |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
canonicalPaths
| anonymous.rs:4:1:4:26 | Use | None | None |
| anonymous.rs:4:1:4:26 | use ...::Trait | None | None |
| anonymous.rs:6:1:35:1 | fn canonicals | None | None |
| anonymous.rs:7:5:7:23 | struct OtherStruct | None | None |
| anonymous.rs:9:5:11:5 | trait OtherTrait | None | None |
Expand Down Expand Up @@ -33,7 +33,7 @@ canonicalPaths
| regular.rs:37:1:41:1 | enum MyEnum | None | None |
| regular.rs:43:1:49:1 | fn enum_qualified_usage | None | None |
| regular.rs:51:1:58:1 | fn enum_unqualified_usage | None | None |
| regular.rs:54:5:54:18 | Use | None | None |
| regular.rs:54:5:54:18 | use MyEnum::* | None | None |
| regular.rs:60:1:66:1 | fn enum_match | None | None |
resolvedPaths
| anonymous.rs:30:17:30:30 | OtherStruct {...} | None | None |
Expand Down
3 changes: 3 additions & 0 deletions rust/ql/test/extractor-tests/crate_graph/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ impl fmt::Display for X {

pub const X_A: X = X::A;
pub static X_B: X = X::B;

pub use std::fs::create_dir as mkdir;
pub use std::{fs::*, path::PathBuf};
90 changes: 90 additions & 0 deletions rust/ql/test/extractor-tests/crate_graph/modules.expected
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,98 @@ lib.rs:
#-----| -> impl AsString for ...::X { ... }
#-----| -> struct X_List
#-----| -> trait AsString
#-----| -> use ...::DirBuilder
#-----| -> use ...::DirEntry
#-----| -> use ...::File
#-----| -> use ...::FileTimes
#-----| -> use ...::FileType
#-----| -> use ...::Metadata
#-----| -> use ...::OpenOptions
#-----| -> use ...::PathBuf
#-----| -> use ...::Permissions
#-----| -> use ...::ReadDir
#-----| -> use ...::canonicalize
#-----| -> use ...::copy
#-----| -> use ...::create_dir
#-----| -> use ...::create_dir as mkdir
#-----| -> use ...::create_dir_all
#-----| -> use ...::exists
#-----| -> use ...::hard_link
#-----| -> use ...::metadata
#-----| -> use ...::read
#-----| -> use ...::read_dir
#-----| -> use ...::read_link
#-----| -> use ...::read_to_string
#-----| -> use ...::remove_dir
#-----| -> use ...::remove_dir_all
#-----| -> use ...::remove_file
#-----| -> use ...::rename
#-----| -> use ...::set_permissions
#-----| -> use ...::soft_link
#-----| -> use ...::symlink_metadata
#-----| -> use ...::write

#-----| struct X_List

#-----| trait AsString
#-----| -> fn as_string

#-----| use ...::DirBuilder

#-----| use ...::DirEntry

#-----| use ...::File

#-----| use ...::FileTimes

#-----| use ...::FileType

#-----| use ...::Metadata

#-----| use ...::OpenOptions

#-----| use ...::PathBuf

#-----| use ...::Permissions

#-----| use ...::ReadDir

#-----| use ...::canonicalize

#-----| use ...::copy

#-----| use ...::create_dir

#-----| use ...::create_dir as mkdir

#-----| use ...::create_dir_all

#-----| use ...::exists

#-----| use ...::hard_link

#-----| use ...::metadata

#-----| use ...::read

#-----| use ...::read_dir

#-----| use ...::read_link

#-----| use ...::read_to_string

#-----| use ...::remove_dir

#-----| use ...::remove_dir_all

#-----| use ...::remove_file

#-----| use ...::rename

#-----| use ...::set_permissions

#-----| use ...::soft_link

#-----| use ...::symlink_metadata

#-----| use ...::write
Loading