diff --git a/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java b/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java
index bfb3a1e36c2343..c8ea38de63192e 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java
@@ -627,8 +627,7 @@ public StarlarkAspect aspect(
*
*
Exactly one of {@link #builder} or {@link #ruleClass} is null except inside {@link #export}.
*/
- public static final class StarlarkRuleFunction
- implements StarlarkCallable, StarlarkExportable, RuleFunction {
+ public static final class StarlarkRuleFunction implements StarlarkExportable, RuleFunction {
private RuleClass.Builder builder;
private RuleClass ruleClass;
@@ -637,6 +636,10 @@ public static final class StarlarkRuleFunction
private final Location definitionLocation;
private Label starlarkLabel;
+ // TODO(adonovan): merge {Starlark,Builtin}RuleFunction and RuleClass,
+ // making the latter a callable, StarlarkExportable value.
+ // (Making RuleClasses first-class values will help us to build a
+ // rich query output mode that includes values from loaded .bzl files.)
public StarlarkRuleFunction(
RuleClass.Builder builder,
RuleClassType type,
@@ -665,7 +668,7 @@ private StarlarkRuleFunction(
@Override
public String getName() {
- return "rule";
+ return ruleClass != null ? ruleClass.getName() : "unexported rule";
}
@Override
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/BUILD b/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/BUILD
index 2af591bc6b634c..8f141dda4ee201 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/BUILD
+++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/BUILD
@@ -12,6 +12,7 @@ java_library(
name = "starlark",
srcs = glob(["*.java"]),
deps = [
+ "//src/main/java/com/google/devtools/build/docgen/annot",
"//src/main/java/com/google/devtools/build/lib:runtime",
"//src/main/java/com/google/devtools/build/lib/actions:file_metadata",
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
@@ -45,6 +46,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/com/google/devtools/build/skyframe",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
+ "//src/main/java/net/starlark/java/annot",
"//src/main/java/net/starlark/java/eval",
"//third_party:guava",
"//third_party:java-diff-utils",
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryModule.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryModule.java
index a45f85f6344dc6..2c9a8734023881 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryModule.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryModule.java
@@ -20,6 +20,7 @@
import static com.google.devtools.build.lib.packages.Type.STRING_LIST;
import com.google.common.collect.ImmutableList;
+import com.google.devtools.build.docgen.annot.DocCategory;
import com.google.devtools.build.lib.analysis.BaseRuleClasses;
import com.google.devtools.build.lib.analysis.starlark.StarlarkAttrModule.Descriptor;
import com.google.devtools.build.lib.cmdline.Label;
@@ -40,6 +41,7 @@
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
import com.google.devtools.build.lib.starlarkbuildapi.repository.RepositoryModuleApi;
import java.util.Map;
+import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.eval.Dict;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Module;
@@ -110,6 +112,12 @@ public StarlarkCallable repositoryRule(
// RepositoryRuleFunction is the result of repository_rule(...).
// It is a callable value; calling it yields a Rule instance.
+ @StarlarkBuiltin(
+ name = "repository_rule",
+ category = DocCategory.BUILTIN,
+ doc =
+ "A callable value that may be invoked during evaluation of the WORKSPACE file to"
+ + " instantiate and return a repository rule.")
private static final class RepositoryRuleFunction
implements StarlarkCallable, StarlarkExportable {
private final RuleClass.Builder builder;
diff --git a/src/main/java/com/google/devtools/build/lib/packages/PackageFactory.java b/src/main/java/com/google/devtools/build/lib/packages/PackageFactory.java
index 35dd758fc17854..eb4baa011f3621 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/PackageFactory.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/PackageFactory.java
@@ -429,7 +429,7 @@ private static ImmutableMap buildRuleFunctions(
/** A callable Starlark value that creates Rules for native RuleClasses. */
// TODO(adonovan): why is this distinct from RuleClass itself?
// Make RuleClass implement StarlarkCallable directly.
- private static class BuiltinRuleFunction implements StarlarkCallable, RuleFunction {
+ private static class BuiltinRuleFunction implements RuleFunction {
private final RuleClass ruleClass;
BuiltinRuleFunction(RuleClass ruleClass) {
diff --git a/src/main/java/com/google/devtools/build/lib/packages/RuleFunction.java b/src/main/java/com/google/devtools/build/lib/packages/RuleFunction.java
index 1be4d07454f593..3d6aab70744642 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/RuleFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/RuleFunction.java
@@ -14,7 +14,18 @@
package com.google.devtools.build.lib.packages;
+import com.google.devtools.build.docgen.annot.DocCategory;
+import net.starlark.java.annot.StarlarkBuiltin;
+import net.starlark.java.eval.StarlarkCallable;
+
/** Interface for a native or Starlark rule function. */
-public interface RuleFunction {
+@StarlarkBuiltin(
+ name = "rule",
+ category = DocCategory.BUILTIN,
+ doc =
+ "A callable value representing the type of a native or Starlark rule. Calling the value"
+ + " during evaluation of a package's BUILD file creates an instance of the rule and"
+ + " adds it to the package's target set.")
+public interface RuleFunction extends StarlarkCallable {
RuleClass getRuleClass();
}
diff --git a/src/main/java/net/starlark/java/eval/BuiltinCallable.java b/src/main/java/net/starlark/java/eval/BuiltinCallable.java
index 7967affea59f91..c1f7d4021e26af 100644
--- a/src/main/java/net/starlark/java/eval/BuiltinCallable.java
+++ b/src/main/java/net/starlark/java/eval/BuiltinCallable.java
@@ -21,6 +21,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import javax.annotation.Nullable;
+import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.spelling.SpellChecker;
@@ -30,8 +31,11 @@
* Starlark value. BuiltinCallables are not produced for Java methods for which {@link
* StarlarkMethod#structField} is true.
*/
-// TODO(adonovan): rename AnnotatedMethod?
-// TODO(adonovan): annotate with type="builtin_function_or_method"
+// TODO(adonovan): rename to BuiltinFunction. Also, support annotated static methods.
+@StarlarkBuiltin(
+ name = "builtin_function_or_method", // (following Python)
+ category = "core",
+ doc = "The type of a built-in function, defined by Java code.")
public final class BuiltinCallable implements StarlarkCallable {
private final Object obj;
diff --git a/src/main/java/net/starlark/java/eval/Starlark.java b/src/main/java/net/starlark/java/eval/Starlark.java
index 84764e2162de7f..25c76fec005850 100644
--- a/src/main/java/net/starlark/java/eval/Starlark.java
+++ b/src/main/java/net/starlark/java/eval/Starlark.java
@@ -329,16 +329,8 @@ public static String classType(Class> c) {
return module.name();
}
- if (StarlarkCallable.class.isAssignableFrom(c)) {
- // All callable values have historically been lumped together as "function".
- // TODO(adonovan): eliminate this case.
- // Built-in types that don't use StarlarkModule should report
- // their own type string, but this is a breaking change as users often
- // use type(x)=="function" for Starlark and built-in functions.
- return "function";
-
- } else if (c.equals(Object.class)) {
- // "Unknown" is another unfortunate choice.
+ if (c.equals(Object.class)) {
+ // "unknown" is another unfortunate choice.
// Object.class does mean "unknown" when talking about the type parameter
// of a collection (List