fixes to avoid ambiguities in analyzing captures for script optimization

This commit is contained in:
Vern Paxson 2023-08-16 17:00:57 -07:00
parent 3e0f814635
commit 4991693a9c

View file

@ -4648,6 +4648,7 @@ LambdaExpr::LambdaExpr(FunctionIngredientsPtr arg_ing, IDPList arg_outer_ids, st
auto ingr_t = ingredients->GetID()->GetType<FuncType>(); auto ingr_t = ingredients->GetID()->GetType<FuncType>();
SetType(ingr_t); SetType(ingr_t);
captures = ingr_t->GetCaptures();
if ( ! CheckCaptures(std::move(when_parent)) ) if ( ! CheckCaptures(std::move(when_parent)) )
{ {
@ -4655,6 +4656,17 @@ LambdaExpr::LambdaExpr(FunctionIngredientsPtr arg_ing, IDPList arg_outer_ids, st
return; return;
} }
// Now that we've validated that the captures match the outer_ids,
// we regenerate the latter to come in the same order as the captures.
// This avoids potentially subtle bugs when doing script optimization
// where one context uses the outer_ids and another uses the captures.
if ( captures )
{
outer_ids.clear();
for ( auto& c : *captures )
outer_ids.append(c.Id().get());
}
// Install a primary version of the function globally. This is used // Install a primary version of the function globally. This is used
// by both broker (for transmitting closures) and script optimization // by both broker (for transmitting closures) and script optimization
// (replacing its AST body with a compiled one). // (replacing its AST body with a compiled one).
@ -4684,8 +4696,6 @@ LambdaExpr::LambdaExpr(FunctionIngredientsPtr arg_ing, IDPList arg_outer_ids, st
lambda_id->SetType(ingr_t); lambda_id->SetType(ingr_t);
lambda_id->SetConst(); lambda_id->SetConst();
captures = ingr_t->GetCaptures();
analyze_lambda(this); analyze_lambda(this);
} }
@ -4714,9 +4724,6 @@ LambdaExpr::LambdaExpr(LambdaExpr* orig) : Expr(EXPR_LAMBDA)
bool LambdaExpr::CheckCaptures(StmtPtr when_parent) bool LambdaExpr::CheckCaptures(StmtPtr when_parent)
{ {
auto ft = type->AsFuncType();
const auto& captures = ft->GetCaptures();
auto desc = when_parent ? "\"when\" statement" : "lambda"; auto desc = when_parent ? "\"when\" statement" : "lambda";
if ( ! captures ) if ( ! captures )