@@ -4663,6 +4663,13 @@ where
4663
4663
}
4664
4664
}
4665
4665
4666
+ fn code_at ( module : & Module , span : SrcSpan ) -> & str {
4667
+ module
4668
+ . code
4669
+ . get ( span. start as usize ..span. end as usize )
4670
+ . expect ( "code location must be valid" )
4671
+ }
4672
+
4666
4673
impl < ' ast , IO > ast:: visit:: Visit < ' ast > for PatternMatchOnValue < ' ast , IO >
4667
4674
where
4668
4675
IO : CommandExecutor + FileSystemWriter + FileSystemReader + BeamCompiler + Clone ,
@@ -5080,12 +5087,11 @@ impl<'a> GenerateFunction<'a> {
5080
5087
function_type : & Arc < Type > ,
5081
5088
given_arguments : Option < & ' a [ TypedCallArg ] > ,
5082
5089
) {
5083
- let name_range = function_name_location. start as usize ..function_name_location. end as usize ;
5084
- let candidate_name = self . module . code . get ( name_range) ;
5090
+ let candidate_name = code_at ( self . module , function_name_location) ;
5085
5091
match ( candidate_name, function_type. fn_types ( ) ) {
5086
- ( None , _ ) | ( _, None ) => ( ) ,
5087
- ( Some ( name) , _) if !is_valid_lowercase_name ( name) => ( ) ,
5088
- ( Some ( name) , Some ( ( arguments_types, return_type) ) ) => {
5092
+ ( _, None ) => ( ) ,
5093
+ ( name, _) if !is_valid_lowercase_name ( name) => ( ) ,
5094
+ ( name, Some ( ( arguments_types, return_type) ) ) => {
5089
5095
self . function_to_generate = Some ( FunctionToGenerate {
5090
5096
name,
5091
5097
arguments_types,
@@ -5438,8 +5444,7 @@ where
5438
5444
type_ : & Arc < Type > ,
5439
5445
given_arguments : Option < Arguments < ' a > > ,
5440
5446
) -> Option < VariantToGenerate < ' a > > {
5441
- let name_range = function_name_location. start as usize ..function_name_location. end as usize ;
5442
- let name = self . module . code . get ( name_range) . expect ( "valid code range" ) ;
5447
+ let name = code_at ( self . module , function_name_location) ;
5443
5448
if !is_valid_uppercase_name ( name) {
5444
5449
return None ;
5445
5450
}
@@ -6149,16 +6154,18 @@ impl<'a> ConvertToPipe<'a> {
6149
6154
return vec ! [ ] ;
6150
6155
} ;
6151
6156
6152
- let arg_range = if arg. uses_label_shorthand ( ) {
6153
- arg. location . start as usize ..arg. location . end as usize - 1
6157
+ let arg_location = if arg. uses_label_shorthand ( ) {
6158
+ SrcSpan {
6159
+ start : arg. location . start ,
6160
+ end : arg. location . end - 1 ,
6161
+ }
6154
6162
} else if arg. label . is_some ( ) {
6155
- let value = arg. value . location ( ) ;
6156
- value. start as usize ..value. end as usize
6163
+ arg. value . location ( )
6157
6164
} else {
6158
- arg. location . start as usize ..arg . location . end as usize
6165
+ arg. location
6159
6166
} ;
6160
6167
6161
- let arg_text = self . module . code . get ( arg_range ) . expect ( "invalid srcspan" ) ;
6168
+ let arg_text = code_at ( self . module , arg_location ) ;
6162
6169
let arg_text = match arg. value {
6163
6170
// If the expression being piped is a binary operation with
6164
6171
// precedence lower than pipes then we have to wrap it in curly
@@ -7774,7 +7781,7 @@ impl<'a> CollapseNestedCase<'a> {
7774
7781
// Notice one key detail: since alternative patterns can't be nested we
7775
7782
// can't simply write `Ok(2 | 3)` but we have to write `Ok(2) | Ok(3)`!
7776
7783
7777
- let pattern_text: String = self . code_at ( matched_pattern_span) . into ( ) ;
7784
+ let pattern_text: String = code_at ( self . module , matched_pattern_span) . into ( ) ;
7778
7785
let matched_variable_span = matched_variable. location ( ) ;
7779
7786
7780
7787
let pattern_with_variable = |new_content : String | {
@@ -7828,19 +7835,19 @@ impl<'a> CollapseNestedCase<'a> {
7828
7835
let pattern_location =
7829
7836
patterns. first ( ) . expect ( "must have a pattern" ) . location ( ) ;
7830
7837
7831
- let mut pattern_code = self . code_at ( pattern_location) . to_string ( ) ;
7838
+ let mut pattern_code = code_at ( self . module , pattern_location) . to_string ( ) ;
7832
7839
if !references_to_matched_variable. is_empty ( ) {
7833
7840
pattern_code = format ! ( "{pattern_code} as {}" , matched_variable. name( ) ) ;
7834
7841
} ;
7835
7842
pattern_with_variable ( pattern_code)
7836
7843
} )
7837
7844
. join ( " | " ) ;
7838
7845
7839
- let clause_code = self . code_at ( clause. then . location ( ) ) ;
7846
+ let clause_code = code_at ( self . module , clause. then . location ( ) ) ;
7840
7847
let guard_code = match ( outer_guard, & clause. guard ) {
7841
7848
( Some ( outer) , Some ( inner) ) => {
7842
- let mut outer_code = self . code_at ( outer. location ( ) ) . to_string ( ) ;
7843
- let mut inner_code = self . code_at ( inner. location ( ) ) . to_string ( ) ;
7849
+ let mut outer_code = code_at ( self . module , outer. location ( ) ) . to_string ( ) ;
7850
+ let mut inner_code = code_at ( self . module , inner. location ( ) ) . to_string ( ) ;
7844
7851
if ast:: BinOp :: And . precedence ( ) > outer. precedence ( ) {
7845
7852
outer_code = format ! ( "{{ {outer_code} }}" )
7846
7853
}
@@ -7850,7 +7857,7 @@ impl<'a> CollapseNestedCase<'a> {
7850
7857
format ! ( " if {outer_code} && {inner_code}" )
7851
7858
}
7852
7859
( None , Some ( guard) ) | ( Some ( guard) , None ) => {
7853
- format ! ( " if {}" , self . code_at( guard. location( ) ) )
7860
+ format ! ( " if {}" , code_at( self . module , guard. location( ) ) )
7854
7861
}
7855
7862
( None , None ) => "" . into ( ) ,
7856
7863
} ;
@@ -7879,13 +7886,6 @@ impl<'a> CollapseNestedCase<'a> {
7879
7886
action
7880
7887
}
7881
7888
7882
- fn code_at ( & self , span : SrcSpan ) -> & str {
7883
- self . module
7884
- . code
7885
- . get ( span. start as usize ..span. end as usize )
7886
- . expect ( "location must be valid" )
7887
- }
7888
-
7889
7889
/// If the clause can be flattened because it's matching on a single variable
7890
7890
/// defined in it, this function will return the info needed by the language
7891
7891
/// server to flatten that case.
0 commit comments