diff options
author | 2022-03-04 12:04:31 -0800 | |
---|---|---|
committer | 2022-03-08 22:58:42 +0000 | |
commit | 816e080c4daff4c82fc41afd369c0c250cac540f (patch) | |
tree | ad2e416aca0218be3e0701b405a0880ffa418c81 /mk2rbc/variable.go | |
parent | fc74246c98f9bfd541ec9e7807376a1b755f18f5 (diff) |
Call rblf.setDefault() when appending to a variable without +=
Bug: 222737841
Test: go test
Change-Id: I10e9e994fb1979e2e06ad30bbe66a21657d1e3db
Diffstat (limited to 'mk2rbc/variable.go')
-rw-r--r-- | mk2rbc/variable.go | 25 |
1 files changed, 19 insertions, 6 deletions
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) } + 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 - 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() + emitSetDefault() emitAppend() case asgnMaybeSet: gctx.writef("if cfg.get(%q) == None:", pcv.nam) |