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 9 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.

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 @@ -14,6 +14,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
Loading