Skip to content

Commit ee4a51c

Browse files
authored
Merge pull request RustPython#4328 from andersk/asdl-box
Remove unnecessary boxing of ASDL product children
2 parents c48c229 + 316c485 commit ee4a51c

File tree

5 files changed

+15
-17
lines changed

5 files changed

+15
-17
lines changed

compiler/ast/asdl_rs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def __init__(self, name):
8989
self.has_userdata = None
9090
self.children = set()
9191
self.boxed = False
92+
self.product = False
9293

9394
def __repr__(self):
9495
return f"<TypeInfo: {self.name}>"
@@ -145,6 +146,7 @@ def visitProduct(self, product, name):
145146
info.has_userdata = True
146147
if len(product.fields) > 2:
147148
info.boxed = True
149+
info.product = True
148150
self.add_children(name, product.fields)
149151

150152
def add_children(self, name, fields):
@@ -236,7 +238,7 @@ def visitField(self, field, parent, vis, depth):
236238
if fieldtype and fieldtype.has_userdata:
237239
typ = f"{typ}<U>"
238240
# don't box if we're doing Vec<T>, but do box if we're doing Vec<Option<Box<T>>>
239-
if fieldtype and fieldtype.boxed and (not field.seq or field.opt):
241+
if fieldtype and fieldtype.boxed and (not (parent.product or field.seq) or field.opt):
240242
typ = f"Box<{typ}>"
241243
if field.opt:
242244
typ = f"Option<{typ}>"

compiler/ast/src/ast_gen.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ pub enum Cmpop {
337337

338338
#[derive(Clone, Debug, PartialEq)]
339339
pub struct Comprehension<U = ()> {
340-
pub target: Box<Expr<U>>,
341-
pub iter: Box<Expr<U>>,
340+
pub target: Expr<U>,
341+
pub iter: Expr<U>,
342342
pub ifs: Vec<Expr<U>>,
343343
pub is_async: usize,
344344
}
@@ -375,7 +375,7 @@ pub type Arg<U = ()> = Located<ArgData<U>, U>;
375375
#[derive(Clone, Debug, PartialEq)]
376376
pub struct KeywordData<U = ()> {
377377
pub arg: Option<Ident>,
378-
pub value: Box<Expr<U>>,
378+
pub value: Expr<U>,
379379
}
380380
pub type Keyword<U = ()> = Located<KeywordData<U>, U>;
381381

@@ -388,13 +388,13 @@ pub type Alias<U = ()> = Located<AliasData, U>;
388388

389389
#[derive(Clone, Debug, PartialEq)]
390390
pub struct Withitem<U = ()> {
391-
pub context_expr: Box<Expr<U>>,
391+
pub context_expr: Expr<U>,
392392
pub optional_vars: Option<Box<Expr<U>>>,
393393
}
394394

395395
#[derive(Clone, Debug, PartialEq)]
396396
pub struct MatchCase<U = ()> {
397-
pub pattern: Box<Pattern<U>>,
397+
pub pattern: Pattern<U>,
398398
pub guard: Option<Box<Expr<U>>>,
399399
pub body: Vec<Stmt<U>>,
400400
}

compiler/parser/python.lalrpop

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -520,12 +520,12 @@ WithItems: Vec<ast::Withitem> = {
520520
<items:TestAs<ExprOrWithitemsGoal>> =>? items.try_into(),
521521
<first:TestAs<ExprOrWithitemsGoal>> "as" <vars:Expression> =>? {
522522
let optional_vars = Some(Box::new(set_context(vars, ast::ExprContext::Store)));
523-
let context_expr = Box::new(first.try_into()?);
523+
let context_expr = first.try_into()?;
524524
Ok(vec![ast::Withitem { context_expr, optional_vars }])
525525
},
526526
<first:TestAs<ExprOrWithitemsGoal>> <n:("as" Expression)?> "," <mut items:OneOrMore<WithItem>> =>? {
527527
let optional_vars = n.map(|val| Box::new(set_context(val.1, ast::ExprContext::Store)));
528-
let context_expr = Box::new(first.try_into()?);
528+
let context_expr = first.try_into()?;
529529
items.insert(0, ast::Withitem { context_expr, optional_vars });
530530
Ok(items)
531531
}
@@ -534,7 +534,6 @@ WithItems: Vec<ast::Withitem> = {
534534
WithItem: ast::Withitem = {
535535
<context_expr:Test> <n:("as" Expression)?> => {
536536
let optional_vars = n.map(|val| Box::new(set_context(val.1, ast::ExprContext::Store)));
537-
let context_expr = Box::new(context_expr);
538537
ast::Withitem { context_expr, optional_vars }
539538
},
540539
};
@@ -1280,8 +1279,8 @@ SingleForComprehension: ast::Comprehension = {
12801279
<location:@L> <is_async:"async"?> "for" <target:ExpressionList> "in" <iter:OrTest> <ifs:ComprehensionIf*> <end_location:@R> => {
12811280
let is_async = is_async.is_some();
12821281
ast::Comprehension {
1283-
target: Box::new(set_context(target, ast::ExprContext::Store)),
1284-
iter: Box::new(iter),
1282+
target: set_context(target, ast::ExprContext::Store),
1283+
iter,
12851284
ifs,
12861285
is_async: if is_async { 1 } else { 0 },
12871286
}

compiler/parser/src/function.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ pub fn parse_args(func_args: Vec<FunctionArgument>) -> Result<ArgumentList, Lexi
7272
keywords.push(ast::Keyword::new(
7373
start,
7474
end,
75-
ast::KeywordData {
76-
arg: name,
77-
value: Box::new(value),
78-
},
75+
ast::KeywordData { arg: name, value },
7976
));
8077
}
8178
None => {

compiler/parser/src/with.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ impl TryFrom<ExprOrWithitems> for Vec<ast::Withitem> {
117117
.items
118118
.into_iter()
119119
.map(|(context_expr, optional_vars)| ast::Withitem {
120-
context_expr: Box::new(context_expr),
120+
context_expr,
121121
optional_vars: optional_vars.map(|expr| {
122122
Box::new(context::set_context(*expr, ast::ExprContext::Store))
123123
}),
124124
})
125125
.collect())
126126
}
127127
_ => Ok(vec![ast::Withitem {
128-
context_expr: Box::new(expr_or_withitems.try_into()?),
128+
context_expr: expr_or_withitems.try_into()?,
129129
optional_vars: None,
130130
}]),
131131
}

0 commit comments

Comments
 (0)