4
4
//! but we can't process `.rlib` and need source code instead. The source code
5
5
//! is typically installed with `rustup component add rust-src` command.
6
6
7
- use std:: {
8
- env, fs,
9
- ops:: { self , Not } ,
10
- path:: Path ,
11
- process:: Command ,
12
- } ;
7
+ use std:: { env, fs, ops:: Not , path:: Path , process:: Command } ;
13
8
14
9
use anyhow:: { format_err, Result } ;
15
- use base_db:: CrateName ;
16
10
use itertools:: Itertools ;
17
- use la_arena:: { Arena , Idx } ;
18
11
use paths:: { AbsPath , AbsPathBuf , Utf8PathBuf } ;
19
12
use rustc_hash:: FxHashMap ;
20
13
use stdx:: format_to;
21
14
use toolchain:: { probe_for_binary, Tool } ;
22
15
23
16
use crate :: {
24
- cargo_workspace:: CargoMetadataConfig , utf8_stdout, CargoWorkspace , ManifestPath ,
17
+ cargo_workspace:: CargoMetadataConfig , utf8_stdout, CargoWorkspace , ManifestPath , ProjectJson ,
25
18
RustSourceWorkspaceConfig ,
26
19
} ;
27
20
@@ -36,58 +29,10 @@ pub struct Sysroot {
36
29
#[ derive( Debug , Clone , Eq , PartialEq ) ]
37
30
pub enum RustLibSrcWorkspace {
38
31
Workspace ( CargoWorkspace ) ,
39
- Stitched ( Stitched ) ,
32
+ Json ( ProjectJson ) ,
40
33
Empty ,
41
34
}
42
35
43
- #[ derive( Debug , Clone , Eq , PartialEq ) ]
44
- pub struct Stitched {
45
- crates : Arena < RustLibSrcCrateData > ,
46
- }
47
-
48
- impl ops:: Index < RustLibSrcCrate > for Stitched {
49
- type Output = RustLibSrcCrateData ;
50
- fn index ( & self , index : RustLibSrcCrate ) -> & RustLibSrcCrateData {
51
- & self . crates [ index]
52
- }
53
- }
54
-
55
- impl Stitched {
56
- pub ( crate ) fn public_deps (
57
- & self ,
58
- ) -> impl Iterator < Item = ( CrateName , RustLibSrcCrate , bool ) > + ' _ {
59
- // core is added as a dependency before std in order to
60
- // mimic rustcs dependency order
61
- [ ( "core" , true ) , ( "alloc" , false ) , ( "std" , true ) , ( "test" , false ) ] . into_iter ( ) . filter_map (
62
- move |( name, prelude) | {
63
- Some ( ( CrateName :: new ( name) . unwrap ( ) , self . by_name ( name) ?, prelude) )
64
- } ,
65
- )
66
- }
67
-
68
- pub ( crate ) fn proc_macro ( & self ) -> Option < RustLibSrcCrate > {
69
- self . by_name ( "proc_macro" )
70
- }
71
-
72
- pub ( crate ) fn crates ( & self ) -> impl ExactSizeIterator < Item = RustLibSrcCrate > + ' _ {
73
- self . crates . iter ( ) . map ( |( id, _data) | id)
74
- }
75
-
76
- fn by_name ( & self , name : & str ) -> Option < RustLibSrcCrate > {
77
- let ( id, _data) = self . crates . iter ( ) . find ( |( _id, data) | data. name == name) ?;
78
- Some ( id)
79
- }
80
- }
81
-
82
- pub ( crate ) type RustLibSrcCrate = Idx < RustLibSrcCrateData > ;
83
-
84
- #[ derive( Debug , Clone , Eq , PartialEq ) ]
85
- pub ( crate ) struct RustLibSrcCrateData {
86
- pub ( crate ) name : String ,
87
- pub ( crate ) root : ManifestPath ,
88
- pub ( crate ) deps : Vec < RustLibSrcCrate > ,
89
- }
90
-
91
36
impl Sysroot {
92
37
pub const fn empty ( ) -> Sysroot {
93
38
Sysroot {
@@ -114,7 +59,7 @@ impl Sysroot {
114
59
pub fn is_rust_lib_src_empty ( & self ) -> bool {
115
60
match & self . workspace {
116
61
RustLibSrcWorkspace :: Workspace ( ws) => ws. packages ( ) . next ( ) . is_none ( ) ,
117
- RustLibSrcWorkspace :: Stitched ( stitched ) => stitched . crates . is_empty ( ) ,
62
+ RustLibSrcWorkspace :: Json ( project_json ) => project_json . n_crates ( ) == 0 ,
118
63
RustLibSrcWorkspace :: Empty => true ,
119
64
}
120
65
}
@@ -126,7 +71,7 @@ impl Sysroot {
126
71
pub fn num_packages ( & self ) -> usize {
127
72
match & self . workspace {
128
73
RustLibSrcWorkspace :: Workspace ( ws) => ws. packages ( ) . count ( ) ,
129
- RustLibSrcWorkspace :: Stitched ( c ) => c . crates ( ) . count ( ) ,
74
+ RustLibSrcWorkspace :: Json ( project_json ) => project_json . n_crates ( ) ,
130
75
RustLibSrcWorkspace :: Empty => 0 ,
131
76
}
132
77
}
@@ -252,52 +197,11 @@ impl Sysroot {
252
197
return Some ( loaded) ;
253
198
}
254
199
}
255
- }
256
- tracing:: debug!( "Stitching sysroot library: {src_root}" ) ;
257
-
258
- let mut stitched = Stitched { crates : Arena :: default ( ) } ;
259
-
260
- for path in SYSROOT_CRATES . trim ( ) . lines ( ) {
261
- let name = path. split ( '/' ) . last ( ) . unwrap ( ) ;
262
- let root = [ format ! ( "{path}/src/lib.rs" ) , format ! ( "lib{path}/lib.rs" ) ]
263
- . into_iter ( )
264
- . map ( |it| src_root. join ( it) )
265
- . filter_map ( |it| ManifestPath :: try_from ( it) . ok ( ) )
266
- . find ( |it| fs:: metadata ( it) . is_ok ( ) ) ;
267
-
268
- if let Some ( root) = root {
269
- stitched. crates . alloc ( RustLibSrcCrateData {
270
- name : name. into ( ) ,
271
- root,
272
- deps : Vec :: new ( ) ,
273
- } ) ;
274
- }
275
- }
276
-
277
- if let Some ( std) = stitched. by_name ( "std" ) {
278
- for dep in STD_DEPS . trim ( ) . lines ( ) {
279
- if let Some ( dep) = stitched. by_name ( dep) {
280
- stitched. crates [ std] . deps . push ( dep)
281
- }
282
- }
283
- }
284
-
285
- if let Some ( alloc) = stitched. by_name ( "alloc" ) {
286
- for dep in ALLOC_DEPS . trim ( ) . lines ( ) {
287
- if let Some ( dep) = stitched. by_name ( dep) {
288
- stitched. crates [ alloc] . deps . push ( dep)
289
- }
290
- }
200
+ } else if let RustSourceWorkspaceConfig :: Json ( project_json) = sysroot_source_config {
201
+ return Some ( RustLibSrcWorkspace :: Json ( project_json. clone ( ) ) ) ;
291
202
}
292
203
293
- if let Some ( proc_macro) = stitched. by_name ( "proc_macro" ) {
294
- for dep in PROC_MACRO_DEPS . trim ( ) . lines ( ) {
295
- if let Some ( dep) = stitched. by_name ( dep) {
296
- stitched. crates [ proc_macro] . deps . push ( dep)
297
- }
298
- }
299
- }
300
- Some ( RustLibSrcWorkspace :: Stitched ( stitched) )
204
+ None
301
205
}
302
206
303
207
pub fn set_workspace ( & mut self , workspace : RustLibSrcWorkspace ) {
@@ -308,7 +212,10 @@ impl Sysroot {
308
212
RustLibSrcWorkspace :: Workspace ( ws) => {
309
213
ws. packages ( ) . any ( |p| ws[ p] . name == "core" )
310
214
}
311
- RustLibSrcWorkspace :: Stitched ( stitched) => stitched. by_name ( "core" ) . is_some ( ) ,
215
+ RustLibSrcWorkspace :: Json ( project_json) => project_json
216
+ . crates ( )
217
+ . filter_map ( |( _, krate) | krate. display_name . clone ( ) )
218
+ . any ( |name| name. canonical_name ( ) . as_str ( ) == "core" ) ,
312
219
RustLibSrcWorkspace :: Empty => true ,
313
220
} ;
314
221
if !has_core {
@@ -484,33 +391,3 @@ fn get_rust_lib_src(sysroot_path: &AbsPath) -> Option<AbsPathBuf> {
484
391
None
485
392
}
486
393
}
487
-
488
- const SYSROOT_CRATES : & str = "
489
- alloc
490
- backtrace
491
- core
492
- panic_abort
493
- panic_unwind
494
- proc_macro
495
- profiler_builtins
496
- std
497
- stdarch/crates/std_detect
498
- test
499
- unwind" ;
500
-
501
- const ALLOC_DEPS : & str = "core" ;
502
-
503
- const STD_DEPS : & str = "
504
- alloc
505
- panic_unwind
506
- panic_abort
507
- core
508
- profiler_builtins
509
- unwind
510
- std_detect
511
- test" ;
512
-
513
- // core is required for our builtin derives to work in the proc_macro lib currently
514
- const PROC_MACRO_DEPS : & str = "
515
- std
516
- core" ;
0 commit comments