diff options
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/nameres/collector.rs | 20 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres/tests/mods.rs | 118 |
2 files changed, 72 insertions, 66 deletions
diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index b8840e37c..7f765caf3 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs | |||
@@ -1,3 +1,5 @@ | |||
1 | use std::borrow::Cow; | ||
2 | |||
1 | use arrayvec::ArrayVec; | 3 | use arrayvec::ArrayVec; |
2 | use ra_db::FileId; | 4 | use ra_db::FileId; |
3 | use ra_syntax::{ast, SmolStr}; | 5 | use ra_syntax::{ast, SmolStr}; |
@@ -650,7 +652,7 @@ fn resolve_submodule( | |||
650 | let mut candidates = ArrayVec::<[_; 3]>::new(); | 652 | let mut candidates = ArrayVec::<[_; 3]>::new(); |
651 | let file_attr_mod = attr_path.map(|file_path| { | 653 | let file_attr_mod = attr_path.map(|file_path| { |
652 | let file_path = normalize_attribute_path(file_path); | 654 | let file_path = normalize_attribute_path(file_path); |
653 | let file_attr_mod = dir_path.join(file_path).normalize(); | 655 | let file_attr_mod = dir_path.join(file_path.as_ref()).normalize(); |
654 | candidates.push(file_attr_mod.clone()); | 656 | candidates.push(file_attr_mod.clone()); |
655 | 657 | ||
656 | file_attr_mod | 658 | file_attr_mod |
@@ -676,14 +678,18 @@ fn resolve_submodule( | |||
676 | } | 678 | } |
677 | } | 679 | } |
678 | 680 | ||
679 | fn normalize_attribute_path(file_path: &SmolStr) -> String { | 681 | fn normalize_attribute_path(file_path: &SmolStr) -> Cow<str> { |
680 | let current_dir = "./"; | 682 | let current_dir = "./"; |
681 | 683 | let windows_path_separator = r#"\"#; | |
682 | let separator = |path: &str| path.replace("\\", "/"); | 684 | let current_dir_normalize = if file_path.starts_with(current_dir) { |
683 | if file_path.starts_with(current_dir) { | 685 | &file_path[current_dir.len()..] |
684 | separator(&file_path[current_dir.len()..]) | 686 | } else { |
687 | file_path.as_str() | ||
688 | }; | ||
689 | if current_dir_normalize.contains(windows_path_separator) { | ||
690 | Cow::Owned(current_dir_normalize.replace(windows_path_separator, "/")) | ||
685 | } else { | 691 | } else { |
686 | separator(file_path.as_str()) | 692 | Cow::Borrowed(current_dir_normalize) |
687 | } | 693 | } |
688 | } | 694 | } |
689 | 695 | ||
diff --git a/crates/ra_hir/src/nameres/tests/mods.rs b/crates/ra_hir/src/nameres/tests/mods.rs index c36054c4b..d714a3276 100644 --- a/crates/ra_hir/src/nameres/tests/mods.rs +++ b/crates/ra_hir/src/nameres/tests/mods.rs | |||
@@ -80,15 +80,15 @@ fn module_resolution_works_for_raw_modules() { | |||
80 | #[test] | 80 | #[test] |
81 | fn module_resolution_decl_path() { | 81 | fn module_resolution_decl_path() { |
82 | let map = def_map_with_crate_graph( | 82 | let map = def_map_with_crate_graph( |
83 | " | 83 | r###" |
84 | //- /library.rs | 84 | //- /library.rs |
85 | #[path = \"bar/baz/foo.rs\"] | 85 | #[path = "bar/baz/foo.rs"] |
86 | mod foo; | 86 | mod foo; |
87 | use self::foo::Bar; | 87 | use self::foo::Bar; |
88 | 88 | ||
89 | //- /bar/baz/foo.rs | 89 | //- /bar/baz/foo.rs |
90 | pub struct Bar; | 90 | pub struct Bar; |
91 | ", | 91 | "###, |
92 | crate_graph! { | 92 | crate_graph! { |
93 | "library": ("/library.rs", []), | 93 | "library": ("/library.rs", []), |
94 | }, | 94 | }, |
@@ -107,19 +107,19 @@ fn module_resolution_decl_path() { | |||
107 | #[test] | 107 | #[test] |
108 | fn module_resolution_module_with_path_in_mod_rs() { | 108 | fn module_resolution_module_with_path_in_mod_rs() { |
109 | let map = def_map_with_crate_graph( | 109 | let map = def_map_with_crate_graph( |
110 | " | 110 | r###" |
111 | //- /main.rs | 111 | //- /main.rs |
112 | mod foo; | 112 | mod foo; |
113 | 113 | ||
114 | //- /foo/mod.rs | 114 | //- /foo/mod.rs |
115 | #[path = \"baz.rs\"] | 115 | #[path = "baz.rs"] |
116 | pub mod bar; | 116 | pub mod bar; |
117 | 117 | ||
118 | use self::bar::Baz; | 118 | use self::bar::Baz; |
119 | 119 | ||
120 | //- /foo/baz.rs | 120 | //- /foo/baz.rs |
121 | pub struct Baz; | 121 | pub struct Baz; |
122 | ", | 122 | "###, |
123 | crate_graph! { | 123 | crate_graph! { |
124 | "main": ("/main.rs", []), | 124 | "main": ("/main.rs", []), |
125 | }, | 125 | }, |
@@ -141,19 +141,19 @@ fn module_resolution_module_with_path_in_mod_rs() { | |||
141 | #[test] | 141 | #[test] |
142 | fn module_resolution_module_with_path_non_crate_root() { | 142 | fn module_resolution_module_with_path_non_crate_root() { |
143 | let map = def_map_with_crate_graph( | 143 | let map = def_map_with_crate_graph( |
144 | " | 144 | r###" |
145 | //- /main.rs | 145 | //- /main.rs |
146 | mod foo; | 146 | mod foo; |
147 | 147 | ||
148 | //- /foo.rs | 148 | //- /foo.rs |
149 | #[path = \"baz.rs\"] | 149 | #[path = "baz.rs"] |
150 | pub mod bar; | 150 | pub mod bar; |
151 | 151 | ||
152 | use self::bar::Baz; | 152 | use self::bar::Baz; |
153 | 153 | ||
154 | //- /baz.rs | 154 | //- /baz.rs |
155 | pub struct Baz; | 155 | pub struct Baz; |
156 | ", | 156 | "###, |
157 | crate_graph! { | 157 | crate_graph! { |
158 | "main": ("/main.rs", []), | 158 | "main": ("/main.rs", []), |
159 | }, | 159 | }, |
@@ -175,15 +175,15 @@ fn module_resolution_module_with_path_non_crate_root() { | |||
175 | #[test] | 175 | #[test] |
176 | fn module_resolution_module_decl_path_super() { | 176 | fn module_resolution_module_decl_path_super() { |
177 | let map = def_map_with_crate_graph( | 177 | let map = def_map_with_crate_graph( |
178 | " | 178 | r###" |
179 | //- /main.rs | 179 | //- /main.rs |
180 | #[path = \"bar/baz/module.rs\"] | 180 | #[path = "bar/baz/module.rs"] |
181 | mod foo; | 181 | mod foo; |
182 | pub struct Baz; | 182 | pub struct Baz; |
183 | 183 | ||
184 | //- /bar/baz/module.rs | 184 | //- /bar/baz/module.rs |
185 | use super::Baz; | 185 | use super::Baz; |
186 | ", | 186 | "###, |
187 | crate_graph! { | 187 | crate_graph! { |
188 | "main": ("/main.rs", []), | 188 | "main": ("/main.rs", []), |
189 | }, | 189 | }, |
@@ -202,14 +202,14 @@ fn module_resolution_module_decl_path_super() { | |||
202 | #[test] | 202 | #[test] |
203 | fn module_resolution_explicit_path_mod_rs() { | 203 | fn module_resolution_explicit_path_mod_rs() { |
204 | let map = def_map_with_crate_graph( | 204 | let map = def_map_with_crate_graph( |
205 | " | 205 | r###" |
206 | //- /main.rs | 206 | //- /main.rs |
207 | #[path = \"module/mod.rs\"] | 207 | #[path = "module/mod.rs"] |
208 | mod foo; | 208 | mod foo; |
209 | 209 | ||
210 | //- /module/mod.rs | 210 | //- /module/mod.rs |
211 | pub struct Baz; | 211 | pub struct Baz; |
212 | ", | 212 | "###, |
213 | crate_graph! { | 213 | crate_graph! { |
214 | "main": ("/main.rs", []), | 214 | "main": ("/main.rs", []), |
215 | }, | 215 | }, |
@@ -227,17 +227,17 @@ fn module_resolution_explicit_path_mod_rs() { | |||
227 | #[test] | 227 | #[test] |
228 | fn module_resolution_relative_path() { | 228 | fn module_resolution_relative_path() { |
229 | let map = def_map_with_crate_graph( | 229 | let map = def_map_with_crate_graph( |
230 | " | 230 | r###" |
231 | //- /main.rs | 231 | //- /main.rs |
232 | mod foo; | 232 | mod foo; |
233 | 233 | ||
234 | //- /foo.rs | 234 | //- /foo.rs |
235 | #[path = \"./sub.rs\"] | 235 | #[path = "./sub.rs"] |
236 | pub mod foo_bar; | 236 | pub mod foo_bar; |
237 | 237 | ||
238 | //- /sub.rs | 238 | //- /sub.rs |
239 | pub struct Baz; | 239 | pub struct Baz; |
240 | ", | 240 | "###, |
241 | crate_graph! { | 241 | crate_graph! { |
242 | "main": ("/main.rs", []), | 242 | "main": ("/main.rs", []), |
243 | }, | 243 | }, |
@@ -258,17 +258,17 @@ fn module_resolution_relative_path() { | |||
258 | #[test] | 258 | #[test] |
259 | fn module_resolution_relative_path_2() { | 259 | fn module_resolution_relative_path_2() { |
260 | let map = def_map_with_crate_graph( | 260 | let map = def_map_with_crate_graph( |
261 | " | 261 | r###" |
262 | //- /main.rs | 262 | //- /main.rs |
263 | mod foo; | 263 | mod foo; |
264 | 264 | ||
265 | //- /foo/mod.rs | 265 | //- /foo/mod.rs |
266 | #[path=\"../sub.rs\"] | 266 | #[path="../sub.rs"] |
267 | pub mod foo_bar; | 267 | pub mod foo_bar; |
268 | 268 | ||
269 | //- /sub.rs | 269 | //- /sub.rs |
270 | pub struct Baz; | 270 | pub struct Baz; |
271 | ", | 271 | "###, |
272 | crate_graph! { | 272 | crate_graph! { |
273 | "main": ("/main.rs", []), | 273 | "main": ("/main.rs", []), |
274 | }, | 274 | }, |
@@ -289,14 +289,14 @@ fn module_resolution_relative_path_2() { | |||
289 | #[test] | 289 | #[test] |
290 | fn module_resolution_explicit_path_mod_rs_2() { | 290 | fn module_resolution_explicit_path_mod_rs_2() { |
291 | let map = def_map_with_crate_graph( | 291 | let map = def_map_with_crate_graph( |
292 | " | 292 | r###" |
293 | //- /main.rs | 293 | //- /main.rs |
294 | #[path = \"module/bar/mod.rs\"] | 294 | #[path = "module/bar/mod.rs"] |
295 | mod foo; | 295 | mod foo; |
296 | 296 | ||
297 | //- /module/bar/mod.rs | 297 | //- /module/bar/mod.rs |
298 | pub struct Baz; | 298 | pub struct Baz; |
299 | ", | 299 | "###, |
300 | crate_graph! { | 300 | crate_graph! { |
301 | "main": ("/main.rs", []), | 301 | "main": ("/main.rs", []), |
302 | }, | 302 | }, |
@@ -314,14 +314,14 @@ fn module_resolution_explicit_path_mod_rs_2() { | |||
314 | #[test] | 314 | #[test] |
315 | fn module_resolution_explicit_path_mod_rs_with_win_separator() { | 315 | fn module_resolution_explicit_path_mod_rs_with_win_separator() { |
316 | let map = def_map_with_crate_graph( | 316 | let map = def_map_with_crate_graph( |
317 | " | 317 | r###" |
318 | //- /main.rs | 318 | //- /main.rs |
319 | #[path = \"module\\bar\\mod.rs\"] | 319 | #[path = "module\bar\mod.rs"] |
320 | mod foo; | 320 | mod foo; |
321 | 321 | ||
322 | //- /module/bar/mod.rs | 322 | //- /module/bar/mod.rs |
323 | pub struct Baz; | 323 | pub struct Baz; |
324 | ", | 324 | "###, |
325 | crate_graph! { | 325 | crate_graph! { |
326 | "main": ("/main.rs", []), | 326 | "main": ("/main.rs", []), |
327 | }, | 327 | }, |
@@ -341,16 +341,16 @@ fn module_resolution_explicit_path_mod_rs_with_win_separator() { | |||
341 | #[ignore] | 341 | #[ignore] |
342 | fn module_resolution_decl_inside_inline_module() { | 342 | fn module_resolution_decl_inside_inline_module() { |
343 | let map = def_map_with_crate_graph( | 343 | let map = def_map_with_crate_graph( |
344 | " | 344 | r###" |
345 | //- /main.rs | 345 | //- /main.rs |
346 | #[path = \"models\"] | 346 | #[path = "models"] |
347 | mod foo { | 347 | mod foo { |
348 | mod bar; | 348 | mod bar; |
349 | } | 349 | } |
350 | 350 | ||
351 | //- /models/bar.rs | 351 | //- /models/bar.rs |
352 | pub struct Baz; | 352 | pub struct Baz; |
353 | ", | 353 | "###, |
354 | crate_graph! { | 354 | crate_graph! { |
355 | "main": ("/main.rs", []), | 355 | "main": ("/main.rs", []), |
356 | }, | 356 | }, |
@@ -373,16 +373,16 @@ fn module_resolution_decl_inside_inline_module() { | |||
373 | #[ignore] | 373 | #[ignore] |
374 | fn module_resolution_decl_inside_inline_module_2() { | 374 | fn module_resolution_decl_inside_inline_module_2() { |
375 | let map = def_map_with_crate_graph( | 375 | let map = def_map_with_crate_graph( |
376 | " | 376 | r###" |
377 | //- /main.rs | 377 | //- /main.rs |
378 | #[path = \"models/db\"] | 378 | #[path = "models/db"] |
379 | mod foo { | 379 | mod foo { |
380 | mod bar; | 380 | mod bar; |
381 | } | 381 | } |
382 | 382 | ||
383 | //- /models/db/bar.rs | 383 | //- /models/db/bar.rs |
384 | pub struct Baz; | 384 | pub struct Baz; |
385 | ", | 385 | "###, |
386 | crate_graph! { | 386 | crate_graph! { |
387 | "main": ("/main.rs", []), | 387 | "main": ("/main.rs", []), |
388 | }, | 388 | }, |
@@ -405,17 +405,17 @@ fn module_resolution_decl_inside_inline_module_2() { | |||
405 | #[ignore] | 405 | #[ignore] |
406 | fn module_resolution_decl_inside_inline_module_3() { | 406 | fn module_resolution_decl_inside_inline_module_3() { |
407 | let map = def_map_with_crate_graph( | 407 | let map = def_map_with_crate_graph( |
408 | " | 408 | r###" |
409 | //- /main.rs | 409 | //- /main.rs |
410 | #[path = \"models/db\"] | 410 | #[path = "models/db"] |
411 | mod foo { | 411 | mod foo { |
412 | #[path = \"users.rs\"] | 412 | #[path = "users.rs"] |
413 | mod bar; | 413 | mod bar; |
414 | } | 414 | } |
415 | 415 | ||
416 | //- /models/db/users.rs | 416 | //- /models/db/users.rs |
417 | pub struct Baz; | 417 | pub struct Baz; |
418 | ", | 418 | "###, |
419 | crate_graph! { | 419 | crate_graph! { |
420 | "main": ("/main.rs", []), | 420 | "main": ("/main.rs", []), |
421 | }, | 421 | }, |
@@ -438,17 +438,17 @@ fn module_resolution_decl_inside_inline_module_3() { | |||
438 | #[ignore] | 438 | #[ignore] |
439 | fn module_resolution_decl_inside_inline_module_empty_path() { | 439 | fn module_resolution_decl_inside_inline_module_empty_path() { |
440 | let map = def_map_with_crate_graph( | 440 | let map = def_map_with_crate_graph( |
441 | " | 441 | r###" |
442 | //- /main.rs | 442 | //- /main.rs |
443 | #[path = \"\"] | 443 | #[path = ""] |
444 | mod foo { | 444 | mod foo { |
445 | #[path = \"users.rs\"] | 445 | #[path = "users.rs"] |
446 | mod bar; | 446 | mod bar; |
447 | } | 447 | } |
448 | 448 | ||
449 | //- /users.rs | 449 | //- /users.rs |
450 | pub struct Baz; | 450 | pub struct Baz; |
451 | ", | 451 | "###, |
452 | crate_graph! { | 452 | crate_graph! { |
453 | "main": ("/main.rs", []), | 453 | "main": ("/main.rs", []), |
454 | }, | 454 | }, |
@@ -469,14 +469,14 @@ fn module_resolution_decl_inside_inline_module_empty_path() { | |||
469 | #[test] | 469 | #[test] |
470 | fn module_resolution_decl_empty_path() { | 470 | fn module_resolution_decl_empty_path() { |
471 | let map = def_map_with_crate_graph( | 471 | let map = def_map_with_crate_graph( |
472 | " | 472 | r###" |
473 | //- /main.rs | 473 | //- /main.rs |
474 | #[path = \"\"] | 474 | #[path = ""] |
475 | mod foo; | 475 | mod foo; |
476 | 476 | ||
477 | //- /foo.rs | 477 | //- /foo.rs |
478 | pub struct Baz; | 478 | pub struct Baz; |
479 | ", | 479 | "###, |
480 | crate_graph! { | 480 | crate_graph! { |
481 | "main": ("/main.rs", []), | 481 | "main": ("/main.rs", []), |
482 | }, | 482 | }, |
@@ -496,16 +496,16 @@ fn module_resolution_decl_empty_path() { | |||
496 | #[ignore] | 496 | #[ignore] |
497 | fn module_resolution_decl_inside_inline_module_relative_path() { | 497 | fn module_resolution_decl_inside_inline_module_relative_path() { |
498 | let map = def_map_with_crate_graph( | 498 | let map = def_map_with_crate_graph( |
499 | " | 499 | r###" |
500 | //- /main.rs | 500 | //- /main.rs |
501 | #[path = \"./models\"] | 501 | #[path = "./models"] |
502 | mod foo { | 502 | mod foo { |
503 | mod bar; | 503 | mod bar; |
504 | } | 504 | } |
505 | 505 | ||
506 | //- /models/bar.rs | 506 | //- /models/bar.rs |
507 | pub struct Baz; | 507 | pub struct Baz; |
508 | ", | 508 | "###, |
509 | crate_graph! { | 509 | crate_graph! { |
510 | "main": ("/main.rs", []), | 510 | "main": ("/main.rs", []), |
511 | }, | 511 | }, |
@@ -528,17 +528,17 @@ fn module_resolution_decl_inside_inline_module_relative_path() { | |||
528 | #[ignore] | 528 | #[ignore] |
529 | fn module_resolution_decl_inside_inline_module_in_crate_root() { | 529 | fn module_resolution_decl_inside_inline_module_in_crate_root() { |
530 | let map = def_map_with_crate_graph( | 530 | let map = def_map_with_crate_graph( |
531 | " | 531 | r###" |
532 | //- /main.rs | 532 | //- /main.rs |
533 | mod foo { | 533 | mod foo { |
534 | #[path = \"baz.rs\"] | 534 | #[path = "baz.rs"] |
535 | mod bar; | 535 | mod bar; |
536 | } | 536 | } |
537 | use self::foo::bar::Baz; | 537 | use self::foo::bar::Baz; |
538 | 538 | ||
539 | //- /foo/baz.rs | 539 | //- /foo/baz.rs |
540 | pub struct Baz; | 540 | pub struct Baz; |
541 | ", | 541 | "###, |
542 | crate_graph! { | 542 | crate_graph! { |
543 | "main": ("/main.rs", []), | 543 | "main": ("/main.rs", []), |
544 | }, | 544 | }, |
@@ -562,20 +562,20 @@ fn module_resolution_decl_inside_inline_module_in_crate_root() { | |||
562 | #[ignore] | 562 | #[ignore] |
563 | fn module_resolution_decl_inside_inline_module_in_mod_rs() { | 563 | fn module_resolution_decl_inside_inline_module_in_mod_rs() { |
564 | let map = def_map_with_crate_graph( | 564 | let map = def_map_with_crate_graph( |
565 | " | 565 | r###" |
566 | //- /main.rs | 566 | //- /main.rs |
567 | mod foo; | 567 | mod foo; |
568 | 568 | ||
569 | //- /foo/mod.rs | 569 | //- /foo/mod.rs |
570 | mod bar { | 570 | mod bar { |
571 | #[path = \"qwe.rs\"] | 571 | #[path = "qwe.rs"] |
572 | pub mod baz; | 572 | pub mod baz; |
573 | } | 573 | } |
574 | use self::bar::baz::Baz; | 574 | use self::bar::baz::Baz; |
575 | 575 | ||
576 | //- /foo/bar/qwe.rs | 576 | //- /foo/bar/qwe.rs |
577 | pub struct Baz; | 577 | pub struct Baz; |
578 | ", | 578 | "###, |
579 | crate_graph! { | 579 | crate_graph! { |
580 | "main": ("/main.rs", []), | 580 | "main": ("/main.rs", []), |
581 | }, | 581 | }, |
@@ -602,20 +602,20 @@ fn module_resolution_decl_inside_inline_module_in_mod_rs() { | |||
602 | #[ignore] | 602 | #[ignore] |
603 | fn module_resolution_decl_inside_inline_module_in_non_crate_root() { | 603 | fn module_resolution_decl_inside_inline_module_in_non_crate_root() { |
604 | let map = def_map_with_crate_graph( | 604 | let map = def_map_with_crate_graph( |
605 | " | 605 | r###" |
606 | //- /main.rs | 606 | //- /main.rs |
607 | mod foo; | 607 | mod foo; |
608 | 608 | ||
609 | //- /foo.rs | 609 | //- /foo.rs |
610 | mod bar { | 610 | mod bar { |
611 | #[path = \"qwe.rs\"] | 611 | #[path = "qwe.rs"] |
612 | pub mod baz; | 612 | pub mod baz; |
613 | } | 613 | } |
614 | use self::bar::baz::Baz; | 614 | use self::bar::baz::Baz; |
615 | 615 | ||
616 | //- /foo/bar/qwe.rs | 616 | //- /foo/bar/qwe.rs |
617 | pub struct Baz; | 617 | pub struct Baz; |
618 | ", | 618 | "###, |
619 | crate_graph! { | 619 | crate_graph! { |
620 | "main": ("/main.rs", []), | 620 | "main": ("/main.rs", []), |
621 | }, | 621 | }, |
@@ -642,12 +642,12 @@ fn module_resolution_decl_inside_inline_module_in_non_crate_root() { | |||
642 | #[ignore] | 642 | #[ignore] |
643 | fn module_resolution_decl_inside_inline_module_in_non_crate_root_2() { | 643 | fn module_resolution_decl_inside_inline_module_in_non_crate_root_2() { |
644 | let map = def_map_with_crate_graph( | 644 | let map = def_map_with_crate_graph( |
645 | " | 645 | r###" |
646 | //- /main.rs | 646 | //- /main.rs |
647 | mod foo; | 647 | mod foo; |
648 | 648 | ||
649 | //- /foo.rs | 649 | //- /foo.rs |
650 | #[path = \"bar\"] | 650 | #[path = "bar"] |
651 | mod bar { | 651 | mod bar { |
652 | pub mod baz; | 652 | pub mod baz; |
653 | } | 653 | } |
@@ -655,7 +655,7 @@ fn module_resolution_decl_inside_inline_module_in_non_crate_root_2() { | |||
655 | 655 | ||
656 | //- /bar/baz.rs | 656 | //- /bar/baz.rs |
657 | pub struct Baz; | 657 | pub struct Baz; |
658 | ", | 658 | "###, |
659 | crate_graph! { | 659 | crate_graph! { |
660 | "main": ("/main.rs", []), | 660 | "main": ("/main.rs", []), |
661 | }, | 661 | }, |