diff options
Diffstat (limited to 'crates/ra_hir_expand/src')
-rw-r--r-- | crates/ra_hir_expand/src/builtin_derive.rs | 115 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/builtin_macro.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/db.rs | 3 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/hygiene.rs | 33 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/lib.rs | 2 |
5 files changed, 110 insertions, 47 deletions
diff --git a/crates/ra_hir_expand/src/builtin_derive.rs b/crates/ra_hir_expand/src/builtin_derive.rs index bb45b0f1d..1dc9cac66 100644 --- a/crates/ra_hir_expand/src/builtin_derive.rs +++ b/crates/ra_hir_expand/src/builtin_derive.rs | |||
@@ -9,7 +9,7 @@ use ra_syntax::{ | |||
9 | }; | 9 | }; |
10 | 10 | ||
11 | use crate::db::AstDatabase; | 11 | use crate::db::AstDatabase; |
12 | use crate::{name, quote, LazyMacroId, MacroDefId, MacroDefKind}; | 12 | use crate::{name, quote, LazyMacroId, MacroCallId, MacroDefId, MacroDefKind}; |
13 | 13 | ||
14 | macro_rules! register_builtin { | 14 | macro_rules! register_builtin { |
15 | ( $($trait:ident => $expand:ident),* ) => { | 15 | ( $($trait:ident => $expand:ident),* ) => { |
@@ -38,7 +38,7 @@ macro_rules! register_builtin { | |||
38 | _ => return None, | 38 | _ => return None, |
39 | }; | 39 | }; |
40 | 40 | ||
41 | Some(MacroDefId { krate: None, ast_id: None, kind: MacroDefKind::BuiltInDerive(kind) }) | 41 | Some(MacroDefId { krate: None, ast_id: None, kind: MacroDefKind::BuiltInDerive(kind), local_inner: false }) |
42 | } | 42 | } |
43 | }; | 43 | }; |
44 | } | 44 | } |
@@ -153,76 +153,113 @@ fn expand_simple_derive( | |||
153 | Ok(expanded) | 153 | Ok(expanded) |
154 | } | 154 | } |
155 | 155 | ||
156 | fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree { | ||
157 | // FIXME: make hygiene works for builtin derive macro | ||
158 | // such that $crate can be used here. | ||
159 | |||
160 | let m: MacroCallId = id.into(); | ||
161 | let file_id = m.as_file().original_file(db); | ||
162 | let cg = db.crate_graph(); | ||
163 | let krates = db.relevant_crates(file_id); | ||
164 | let krate = match krates.get(0) { | ||
165 | Some(krate) => krate, | ||
166 | None => { | ||
167 | let tt = quote! { core }; | ||
168 | return tt.token_trees[0].clone(); | ||
169 | } | ||
170 | }; | ||
171 | |||
172 | // XXX | ||
173 | // All crates except core itself should have a dependency on core, | ||
174 | // We detect `core` by seeing whether it doesn't have such a dependency. | ||
175 | let tt = if cg[*krate].dependencies.iter().any(|dep| dep.name == "core") { | ||
176 | quote! { core } | ||
177 | } else { | ||
178 | quote! { crate } | ||
179 | }; | ||
180 | |||
181 | tt.token_trees[0].clone() | ||
182 | } | ||
183 | |||
156 | fn copy_expand( | 184 | fn copy_expand( |
157 | _db: &dyn AstDatabase, | 185 | db: &dyn AstDatabase, |
158 | _id: LazyMacroId, | 186 | id: LazyMacroId, |
159 | tt: &tt::Subtree, | 187 | tt: &tt::Subtree, |
160 | ) -> Result<tt::Subtree, mbe::ExpandError> { | 188 | ) -> Result<tt::Subtree, mbe::ExpandError> { |
161 | expand_simple_derive(tt, quote! { std::marker::Copy }) | 189 | let krate = find_builtin_crate(db, id); |
190 | expand_simple_derive(tt, quote! { #krate::marker::Copy }) | ||
162 | } | 191 | } |
163 | 192 | ||
164 | fn clone_expand( | 193 | fn clone_expand( |
165 | _db: &dyn AstDatabase, | 194 | db: &dyn AstDatabase, |
166 | _id: LazyMacroId, | 195 | id: LazyMacroId, |
167 | tt: &tt::Subtree, | 196 | tt: &tt::Subtree, |
168 | ) -> Result<tt::Subtree, mbe::ExpandError> { | 197 | ) -> Result<tt::Subtree, mbe::ExpandError> { |
169 | expand_simple_derive(tt, quote! { std::clone::Clone }) | 198 | let krate = find_builtin_crate(db, id); |
199 | expand_simple_derive(tt, quote! { #krate::clone::Clone }) | ||
170 | } | 200 | } |
171 | 201 | ||
172 | fn default_expand( | 202 | fn default_expand( |
173 | _db: &dyn AstDatabase, | 203 | db: &dyn AstDatabase, |
174 | _id: LazyMacroId, | 204 | id: LazyMacroId, |
175 | tt: &tt::Subtree, | 205 | tt: &tt::Subtree, |
176 | ) -> Result<tt::Subtree, mbe::ExpandError> { | 206 | ) -> Result<tt::Subtree, mbe::ExpandError> { |
177 | expand_simple_derive(tt, quote! { std::default::Default }) | 207 | let krate = find_builtin_crate(db, id); |
208 | expand_simple_derive(tt, quote! { #krate::default::Default }) | ||
178 | } | 209 | } |
179 | 210 | ||
180 | fn debug_expand( | 211 | fn debug_expand( |
181 | _db: &dyn AstDatabase, | 212 | db: &dyn AstDatabase, |
182 | _id: LazyMacroId, | 213 | id: LazyMacroId, |
183 | tt: &tt::Subtree, | 214 | tt: &tt::Subtree, |
184 | ) -> Result<tt::Subtree, mbe::ExpandError> { | 215 | ) -> Result<tt::Subtree, mbe::ExpandError> { |
185 | expand_simple_derive(tt, quote! { std::fmt::Debug }) | 216 | let krate = find_builtin_crate(db, id); |
217 | expand_simple_derive(tt, quote! { #krate::fmt::Debug }) | ||
186 | } | 218 | } |
187 | 219 | ||
188 | fn hash_expand( | 220 | fn hash_expand( |
189 | _db: &dyn AstDatabase, | 221 | db: &dyn AstDatabase, |
190 | _id: LazyMacroId, | 222 | id: LazyMacroId, |
191 | tt: &tt::Subtree, | 223 | tt: &tt::Subtree, |
192 | ) -> Result<tt::Subtree, mbe::ExpandError> { | 224 | ) -> Result<tt::Subtree, mbe::ExpandError> { |
193 | expand_simple_derive(tt, quote! { std::hash::Hash }) | 225 | let krate = find_builtin_crate(db, id); |
226 | expand_simple_derive(tt, quote! { #krate::hash::Hash }) | ||
194 | } | 227 | } |
195 | 228 | ||
196 | fn eq_expand( | 229 | fn eq_expand( |
197 | _db: &dyn AstDatabase, | 230 | db: &dyn AstDatabase, |
198 | _id: LazyMacroId, | 231 | id: LazyMacroId, |
199 | tt: &tt::Subtree, | 232 | tt: &tt::Subtree, |
200 | ) -> Result<tt::Subtree, mbe::ExpandError> { | 233 | ) -> Result<tt::Subtree, mbe::ExpandError> { |
201 | expand_simple_derive(tt, quote! { std::cmp::Eq }) | 234 | let krate = find_builtin_crate(db, id); |
235 | expand_simple_derive(tt, quote! { #krate::cmp::Eq }) | ||
202 | } | 236 | } |
203 | 237 | ||
204 | fn partial_eq_expand( | 238 | fn partial_eq_expand( |
205 | _db: &dyn AstDatabase, | 239 | db: &dyn AstDatabase, |
206 | _id: LazyMacroId, | 240 | id: LazyMacroId, |
207 | tt: &tt::Subtree, | 241 | tt: &tt::Subtree, |
208 | ) -> Result<tt::Subtree, mbe::ExpandError> { | 242 | ) -> Result<tt::Subtree, mbe::ExpandError> { |
209 | expand_simple_derive(tt, quote! { std::cmp::PartialEq }) | 243 | let krate = find_builtin_crate(db, id); |
244 | expand_simple_derive(tt, quote! { #krate::cmp::PartialEq }) | ||
210 | } | 245 | } |
211 | 246 | ||
212 | fn ord_expand( | 247 | fn ord_expand( |
213 | _db: &dyn AstDatabase, | 248 | db: &dyn AstDatabase, |
214 | _id: LazyMacroId, | 249 | id: LazyMacroId, |
215 | tt: &tt::Subtree, | 250 | tt: &tt::Subtree, |
216 | ) -> Result<tt::Subtree, mbe::ExpandError> { | 251 | ) -> Result<tt::Subtree, mbe::ExpandError> { |
217 | expand_simple_derive(tt, quote! { std::cmp::Ord }) | 252 | let krate = find_builtin_crate(db, id); |
253 | expand_simple_derive(tt, quote! { #krate::cmp::Ord }) | ||
218 | } | 254 | } |
219 | 255 | ||
220 | fn partial_ord_expand( | 256 | fn partial_ord_expand( |
221 | _db: &dyn AstDatabase, | 257 | db: &dyn AstDatabase, |
222 | _id: LazyMacroId, | 258 | id: LazyMacroId, |
223 | tt: &tt::Subtree, | 259 | tt: &tt::Subtree, |
224 | ) -> Result<tt::Subtree, mbe::ExpandError> { | 260 | ) -> Result<tt::Subtree, mbe::ExpandError> { |
225 | expand_simple_derive(tt, quote! { std::cmp::PartialOrd }) | 261 | let krate = find_builtin_crate(db, id); |
262 | expand_simple_derive(tt, quote! { #krate::cmp::PartialOrd }) | ||
226 | } | 263 | } |
227 | 264 | ||
228 | #[cfg(test)] | 265 | #[cfg(test)] |
@@ -234,8 +271,18 @@ mod tests { | |||
234 | 271 | ||
235 | fn expand_builtin_derive(s: &str, name: Name) -> String { | 272 | fn expand_builtin_derive(s: &str, name: Name) -> String { |
236 | let def = find_builtin_derive(&name).unwrap(); | 273 | let def = find_builtin_derive(&name).unwrap(); |
274 | let fixture = format!( | ||
275 | r#"//- /main.rs crate:main deps:core | ||
276 | <|> | ||
277 | {} | ||
278 | //- /lib.rs crate:core | ||
279 | // empty | ||
280 | "#, | ||
281 | s | ||
282 | ); | ||
237 | 283 | ||
238 | let (db, file_id) = TestDB::with_single_file(&s); | 284 | let (db, file_pos) = TestDB::with_position(&fixture); |
285 | let file_id = file_pos.file_id; | ||
239 | let parsed = db.parse(file_id); | 286 | let parsed = db.parse(file_id); |
240 | let items: Vec<_> = | 287 | let items: Vec<_> = |
241 | parsed.syntax_node().descendants().filter_map(ast::ModuleItem::cast).collect(); | 288 | parsed.syntax_node().descendants().filter_map(ast::ModuleItem::cast).collect(); |
@@ -264,7 +311,7 @@ mod tests { | |||
264 | known::Copy, | 311 | known::Copy, |
265 | ); | 312 | ); |
266 | 313 | ||
267 | assert_eq!(expanded, "impl< >std::marker::CopyforFoo< >{}"); | 314 | assert_eq!(expanded, "impl< >core::marker::CopyforFoo< >{}"); |
268 | } | 315 | } |
269 | 316 | ||
270 | #[test] | 317 | #[test] |
@@ -279,7 +326,7 @@ mod tests { | |||
279 | 326 | ||
280 | assert_eq!( | 327 | assert_eq!( |
281 | expanded, | 328 | expanded, |
282 | "impl<T0:std::marker::Copy,T1:std::marker::Copy>std::marker::CopyforFoo<T0,T1>{}" | 329 | "impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}" |
283 | ); | 330 | ); |
284 | } | 331 | } |
285 | 332 | ||
@@ -297,7 +344,7 @@ mod tests { | |||
297 | 344 | ||
298 | assert_eq!( | 345 | assert_eq!( |
299 | expanded, | 346 | expanded, |
300 | "impl<T0:std::marker::Copy,T1:std::marker::Copy>std::marker::CopyforFoo<T0,T1>{}" | 347 | "impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}" |
301 | ); | 348 | ); |
302 | } | 349 | } |
303 | 350 | ||
@@ -313,7 +360,7 @@ mod tests { | |||
313 | 360 | ||
314 | assert_eq!( | 361 | assert_eq!( |
315 | expanded, | 362 | expanded, |
316 | "impl<T0:std::clone::Clone,T1:std::clone::Clone>std::clone::CloneforFoo<T0,T1>{}" | 363 | "impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}" |
317 | ); | 364 | ); |
318 | } | 365 | } |
319 | } | 366 | } |
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs index e0fef613d..d8b3d342c 100644 --- a/crates/ra_hir_expand/src/builtin_macro.rs +++ b/crates/ra_hir_expand/src/builtin_macro.rs | |||
@@ -73,11 +73,13 @@ pub fn find_builtin_macro( | |||
73 | krate: Some(krate), | 73 | krate: Some(krate), |
74 | ast_id: Some(ast_id), | 74 | ast_id: Some(ast_id), |
75 | kind: MacroDefKind::BuiltIn(kind), | 75 | kind: MacroDefKind::BuiltIn(kind), |
76 | local_inner: false, | ||
76 | }), | 77 | }), |
77 | Either::Right(kind) => Some(MacroDefId { | 78 | Either::Right(kind) => Some(MacroDefId { |
78 | krate: Some(krate), | 79 | krate: Some(krate), |
79 | ast_id: Some(ast_id), | 80 | ast_id: Some(ast_id), |
80 | kind: MacroDefKind::BuiltInEager(kind), | 81 | kind: MacroDefKind::BuiltInEager(kind), |
82 | local_inner: false, | ||
81 | }), | 83 | }), |
82 | } | 84 | } |
83 | } | 85 | } |
@@ -406,6 +408,7 @@ mod tests { | |||
406 | krate: Some(CrateId(0)), | 408 | krate: Some(CrateId(0)), |
407 | ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(¯o_calls[0]))), | 409 | ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(¯o_calls[0]))), |
408 | kind: MacroDefKind::BuiltIn(expander), | 410 | kind: MacroDefKind::BuiltIn(expander), |
411 | local_inner: false, | ||
409 | }; | 412 | }; |
410 | 413 | ||
411 | let loc = MacroCallLoc { | 414 | let loc = MacroCallLoc { |
@@ -425,6 +428,7 @@ mod tests { | |||
425 | krate: Some(CrateId(0)), | 428 | krate: Some(CrateId(0)), |
426 | ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(¯o_calls[0]))), | 429 | ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(¯o_calls[0]))), |
427 | kind: MacroDefKind::BuiltInEager(expander), | 430 | kind: MacroDefKind::BuiltInEager(expander), |
431 | local_inner: false, | ||
428 | }; | 432 | }; |
429 | 433 | ||
430 | let args = macro_calls[1].token_tree().unwrap(); | 434 | let args = macro_calls[1].token_tree().unwrap(); |
diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index 047452306..4c12d0a15 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs | |||
@@ -330,7 +330,7 @@ fn to_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind { | |||
330 | FragmentKind::Expr | 330 | FragmentKind::Expr |
331 | } | 331 | } |
332 | // FIXME: Expand to statements in appropriate positions; HIR lowering needs to handle that | 332 | // FIXME: Expand to statements in appropriate positions; HIR lowering needs to handle that |
333 | EXPR_STMT | BLOCK => FragmentKind::Expr, | 333 | EXPR_STMT | BLOCK_EXPR => FragmentKind::Expr, |
334 | ARG_LIST => FragmentKind::Expr, | 334 | ARG_LIST => FragmentKind::Expr, |
335 | TRY_EXPR => FragmentKind::Expr, | 335 | TRY_EXPR => FragmentKind::Expr, |
336 | TUPLE_EXPR => FragmentKind::Expr, | 336 | TUPLE_EXPR => FragmentKind::Expr, |
@@ -342,7 +342,6 @@ fn to_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind { | |||
342 | CONDITION => FragmentKind::Expr, | 342 | CONDITION => FragmentKind::Expr, |
343 | BREAK_EXPR => FragmentKind::Expr, | 343 | BREAK_EXPR => FragmentKind::Expr, |
344 | RETURN_EXPR => FragmentKind::Expr, | 344 | RETURN_EXPR => FragmentKind::Expr, |
345 | BLOCK_EXPR => FragmentKind::Expr, | ||
346 | MATCH_EXPR => FragmentKind::Expr, | 345 | MATCH_EXPR => FragmentKind::Expr, |
347 | MATCH_ARM => FragmentKind::Expr, | 346 | MATCH_ARM => FragmentKind::Expr, |
348 | MATCH_GUARD => FragmentKind::Expr, | 347 | MATCH_GUARD => FragmentKind::Expr, |
diff --git a/crates/ra_hir_expand/src/hygiene.rs b/crates/ra_hir_expand/src/hygiene.rs index 0b41d0e95..6b482a60c 100644 --- a/crates/ra_hir_expand/src/hygiene.rs +++ b/crates/ra_hir_expand/src/hygiene.rs | |||
@@ -12,35 +12,38 @@ use crate::{ | |||
12 | HirFileId, HirFileIdRepr, MacroCallId, MacroDefKind, | 12 | HirFileId, HirFileIdRepr, MacroCallId, MacroDefKind, |
13 | }; | 13 | }; |
14 | 14 | ||
15 | #[derive(Debug)] | 15 | #[derive(Clone, Debug)] |
16 | pub struct Hygiene { | 16 | pub struct Hygiene { |
17 | // This is what `$crate` expands to | 17 | // This is what `$crate` expands to |
18 | def_crate: Option<CrateId>, | 18 | def_crate: Option<CrateId>, |
19 | |||
20 | // Indiciate this is a local inner macro | ||
21 | local_inner: bool, | ||
19 | } | 22 | } |
20 | 23 | ||
21 | impl Hygiene { | 24 | impl Hygiene { |
22 | pub fn new(db: &dyn AstDatabase, file_id: HirFileId) -> Hygiene { | 25 | pub fn new(db: &dyn AstDatabase, file_id: HirFileId) -> Hygiene { |
23 | let def_crate = match file_id.0 { | 26 | let (def_crate, local_inner) = match file_id.0 { |
24 | HirFileIdRepr::FileId(_) => None, | 27 | HirFileIdRepr::FileId(_) => (None, false), |
25 | HirFileIdRepr::MacroFile(macro_file) => match macro_file.macro_call_id { | 28 | HirFileIdRepr::MacroFile(macro_file) => match macro_file.macro_call_id { |
26 | MacroCallId::LazyMacro(id) => { | 29 | MacroCallId::LazyMacro(id) => { |
27 | let loc = db.lookup_intern_macro(id); | 30 | let loc = db.lookup_intern_macro(id); |
28 | match loc.def.kind { | 31 | match loc.def.kind { |
29 | MacroDefKind::Declarative => loc.def.krate, | 32 | MacroDefKind::Declarative => (loc.def.krate, loc.def.local_inner), |
30 | MacroDefKind::BuiltIn(_) => None, | 33 | MacroDefKind::BuiltIn(_) => (None, false), |
31 | MacroDefKind::BuiltInDerive(_) => None, | 34 | MacroDefKind::BuiltInDerive(_) => (None, false), |
32 | MacroDefKind::BuiltInEager(_) => None, | 35 | MacroDefKind::BuiltInEager(_) => (None, false), |
33 | MacroDefKind::CustomDerive(_) => None, | 36 | MacroDefKind::CustomDerive(_) => (None, false), |
34 | } | 37 | } |
35 | } | 38 | } |
36 | MacroCallId::EagerMacro(_id) => None, | 39 | MacroCallId::EagerMacro(_id) => (None, false), |
37 | }, | 40 | }, |
38 | }; | 41 | }; |
39 | Hygiene { def_crate } | 42 | Hygiene { def_crate, local_inner } |
40 | } | 43 | } |
41 | 44 | ||
42 | pub fn new_unhygienic() -> Hygiene { | 45 | pub fn new_unhygienic() -> Hygiene { |
43 | Hygiene { def_crate: None } | 46 | Hygiene { def_crate: None, local_inner: false } |
44 | } | 47 | } |
45 | 48 | ||
46 | // FIXME: this should just return name | 49 | // FIXME: this should just return name |
@@ -52,4 +55,12 @@ impl Hygiene { | |||
52 | } | 55 | } |
53 | Either::Left(name_ref.as_name()) | 56 | Either::Left(name_ref.as_name()) |
54 | } | 57 | } |
58 | |||
59 | pub fn local_inner_macros(&self) -> Option<CrateId> { | ||
60 | if self.local_inner { | ||
61 | self.def_crate | ||
62 | } else { | ||
63 | None | ||
64 | } | ||
65 | } | ||
55 | } | 66 | } |
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 754a0f005..f440c073b 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs | |||
@@ -204,6 +204,8 @@ pub struct MacroDefId { | |||
204 | pub krate: Option<CrateId>, | 204 | pub krate: Option<CrateId>, |
205 | pub ast_id: Option<AstId<ast::MacroCall>>, | 205 | pub ast_id: Option<AstId<ast::MacroCall>>, |
206 | pub kind: MacroDefKind, | 206 | pub kind: MacroDefKind, |
207 | |||
208 | pub local_inner: bool, | ||
207 | } | 209 | } |
208 | 210 | ||
209 | impl MacroDefId { | 211 | impl MacroDefId { |