aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2020-04-27 18:48:55 +0100
committerEdwin Cheng <[email protected]>2020-04-27 18:48:55 +0100
commit6a5014329aecf73da81943216729ab64fa255368 (patch)
treee3b13ad7e6416ab9fa82365fad96e1f08384ba05 /crates
parentef67e0a497a3f0b65c11bf443e0d35c8e51bd26f (diff)
Use core instead of std for builtin derive macros
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_expand/src/builtin_derive.rs93
-rw-r--r--crates/ra_hir_ty/src/tests/macros.rs8
2 files changed, 65 insertions, 36 deletions
diff --git a/crates/ra_hir_expand/src/builtin_derive.rs b/crates/ra_hir_expand/src/builtin_derive.rs
index bb45b0f1d..dae623dd0 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
11use crate::db::AstDatabase; 11use crate::db::AstDatabase;
12use crate::{name, quote, LazyMacroId, MacroDefId, MacroDefKind}; 12use crate::{name, quote, LazyMacroId, MacroCallId, MacroDefId, MacroDefKind};
13 13
14macro_rules! register_builtin { 14macro_rules! register_builtin {
15 ( $($trait:ident => $expand:ident),* ) => { 15 ( $($trait:ident => $expand:ident),* ) => {
@@ -153,76 +153,105 @@ fn expand_simple_derive(
153 Ok(expanded) 153 Ok(expanded)
154} 154}
155 155
156fn 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 crates = db.relevant_crates(file_id);
164 let mut crate_names =
165 crates.iter().filter_map(|krate| cg[*krate].display_name.clone()).map(|it| it.to_string());
166
167 let tt = if crate_names.any(|name| name == "std" || name == "core") {
168 quote! { krate }
169 } else {
170 quote! { core }
171 };
172
173 tt.token_trees[0].clone()
174}
175
156fn copy_expand( 176fn copy_expand(
157 _db: &dyn AstDatabase, 177 db: &dyn AstDatabase,
158 _id: LazyMacroId, 178 id: LazyMacroId,
159 tt: &tt::Subtree, 179 tt: &tt::Subtree,
160) -> Result<tt::Subtree, mbe::ExpandError> { 180) -> Result<tt::Subtree, mbe::ExpandError> {
161 expand_simple_derive(tt, quote! { std::marker::Copy }) 181 let krate = find_builtin_crate(db, id);
182 expand_simple_derive(tt, quote! { #krate::marker::Copy })
162} 183}
163 184
164fn clone_expand( 185fn clone_expand(
165 _db: &dyn AstDatabase, 186 db: &dyn AstDatabase,
166 _id: LazyMacroId, 187 id: LazyMacroId,
167 tt: &tt::Subtree, 188 tt: &tt::Subtree,
168) -> Result<tt::Subtree, mbe::ExpandError> { 189) -> Result<tt::Subtree, mbe::ExpandError> {
169 expand_simple_derive(tt, quote! { std::clone::Clone }) 190 let krate = find_builtin_crate(db, id);
191 expand_simple_derive(tt, quote! { #krate::clone::Clone })
170} 192}
171 193
172fn default_expand( 194fn default_expand(
173 _db: &dyn AstDatabase, 195 db: &dyn AstDatabase,
174 _id: LazyMacroId, 196 id: LazyMacroId,
175 tt: &tt::Subtree, 197 tt: &tt::Subtree,
176) -> Result<tt::Subtree, mbe::ExpandError> { 198) -> Result<tt::Subtree, mbe::ExpandError> {
177 expand_simple_derive(tt, quote! { std::default::Default }) 199 let krate = find_builtin_crate(db, id);
200 expand_simple_derive(tt, quote! { #krate::default::Default })
178} 201}
179 202
180fn debug_expand( 203fn debug_expand(
181 _db: &dyn AstDatabase, 204 db: &dyn AstDatabase,
182 _id: LazyMacroId, 205 id: LazyMacroId,
183 tt: &tt::Subtree, 206 tt: &tt::Subtree,
184) -> Result<tt::Subtree, mbe::ExpandError> { 207) -> Result<tt::Subtree, mbe::ExpandError> {
185 expand_simple_derive(tt, quote! { std::fmt::Debug }) 208 let krate = find_builtin_crate(db, id);
209 expand_simple_derive(tt, quote! { #krate::fmt::Debug })
186} 210}
187 211
188fn hash_expand( 212fn hash_expand(
189 _db: &dyn AstDatabase, 213 db: &dyn AstDatabase,
190 _id: LazyMacroId, 214 id: LazyMacroId,
191 tt: &tt::Subtree, 215 tt: &tt::Subtree,
192) -> Result<tt::Subtree, mbe::ExpandError> { 216) -> Result<tt::Subtree, mbe::ExpandError> {
193 expand_simple_derive(tt, quote! { std::hash::Hash }) 217 let krate = find_builtin_crate(db, id);
218 expand_simple_derive(tt, quote! { #krate::hash::Hash })
194} 219}
195 220
196fn eq_expand( 221fn eq_expand(
197 _db: &dyn AstDatabase, 222 db: &dyn AstDatabase,
198 _id: LazyMacroId, 223 id: LazyMacroId,
199 tt: &tt::Subtree, 224 tt: &tt::Subtree,
200) -> Result<tt::Subtree, mbe::ExpandError> { 225) -> Result<tt::Subtree, mbe::ExpandError> {
201 expand_simple_derive(tt, quote! { std::cmp::Eq }) 226 let krate = find_builtin_crate(db, id);
227 expand_simple_derive(tt, quote! { #krate::cmp::Eq })
202} 228}
203 229
204fn partial_eq_expand( 230fn partial_eq_expand(
205 _db: &dyn AstDatabase, 231 db: &dyn AstDatabase,
206 _id: LazyMacroId, 232 id: LazyMacroId,
207 tt: &tt::Subtree, 233 tt: &tt::Subtree,
208) -> Result<tt::Subtree, mbe::ExpandError> { 234) -> Result<tt::Subtree, mbe::ExpandError> {
209 expand_simple_derive(tt, quote! { std::cmp::PartialEq }) 235 let krate = find_builtin_crate(db, id);
236 expand_simple_derive(tt, quote! { #krate::cmp::PartialEq })
210} 237}
211 238
212fn ord_expand( 239fn ord_expand(
213 _db: &dyn AstDatabase, 240 db: &dyn AstDatabase,
214 _id: LazyMacroId, 241 id: LazyMacroId,
215 tt: &tt::Subtree, 242 tt: &tt::Subtree,
216) -> Result<tt::Subtree, mbe::ExpandError> { 243) -> Result<tt::Subtree, mbe::ExpandError> {
217 expand_simple_derive(tt, quote! { std::cmp::Ord }) 244 let krate = find_builtin_crate(db, id);
245 expand_simple_derive(tt, quote! { #krate::cmp::Ord })
218} 246}
219 247
220fn partial_ord_expand( 248fn partial_ord_expand(
221 _db: &dyn AstDatabase, 249 db: &dyn AstDatabase,
222 _id: LazyMacroId, 250 id: LazyMacroId,
223 tt: &tt::Subtree, 251 tt: &tt::Subtree,
224) -> Result<tt::Subtree, mbe::ExpandError> { 252) -> Result<tt::Subtree, mbe::ExpandError> {
225 expand_simple_derive(tt, quote! { std::cmp::PartialOrd }) 253 let krate = find_builtin_crate(db, id);
254 expand_simple_derive(tt, quote! { #krate::cmp::PartialOrd })
226} 255}
227 256
228#[cfg(test)] 257#[cfg(test)]
@@ -264,7 +293,7 @@ mod tests {
264 known::Copy, 293 known::Copy,
265 ); 294 );
266 295
267 assert_eq!(expanded, "impl< >std::marker::CopyforFoo< >{}"); 296 assert_eq!(expanded, "impl< >core::marker::CopyforFoo< >{}");
268 } 297 }
269 298
270 #[test] 299 #[test]
@@ -279,7 +308,7 @@ mod tests {
279 308
280 assert_eq!( 309 assert_eq!(
281 expanded, 310 expanded,
282 "impl<T0:std::marker::Copy,T1:std::marker::Copy>std::marker::CopyforFoo<T0,T1>{}" 311 "impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"
283 ); 312 );
284 } 313 }
285 314
@@ -297,7 +326,7 @@ mod tests {
297 326
298 assert_eq!( 327 assert_eq!(
299 expanded, 328 expanded,
300 "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>{}"
301 ); 330 );
302 } 331 }
303 332
@@ -313,7 +342,7 @@ mod tests {
313 342
314 assert_eq!( 343 assert_eq!(
315 expanded, 344 expanded,
316 "impl<T0:std::clone::Clone,T1:std::clone::Clone>std::clone::CloneforFoo<T0,T1>{}" 345 "impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}"
317 ); 346 );
318 } 347 }
319} 348}
diff --git a/crates/ra_hir_ty/src/tests/macros.rs b/crates/ra_hir_ty/src/tests/macros.rs
index 6b5267232..4f82ff702 100644
--- a/crates/ra_hir_ty/src/tests/macros.rs
+++ b/crates/ra_hir_ty/src/tests/macros.rs
@@ -622,14 +622,14 @@ fn main() {
622fn infer_derive_clone_simple() { 622fn infer_derive_clone_simple() {
623 let (db, pos) = TestDB::with_position( 623 let (db, pos) = TestDB::with_position(
624 r#" 624 r#"
625//- /main.rs crate:main deps:std 625//- /main.rs crate:main deps:core
626#[derive(Clone)] 626#[derive(Clone)]
627struct S; 627struct S;
628fn test() { 628fn test() {
629 S.clone()<|>; 629 S.clone()<|>;
630} 630}
631 631
632//- /lib.rs crate:std 632//- /lib.rs crate:core
633#[prelude_import] 633#[prelude_import]
634use clone::*; 634use clone::*;
635mod clone { 635mod clone {
@@ -646,7 +646,7 @@ mod clone {
646fn infer_derive_clone_with_params() { 646fn infer_derive_clone_with_params() {
647 let (db, pos) = TestDB::with_position( 647 let (db, pos) = TestDB::with_position(
648 r#" 648 r#"
649//- /main.rs crate:main deps:std 649//- /main.rs crate:main deps:core
650#[derive(Clone)] 650#[derive(Clone)]
651struct S; 651struct S;
652#[derive(Clone)] 652#[derive(Clone)]
@@ -656,7 +656,7 @@ fn test() {
656 (Wrapper(S).clone(), Wrapper(NonClone).clone())<|>; 656 (Wrapper(S).clone(), Wrapper(NonClone).clone())<|>;
657} 657}
658 658
659//- /lib.rs crate:std 659//- /lib.rs crate:core
660#[prelude_import] 660#[prelude_import]
661use clone::*; 661use clone::*;
662mod clone { 662mod clone {