@@ -6,33 +6,13 @@ use std::{
66 iter,
77} ;
88
9- use crate :: { body:: LowerCtx , db:: DefDatabase , intern:: Interned , type_ref:: LifetimeRef } ;
10- use base_db:: CrateId ;
11- use hir_expand:: {
12- hygiene:: Hygiene ,
13- name:: { name, Name } ,
14- } ;
9+ use crate :: { body:: LowerCtx , intern:: Interned , type_ref:: LifetimeRef } ;
10+ use hir_expand:: name:: { name, Name } ;
1511use syntax:: ast;
1612
1713use crate :: type_ref:: { TypeBound , TypeRef } ;
1814
19- #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
20- pub struct ModPath {
21- pub kind : PathKind ,
22- segments : Vec < Name > ,
23- }
24-
25- #[ derive( Debug , Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
26- pub enum PathKind {
27- Plain ,
28- /// `self::` is `Super(0)`
29- Super ( u8 ) ,
30- Crate ,
31- /// Absolute path (::foo)
32- Abs ,
33- /// `$crate` from macro expansion
34- DollarCrate ( CrateId ) ,
35- }
15+ pub use hir_expand:: mod_path:: { path, ModPath , PathKind } ;
3616
3717#[ derive( Debug , Clone , PartialEq , Eq ) ]
3818pub enum ImportAlias {
@@ -51,67 +31,6 @@ impl Display for ImportAlias {
5131 }
5232}
5333
54- impl ModPath {
55- pub fn from_src ( db : & dyn DefDatabase , path : ast:: Path , hygiene : & Hygiene ) -> Option < ModPath > {
56- lower:: convert_path ( db, None , path, hygiene)
57- }
58-
59- pub fn from_segments ( kind : PathKind , segments : impl IntoIterator < Item = Name > ) -> ModPath {
60- let segments = segments. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
61- ModPath { kind, segments }
62- }
63-
64- /// Creates a `ModPath` from a `PathKind`, with no extra path segments.
65- pub const fn from_kind ( kind : PathKind ) -> ModPath {
66- ModPath { kind, segments : Vec :: new ( ) }
67- }
68-
69- pub fn segments ( & self ) -> & [ Name ] {
70- & self . segments
71- }
72-
73- pub fn push_segment ( & mut self , segment : Name ) {
74- self . segments . push ( segment) ;
75- }
76-
77- pub fn pop_segment ( & mut self ) -> Option < Name > {
78- self . segments . pop ( )
79- }
80-
81- /// Returns the number of segments in the path (counting special segments like `$crate` and
82- /// `super`).
83- pub fn len ( & self ) -> usize {
84- self . segments . len ( )
85- + match self . kind {
86- PathKind :: Plain => 0 ,
87- PathKind :: Super ( i) => i as usize ,
88- PathKind :: Crate => 1 ,
89- PathKind :: Abs => 0 ,
90- PathKind :: DollarCrate ( _) => 1 ,
91- }
92- }
93-
94- pub fn is_ident ( & self ) -> bool {
95- self . as_ident ( ) . is_some ( )
96- }
97-
98- pub fn is_self ( & self ) -> bool {
99- self . kind == PathKind :: Super ( 0 ) && self . segments . is_empty ( )
100- }
101-
102- /// If this path is a single identifier, like `foo`, return its name.
103- pub fn as_ident ( & self ) -> Option < & Name > {
104- if self . kind != PathKind :: Plain {
105- return None ;
106- }
107-
108- match & * self . segments {
109- [ name] => Some ( name) ,
110- _ => None ,
111- }
112- }
113- }
114-
11534#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
11635pub struct Path {
11736 /// Type based path like `<T>::foo`.
@@ -185,10 +104,7 @@ impl Path {
185104 }
186105
187106 pub fn segments ( & self ) -> PathSegments < ' _ > {
188- PathSegments {
189- segments : self . mod_path . segments . as_slice ( ) ,
190- generic_args : & self . generic_args ,
191- }
107+ PathSegments { segments : self . mod_path . segments ( ) , generic_args : & self . generic_args }
192108 }
193109
194110 pub fn mod_path ( & self ) -> & ModPath {
@@ -203,7 +119,7 @@ impl Path {
203119 type_anchor : self . type_anchor . clone ( ) ,
204120 mod_path : Interned :: new ( ModPath :: from_segments (
205121 self . mod_path . kind . clone ( ) ,
206- self . mod_path . segments [ ..self . mod_path . segments . len ( ) - 1 ] . iter ( ) . cloned ( ) ,
122+ self . mod_path . segments ( ) [ ..self . mod_path . segments ( ) . len ( ) - 1 ] . iter ( ) . cloned ( ) ,
207123 ) ) ,
208124 generic_args : self . generic_args [ ..self . generic_args . len ( ) - 1 ] . to_vec ( ) . into ( ) ,
209125 } ;
@@ -296,76 +212,3 @@ impl From<Name> for Box<Path> {
296212 Box :: new ( Path :: from ( name) )
297213 }
298214}
299-
300- impl From < Name > for ModPath {
301- fn from ( name : Name ) -> ModPath {
302- ModPath :: from_segments ( PathKind :: Plain , iter:: once ( name) )
303- }
304- }
305-
306- impl Display for ModPath {
307- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
308- let mut first_segment = true ;
309- let mut add_segment = |s| -> fmt:: Result {
310- if !first_segment {
311- f. write_str ( "::" ) ?;
312- }
313- first_segment = false ;
314- f. write_str ( s) ?;
315- Ok ( ( ) )
316- } ;
317- match self . kind {
318- PathKind :: Plain => { }
319- PathKind :: Super ( 0 ) => add_segment ( "self" ) ?,
320- PathKind :: Super ( n) => {
321- for _ in 0 ..n {
322- add_segment ( "super" ) ?;
323- }
324- }
325- PathKind :: Crate => add_segment ( "crate" ) ?,
326- PathKind :: Abs => add_segment ( "" ) ?,
327- PathKind :: DollarCrate ( _) => add_segment ( "$crate" ) ?,
328- }
329- for segment in & self . segments {
330- if !first_segment {
331- f. write_str ( "::" ) ?;
332- }
333- first_segment = false ;
334- write ! ( f, "{}" , segment) ?;
335- }
336- Ok ( ( ) )
337- }
338- }
339-
340- pub use hir_expand:: name as __name;
341-
342- #[ macro_export]
343- macro_rules! __known_path {
344- ( core:: iter:: IntoIterator ) => { } ;
345- ( core:: iter:: Iterator ) => { } ;
346- ( core:: result:: Result ) => { } ;
347- ( core:: option:: Option ) => { } ;
348- ( core:: ops:: Range ) => { } ;
349- ( core:: ops:: RangeFrom ) => { } ;
350- ( core:: ops:: RangeFull ) => { } ;
351- ( core:: ops:: RangeTo ) => { } ;
352- ( core:: ops:: RangeToInclusive ) => { } ;
353- ( core:: ops:: RangeInclusive ) => { } ;
354- ( core:: future:: Future ) => { } ;
355- ( core:: ops:: Try ) => { } ;
356- ( $path: path) => {
357- compile_error!( "Please register your known path in the path module" )
358- } ;
359- }
360-
361- #[ macro_export]
362- macro_rules! __path {
363- ( $start: ident $( :: $seg: ident) * ) => ( {
364- $crate:: __known_path!( $start $( :: $seg) * ) ;
365- $crate:: path:: ModPath :: from_segments( $crate:: path:: PathKind :: Abs , vec![
366- $crate:: path:: __name![ $start] , $( $crate:: path:: __name![ $seg] , ) *
367- ] )
368- } ) ;
369- }
370-
371- pub use crate :: __path as path;
0 commit comments