From 816e080c4daff4c82fc41afd369c0c250cac540f Mon Sep 17 00:00:00 2001 From: Cole Faust Date: Fri, 4 Mar 2022 12:04:31 -0800 Subject: [PATCH] Call rblf.setDefault() when appending to a variable without += Bug: 222737841 Test: go test Change-Id: I10e9e994fb1979e2e06ad30bbe66a21657d1e3db --- mk2rbc/mk2rbc_test.go | 1 + mk2rbc/variable.go | 29 +++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/mk2rbc/mk2rbc_test.go b/mk2rbc/mk2rbc_test.go index a162021b4..ce1a1f9b1 100644 --- a/mk2rbc/mk2rbc_test.go +++ b/mk2rbc/mk2rbc_test.go @@ -985,6 +985,7 @@ endif def init(g, handle): cfg = rblf.cfg(handle) if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []): + rblf.setdefault(handle, "PRODUCT_PACKAGES") cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split() `, }, diff --git a/mk2rbc/variable.go b/mk2rbc/variable.go index f7adca568..73afd1923 100644 --- a/mk2rbc/variable.go +++ b/mk2rbc/variable.go @@ -88,20 +88,33 @@ func (pcv productConfigVariable) emitSet(gctx *generationContext, asgn *assignme } value.emit(gctx) } - - switch asgn.flavor { - case asgnSet: - emitAssignment() - case asgnAppend: - emitAppend() - case asgnMaybeAppend: - // If we are not sure variable has been assigned before, emit setdefault + emitSetDefault := func() { if pcv.typ == starlarkTypeList { gctx.writef("%s(handle, %q)", cfnSetListDefault, pcv.name()) } else { gctx.writef("cfg.setdefault(%q, %s)", pcv.name(), pcv.defaultValueString()) } gctx.newLine() + } + + switch asgn.flavor { + case asgnSet: + isSelfReferential := false + asgn.value.transform(func(expr starlarkExpr) starlarkExpr { + if ref, ok := expr.(*variableRefExpr); ok && ref.ref.name() == pcv.name() { + isSelfReferential = true + } + return nil + }) + if isSelfReferential { + emitSetDefault() + } + emitAssignment() + case asgnAppend: + emitAppend() + case asgnMaybeAppend: + // If we are not sure variable has been assigned before, emit setdefault + emitSetDefault() emitAppend() case asgnMaybeSet: gctx.writef("if cfg.get(%q) == None:", pcv.nam)