Skip to content

Commit 9cf2420

Browse files
author
Paolo Tranquilli
committed
Rust: restrict canonical path calculations
1 parent 647515b commit 9cf2420

File tree

73 files changed

+583
-327
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+583
-327
lines changed

rust/extractor/src/generated/.generated.list

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/extractor/src/generated/top.rs

Lines changed: 108 additions & 63 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/extractor/src/translate/base.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::mappings::{AddressableAst, AddressableHir};
1+
use super::mappings::{AddressableAst, AddressableHir, PathAst};
22
use crate::generated::MacroCall;
33
use crate::generated::{self};
44
use crate::rust_analyzer::FileSemanticInformation;
@@ -53,8 +53,20 @@ macro_rules! emit_detached {
5353
$self.extract_canonical_origin_of_enum_variant(&$node, $label);
5454
};
5555
// TODO canonical origin of other items
56-
(Path, $self:ident, $node:ident, $label:ident) => {
57-
$self.extract_canonical_destination(&$node, $label);
56+
(PathExpr, $self:ident, $node:ident, $label:ident) => {
57+
$self.extract_path_canonical_destination(&$node, $label.into());
58+
};
59+
(RecordExpr, $self:ident, $node:ident, $label:ident) => {
60+
$self.extract_path_canonical_destination(&$node, $label.into());
61+
};
62+
(PathPat, $self:ident, $node:ident, $label:ident) => {
63+
$self.extract_path_canonical_destination(&$node, $label.into());
64+
};
65+
(RecordPat, $self:ident, $node:ident, $label:ident) => {
66+
$self.extract_path_canonical_destination(&$node, $label.into());
67+
};
68+
(TupleStructPat, $self:ident, $node:ident, $label:ident) => {
69+
$self.extract_path_canonical_destination(&$node, $label.into());
5870
};
5971
(MethodCallExpr, $self:ident, $node:ident, $label:ident) => {
6072
$self.extract_method_canonical_destination(&$node, $label);
@@ -506,25 +518,22 @@ impl<'a> Translator<'a> {
506518
})();
507519
}
508520

509-
pub(crate) fn extract_canonical_destination(
521+
pub(crate) fn extract_path_canonical_destination(
510522
&mut self,
511-
item: &ast::Path,
512-
label: Label<generated::Path>,
523+
item: &impl PathAst,
524+
label: Label<generated::Resolvable>,
513525
) {
514526
(|| {
527+
let path = item.path()?;
515528
let sema = self.semantics.as_ref()?;
516-
let resolution = sema.resolve_path(item)?;
529+
let resolution = sema.resolve_path(&path)?;
517530
let PathResolution::Def(def) = resolution else {
518531
return None;
519532
};
520533
let origin = self.origin_from_module_def(def)?;
521534
let path = self.canonical_path_from_module_def(def)?;
522-
generated::Resolvable::emit_resolved_crate_origin(
523-
label.into(),
524-
origin,
525-
&mut self.trap.writer,
526-
);
527-
generated::Resolvable::emit_resolved_path(label.into(), path, &mut self.trap.writer);
535+
generated::Resolvable::emit_resolved_crate_origin(label, origin, &mut self.trap.writer);
536+
generated::Resolvable::emit_resolved_path(label, path, &mut self.trap.writer);
528537
Some(())
529538
})();
530539
}

rust/extractor/src/translate/mappings.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,37 @@ impl AddressableAst for ast::Union {
170170
impl AddressableAst for ast::Module {
171171
type Hir = Module;
172172
}
173+
174+
pub trait PathAst: AstNode {
175+
fn path(&self) -> Option<ast::Path>;
176+
}
177+
178+
impl PathAst for ast::PathExpr {
179+
fn path(&self) -> Option<ast::Path> {
180+
self.path()
181+
}
182+
}
183+
184+
impl PathAst for ast::RecordExpr {
185+
fn path(&self) -> Option<ast::Path> {
186+
self.path()
187+
}
188+
}
189+
190+
impl PathAst for ast::PathPat {
191+
fn path(&self) -> Option<ast::Path> {
192+
self.path()
193+
}
194+
}
195+
196+
impl PathAst for ast::RecordPat {
197+
fn path(&self) -> Option<ast::Path> {
198+
self.path()
199+
}
200+
}
201+
202+
impl PathAst for ast::TupleStructPat {
203+
fn path(&self) -> Option<ast::Path> {
204+
self.path()
205+
}
206+
}

0 commit comments

Comments
 (0)