1- use rustc_ast as ast;
2- use rustc_ast:: { ptr:: P , tokenstream:: TokenStream } ;
3- use rustc_expand:: base:: { self , DummyResult } ;
1+ use rustc_ast:: { ptr:: P , token, tokenstream:: TokenStream , ExprKind , LitIntType , LitKind , UintTy } ;
2+ use rustc_expand:: base:: { get_exprs_from_tts, DummyResult , ExtCtxt , MacEager , MacResult } ;
43use rustc_session:: errors:: report_lit_error;
54use rustc_span:: { ErrorGuaranteed , Span } ;
65
76use crate :: errors;
87
98/// Emits errors for literal expressions that are invalid inside and outside of an array.
109fn invalid_type_err (
11- cx : & mut base :: ExtCtxt < ' _ > ,
12- token_lit : ast :: token:: Lit ,
10+ cx : & mut ExtCtxt < ' _ > ,
11+ token_lit : token:: Lit ,
1312 span : Span ,
1413 is_nested : bool ,
1514) -> ErrorGuaranteed {
@@ -18,18 +17,18 @@ fn invalid_type_err(
1817 } ;
1918 let snippet = cx. sess . source_map ( ) . span_to_snippet ( span) . ok ( ) ;
2019 let dcx = cx. dcx ( ) ;
21- match ast :: LitKind :: from_token_lit ( token_lit) {
22- Ok ( ast :: LitKind :: CStr ( _, _) ) => {
20+ match LitKind :: from_token_lit ( token_lit) {
21+ Ok ( LitKind :: CStr ( _, _) ) => {
2322 // Avoid ambiguity in handling of terminal `NUL` by refusing to
2423 // concatenate C string literals as bytes.
2524 dcx. emit_err ( errors:: ConcatCStrLit { span } )
2625 }
27- Ok ( ast :: LitKind :: Char ( _) ) => {
26+ Ok ( LitKind :: Char ( _) ) => {
2827 let sugg =
2928 snippet. map ( |snippet| ConcatBytesInvalidSuggestion :: CharLit { span, snippet } ) ;
3029 dcx. emit_err ( ConcatBytesInvalid { span, lit_kind : "character" , sugg } )
3130 }
32- Ok ( ast :: LitKind :: Str ( _, _) ) => {
31+ Ok ( LitKind :: Str ( _, _) ) => {
3332 // suggestion would be invalid if we are nested
3433 let sugg = if !is_nested {
3534 snippet. map ( |snippet| ConcatBytesInvalidSuggestion :: StrLit { span, snippet } )
@@ -38,27 +37,24 @@ fn invalid_type_err(
3837 } ;
3938 dcx. emit_err ( ConcatBytesInvalid { span, lit_kind : "string" , sugg } )
4039 }
41- Ok ( ast :: LitKind :: Float ( _, _) ) => {
40+ Ok ( LitKind :: Float ( _, _) ) => {
4241 dcx. emit_err ( ConcatBytesInvalid { span, lit_kind : "float" , sugg : None } )
4342 }
44- Ok ( ast :: LitKind :: Bool ( _) ) => {
43+ Ok ( LitKind :: Bool ( _) ) => {
4544 dcx. emit_err ( ConcatBytesInvalid { span, lit_kind : "boolean" , sugg : None } )
4645 }
47- Ok ( ast :: LitKind :: Int ( _, _) ) if !is_nested => {
46+ Ok ( LitKind :: Int ( _, _) ) if !is_nested => {
4847 let sugg =
49- snippet. map ( |snippet| ConcatBytesInvalidSuggestion :: IntLit { span : span , snippet } ) ;
48+ snippet. map ( |snippet| ConcatBytesInvalidSuggestion :: IntLit { span, snippet } ) ;
5049 dcx. emit_err ( ConcatBytesInvalid { span, lit_kind : "numeric" , sugg } )
5150 }
52- Ok ( ast:: LitKind :: Int (
53- val,
54- ast:: LitIntType :: Unsuffixed | ast:: LitIntType :: Unsigned ( ast:: UintTy :: U8 ) ,
55- ) ) => {
51+ Ok ( LitKind :: Int ( val, LitIntType :: Unsuffixed | LitIntType :: Unsigned ( UintTy :: U8 ) ) ) => {
5652 assert ! ( val. get( ) > u8 :: MAX . into( ) ) ; // must be an error
5753 dcx. emit_err ( ConcatBytesOob { span } )
5854 }
59- Ok ( ast :: LitKind :: Int ( _, _) ) => dcx. emit_err ( ConcatBytesNonU8 { span } ) ,
60- Ok ( ast :: LitKind :: ByteStr ( ..) | ast :: LitKind :: Byte ( _) ) => unreachable ! ( ) ,
61- Ok ( ast :: LitKind :: Err ( guar) ) => guar,
55+ Ok ( LitKind :: Int ( _, _) ) => dcx. emit_err ( ConcatBytesNonU8 { span } ) ,
56+ Ok ( LitKind :: ByteStr ( ..) | LitKind :: Byte ( _) ) => unreachable ! ( ) ,
57+ Ok ( LitKind :: Err ( guar) ) => guar,
6258 Err ( err) => report_lit_error ( & cx. sess . parse_sess , err, token_lit, span) ,
6359 }
6460}
@@ -68,24 +64,24 @@ fn invalid_type_err(
6864/// Otherwise, returns `None`, and either pushes the `expr`'s span to `missing_literals` or
6965/// updates `guar` accordingly.
7066fn handle_array_element (
71- cx : & mut base :: ExtCtxt < ' _ > ,
67+ cx : & mut ExtCtxt < ' _ > ,
7268 guar : & mut Option < ErrorGuaranteed > ,
7369 missing_literals : & mut Vec < rustc_span:: Span > ,
7470 expr : & P < rustc_ast:: Expr > ,
7571) -> Option < u8 > {
7672 let dcx = cx. dcx ( ) ;
7773
7874 match expr. kind {
79- ast :: ExprKind :: Lit ( token_lit) => {
80- match ast :: LitKind :: from_token_lit ( token_lit) {
81- Ok ( ast :: LitKind :: Int (
75+ ExprKind :: Lit ( token_lit) => {
76+ match LitKind :: from_token_lit ( token_lit) {
77+ Ok ( LitKind :: Int (
8278 val,
83- ast :: LitIntType :: Unsuffixed | ast :: LitIntType :: Unsigned ( ast :: UintTy :: U8 ) ,
79+ LitIntType :: Unsuffixed | LitIntType :: Unsigned ( UintTy :: U8 ) ,
8480 ) ) if let Ok ( val) = u8:: try_from ( val. get ( ) ) => {
8581 return Some ( val) ;
8682 }
87- Ok ( ast :: LitKind :: Byte ( val) ) => return Some ( val) ,
88- Ok ( ast :: LitKind :: ByteStr ( ..) ) => {
83+ Ok ( LitKind :: Byte ( val) ) => return Some ( val) ,
84+ Ok ( LitKind :: ByteStr ( ..) ) => {
8985 guar. get_or_insert_with ( || {
9086 dcx. emit_err ( errors:: ConcatBytesArray { span : expr. span , bytestr : true } )
9187 } ) ;
@@ -95,12 +91,12 @@ fn handle_array_element(
9591 }
9692 } ;
9793 }
98- ast :: ExprKind :: Array ( _) | ast :: ExprKind :: Repeat ( _, _) => {
94+ ExprKind :: Array ( _) | ExprKind :: Repeat ( _, _) => {
9995 guar. get_or_insert_with ( || {
10096 dcx. emit_err ( errors:: ConcatBytesArray { span : expr. span , bytestr : false } )
10197 } ) ;
10298 }
103- ast :: ExprKind :: IncludedBytes ( ..) => {
99+ ExprKind :: IncludedBytes ( ..) => {
104100 guar. get_or_insert_with ( || {
105101 dcx. emit_err ( errors:: ConcatBytesArray { span : expr. span , bytestr : false } )
106102 } ) ;
@@ -112,11 +108,11 @@ fn handle_array_element(
112108}
113109
114110pub fn expand_concat_bytes (
115- cx : & mut base :: ExtCtxt < ' _ > ,
116- sp : rustc_span :: Span ,
111+ cx : & mut ExtCtxt < ' _ > ,
112+ sp : Span ,
117113 tts : TokenStream ,
118- ) -> Box < dyn base :: MacResult + ' static > {
119- let es = match base :: get_exprs_from_tts ( cx, tts) {
114+ ) -> Box < dyn MacResult + ' static > {
115+ let es = match get_exprs_from_tts ( cx, tts) {
120116 Ok ( es) => es,
121117 Err ( guar) => return DummyResult :: any ( sp, guar) ,
122118 } ;
@@ -125,7 +121,7 @@ pub fn expand_concat_bytes(
125121 let mut guar = None ;
126122 for e in es {
127123 match & e. kind {
128- ast :: ExprKind :: Array ( exprs) => {
124+ ExprKind :: Array ( exprs) => {
129125 for expr in exprs {
130126 if let Some ( elem) =
131127 handle_array_element ( cx, & mut guar, & mut missing_literals, expr)
@@ -134,10 +130,9 @@ pub fn expand_concat_bytes(
134130 }
135131 }
136132 }
137- ast:: ExprKind :: Repeat ( expr, count) => {
138- if let ast:: ExprKind :: Lit ( token_lit) = count. value . kind
139- && let Ok ( ast:: LitKind :: Int ( count_val, _) ) =
140- ast:: LitKind :: from_token_lit ( token_lit)
133+ ExprKind :: Repeat ( expr, count) => {
134+ if let ExprKind :: Lit ( token_lit) = count. value . kind
135+ && let Ok ( LitKind :: Int ( count_val, _) ) = LitKind :: from_token_lit ( token_lit)
141136 {
142137 if let Some ( elem) =
143138 handle_array_element ( cx, & mut guar, & mut missing_literals, expr)
@@ -152,35 +147,35 @@ pub fn expand_concat_bytes(
152147 ) ;
153148 }
154149 }
155- & ast :: ExprKind :: Lit ( token_lit) => match ast :: LitKind :: from_token_lit ( token_lit) {
156- Ok ( ast :: LitKind :: Byte ( val) ) => {
150+ & ExprKind :: Lit ( token_lit) => match LitKind :: from_token_lit ( token_lit) {
151+ Ok ( LitKind :: Byte ( val) ) => {
157152 accumulator. push ( val) ;
158153 }
159- Ok ( ast :: LitKind :: ByteStr ( ref bytes, _) ) => {
154+ Ok ( LitKind :: ByteStr ( ref bytes, _) ) => {
160155 accumulator. extend_from_slice ( bytes) ;
161156 }
162157 _ => {
163158 guar. get_or_insert_with ( || invalid_type_err ( cx, token_lit, e. span , false ) ) ;
164159 }
165160 } ,
166- ast :: ExprKind :: IncludedBytes ( bytes) => {
161+ ExprKind :: IncludedBytes ( bytes) => {
167162 accumulator. extend_from_slice ( bytes) ;
168163 }
169- ast :: ExprKind :: Err ( guarantee) => {
164+ ExprKind :: Err ( guarantee) => {
170165 guar = Some ( * guarantee) ;
171166 }
172- ast :: ExprKind :: Dummy => cx. dcx ( ) . span_bug ( e. span , "concatenating `ExprKind::Dummy`" ) ,
167+ ExprKind :: Dummy => cx. dcx ( ) . span_bug ( e. span , "concatenating `ExprKind::Dummy`" ) ,
173168 _ => {
174169 missing_literals. push ( e. span ) ;
175170 }
176171 }
177172 }
178173 if !missing_literals. is_empty ( ) {
179174 let guar = cx. dcx ( ) . emit_err ( errors:: ConcatBytesMissingLiteral { spans : missing_literals } ) ;
180- return base :: MacEager :: expr ( DummyResult :: raw_expr ( sp, Some ( guar) ) ) ;
175+ return MacEager :: expr ( DummyResult :: raw_expr ( sp, Some ( guar) ) ) ;
181176 } else if let Some ( guar) = guar {
182- return base :: MacEager :: expr ( DummyResult :: raw_expr ( sp, Some ( guar) ) ) ;
177+ return MacEager :: expr ( DummyResult :: raw_expr ( sp, Some ( guar) ) ) ;
183178 }
184179 let sp = cx. with_def_site_ctxt ( sp) ;
185- base :: MacEager :: expr ( cx. expr_byte_str ( sp, accumulator) )
180+ MacEager :: expr ( cx. expr_byte_str ( sp, accumulator) )
186181}
0 commit comments