Skip to content

Commit 6da8853

Browse files
committed
Rust: extract types for expressions and patterns
1 parent 80a6485 commit 6da8853

File tree

1 file changed

+68
-2
lines changed
  • rust/extractor/src/translate

1 file changed

+68
-2
lines changed

rust/extractor/src/translate/base.rs

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::mappings::{AddressableAst, AddressableHir};
2-
use crate::generated::MacroCall;
32
use crate::generated::{self};
3+
use crate::generated::{Expr, MacroCall, Pat};
44
use crate::rust_analyzer::FileSemanticInformation;
55
use crate::trap::{DiagnosticSeverity, TrapFile, TrapId};
66
use crate::trap::{Label, TrapClass};
@@ -10,7 +10,10 @@ use log::Level;
1010
use ra_ap_base_db::salsa::InternKey;
1111
use ra_ap_base_db::CrateOrigin;
1212
use ra_ap_hir::db::ExpandDatabase;
13-
use ra_ap_hir::{Adt, Crate, ItemContainer, Module, ModuleDef, PathResolution, Semantics, Type};
13+
use ra_ap_hir::{
14+
Adt, Crate, HirDisplay, ItemContainer, Module, ModuleDef, PathResolution, Semantics, Type,
15+
TypeInfo,
16+
};
1417
use ra_ap_hir_def::type_ref::Mutability;
1518
use ra_ap_hir_def::ModuleId;
1619
use ra_ap_hir_expand::ExpandTo;
@@ -26,6 +29,12 @@ use ra_ap_syntax::{
2629

2730
#[macro_export]
2831
macro_rules! emit_detached {
32+
(Expr, $self:ident, $node:ident, $label:ident) => {
33+
$self.extract_expr_type($node, $label);
34+
};
35+
(Pat, $self:ident, $node:ident, $label:ident) => {
36+
$self.extract_pat_type($node, $label);
37+
};
2938
(MacroCall, $self:ident, $node:ident, $label:ident) => {
3039
$self.extract_macro_call_expanded($node, $label);
3140
};
@@ -269,6 +278,63 @@ impl<'a> Translator<'a> {
269278
}
270279
}
271280
}
281+
pub(crate) fn extract_expr_type(&mut self, expr: &ast::Expr, label: Label<generated::Expr>) {
282+
if let Some(semantics) = self.semantics {
283+
if let Some(tp) = semantics.type_of_expr(expr) {
284+
Expr::emit_type(
285+
label,
286+
Translator::display_type(semantics, tp),
287+
&mut self.trap.writer,
288+
)
289+
} else {
290+
let text_range = self
291+
.text_range_for_node(expr)
292+
.unwrap_or(TextRange::empty(0.into()));
293+
self.emit_diagnostic(
294+
DiagnosticSeverity::Info,
295+
"types".to_owned(),
296+
"missing type for expression".to_owned(),
297+
"missing type for expression".to_owned(),
298+
self.location(text_range),
299+
);
300+
}
301+
}
302+
}
303+
pub(crate) fn extract_pat_type(&mut self, pat: &ast::Pat, label: Label<generated::Pat>) {
304+
if let Some(semantics) = self.semantics {
305+
if let Some(tp) = semantics.type_of_pat(pat) {
306+
Pat::emit_type(
307+
label,
308+
Translator::display_type(semantics, tp),
309+
&mut self.trap.writer,
310+
)
311+
} else {
312+
let text_range = self
313+
.text_range_for_node(pat)
314+
.unwrap_or(TextRange::empty(0.into()));
315+
self.emit_diagnostic(
316+
DiagnosticSeverity::Info,
317+
"types".to_owned(),
318+
"missing type for pattern".to_owned(),
319+
"missing type for pattern".to_owned(),
320+
self.location(text_range),
321+
);
322+
}
323+
}
324+
}
325+
fn display_type(semantics: &Semantics<'_, RootDatabase>, tp: TypeInfo) -> String {
326+
let tp = tp.original();
327+
let display = tp.into_displayable(
328+
semantics.db,
329+
None,
330+
None,
331+
false,
332+
ra_ap_hir_ty::display::DisplayTarget::Test,
333+
ra_ap_hir_ty::display::ClosureStyle::ImplFn,
334+
true,
335+
);
336+
format!("{display}")
337+
}
272338
pub(crate) fn extract_macro_call_expanded(
273339
&mut self,
274340
mcall: &ast::MacroCall,

0 commit comments

Comments
 (0)