File tree Expand file tree Collapse file tree 5 files changed +66
-3
lines changed Expand file tree Collapse file tree 5 files changed +66
-3
lines changed Original file line number Diff line number Diff line change @@ -72,6 +72,10 @@ fn main() {
72
72
println ! ( "cargo:rustc-cfg=no_libprocmacro_unwind_safe" ) ;
73
73
}
74
74
75
+ if version. minor < 34 {
76
+ println ! ( "cargo:rustc-cfg=no_try_from" ) ;
77
+ }
78
+
75
79
if version. minor < 39 {
76
80
println ! ( "cargo:rustc-cfg=no_bind_by_move_pattern_guard" ) ;
77
81
}
Original file line number Diff line number Diff line change
1
+ pub ( crate ) fn usize_to_u32 ( u : usize ) -> Option < u32 > {
2
+ #[ cfg( not( no_try_from) ) ]
3
+ {
4
+ use core:: convert:: TryFrom ;
5
+
6
+ u32:: try_from ( u) . ok ( )
7
+ }
8
+
9
+ #[ cfg( no_try_from) ]
10
+ {
11
+ use core:: mem;
12
+
13
+ if mem:: size_of :: < usize > ( ) <= mem:: size_of :: < u32 > ( ) || u <= u32:: max_value ( ) as usize {
14
+ Some ( u as u32 )
15
+ } else {
16
+ None
17
+ }
18
+ }
19
+ }
Original file line number Diff line number Diff line change @@ -1012,8 +1012,46 @@ impl Literal {
1012
1012
self . span = span;
1013
1013
}
1014
1014
1015
- pub fn subspan < R : RangeBounds < usize > > ( & self , _range : R ) -> Option < Span > {
1016
- None
1015
+ pub fn subspan < R : RangeBounds < usize > > ( & self , range : R ) -> Option < Span > {
1016
+ #[ cfg( not( span_locations) ) ]
1017
+ {
1018
+ let _ = range;
1019
+ None
1020
+ }
1021
+
1022
+ #[ cfg( span_locations) ]
1023
+ {
1024
+ use crate :: convert:: usize_to_u32;
1025
+ use core:: ops:: Bound ;
1026
+
1027
+ let lo = match range. start_bound ( ) {
1028
+ Bound :: Included ( start) => {
1029
+ let start = usize_to_u32 ( * start) ?;
1030
+ self . span . lo . checked_add ( start) ?
1031
+ }
1032
+ Bound :: Excluded ( start) => {
1033
+ let start = usize_to_u32 ( * start) ?;
1034
+ self . span . lo . checked_add ( start) ?. checked_add ( 1 ) ?
1035
+ }
1036
+ Bound :: Unbounded => self . span . lo ,
1037
+ } ;
1038
+ let hi = match range. end_bound ( ) {
1039
+ Bound :: Included ( end) => {
1040
+ let end = usize_to_u32 ( * end) ?;
1041
+ self . span . lo . checked_add ( end) ?. checked_add ( 1 ) ?
1042
+ }
1043
+ Bound :: Excluded ( end) => {
1044
+ let end = usize_to_u32 ( * end) ?;
1045
+ self . span . lo . checked_add ( end) ?
1046
+ }
1047
+ Bound :: Unbounded => self . span . hi ,
1048
+ } ;
1049
+ if lo <= hi && hi <= self . span . hi {
1050
+ Some ( Span { lo, hi } )
1051
+ } else {
1052
+ None
1053
+ }
1054
+ }
1017
1055
}
1018
1056
}
1019
1057
Original file line number Diff line number Diff line change @@ -142,6 +142,8 @@ use crate::fallback as imp;
142
142
#[ cfg( wrap_proc_macro) ]
143
143
mod imp;
144
144
145
+ #[ cfg( span_locations) ]
146
+ mod convert;
145
147
#[ cfg( span_locations) ]
146
148
mod location;
147
149
Original file line number Diff line number Diff line change @@ -282,7 +282,7 @@ fn literal_span() {
282
282
assert_eq ! ( positive. span( ) . end( ) . column, 3 ) ;
283
283
assert_eq ! ( negative. span( ) . start( ) . column, 0 ) ;
284
284
assert_eq ! ( negative. span( ) . end( ) . column, 4 ) ;
285
- assert_eq ! ( subspan. unwrap( ) . source_text( ) . unwrap( ) , "." ) ; // FIXME
285
+ assert_eq ! ( subspan. unwrap( ) . source_text( ) . unwrap( ) , "." ) ;
286
286
}
287
287
288
288
assert ! ( positive. subspan( 1 ..4 ) . is_none( ) ) ;
You can’t perform that action at this time.
0 commit comments