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: extract types #18000

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ use_repo(py_deps, "vendor__anyhow-1.0.44", "vendor__cc-1.0.70", "vendor__clap-2.
# deps for ruby+rust
# keep in sync by running `misc/bazel/3rdparty/update_cargo_deps.sh`
tree_sitter_extractors_deps = use_extension("//misc/bazel/3rdparty:tree_sitter_extractors_extension.bzl", "r")
use_repo(tree_sitter_extractors_deps, "vendor__anyhow-1.0.93", "vendor__argfile-0.2.1", "vendor__chrono-0.4.38", "vendor__clap-4.5.20", "vendor__encoding-0.2.33", "vendor__figment-0.10.19", "vendor__flate2-1.0.34", "vendor__glob-0.3.1", "vendor__globset-0.4.15", "vendor__itertools-0.10.5", "vendor__itertools-0.13.0", "vendor__lazy_static-1.5.0", "vendor__log-0.4.22", "vendor__num-traits-0.2.19", "vendor__num_cpus-1.16.0", "vendor__proc-macro2-1.0.89", "vendor__quote-1.0.37", "vendor__ra_ap_base_db-0.0.232", "vendor__ra_ap_cfg-0.0.232", "vendor__ra_ap_hir-0.0.232", "vendor__ra_ap_hir_def-0.0.232", "vendor__ra_ap_hir_expand-0.0.232", "vendor__ra_ap_ide_db-0.0.232", "vendor__ra_ap_intern-0.0.232", "vendor__ra_ap_load-cargo-0.0.232", "vendor__ra_ap_parser-0.0.232", "vendor__ra_ap_paths-0.0.232", "vendor__ra_ap_project_model-0.0.232", "vendor__ra_ap_span-0.0.232", "vendor__ra_ap_syntax-0.0.232", "vendor__ra_ap_vfs-0.0.232", "vendor__rand-0.8.5", "vendor__rayon-1.10.0", "vendor__regex-1.11.1", "vendor__serde-1.0.214", "vendor__serde_json-1.0.132", "vendor__serde_with-3.11.0", "vendor__stderrlog-0.6.0", "vendor__syn-2.0.87", "vendor__tracing-0.1.40", "vendor__tracing-subscriber-0.3.18", "vendor__tree-sitter-0.24.4", "vendor__tree-sitter-embedded-template-0.23.2", "vendor__tree-sitter-json-0.24.8", "vendor__tree-sitter-ql-0.23.1", "vendor__tree-sitter-ruby-0.23.1", "vendor__triomphe-0.1.14", "vendor__ungrammar-1.16.1")
use_repo(tree_sitter_extractors_deps, "vendor__anyhow-1.0.93", "vendor__argfile-0.2.1", "vendor__chrono-0.4.38", "vendor__clap-4.5.20", "vendor__encoding-0.2.33", "vendor__figment-0.10.19", "vendor__flate2-1.0.34", "vendor__glob-0.3.1", "vendor__globset-0.4.15", "vendor__itertools-0.10.5", "vendor__itertools-0.13.0", "vendor__lazy_static-1.5.0", "vendor__log-0.4.22", "vendor__num-traits-0.2.19", "vendor__num_cpus-1.16.0", "vendor__proc-macro2-1.0.89", "vendor__quote-1.0.37", "vendor__ra_ap_base_db-0.0.232", "vendor__ra_ap_cfg-0.0.232", "vendor__ra_ap_hir-0.0.232", "vendor__ra_ap_hir_def-0.0.232", "vendor__ra_ap_hir_expand-0.0.232", "vendor__ra_ap_hir_ty-0.0.232", "vendor__ra_ap_ide_db-0.0.232", "vendor__ra_ap_intern-0.0.232", "vendor__ra_ap_load-cargo-0.0.232", "vendor__ra_ap_parser-0.0.232", "vendor__ra_ap_paths-0.0.232", "vendor__ra_ap_project_model-0.0.232", "vendor__ra_ap_span-0.0.232", "vendor__ra_ap_syntax-0.0.232", "vendor__ra_ap_vfs-0.0.232", "vendor__rand-0.8.5", "vendor__rayon-1.10.0", "vendor__regex-1.11.1", "vendor__serde-1.0.214", "vendor__serde_json-1.0.132", "vendor__serde_with-3.11.0", "vendor__stderrlog-0.6.0", "vendor__syn-2.0.87", "vendor__tracing-0.1.40", "vendor__tracing-subscriber-0.3.18", "vendor__tree-sitter-0.24.4", "vendor__tree-sitter-embedded-template-0.23.2", "vendor__tree-sitter-json-0.24.8", "vendor__tree-sitter-ql-0.23.1", "vendor__tree-sitter-ruby-0.23.1", "vendor__triomphe-0.1.14", "vendor__ungrammar-1.16.1")

dotnet = use_extension("@rules_dotnet//dotnet:extensions.bzl", "dotnet")
dotnet.toolchain(dotnet_version = "9.0.100")
Expand Down
6 changes: 6 additions & 0 deletions misc/bazel/3rdparty/tree_sitter_extractors_deps/BUILD.bazel

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

2 changes: 2 additions & 0 deletions misc/bazel/3rdparty/tree_sitter_extractors_deps/defs.bzl

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

30 changes: 18 additions & 12 deletions rust/ast-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,10 @@ use ra_ap_syntax::ast::{{
use ra_ap_syntax::{{ast, AstNode}};

impl Translator<'_> {{
fn emit_else_branch(&mut self, node: ast::ElseBranch) -> Label<generated::Expr> {{
fn emit_else_branch(&mut self, node: &ast::ElseBranch) -> Label<generated::Expr> {{
match node {{
ast::ElseBranch::IfExpr(inner) => self.emit_if_expr(inner).into(),
ast::ElseBranch::Block(inner) => self.emit_block_expr(inner).into(),
ast::ElseBranch::IfExpr(ref inner) => self.emit_if_expr(inner).into(),
ast::ElseBranch::Block(ref inner) => self.emit_block_expr(inner).into(),
}}
}}\n"
)?;
Expand All @@ -465,22 +465,28 @@ impl Translator<'_> {{

writeln!(
buf,
" pub(crate) fn emit_{}(&mut self, node: ast::{}) -> Label<generated::{}> {{",
" #[allow(clippy::let_and_return)] pub(crate) fn emit_{}(&mut self, node: &ast::{}) -> Label<generated::{}> {{",
to_lower_snake_case(type_name),
type_name,
class_name
)?;
writeln!(buf, " match node {{")?;
writeln!(buf, " let label = match node {{")?;
for variant in &node.variants {
writeln!(
buf,
" ast::{}::{}(inner) => self.emit_{}(inner).into(),",
" ast::{}::{}(ref inner) => self.emit_{}(inner).into(),",
type_name,
variant,
to_lower_snake_case(variant)
)?;
}
writeln!(buf, " }}")?;
writeln!(buf, " }};")?;
writeln!(
buf,
" emit_detached!({}, self, node, label);",
class_name
)?;
writeln!(buf, " label")?;
writeln!(buf, " }}\n")?;
}

Expand All @@ -490,7 +496,7 @@ impl Translator<'_> {{

writeln!(
buf,
" pub(crate) fn emit_{}(&mut self, node: ast::{}) -> Label<generated::{}> {{",
" pub(crate) fn emit_{}(&mut self, node: &ast::{}) -> Label<generated::{}> {{",
to_lower_snake_case(type_name),
type_name,
class_name
Expand Down Expand Up @@ -519,15 +525,15 @@ impl Translator<'_> {{
} else if field.is_many {
writeln!(
buf,
" let {} = node.{}().map(|x| self.emit_{}(x)).collect();",
" let {} = node.{}().map(|x| self.emit_{}(&x)).collect();",
class_field_name,
struct_field_name,
to_lower_snake_case(type_name)
)?;
} else {
writeln!(
buf,
" let {} = node.{}().map(|x| self.emit_{}(x));",
" let {} = node.{}().map(|x| self.emit_{}(&x));",
class_field_name,
struct_field_name,
to_lower_snake_case(type_name)
Expand All @@ -549,15 +555,15 @@ impl Translator<'_> {{
writeln!(buf, " {},", class_field_name)?;
}
writeln!(buf, " }});")?;
writeln!(buf, " self.emit_location(label, &node);")?;
writeln!(buf, " self.emit_location(label, node);")?;
writeln!(
buf,
" emit_detached!({}, self, node, label);",
class_name
)?;
writeln!(
buf,
" self.emit_tokens(&node, label.into(), node.syntax().children_with_tokens());"
" self.emit_tokens(node, label.into(), node.syntax().children_with_tokens());"
)?;
writeln!(buf, " label")?;

Expand Down
1 change: 1 addition & 0 deletions rust/extractor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ra_ap_hir = "0.0.232"
ra_ap_hir_def = "0.0.232"
ra_ap_ide_db = "0.0.232"
ra_ap_hir_expand = "0.0.232"
ra_ap_hir_ty = "0.0.232"
ra_ap_load-cargo = "0.0.232"
ra_ap_paths = "0.0.232"
ra_ap_project_model = "0.0.232"
Expand Down
2 changes: 1 addition & 1 deletion rust/extractor/src/generated/.generated.list

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

12 changes: 12 additions & 0 deletions rust/extractor/src/generated/top.rs

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

2 changes: 1 addition & 1 deletion rust/extractor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Extractor<'_> {
no_location,
);
}
translator.emit_source_file(ast);
translator.emit_source_file(&ast);
translator.trap.commit().unwrap_or_else(|err| {
log::error!(
"Failed to write trap file for: {}: {}",
Expand Down
98 changes: 82 additions & 16 deletions rust/extractor/src/translate/base.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::mappings::{AddressableAst, AddressableHir};
use crate::generated::MacroCall;
use crate::generated::{self};
use crate::generated::{Expr, MacroCall, Pat};
use crate::rust_analyzer::FileSemanticInformation;
use crate::trap::{DiagnosticSeverity, TrapFile, TrapId};
use crate::trap::{Label, TrapClass};
Expand All @@ -10,7 +10,10 @@ use log::Level;
use ra_ap_base_db::salsa::InternKey;
use ra_ap_base_db::CrateOrigin;
use ra_ap_hir::db::ExpandDatabase;
use ra_ap_hir::{Adt, Crate, ItemContainer, Module, ModuleDef, PathResolution, Semantics, Type};
use ra_ap_hir::{
Adt, Crate, HirDisplay, ItemContainer, Module, ModuleDef, PathResolution, Semantics, Type,
TypeInfo,
};
use ra_ap_hir_def::type_ref::Mutability;
use ra_ap_hir_def::ModuleId;
use ra_ap_hir_expand::ExpandTo;
Expand All @@ -26,33 +29,39 @@ use ra_ap_syntax::{

#[macro_export]
macro_rules! emit_detached {
(Expr, $self:ident, $node:ident, $label:ident) => {
$self.extract_expr_type($node, $label);
};
(Pat, $self:ident, $node:ident, $label:ident) => {
$self.extract_pat_type($node, $label);
};
(MacroCall, $self:ident, $node:ident, $label:ident) => {
$self.extract_macro_call_expanded(&$node, $label);
$self.extract_macro_call_expanded($node, $label);
};
(Function, $self:ident, $node:ident, $label:ident) => {
$self.extract_canonical_origin(&$node, $label.into());
$self.extract_canonical_origin($node, $label.into());
};
(Trait, $self:ident, $node:ident, $label:ident) => {
$self.extract_canonical_origin(&$node, $label.into());
$self.extract_canonical_origin($node, $label.into());
};
(Struct, $self:ident, $node:ident, $label:ident) => {
$self.extract_canonical_origin(&$node, $label.into());
$self.extract_canonical_origin($node, $label.into());
};
(Enum, $self:ident, $node:ident, $label:ident) => {
$self.extract_canonical_origin(&$node, $label.into());
$self.extract_canonical_origin($node, $label.into());
};
(Union, $self:ident, $node:ident, $label:ident) => {
$self.extract_canonical_origin(&$node, $label.into());
$self.extract_canonical_origin($node, $label.into());
};
(Module, $self:ident, $node:ident, $label:ident) => {
$self.extract_canonical_origin(&$node, $label.into());
$self.extract_canonical_origin($node, $label.into());
};
// TODO canonical origin of other items
(Path, $self:ident, $node:ident, $label:ident) => {
$self.extract_canonical_destination(&$node, $label);
$self.extract_canonical_destination($node, $label);
};
(MethodCallExpr, $self:ident, $node:ident, $label:ident) => {
$self.extract_method_canonical_destination(&$node, $label);
$self.extract_method_canonical_destination($node, $label);
};
($($_:tt)*) => {};
}
Expand Down Expand Up @@ -252,23 +261,80 @@ impl<'a> Translator<'a> {
) -> Option<Label<generated::AstNode>> {
match expand_to {
ra_ap_hir_expand::ExpandTo::Statements => {
ast::MacroStmts::cast(expanded).map(|x| self.emit_macro_stmts(x).into())
ast::MacroStmts::cast(expanded).map(|x| self.emit_macro_stmts(&x).into())
}
ra_ap_hir_expand::ExpandTo::Items => {
ast::MacroItems::cast(expanded).map(|x| self.emit_macro_items(x).into())
ast::MacroItems::cast(expanded).map(|x| self.emit_macro_items(&x).into())
}

ra_ap_hir_expand::ExpandTo::Pattern => {
ast::Pat::cast(expanded).map(|x| self.emit_pat(x).into())
ast::Pat::cast(expanded).map(|x| self.emit_pat(&x).into())
}
ra_ap_hir_expand::ExpandTo::Type => {
ast::Type::cast(expanded).map(|x| self.emit_type(x).into())
ast::Type::cast(expanded).map(|x| self.emit_type(&x).into())
}
ra_ap_hir_expand::ExpandTo::Expr => {
ast::Expr::cast(expanded).map(|x| self.emit_expr(x).into())
ast::Expr::cast(expanded).map(|x| self.emit_expr(&x).into())
}
}
}
pub(crate) fn extract_expr_type(&mut self, expr: &ast::Expr, label: Label<generated::Expr>) {
if let Some(semantics) = self.semantics {
if let Some(tp) = semantics.type_of_expr(expr) {
Expr::emit_type(
label,
Translator::display_type(semantics, tp),
&mut self.trap.writer,
)
} else {
let text_range = self
.text_range_for_node(expr)
.unwrap_or(TextRange::empty(0.into()));
self.emit_diagnostic(
DiagnosticSeverity::Info,
"types".to_owned(),
"missing type for expression".to_owned(),
"missing type for expression".to_owned(),
self.location(text_range),
);
}
}
}
pub(crate) fn extract_pat_type(&mut self, pat: &ast::Pat, label: Label<generated::Pat>) {
if let Some(semantics) = self.semantics {
if let Some(tp) = semantics.type_of_pat(pat) {
Pat::emit_type(
label,
Translator::display_type(semantics, tp),
&mut self.trap.writer,
)
} else {
let text_range = self
.text_range_for_node(pat)
.unwrap_or(TextRange::empty(0.into()));
self.emit_diagnostic(
DiagnosticSeverity::Info,
"types".to_owned(),
"missing type for pattern".to_owned(),
"missing type for pattern".to_owned(),
self.location(text_range),
);
}
}
}
fn display_type(semantics: &Semantics<'_, RootDatabase>, tp: TypeInfo) -> String {
let tp = tp.original();
let display = tp.into_displayable(
semantics.db,
None,
None,
false,
ra_ap_hir_ty::display::DisplayTarget::Test,
ra_ap_hir_ty::display::ClosureStyle::ImplFn,
true,
);
format!("{display}")
}
pub(crate) fn extract_macro_call_expanded(
&mut self,
mcall: &ast::MacroCall,
Expand Down
Loading