Skip to content

Commit 989c06b

Browse files
bors[bot]Veykril
andauthored
Merge #11171
11171: fix: Fix tool module classification not working correctly r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 35737eb + 762a3b3 commit 989c06b

File tree

3 files changed

+23
-33
lines changed

3 files changed

+23
-33
lines changed

crates/hir/src/source_analyzer.rs

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,7 @@ impl SourceAnalyzer {
290290
return Some(PathResolution::Def(ModuleDef::Variant(variant.into())));
291291
}
292292
prefer_value_ns = true;
293-
}
294-
295-
if let Some(path_pat) = parent().and_then(ast::PathPat::cast) {
293+
} else if let Some(path_pat) = parent().and_then(ast::PathPat::cast) {
296294
let pat_id = self.pat_id(&path_pat.into())?;
297295
if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_pat(pat_id) {
298296
return Some(PathResolution::AssocItem(assoc.into()));
@@ -302,9 +300,7 @@ impl SourceAnalyzer {
302300
{
303301
return Some(PathResolution::Def(ModuleDef::Variant(variant.into())));
304302
}
305-
}
306-
307-
if let Some(rec_lit) = parent().and_then(ast::RecordExpr::cast) {
303+
} else if let Some(rec_lit) = parent().and_then(ast::RecordExpr::cast) {
308304
let expr_id = self.expr_id(db, &rec_lit.into())?;
309305
if let Some(VariantId::EnumVariantId(variant)) =
310306
self.infer.as_ref()?.variant_resolution_for_expr(expr_id)
@@ -331,32 +327,34 @@ impl SourceAnalyzer {
331327
// Case where path is a qualifier of a use tree, e.g. foo::bar::{Baz, Qux} where we are
332328
// trying to resolve foo::bar.
333329
if let Some(use_tree) = parent().and_then(ast::UseTree::cast) {
334-
if let Some(qualifier) = use_tree.path() {
335-
if path == &qualifier && use_tree.coloncolon_token().is_some() {
336-
return resolve_hir_path_qualifier(db, &self.resolver, &hir_path);
337-
}
330+
if use_tree.coloncolon_token().is_some() {
331+
return resolve_hir_path_qualifier(db, &self.resolver, &hir_path);
338332
}
339333
}
340334

341335
let is_path_of_attr = path
342-
.top_path()
343336
.syntax()
344337
.ancestors()
345-
.nth(2) // Path -> Meta -> Attr
346-
.map_or(false, |it| ast::Attr::can_cast(it.kind()));
338+
.map(|it| it.kind())
339+
.take_while(|&kind| ast::Path::can_cast(kind) || ast::Meta::can_cast(kind))
340+
.last()
341+
.map_or(false, ast::Meta::can_cast);
347342

348343
// Case where path is a qualifier of another path, e.g. foo::bar::Baz where we are
349344
// trying to resolve foo::bar.
350-
if let Some(outer_path) = path.parent_path() {
351-
if let Some(qualifier) = outer_path.qualifier() {
352-
if path == &qualifier {
353-
return resolve_hir_path_qualifier(db, &self.resolver, &hir_path);
345+
if path.parent_path().is_some() {
346+
return match resolve_hir_path_qualifier(db, &self.resolver, &hir_path) {
347+
None if is_path_of_attr => {
348+
path.first_segment().and_then(|it| it.name_ref()).and_then(|name_ref| {
349+
ToolModule::by_name(&name_ref.text()).map(PathResolution::ToolModule)
350+
})
354351
}
355-
}
352+
res => res,
353+
};
356354
} else if is_path_of_attr {
357355
// Case where we are resolving the final path segment of a path in an attribute
358356
// in this case we have to check for inert/builtin attributes and tools and prioritize
359-
// resolution of attributes over other namesapces
357+
// resolution of attributes over other namespaces
360358
let name_ref = path.as_single_name_ref();
361359
let builtin =
362360
name_ref.as_ref().map(ast::NameRef::text).as_deref().and_then(BuiltinAttr::by_name);
@@ -365,27 +363,17 @@ impl SourceAnalyzer {
365363
}
366364
return match resolve_hir_path_as_macro(db, &self.resolver, &hir_path) {
367365
res @ Some(m) if m.is_attr() => res.map(PathResolution::Macro),
368-
_ => name_ref.and_then(|name_ref| {
366+
// this labels any path that starts with a tool module as the tool itself, this is technically wrong
367+
// but there is no benefit in differentiating these two cases for the time being
368+
_ => path.first_segment().and_then(|it| it.name_ref()).and_then(|name_ref| {
369369
ToolModule::by_name(&name_ref.text()).map(PathResolution::ToolModule)
370370
}),
371371
};
372372
}
373-
374-
let res = if parent().map_or(false, |it| ast::Visibility::can_cast(it.kind())) {
373+
if parent().map_or(false, |it| ast::Visibility::can_cast(it.kind())) {
375374
resolve_hir_path_qualifier(db, &self.resolver, &hir_path)
376375
} else {
377376
resolve_hir_path_(db, &self.resolver, &hir_path, prefer_value_ns)
378-
};
379-
match res {
380-
Some(_) => res,
381-
// this labels any path that starts with a tool module as the tool itself, this is technically wrong
382-
// but there is no benefit in differentiating these two cases for the time being
383-
None if is_path_of_attr => path
384-
.first_segment()
385-
.and_then(|seg| seg.name_ref())
386-
.and_then(|name_ref| ToolModule::by_name(&name_ref.text()))
387-
.map(PathResolution::ToolModule),
388-
None => None,
389377
}
390378
}
391379

crates/ide/src/syntax_highlighting/test_data/highlighting.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<span class="keyword">mod</span> <span class="module declaration">inner</span> <span class="brace">{</span><span class="brace">}</span>
4545

4646
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute library">allow</span><span class="parenthesis attribute">(</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span>
47+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="tool_module attribute library">rustfmt</span><span class="operator attribute">::</span><span class="tool_module attribute library">skip</span><span class="attribute_bracket attribute">]</span>
4748
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="module attribute crate_root library">proc_macros</span><span class="operator attribute">::</span><span class="attribute attribute library">identity</span><span class="attribute_bracket attribute">]</span>
4849
<span class="keyword">pub</span> <span class="keyword">mod</span> <span class="module declaration public">ops</span> <span class="brace">{</span>
4950
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute library">lang</span> <span class="operator attribute">=</span> <span class="string_literal attribute">"fn_once"</span><span class="attribute_bracket attribute">]</span>

crates/ide/src/syntax_highlighting/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use inner::{self as inner_mod};
1717
mod inner {}
1818
1919
#[allow()]
20+
#[rustfmt::skip]
2021
#[proc_macros::identity]
2122
pub mod ops {
2223
#[lang = "fn_once"]

0 commit comments

Comments
 (0)