Skip to content

Commit dbb702c

Browse files
committed
internal: remove accidental code re-use
FragmentKind played two roles: * entry point to the parser * syntactic category of a macro call These are different use-cases, and warrant different types. For example, macro can't expand to visibility, but we have such fragment today. This PR introduces `ExpandsTo` enum to separate this two use-cases. I suspect we might further split `FragmentKind` into `$x:specifier` enum specific to MBE, and a general parser entry point, but that's for another PR!
1 parent 847d0fa commit dbb702c

File tree

18 files changed

+172
-139
lines changed

18 files changed

+172
-139
lines changed

Cargo.lock

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

crates/hir_def/src/attr.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,7 @@ impl Attr {
695695
hygiene: &Hygiene,
696696
id: AttrId,
697697
) -> Option<Attr> {
698-
let (parse, _) =
699-
mbe::token_tree_to_syntax_node(tt, hir_expand::FragmentKind::MetaItem).ok()?;
698+
let (parse, _) = mbe::token_tree_to_syntax_node(tt, mbe::FragmentKind::MetaItem).ok()?;
700699
let ast = ast::Meta::cast(parse.syntax_node())?;
701700

702701
Self::from_src(db, ast, hygiene, id)

crates/hir_def/src/item_tree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use hir_expand::{
5151
ast_id_map::FileAstId,
5252
hygiene::Hygiene,
5353
name::{name, AsName, Name},
54-
FragmentKind, HirFileId, InFile,
54+
ExpandTo, HirFileId, InFile,
5555
};
5656
use la_arena::{Arena, Idx, RawIdx};
5757
use profile::Count;
@@ -739,7 +739,7 @@ pub struct MacroCall {
739739
/// Path to the called macro.
740740
pub path: Interned<ModPath>,
741741
pub ast_id: FileAstId<ast::MacroCall>,
742-
pub fragment: FragmentKind,
742+
pub expand_to: ExpandTo,
743743
}
744744

745745
#[derive(Debug, Clone, Eq, PartialEq)]

crates/hir_def/src/item_tree/lower.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,8 @@ impl<'a> Ctx<'a> {
573573
fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>> {
574574
let path = Interned::new(ModPath::from_src(self.db, m.path()?, &self.hygiene)?);
575575
let ast_id = self.source_ast_id_map.ast_id(m);
576-
let fragment = hir_expand::to_fragment_kind(m);
577-
let res = MacroCall { path, ast_id, fragment };
576+
let expand_to = hir_expand::ExpandTo::from_call_site(m);
577+
let res = MacroCall { path, ast_id, expand_to };
578578
Some(id(self.data().macro_calls.alloc(res)))
579579
}
580580

crates/hir_def/src/item_tree/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ impl<'a> Printer<'a> {
440440
}
441441
}
442442
ModItem::MacroCall(it) => {
443-
let MacroCall { path, ast_id: _, fragment: _ } = &self.tree[it];
443+
let MacroCall { path, ast_id: _, expand_to: _ } = &self.tree[it];
444444
wln!(self, "{}!(...);", path);
445445
}
446446
ModItem::MacroRules(it) => {

crates/hir_def/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use hir_expand::{
6161
ast_id_map::FileAstId,
6262
eager::{expand_eager_macro, ErrorEmitted, ErrorSink},
6363
hygiene::Hygiene,
64-
AstId, FragmentKind, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
64+
AstId, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
6565
};
6666
use la_arena::Idx;
6767
use nameres::DefMap;
@@ -667,7 +667,7 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
667667
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
668668
mut error_sink: &mut dyn FnMut(mbe::ExpandError),
669669
) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
670-
let fragment = hir_expand::to_fragment_kind(self.value);
670+
let expands_to = hir_expand::ExpandTo::from_call_site(self.value);
671671
let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
672672
let h = Hygiene::new(db.upcast(), self.file_id);
673673
let path = self.value.path().and_then(|path| path::ModPath::from_src(db, path, &h));
@@ -683,7 +683,7 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
683683

684684
macro_call_as_call_id(
685685
&AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
686-
fragment,
686+
expands_to,
687687
db,
688688
krate,
689689
resolver,
@@ -712,7 +712,7 @@ pub struct UnresolvedMacro {
712712

713713
fn macro_call_as_call_id(
714714
call: &AstIdWithPath<ast::MacroCall>,
715-
fragment: FragmentKind,
715+
expand_to: ExpandTo,
716716
db: &dyn db::DefDatabase,
717717
krate: CrateId,
718718
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
@@ -738,7 +738,7 @@ fn macro_call_as_call_id(
738738
Ok(def.as_lazy_macro(
739739
db.upcast(),
740740
krate,
741-
MacroCallKind::FnLike { ast_id: call.ast_id, fragment },
741+
MacroCallKind::FnLike { ast_id: call.ast_id, expand_to },
742742
))
743743
};
744744
Ok(res)

crates/hir_def/src/nameres/collector.rs

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use hir_expand::{
1414
builtin_macro::find_builtin_macro,
1515
name::{name, AsName, Name},
1616
proc_macro::ProcMacroExpander,
17-
FragmentKind, HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
17+
ExpandTo, HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
1818
};
1919
use hir_expand::{InFile, MacroCallLoc};
2020
use itertools::Itertools;
@@ -223,7 +223,7 @@ struct MacroDirective {
223223

224224
#[derive(Clone, Debug, Eq, PartialEq)]
225225
enum MacroDirectiveKind {
226-
FnLike { ast_id: AstIdWithPath<ast::MacroCall>, fragment: FragmentKind },
226+
FnLike { ast_id: AstIdWithPath<ast::MacroCall>, expand_to: ExpandTo },
227227
Derive { ast_id: AstIdWithPath<ast::Item>, derive_attr: AttrId },
228228
Attr { ast_id: AstIdWithPath<ast::Item>, attr: Attr, mod_item: ModItem },
229229
}
@@ -1021,10 +1021,10 @@ impl DefCollector<'_> {
10211021
};
10221022

10231023
match &directive.kind {
1024-
MacroDirectiveKind::FnLike { ast_id, fragment } => {
1024+
MacroDirectiveKind::FnLike { ast_id, expand_to } => {
10251025
match macro_call_as_call_id(
10261026
ast_id,
1027-
*fragment,
1027+
*expand_to,
10281028
self.db,
10291029
self.def_map.krate,
10301030
&resolver,
@@ -1223,32 +1223,34 @@ impl DefCollector<'_> {
12231223

12241224
for directive in &self.unresolved_macros {
12251225
match &directive.kind {
1226-
MacroDirectiveKind::FnLike { ast_id, fragment } => match macro_call_as_call_id(
1227-
ast_id,
1228-
*fragment,
1229-
self.db,
1230-
self.def_map.krate,
1231-
|path| {
1232-
let resolved_res = self.def_map.resolve_path_fp_with_macro(
1233-
self.db,
1234-
ResolveMode::Other,
1235-
directive.module_id,
1236-
&path,
1237-
BuiltinShadowMode::Module,
1238-
);
1239-
resolved_res.resolved_def.take_macros()
1240-
},
1241-
&mut |_| (),
1242-
) {
1243-
Ok(_) => (),
1244-
Err(UnresolvedMacro { path }) => {
1245-
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
1246-
directive.module_id,
1247-
ast_id.ast_id,
1248-
path,
1249-
));
1226+
MacroDirectiveKind::FnLike { ast_id, expand_to } => {
1227+
match macro_call_as_call_id(
1228+
ast_id,
1229+
*expand_to,
1230+
self.db,
1231+
self.def_map.krate,
1232+
|path| {
1233+
let resolved_res = self.def_map.resolve_path_fp_with_macro(
1234+
self.db,
1235+
ResolveMode::Other,
1236+
directive.module_id,
1237+
&path,
1238+
BuiltinShadowMode::Module,
1239+
);
1240+
resolved_res.resolved_def.take_macros()
1241+
},
1242+
&mut |_| (),
1243+
) {
1244+
Ok(_) => (),
1245+
Err(UnresolvedMacro { path }) => {
1246+
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
1247+
directive.module_id,
1248+
ast_id.ast_id,
1249+
path,
1250+
));
1251+
}
12501252
}
1251-
},
1253+
}
12521254
MacroDirectiveKind::Derive { .. } | MacroDirectiveKind::Attr { .. } => {
12531255
// FIXME: we might want to diagnose this too
12541256
}
@@ -1899,7 +1901,7 @@ impl ModCollector<'_, '_> {
18991901
let mut error = None;
19001902
match macro_call_as_call_id(
19011903
&ast_id,
1902-
mac.fragment,
1904+
mac.expand_to,
19031905
self.def_collector.db,
19041906
self.def_collector.def_map.krate,
19051907
|path| {
@@ -1930,12 +1932,11 @@ impl ModCollector<'_, '_> {
19301932
// Built-in macro failed eager expansion.
19311933

19321934
// FIXME: don't parse the file here
1933-
let fragment = hir_expand::to_fragment_kind(
1934-
&ast_id.ast_id.to_node(self.def_collector.db.upcast()),
1935-
);
1935+
let macro_call = ast_id.ast_id.to_node(self.def_collector.db.upcast());
1936+
let expand_to = hir_expand::ExpandTo::from_call_site(&macro_call);
19361937
self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error(
19371938
self.module_id,
1938-
MacroCallKind::FnLike { ast_id: ast_id.ast_id, fragment },
1939+
MacroCallKind::FnLike { ast_id: ast_id.ast_id, expand_to },
19391940
error.unwrap().to_string(),
19401941
));
19411942
return;
@@ -1947,7 +1948,7 @@ impl ModCollector<'_, '_> {
19471948
self.def_collector.unresolved_macros.push(MacroDirective {
19481949
module_id: self.module_id,
19491950
depth: self.macro_depth + 1,
1950-
kind: MacroDirectiveKind::FnLike { ast_id, fragment: mac.fragment },
1951+
kind: MacroDirectiveKind::FnLike { ast_id, expand_to: mac.expand_to },
19511952
});
19521953
}
19531954

crates/hir_expand/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ itertools = "0.10.0"
1919
base_db = { path = "../base_db", version = "0.0.0" }
2020
cfg = { path = "../cfg", version = "0.0.0" }
2121
syntax = { path = "../syntax", version = "0.0.0" }
22-
parser = { path = "../parser", version = "0.0.0" }
2322
profile = { path = "../profile", version = "0.0.0" }
2423
tt = { path = "../tt", version = "0.0.0" }
2524
mbe = { path = "../mbe", version = "0.0.0" }

crates/hir_expand/src/builtin_derive.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use tracing::debug;
44

55
use mbe::ExpandResult;
6-
use parser::FragmentKind;
76
use syntax::{
87
ast::{self, AstNode, GenericParamsOwner, ModuleItemOwner, NameOwner},
98
match_ast,
@@ -73,7 +72,7 @@ struct BasicAdtInfo {
7372
}
7473

7574
fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, mbe::ExpandError> {
76-
let (parsed, token_map) = mbe::token_tree_to_syntax_node(tt, FragmentKind::Items)?; // FragmentKind::Items doesn't parse attrs?
75+
let (parsed, token_map) = mbe::token_tree_to_syntax_node(tt, mbe::FragmentKind::Items)?; // FragmentKind::Items doesn't parse attrs?
7776
let macro_items = ast::MacroItems::cast(parsed.syntax_node()).ok_or_else(|| {
7877
debug!("derive node didn't parse");
7978
mbe::ExpandError::UnexpectedToken

crates/hir_expand/src/builtin_macro.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -554,17 +554,19 @@ fn option_env_expand(
554554

555555
#[cfg(test)]
556556
mod tests {
557-
use super::*;
558-
use crate::{
559-
name::AsName, test_db::TestDB, AstNode, EagerCallInfo, MacroCallId, MacroCallKind,
560-
MacroCallLoc,
561-
};
557+
use std::sync::Arc;
558+
562559
use base_db::{fixture::WithFixture, SourceDatabase};
563560
use expect_test::{expect, Expect};
564-
use parser::FragmentKind;
565-
use std::sync::Arc;
566561
use syntax::ast::NameOwner;
567562

563+
use crate::{
564+
name::AsName, test_db::TestDB, AstNode, EagerCallInfo, ExpandTo, MacroCallId,
565+
MacroCallKind, MacroCallLoc,
566+
};
567+
568+
use super::*;
569+
568570
fn expand_builtin_macro(ra_fixture: &str) -> String {
569571
let (db, file_id) = TestDB::with_single_file(ra_fixture);
570572
let parsed = db.parse(file_id);
@@ -599,7 +601,7 @@ mod tests {
599601
eager: None,
600602
kind: MacroCallKind::FnLike {
601603
ast_id: AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call)),
602-
fragment: FragmentKind::Expr,
604+
expand_to: ExpandTo::Expr,
603605
},
604606
};
605607

@@ -614,7 +616,6 @@ mod tests {
614616
local_inner: false,
615617
};
616618

617-
let fragment = crate::to_fragment_kind(&macro_call);
618619
let args = macro_call.token_tree().unwrap();
619620
let parsed_args = mbe::syntax_node_to_token_tree(args.syntax()).0;
620621
let call_id = AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call));
@@ -626,18 +627,19 @@ mod tests {
626627
arg_or_expansion: Arc::new(parsed_args.clone()),
627628
included_file: None,
628629
}),
629-
kind: MacroCallKind::FnLike { ast_id: call_id, fragment: FragmentKind::Expr },
630+
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to: ExpandTo::Expr },
630631
});
631632

632633
let expanded = expander.expand(&db, arg_id, &parsed_args).value.unwrap();
634+
let expand_to = crate::ExpandTo::from_call_site(&macro_call);
633635
let loc = MacroCallLoc {
634636
def,
635637
krate,
636638
eager: Some(EagerCallInfo {
637639
arg_or_expansion: Arc::new(expanded.subtree),
638640
included_file: expanded.included_file,
639641
}),
640-
kind: MacroCallKind::FnLike { ast_id: call_id, fragment },
642+
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to },
641643
};
642644

643645
let id: MacroCallId = db.intern_macro(loc);

0 commit comments

Comments
 (0)