Skip to content

Commit

Permalink
Improve errors in variable definitions by adding labels to the variab…
Browse files Browse the repository at this point in the history
…les.

BEGIN_PUBLIC
Improve errors in variable definitions by adding labels to the variables.
END_PUBLIC

PiperOrigin-RevId: 618984216
Change-Id: I5d6d11ba2b72f426b9f01bcbb528b0914c98c964
  • Loading branch information
Googler authored and copybara-github committed Mar 25, 2024
1 parent 7c0a3bb commit 61def7a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
3 changes: 2 additions & 1 deletion cc/toolchains/cc_toolchain_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ VariableInfo = provider(
# @unsorted-dict-items
fields = {
"name": "(str) The variable name",
"label": "(Label) The label defining this provider. Place in error messages to simplify debugging",
"actions": "(Optional[depset[ActionTypeInfo]]) The actions this variable is available for",
"type": "A type constructed using variables.types.*",
},
Expand All @@ -58,7 +59,7 @@ VariableInfo = provider(
BuiltinVariablesInfo = provider(
doc = "The builtin variables",
fields = {
"variables": "(dict[str, struct(type=type, actions=Optional[depset[ActionTypeInfo]]) A mapping from variable name to variable metadata.",
"variables": "(dict[str, VariableInfo]) A mapping from variable name to variable metadata.",
},
)

Expand Down
8 changes: 3 additions & 5 deletions cc/toolchains/impl/variables.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ types = struct(
def _cc_variable_impl(ctx):
return [VariableInfo(
name = ctx.label.name,
label = ctx.label,
type = json.decode(ctx.attr.type),
actions = collect_action_types(ctx.attr.actions) if ctx.attr.actions else None,
)]
Expand Down Expand Up @@ -84,10 +85,7 @@ def cc_variable(name, type, **kwargs):

def _cc_builtin_variables_impl(ctx):
return [BuiltinVariablesInfo(variables = {
variable.name: struct(
actions = variable.actions,
type = variable.type,
)
variable.name: variable
for variable in collect_provider(ctx.attr.srcs, VariableInfo)
})]

Expand Down Expand Up @@ -131,7 +129,7 @@ def get_type(*, name, variables, overrides, actions, args_label, nested_label, f
for action in actions:
if action not in valid_actions:
fail("The variable {var} is inaccessible from the action {action}. This is required because it is referenced in {nested_label}, which is included by {args_label}, which references that action".format(
var = outer,
var = variables[outer].label,
nested_label = nested_label,
args_label = args_label,
action = action.label,
Expand Down
2 changes: 1 addition & 1 deletion tests/rule_based_toolchain/variables/variables_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ nested_str_list: List[string]""")

expect_type("struct", actions = [c_compile]).ok()
expect_type("struct", actions = [c_compile, cpp_compile]).err().equals(
"The variable struct is inaccessible from the action %s. This is required because it is referenced in %s, which is included by %s, which references that action" % (cpp_compile.label, _NESTED_LABEL, _ARGS_LABEL),
"The variable %s is inaccessible from the action %s. This is required because it is referenced in %s, which is included by %s, which references that action" % (targets.struct.label, cpp_compile.label, _NESTED_LABEL, _ARGS_LABEL),
)

expect_type("struct.nested_str_list", actions = [c_compile]).ok()
Expand Down

0 comments on commit 61def7a

Please sign in to comment.