Skip to content

Commit cf72b62

Browse files
committed
Resolve $crate in derive paths
1 parent 2400b36 commit cf72b62

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

crates/hir-def/src/nameres/collector.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use hir_expand::{
1414
builtin_attr_macro::find_builtin_attr,
1515
builtin_derive_macro::find_builtin_derive,
1616
builtin_fn_macro::find_builtin_macro,
17+
hygiene::Hygiene,
1718
name::{name, AsName, Name},
1819
proc_macro::ProcMacroExpander,
1920
ExpandResult, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroCallLoc,
@@ -312,13 +313,14 @@ impl DefCollector<'_> {
312313
}
313314

314315
if *attr_name == hir_expand::name![feature] {
315-
let features =
316-
attr.parse_path_comma_token_tree().into_iter().flatten().filter_map(
317-
|feat| match feat.segments() {
318-
[name] => Some(name.to_smol_str()),
319-
_ => None,
320-
},
321-
);
316+
let features = attr
317+
.parse_path_comma_token_tree(self.db.upcast(), Hygiene::new_unhygienic())
318+
.into_iter()
319+
.flatten()
320+
.filter_map(|feat| match feat.segments() {
321+
[name] => Some(name.to_smol_str()),
322+
_ => None,
323+
});
322324
self.def_map.unstable_features.extend(features);
323325
}
324326

@@ -1223,8 +1225,9 @@ impl DefCollector<'_> {
12231225
}
12241226
};
12251227
let ast_id = ast_id.with_value(ast_adt_id);
1228+
let hygiene = Hygiene::new(self.db.upcast(), file_id);
12261229

1227-
match attr.parse_path_comma_token_tree() {
1230+
match attr.parse_path_comma_token_tree(self.db.upcast(), hygiene) {
12281231
Some(derive_macros) => {
12291232
let mut len = 0;
12301233
for (idx, path) in derive_macros.enumerate() {

crates/hir-def/src/nameres/tests/macros.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,29 @@ pub struct bar;
664664
);
665665
}
666666

667+
#[test]
668+
fn macro_dollar_crate_is_correct_in_derive_meta() {
669+
let map = compute_crate_def_map(
670+
r#"
671+
//- minicore: derive, clone
672+
//- /main.rs crate:main deps:lib
673+
lib::foo!();
674+
675+
//- /lib.rs crate:lib
676+
#[macro_export]
677+
macro_rules! foo {
678+
() => {
679+
#[derive($crate::Clone)]
680+
struct S;
681+
}
682+
}
683+
684+
pub use core::clone::Clone;
685+
"#,
686+
);
687+
assert_eq!(map.modules[map.root].scope.impls().len(), 1);
688+
}
689+
667690
#[test]
668691
fn expand_derive() {
669692
let map = compute_crate_def_map(

crates/hir-expand/src/attrs.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ use syntax::{ast, match_ast, AstNode, SmolStr, SyntaxNode};
1212
use crate::{
1313
db::ExpandDatabase,
1414
hygiene::Hygiene,
15-
mod_path::{ModPath, PathKind},
16-
name::AsName,
15+
mod_path::ModPath,
1716
tt::{self, Subtree},
1817
InFile,
1918
};
@@ -267,7 +266,11 @@ impl Attr {
267266
}
268267

269268
/// Parses this attribute as a token tree consisting of comma separated paths.
270-
pub fn parse_path_comma_token_tree(&self) -> Option<impl Iterator<Item = ModPath> + '_> {
269+
pub fn parse_path_comma_token_tree<'a>(
270+
&'a self,
271+
db: &'a dyn ExpandDatabase,
272+
hygiene: Hygiene,
273+
) -> Option<impl Iterator<Item = ModPath> + 'a> {
271274
let args = self.token_tree_value()?;
272275

273276
if args.delimiter.kind != DelimiterKind::Parenthesis {
@@ -276,15 +279,25 @@ impl Attr {
276279
let paths = args
277280
.token_trees
278281
.split(|tt| matches!(tt, tt::TokenTree::Leaf(tt::Leaf::Punct(Punct { char: ',', .. }))))
279-
.filter_map(|tts| {
282+
.filter_map(move |tts| {
280283
if tts.is_empty() {
281284
return None;
282285
}
283-
let segments = tts.iter().filter_map(|tt| match tt {
284-
tt::TokenTree::Leaf(tt::Leaf::Ident(id)) => Some(id.as_name()),
285-
_ => None,
286-
});
287-
Some(ModPath::from_segments(PathKind::Plain, segments))
286+
// FIXME: This is necessarily a hack. It'd be nice if we could avoid allocation here.
287+
let subtree = tt::Subtree {
288+
delimiter: tt::Delimiter::unspecified(),
289+
token_trees: tts.into_iter().cloned().collect(),
290+
};
291+
let (parse, _) =
292+
mbe::token_tree_to_syntax_node(&subtree, mbe::TopEntryPoint::MetaItem);
293+
let meta = ast::Meta::cast(parse.syntax_node())?;
294+
// Only simple paths are allowed.
295+
if meta.eq_token().is_some() || meta.expr().is_some() || meta.token_tree().is_some()
296+
{
297+
return None;
298+
}
299+
let path = meta.path()?;
300+
ModPath::from_src(db, path, &hygiene)
288301
});
289302

290303
Some(paths)

0 commit comments

Comments
 (0)