From 4991693a9c89d7b57f6e7676a7d3ac105d0640f5 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Wed, 16 Aug 2023 17:00:57 -0700 Subject: [PATCH] fixes to avoid ambiguities in analyzing captures for script optimization --- src/Expr.cc | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Expr.cc b/src/Expr.cc index 817dd70418..6513cacecb 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -4648,6 +4648,7 @@ LambdaExpr::LambdaExpr(FunctionIngredientsPtr arg_ing, IDPList arg_outer_ids, st auto ingr_t = ingredients->GetID()->GetType(); SetType(ingr_t); + captures = ingr_t->GetCaptures(); if ( ! CheckCaptures(std::move(when_parent)) ) { @@ -4655,6 +4656,17 @@ LambdaExpr::LambdaExpr(FunctionIngredientsPtr arg_ing, IDPList arg_outer_ids, st 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 // by both broker (for transmitting closures) and script optimization // (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->SetConst(); - captures = ingr_t->GetCaptures(); - analyze_lambda(this); } @@ -4714,9 +4724,6 @@ LambdaExpr::LambdaExpr(LambdaExpr* orig) : Expr(EXPR_LAMBDA) bool LambdaExpr::CheckCaptures(StmtPtr when_parent) { - auto ft = type->AsFuncType(); - const auto& captures = ft->GetCaptures(); - auto desc = when_parent ? "\"when\" statement" : "lambda"; if ( ! captures )