Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.5.0] Backport support for all attribute types in Starlark documentation extraction #25093

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import net.starlark.java.eval.Dict;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkList;
import net.starlark.java.eval.StarlarkValue;
import net.starlark.java.eval.Structure;

Expand Down Expand Up @@ -1439,7 +1440,7 @@ private Object computeValue(EventHandler eventHandler, AttributeMap rule)
if (!Starlark.isNullOrNone(value)) {
// Some attribute values are not valid Starlark values:
// visibility is an ImmutableList, for example.
attrValues.put(attr.getName(), Starlark.fromJava(value, /*mutability=*/ null));
attrValues.put(attr.getName(), Starlark.fromJava(value, /* mutability= */ null));
}
}
}
Expand Down Expand Up @@ -1732,7 +1733,6 @@ LabelLateBoundDefault<FragmentT> fromTargetConfigurationWithRuleBasedDefault(
+ "configuration.");
return new LabelLateBoundDefault<>(fragmentClass, defaultValueEvaluator, resolver);
}

}

/** A {@link LateBoundDefault} for a {@link List} of {@link Label} objects. */
Expand Down Expand Up @@ -2364,8 +2364,8 @@ public Attribute.Builder<?> cloneBuilder() {
* </ol>
*/
public static Object valueToStarlark(Object x) {
// Is x a non-empty string_list_dict?
if (x instanceof Map) {
// Is x a non-empty string_list_dict?
Map<?, ?> map = (Map<?, ?>) x;
if (!map.isEmpty() && map.values().iterator().next() instanceof List) {
// Recursively convert subelements.
Expand All @@ -2375,6 +2375,15 @@ public static Object valueToStarlark(Object x) {
}
return dict.buildImmutable();
}
} else if (x instanceof Set) {
// Until Starlark gains a set data type, shallow-convert Java sets (e.g. DISTRIBUTION values)
// to Starlark lists.
Set<?> set = (Set<?>) x;
return StarlarkList.immutableCopyOf(set);
} else if (x instanceof TriState) {
// Convert TriState to integer (same as in query output and native.existing_rules())
TriState triState = (TriState) x;
return triState.toInt();
}

// For all other attribute values, shallow conversion is safe.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.util.function.Predicate;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Module;
import net.starlark.java.eval.Starlark.InvalidStarlarkValueException;
import net.starlark.java.eval.StarlarkFunction;
import net.starlark.java.eval.Structure;

Expand Down Expand Up @@ -511,9 +512,11 @@ private static AttributeType getAttributeType(Attribute attribute, String where)
Type<?> type = attribute.getType();
if (type.equals(Type.INTEGER)) {
return AttributeType.INT;
} else if (type.equals(BuildType.LABEL)) {
} else if (type.equals(BuildType.LABEL)
|| type.equals(BuildType.NODEP_LABEL)
|| type.equals(BuildType.GENQUERY_SCOPE_TYPE)) {
return AttributeType.LABEL;
} else if (type.equals(Type.STRING)) {
} else if (type.equals(Type.STRING) || type.equals(Type.STRING_NO_INTERN)) {
if (attribute.getPublicName().equals("name")) {
return AttributeType.NAME;
} else {
Expand All @@ -523,12 +526,16 @@ private static AttributeType getAttributeType(Attribute attribute, String where)
return AttributeType.STRING_LIST;
} else if (type.equals(Type.INTEGER_LIST)) {
return AttributeType.INT_LIST;
} else if (type.equals(BuildType.LABEL_LIST)) {
} else if (type.equals(BuildType.LABEL_LIST)
|| type.equals(BuildType.NODEP_LABEL_LIST)
|| type.equals(BuildType.GENQUERY_SCOPE_TYPE_LIST)) {
return AttributeType.LABEL_LIST;
} else if (type.equals(Type.BOOLEAN)) {
return AttributeType.BOOLEAN;
} else if (type.equals(BuildType.LABEL_KEYED_STRING_DICT)) {
return AttributeType.LABEL_STRING_DICT;
} else if (type.equals(BuildType.LABEL_DICT_UNARY)) {
return AttributeType.LABEL_DICT_UNARY;
} else if (type.equals(Type.STRING_DICT)) {
return AttributeType.STRING_DICT;
} else if (type.equals(Type.STRING_LIST_DICT)) {
Expand All @@ -537,12 +544,16 @@ private static AttributeType getAttributeType(Attribute attribute, String where)
return AttributeType.OUTPUT;
} else if (type.equals(BuildType.OUTPUT_LIST)) {
return AttributeType.OUTPUT_LIST;
} else if (type.equals(BuildType.LICENSE)) {
} else if (type.equals(BuildType.LICENSE) || type.equals(BuildType.DISTRIBUTIONS)) {
// TODO(https://github.com/bazelbuild/bazel/issues/6420): deprecated, disabled in Bazel by
// default, broken and with almost no remaining users, so we don't have an AttributeType for
// it. Until this type is removed, following the example of legacy Stardoc, pretend it's a
// list of strings.
return AttributeType.STRING_LIST;
} else if (type.equals(BuildType.TRISTATE)) {
// Given that the native TRISTATE type is not exposed to Starlark attr API, let's treat it
// as an integer.
return AttributeType.INT;
}

throw new ExtractionException(
Expand All @@ -569,8 +580,16 @@ private AttributeInfo buildAttributeInfo(Attribute attribute, String where)
}

if (!attribute.isMandatory()) {
Object defaultValue = Attribute.valueToStarlark(attribute.getDefaultValueUnchecked());
builder.setDefaultValue(labelRenderer.reprWithoutLabelConstructor(defaultValue));
try {
Object defaultValue = Attribute.valueToStarlark(attribute.getDefaultValueUnchecked());
builder.setDefaultValue(labelRenderer.reprWithoutLabelConstructor(defaultValue));
} catch (InvalidStarlarkValueException e) {
throw new ExtractionException(
String.format(
"in %s attribute %s: failed to convert default value to Starlark: %s",
where, attribute.getPublicName(), e.getMessage()),
e);
}
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ public void attributeTypes() throws Exception {
" 'j': attr.string_list_dict(),",
" 'k': attr.output(),",
" 'l': attr.output_list(),",
" 'm': attr.string_keyed_label_dict(),",
" }",
")");
ModuleInfo moduleInfo = getExtractor().extractFrom(module);
Expand Down Expand Up @@ -731,6 +732,11 @@ public void attributeTypes() throws Exception {
.setType(AttributeType.OUTPUT_LIST)
.setDefaultValue("[]")
.setNonconfigurable(true)
.build(),
AttributeInfo.newBuilder()
.setName("m")
.setType(AttributeType.LABEL_DICT_UNARY)
.setDefaultValue("{}")
.build());
}

Expand Down
Loading