@@ -72,6 +72,52 @@ fn invalid_type_err(cx: &mut base::ExtCtxt<'_>, expr: &P<rustc_ast::Expr>, is_ne
72
72
}
73
73
}
74
74
75
+ fn handle_array_element (
76
+ cx : & mut base:: ExtCtxt < ' _ > ,
77
+ has_errors : & mut bool ,
78
+ missing_literals : & mut Vec < rustc_span:: Span > ,
79
+ expr : & P < rustc_ast:: Expr > ,
80
+ ) -> Option < u8 > {
81
+ match expr. kind {
82
+ ast:: ExprKind :: Array ( _) | ast:: ExprKind :: Repeat ( _, _) => {
83
+ if !* has_errors {
84
+ cx. span_err ( expr. span , "cannot concatenate doubly nested array" ) ;
85
+ }
86
+ * has_errors = true ;
87
+ None
88
+ }
89
+ ast:: ExprKind :: Lit ( ref lit) => match lit. kind {
90
+ ast:: LitKind :: Int (
91
+ val,
92
+ ast:: LitIntType :: Unsuffixed | ast:: LitIntType :: Unsigned ( ast:: UintTy :: U8 ) ,
93
+ ) if val <= u8:: MAX . into ( ) => Some ( val as u8 ) ,
94
+
95
+ ast:: LitKind :: Byte ( val) => Some ( val) ,
96
+ ast:: LitKind :: ByteStr ( _) => {
97
+ if !* has_errors {
98
+ cx. struct_span_err ( expr. span , "cannot concatenate doubly nested array" )
99
+ . note ( "byte strings are treated as arrays of bytes" )
100
+ . help ( "try flattening the array" )
101
+ . emit ( ) ;
102
+ }
103
+ * has_errors = true ;
104
+ None
105
+ }
106
+ _ => {
107
+ if !* has_errors {
108
+ invalid_type_err ( cx, expr, true ) ;
109
+ }
110
+ * has_errors = true ;
111
+ None
112
+ }
113
+ } ,
114
+ _ => {
115
+ missing_literals. push ( expr. span ) ;
116
+ None
117
+ }
118
+ }
119
+ }
120
+
75
121
pub fn expand_concat_bytes (
76
122
cx : & mut base:: ExtCtxt < ' _ > ,
77
123
sp : rustc_span:: Span ,
@@ -88,48 +134,27 @@ pub fn expand_concat_bytes(
88
134
match e. kind {
89
135
ast:: ExprKind :: Array ( ref exprs) => {
90
136
for expr in exprs {
91
- match expr. kind {
92
- ast:: ExprKind :: Array ( _) => {
93
- if !has_errors {
94
- cx. span_err ( expr. span , "cannot concatenate doubly nested array" ) ;
95
- }
96
- has_errors = true ;
97
- }
98
- ast:: ExprKind :: Lit ( ref lit) => match lit. kind {
99
- ast:: LitKind :: Int (
100
- val,
101
- ast:: LitIntType :: Unsuffixed
102
- | ast:: LitIntType :: Unsigned ( ast:: UintTy :: U8 ) ,
103
- ) if val <= u8:: MAX . into ( ) => {
104
- accumulator. push ( val as u8 ) ;
105
- }
106
-
107
- ast:: LitKind :: Byte ( val) => {
108
- accumulator. push ( val) ;
109
- }
110
- ast:: LitKind :: ByteStr ( _) => {
111
- if !has_errors {
112
- cx. struct_span_err (
113
- expr. span ,
114
- "cannot concatenate doubly nested array" ,
115
- )
116
- . note ( "byte strings are treated as arrays of bytes" )
117
- . help ( "try flattening the array" )
118
- . emit ( ) ;
119
- }
120
- has_errors = true ;
121
- }
122
- _ => {
123
- if !has_errors {
124
- invalid_type_err ( cx, expr, true ) ;
125
- }
126
- has_errors = true ;
127
- }
128
- } ,
129
- _ => {
130
- missing_literals. push ( expr. span ) ;
137
+ if let Some ( elem) =
138
+ handle_array_element ( cx, & mut has_errors, & mut missing_literals, expr)
139
+ {
140
+ accumulator. push ( elem) ;
141
+ }
142
+ }
143
+ }
144
+ ast:: ExprKind :: Repeat ( ref expr, ref count) => {
145
+ if let ast:: ExprKind :: Lit ( ast:: Lit {
146
+ kind : ast:: LitKind :: Int ( count_val, _) , ..
147
+ } ) = count. value . kind
148
+ {
149
+ if let Some ( elem) =
150
+ handle_array_element ( cx, & mut has_errors, & mut missing_literals, expr)
151
+ {
152
+ for _ in 0 ..count_val {
153
+ accumulator. push ( elem) ;
131
154
}
132
155
}
156
+ } else {
157
+ cx. span_err ( count. value . span , "repeat count is not a number" ) ;
133
158
}
134
159
}
135
160
ast:: ExprKind :: Lit ( ref lit) => match lit. kind {
0 commit comments