Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 421a192d authored by Cole Faust's avatar Cole Faust
Browse files

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
parent 23162405
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -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 {
+7 −3
Original line number Diff line number Diff line
@@ -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"))
`,
	},
}