diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-03-18 14:38:04 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-03-18 14:38:04 +0000 |
commit | 3ab9b39dd47d99ffd97f485c27f38b8944e12a3e (patch) | |
tree | 308ffffd0b61517f13aedad9621430f4c41014e3 /crates/hir_expand | |
parent | 816bc7389516dda1eb4821f2ac4d5993cd5611dd (diff) | |
parent | b84efbaacfc980ba167edc145aa7ca5d738448ff (diff) |
Merge #8087
8087: Make MacroDefId's `AstId` mandatory when possible r=jonas-schievink a=jonas-schievink
This makes it clearer (in the type definition) which macros have or don't have an `AstId`
bors r+
Co-authored-by: Jonas Schievink <[email protected]>
Diffstat (limited to 'crates/hir_expand')
-rw-r--r-- | crates/hir_expand/src/builtin_derive.rs | 6 | ||||
-rw-r--r-- | crates/hir_expand/src/builtin_macro.rs | 13 | ||||
-rw-r--r-- | crates/hir_expand/src/db.rs | 10 | ||||
-rw-r--r-- | crates/hir_expand/src/eager.rs | 10 | ||||
-rw-r--r-- | crates/hir_expand/src/hygiene.rs | 10 | ||||
-rw-r--r-- | crates/hir_expand/src/lib.rs | 24 |
6 files changed, 39 insertions, 34 deletions
diff --git a/crates/hir_expand/src/builtin_derive.rs b/crates/hir_expand/src/builtin_derive.rs index a8d267c30..60fd2ebdd 100644 --- a/crates/hir_expand/src/builtin_derive.rs +++ b/crates/hir_expand/src/builtin_derive.rs | |||
@@ -61,8 +61,7 @@ pub fn find_builtin_derive( | |||
61 | let expander = BuiltinDeriveExpander::find_by_name(ident)?; | 61 | let expander = BuiltinDeriveExpander::find_by_name(ident)?; |
62 | Some(MacroDefId { | 62 | Some(MacroDefId { |
63 | krate, | 63 | krate, |
64 | ast_id: Some(ast_id), | 64 | kind: MacroDefKind::BuiltInDerive(expander, ast_id), |
65 | kind: MacroDefKind::BuiltInDerive(expander), | ||
66 | local_inner: false, | 65 | local_inner: false, |
67 | }) | 66 | }) |
68 | } | 67 | } |
@@ -314,8 +313,7 @@ $0 | |||
314 | let loc = MacroCallLoc { | 313 | let loc = MacroCallLoc { |
315 | def: MacroDefId { | 314 | def: MacroDefId { |
316 | krate: CrateId(0), | 315 | krate: CrateId(0), |
317 | ast_id: Some(macro_ast_id), | 316 | kind: MacroDefKind::BuiltInDerive(expander, macro_ast_id), |
318 | kind: MacroDefKind::BuiltInDerive(expander), | ||
319 | local_inner: false, | 317 | local_inner: false, |
320 | }, | 318 | }, |
321 | krate: CrateId(0), | 319 | krate: CrateId(0), |
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index fce09a9e7..8529b43b6 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs | |||
@@ -71,14 +71,12 @@ pub fn find_builtin_macro( | |||
71 | match kind { | 71 | match kind { |
72 | Either::Left(kind) => Some(MacroDefId { | 72 | Either::Left(kind) => Some(MacroDefId { |
73 | krate, | 73 | krate, |
74 | ast_id: Some(ast_id), | 74 | kind: MacroDefKind::BuiltIn(kind, ast_id), |
75 | kind: MacroDefKind::BuiltIn(kind), | ||
76 | local_inner: false, | 75 | local_inner: false, |
77 | }), | 76 | }), |
78 | Either::Right(kind) => Some(MacroDefId { | 77 | Either::Right(kind) => Some(MacroDefId { |
79 | krate, | 78 | krate, |
80 | ast_id: Some(ast_id), | 79 | kind: MacroDefKind::BuiltInEager(kind, ast_id), |
81 | kind: MacroDefKind::BuiltInEager(kind), | ||
82 | local_inner: false, | 80 | local_inner: false, |
83 | }), | 81 | }), |
84 | } | 82 | } |
@@ -512,6 +510,7 @@ mod tests { | |||
512 | let macro_call = macro_calls.pop().unwrap(); | 510 | let macro_call = macro_calls.pop().unwrap(); |
513 | 511 | ||
514 | let expander = find_by_name(¯o_rules.name().unwrap().as_name()).unwrap(); | 512 | let expander = find_by_name(¯o_rules.name().unwrap().as_name()).unwrap(); |
513 | let ast_id = AstId::new(file_id.into(), ast_id_map.ast_id(¯o_rules)); | ||
515 | 514 | ||
516 | let krate = CrateId(0); | 515 | let krate = CrateId(0); |
517 | let file_id = match expander { | 516 | let file_id = match expander { |
@@ -519,8 +518,7 @@ mod tests { | |||
519 | // the first one should be a macro_rules | 518 | // the first one should be a macro_rules |
520 | let def = MacroDefId { | 519 | let def = MacroDefId { |
521 | krate: CrateId(0), | 520 | krate: CrateId(0), |
522 | ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(¯o_rules))), | 521 | kind: MacroDefKind::BuiltIn(expander, ast_id), |
523 | kind: MacroDefKind::BuiltIn(expander), | ||
524 | local_inner: false, | 522 | local_inner: false, |
525 | }; | 523 | }; |
526 | 524 | ||
@@ -540,8 +538,7 @@ mod tests { | |||
540 | // the first one should be a macro_rules | 538 | // the first one should be a macro_rules |
541 | let def = MacroDefId { | 539 | let def = MacroDefId { |
542 | krate, | 540 | krate, |
543 | ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(¯o_rules))), | 541 | kind: MacroDefKind::BuiltInEager(expander, ast_id), |
544 | kind: MacroDefKind::BuiltInEager(expander), | ||
545 | local_inner: false, | 542 | local_inner: false, |
546 | }; | 543 | }; |
547 | 544 | ||
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index a3070f1f9..4107d7781 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs | |||
@@ -130,8 +130,8 @@ fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> { | |||
130 | 130 | ||
131 | fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> { | 131 | fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> { |
132 | match id.kind { | 132 | match id.kind { |
133 | MacroDefKind::Declarative => { | 133 | MacroDefKind::Declarative(ast_id) => { |
134 | let macro_rules = match id.ast_id?.to_node(db) { | 134 | let macro_rules = match ast_id.to_node(db) { |
135 | syntax::ast::Macro::MacroRules(mac) => mac, | 135 | syntax::ast::Macro::MacroRules(mac) => mac, |
136 | syntax::ast::Macro::MacroDef(_) => return None, | 136 | syntax::ast::Macro::MacroDef(_) => return None, |
137 | }; | 137 | }; |
@@ -150,13 +150,13 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, | |||
150 | }; | 150 | }; |
151 | Some(Arc::new((TokenExpander::MacroRules(rules), tmap))) | 151 | Some(Arc::new((TokenExpander::MacroRules(rules), tmap))) |
152 | } | 152 | } |
153 | MacroDefKind::BuiltIn(expander) => { | 153 | MacroDefKind::BuiltIn(expander, _) => { |
154 | Some(Arc::new((TokenExpander::Builtin(expander), mbe::TokenMap::default()))) | 154 | Some(Arc::new((TokenExpander::Builtin(expander), mbe::TokenMap::default()))) |
155 | } | 155 | } |
156 | MacroDefKind::BuiltInDerive(expander) => { | 156 | MacroDefKind::BuiltInDerive(expander, _) => { |
157 | Some(Arc::new((TokenExpander::BuiltinDerive(expander), mbe::TokenMap::default()))) | 157 | Some(Arc::new((TokenExpander::BuiltinDerive(expander), mbe::TokenMap::default()))) |
158 | } | 158 | } |
159 | MacroDefKind::BuiltInEager(_) => None, | 159 | MacroDefKind::BuiltInEager(..) => None, |
160 | MacroDefKind::ProcMacro(expander) => { | 160 | MacroDefKind::ProcMacro(expander) => { |
161 | Some(Arc::new((TokenExpander::ProcMacro(expander), mbe::TokenMap::default()))) | 161 | Some(Arc::new((TokenExpander::ProcMacro(expander), mbe::TokenMap::default()))) |
162 | } | 162 | } |
diff --git a/crates/hir_expand/src/eager.rs b/crates/hir_expand/src/eager.rs index dc618a9ee..ddadaffd3 100644 --- a/crates/hir_expand/src/eager.rs +++ b/crates/hir_expand/src/eager.rs | |||
@@ -140,7 +140,7 @@ pub fn expand_eager_macro( | |||
140 | let subtree = | 140 | let subtree = |
141 | diagnostic_sink.option(to_subtree(&result), || err("failed to parse macro result"))?; | 141 | diagnostic_sink.option(to_subtree(&result), || err("failed to parse macro result"))?; |
142 | 142 | ||
143 | if let MacroDefKind::BuiltInEager(eager) = def.kind { | 143 | if let MacroDefKind::BuiltInEager(eager, _) = def.kind { |
144 | let res = eager.expand(db, arg_id, &subtree); | 144 | let res = eager.expand(db, arg_id, &subtree); |
145 | 145 | ||
146 | let (subtree, fragment) = diagnostic_sink.expand_result_option(res)?; | 146 | let (subtree, fragment) = diagnostic_sink.expand_result_option(res)?; |
@@ -193,7 +193,7 @@ fn eager_macro_recur( | |||
193 | let def = diagnostic_sink | 193 | let def = diagnostic_sink |
194 | .option_with(|| macro_resolver(child.path()?), || err("failed to resolve macro"))?; | 194 | .option_with(|| macro_resolver(child.path()?), || err("failed to resolve macro"))?; |
195 | let insert = match def.kind { | 195 | let insert = match def.kind { |
196 | MacroDefKind::BuiltInEager(_) => { | 196 | MacroDefKind::BuiltInEager(..) => { |
197 | let id: MacroCallId = expand_eager_macro( | 197 | let id: MacroCallId = expand_eager_macro( |
198 | db, | 198 | db, |
199 | krate, | 199 | krate, |
@@ -206,9 +206,9 @@ fn eager_macro_recur( | |||
206 | db.parse_or_expand(id.as_file()) | 206 | db.parse_or_expand(id.as_file()) |
207 | .expect("successful macro expansion should be parseable") | 207 | .expect("successful macro expansion should be parseable") |
208 | } | 208 | } |
209 | MacroDefKind::Declarative | 209 | MacroDefKind::Declarative(_) |
210 | | MacroDefKind::BuiltIn(_) | 210 | | MacroDefKind::BuiltIn(..) |
211 | | MacroDefKind::BuiltInDerive(_) | 211 | | MacroDefKind::BuiltInDerive(..) |
212 | | MacroDefKind::ProcMacro(_) => { | 212 | | MacroDefKind::ProcMacro(_) => { |
213 | let res = lazy_expand(db, &def, curr.with_value(child.clone()), krate); | 213 | let res = lazy_expand(db, &def, curr.with_value(child.clone()), krate); |
214 | let val = diagnostic_sink.expand_result_option(res)?; | 214 | let val = diagnostic_sink.expand_result_option(res)?; |
diff --git a/crates/hir_expand/src/hygiene.rs b/crates/hir_expand/src/hygiene.rs index 87cad326d..e758b3c0a 100644 --- a/crates/hir_expand/src/hygiene.rs +++ b/crates/hir_expand/src/hygiene.rs | |||
@@ -145,7 +145,7 @@ fn make_hygiene_info( | |||
145 | ) -> Option<HygieneInfo> { | 145 | ) -> Option<HygieneInfo> { |
146 | let arg_tt = loc.kind.arg(db)?; | 146 | let arg_tt = loc.kind.arg(db)?; |
147 | 147 | ||
148 | let def_offset = loc.def.ast_id.and_then(|id| { | 148 | let def_offset = loc.def.ast_id().and_then(|id| { |
149 | let def_tt = match id.to_node(db) { | 149 | let def_tt = match id.to_node(db) { |
150 | ast::Macro::MacroRules(mac) => mac.token_tree()?.syntax().text_range().start(), | 150 | ast::Macro::MacroRules(mac) => mac.token_tree()?.syntax().text_range().start(), |
151 | ast::Macro::MacroDef(_) => return None, | 151 | ast::Macro::MacroDef(_) => return None, |
@@ -176,12 +176,12 @@ impl HygieneFrame { | |||
176 | let loc = db.lookup_intern_macro(id); | 176 | let loc = db.lookup_intern_macro(id); |
177 | let info = make_hygiene_info(db, macro_file, &loc); | 177 | let info = make_hygiene_info(db, macro_file, &loc); |
178 | match loc.def.kind { | 178 | match loc.def.kind { |
179 | MacroDefKind::Declarative => { | 179 | MacroDefKind::Declarative(_) => { |
180 | (info, Some(loc.def.krate), loc.def.local_inner) | 180 | (info, Some(loc.def.krate), loc.def.local_inner) |
181 | } | 181 | } |
182 | MacroDefKind::BuiltIn(_) => (info, Some(loc.def.krate), false), | 182 | MacroDefKind::BuiltIn(..) => (info, Some(loc.def.krate), false), |
183 | MacroDefKind::BuiltInDerive(_) => (info, None, false), | 183 | MacroDefKind::BuiltInDerive(..) => (info, None, false), |
184 | MacroDefKind::BuiltInEager(_) => (info, None, false), | 184 | MacroDefKind::BuiltInEager(..) => (info, None, false), |
185 | MacroDefKind::ProcMacro(_) => (info, None, false), | 185 | MacroDefKind::ProcMacro(_) => (info, None, false), |
186 | } | 186 | } |
187 | } | 187 | } |
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 7532d00b8..83e11019f 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs | |||
@@ -143,7 +143,7 @@ impl HirFileId { | |||
143 | 143 | ||
144 | let arg_tt = loc.kind.arg(db)?; | 144 | let arg_tt = loc.kind.arg(db)?; |
145 | 145 | ||
146 | let def = loc.def.ast_id.and_then(|id| { | 146 | let def = loc.def.ast_id().and_then(|id| { |
147 | let def_tt = match id.to_node(db) { | 147 | let def_tt = match id.to_node(db) { |
148 | ast::Macro::MacroRules(mac) => mac.token_tree()?, | 148 | ast::Macro::MacroRules(mac) => mac.token_tree()?, |
149 | ast::Macro::MacroDef(_) => return None, | 149 | ast::Macro::MacroDef(_) => return None, |
@@ -180,7 +180,7 @@ impl HirFileId { | |||
180 | }; | 180 | }; |
181 | let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id); | 181 | let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id); |
182 | let item = match loc.def.kind { | 182 | let item = match loc.def.kind { |
183 | MacroDefKind::BuiltInDerive(_) => loc.kind.node(db), | 183 | MacroDefKind::BuiltInDerive(..) => loc.kind.node(db), |
184 | _ => return None, | 184 | _ => return None, |
185 | }; | 185 | }; |
186 | Some(item.with_value(ast::Item::cast(item.value.clone())?)) | 186 | Some(item.with_value(ast::Item::cast(item.value.clone())?)) |
@@ -224,7 +224,6 @@ impl From<EagerMacroId> for MacroCallId { | |||
224 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 224 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
225 | pub struct MacroDefId { | 225 | pub struct MacroDefId { |
226 | pub krate: CrateId, | 226 | pub krate: CrateId, |
227 | pub ast_id: Option<AstId<ast::Macro>>, | ||
228 | pub kind: MacroDefKind, | 227 | pub kind: MacroDefKind, |
229 | 228 | ||
230 | pub local_inner: bool, | 229 | pub local_inner: bool, |
@@ -239,15 +238,26 @@ impl MacroDefId { | |||
239 | ) -> LazyMacroId { | 238 | ) -> LazyMacroId { |
240 | db.intern_macro(MacroCallLoc { def: self, krate, kind }) | 239 | db.intern_macro(MacroCallLoc { def: self, krate, kind }) |
241 | } | 240 | } |
241 | |||
242 | pub fn ast_id(&self) -> Option<AstId<ast::Macro>> { | ||
243 | let id = match &self.kind { | ||
244 | MacroDefKind::Declarative(id) => id, | ||
245 | MacroDefKind::BuiltIn(_, id) => id, | ||
246 | MacroDefKind::BuiltInDerive(_, id) => id, | ||
247 | MacroDefKind::BuiltInEager(_, id) => id, | ||
248 | MacroDefKind::ProcMacro(_) => return None, | ||
249 | }; | ||
250 | Some(*id) | ||
251 | } | ||
242 | } | 252 | } |
243 | 253 | ||
244 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 254 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
245 | pub enum MacroDefKind { | 255 | pub enum MacroDefKind { |
246 | Declarative, | 256 | Declarative(AstId<ast::Macro>), |
247 | BuiltIn(BuiltinFnLikeExpander), | 257 | BuiltIn(BuiltinFnLikeExpander, AstId<ast::Macro>), |
248 | // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander | 258 | // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander |
249 | BuiltInDerive(BuiltinDeriveExpander), | 259 | BuiltInDerive(BuiltinDeriveExpander, AstId<ast::Macro>), |
250 | BuiltInEager(EagerExpander), | 260 | BuiltInEager(EagerExpander, AstId<ast::Macro>), |
251 | ProcMacro(ProcMacroExpander), | 261 | ProcMacro(ProcMacroExpander), |
252 | } | 262 | } |
253 | 263 | ||