From 421a192d056eeb02011d52c89b7c972ddb19ee87 Mon Sep 17 00:00:00 2001 From: Cole Faust Date: Wed, 16 Mar 2022 14:35:45 -0700 Subject: [PATCH] Convert values to strings when assigning to a string variable Currently, a string variable can easily be reassigned to a different type. Make it so that the value it's being reassigned to is converted to a string first. Bug: 224601891 Test: go test Change-Id: I82252cf9e106b5a3677458cf1df2e9d1dfefe0f6 --- mk2rbc/mk2rbc.go | 6 ++++++ mk2rbc/mk2rbc_test.go | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/mk2rbc/mk2rbc.go b/mk2rbc/mk2rbc.go index 35d7d4de4..212b7f658 100644 --- a/mk2rbc/mk2rbc.go +++ b/mk2rbc/mk2rbc.go @@ -566,6 +566,12 @@ func (ctx *parseContext) handleAssignment(a *mkparser.Assignment) []starlarkNode } } + if asgn.lhs.valueType() == starlarkTypeString && + asgn.value.typ() != starlarkTypeUnknown && + asgn.value.typ() != starlarkTypeString { + asgn.value = &toStringExpr{expr: asgn.value} + } + asgn.previous = ctx.lastAssignment(lhs) ctx.setLastAssignment(lhs, asgn) switch a.Type { diff --git a/mk2rbc/mk2rbc_test.go b/mk2rbc/mk2rbc_test.go index 408466041..08926e5fa 100644 --- a/mk2rbc/mk2rbc_test.go +++ b/mk2rbc/mk2rbc_test.go @@ -1265,7 +1265,7 @@ TEST_VAR_LIST := foo TEST_VAR_LIST += bar TEST_VAR_2 := $(if $(TEST_VAR),bar) TEST_VAR_3 := $(if $(TEST_VAR),bar,baz) -TEST_VAR_3 := $(if $(TEST_VAR),$(TEST_VAR_LIST)) +TEST_VAR_4 := $(if $(TEST_VAR),$(TEST_VAR_LIST)) `, expected: `load("//build/make/core:product_config.rbc", "rblf") @@ -1276,7 +1276,7 @@ def init(g, handle): g["TEST_VAR_LIST"] += ["bar"] g["TEST_VAR_2"] = ("bar" if g["TEST_VAR"] else "") g["TEST_VAR_3"] = ("bar" if g["TEST_VAR"] else "baz") - g["TEST_VAR_3"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else []) + g["TEST_VAR_4"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else []) `, }, { @@ -1419,6 +1419,7 @@ def init(g, handle): # Duplicated variable #RBC# type_hint list MY_VAR_2 #RBC# type_hint list my-local-var-with-dashes +#RBC# type_hint string MY_STRING_VAR MY_VAR := foo MY_VAR_UNHINTED := foo @@ -1431,6 +1432,8 @@ MY_VAR_2 := foo MY_VAR_4 := foo my-local-var-with-dashes := foo + +MY_STRING_VAR := $(wildcard foo/bar.mk) `, expected: `# Test type hints # Unsupported type @@ -1448,9 +1451,10 @@ def init(g, handle): # Vars set after other statements still get the hint g["MY_VAR_2"] = ["foo"] # You can't specify a type hint after the first statement - rblf.mk2rbc_error("product.mk:19", "type_hint annotations must come before the first Makefile statement") + rblf.mk2rbc_error("product.mk:20", "type_hint annotations must come before the first Makefile statement") g["MY_VAR_4"] = "foo" _my_local_var_with_dashes = ["foo"] + g["MY_STRING_VAR"] = " ".join(rblf.expand_wildcard("foo/bar.mk")) `, }, }