@@ -9,8 +9,9 @@ Inspirational file: https://github.com/python/cpython/blob/master/Python/symtabl
9
9
10
10
use crate :: error:: { CompileError , CompileErrorType } ;
11
11
use crate :: IndexMap ;
12
+ use alloc:: { borrow:: ToOwned , format, string:: String , vec, vec:: Vec } ;
13
+ use core:: fmt;
12
14
use rustpython_ast:: { self as ast, Location } ;
13
- use std:: fmt;
14
15
15
16
pub fn make_symbol_table ( program : & ast:: Program ) -> Result < SymbolTable , SymbolTableError > {
16
17
let mut builder = SymbolTableBuilder :: default ( ) ;
@@ -187,8 +188,8 @@ impl SymbolTable {
187
188
}
188
189
}
189
190
190
- impl std :: fmt:: Debug for SymbolTable {
191
- fn fmt ( & self , f : & mut std :: fmt:: Formatter ) -> std :: fmt:: Result {
191
+ impl fmt:: Debug for SymbolTable {
192
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
192
193
write ! (
193
194
f,
194
195
"SymbolTable({:?} symbols, {:?} sub scopes)" ,
@@ -209,8 +210,9 @@ fn analyze_symbol_table(symbol_table: &mut SymbolTable) -> SymbolTableResult {
209
210
type SymbolMap = IndexMap < String , Symbol > ;
210
211
211
212
mod stack {
212
- use std:: panic;
213
- use std:: ptr:: NonNull ;
213
+ use alloc:: vec:: Vec ;
214
+ use core:: ptr:: NonNull ;
215
+
214
216
pub struct StackStack < T > {
215
217
v : Vec < NonNull < T > > ,
216
218
}
@@ -227,9 +229,10 @@ mod stack {
227
229
F : FnOnce ( & mut Self ) -> R ,
228
230
{
229
231
self . v . push ( x. into ( ) ) ;
230
- let res = panic:: catch_unwind ( panic:: AssertUnwindSafe ( || f ( self ) ) ) ;
231
- self . v . pop ( ) ;
232
- res. unwrap_or_else ( |x| panic:: resume_unwind ( x) )
232
+ let mut this = scopeguard:: guard ( self , |this| {
233
+ this. v . pop ( ) ;
234
+ } ) ;
235
+ f ( & mut this)
233
236
}
234
237
235
238
pub fn iter ( & self ) -> impl Iterator < Item = & T > + DoubleEndedIterator + ' _ {
@@ -273,7 +276,7 @@ struct SymbolTableAnalyzer {
273
276
274
277
impl SymbolTableAnalyzer {
275
278
fn analyze_symbol_table ( & mut self , symbol_table : & mut SymbolTable ) -> SymbolTableResult {
276
- let symbols = std :: mem:: take ( & mut symbol_table. symbols ) ;
279
+ let symbols = core :: mem:: take ( & mut symbol_table. symbols ) ;
277
280
let sub_tables = & mut * symbol_table. sub_tables ;
278
281
279
282
let mut info = ( symbols, symbol_table. typ ) ;
@@ -469,7 +472,7 @@ impl SymbolTableAnalyzer {
469
472
SymbolTableType :: Class => {
470
473
// named expressions are forbidden in comprehensions on class scope
471
474
return Err ( SymbolTableError {
472
- error : "assignment expression within a comprehension cannot be used in a class body" . to_string ( ) ,
475
+ error : "assignment expression within a comprehension cannot be used in a class body" . to_owned ( ) ,
473
476
// TODO: accurate location info, somehow
474
477
location : Location :: default ( ) ,
475
478
} ) ;
@@ -999,7 +1002,7 @@ impl SymbolTableBuilder {
999
1002
// comprehension iterator definitions
1000
1003
if let ExpressionContext :: IterDefinitionExp = context {
1001
1004
return Err ( SymbolTableError {
1002
- error : "assignment expression cannot be used in a comprehension iterable expression" . to_string ( ) ,
1005
+ error : "assignment expression cannot be used in a comprehension iterable expression" . to_owned ( ) ,
1003
1006
// TODO: accurate location info, somehow
1004
1007
location : Location :: default ( ) ,
1005
1008
} ) ;
@@ -1218,7 +1221,7 @@ impl SymbolTableBuilder {
1218
1221
return Err ( SymbolTableError {
1219
1222
error :
1220
1223
"assignment expression cannot be used in a comprehension iterable expression"
1221
- . to_string ( ) ,
1224
+ . to_owned ( ) ,
1222
1225
location,
1223
1226
} ) ;
1224
1227
}
0 commit comments