22
22
import com .google .devtools .build .lib .analysis .RequiredConfigFragmentsProvider ;
23
23
import com .google .devtools .build .lib .analysis .RuleContext ;
24
24
import com .google .devtools .build .lib .analysis .StarlarkProviderValidationUtil ;
25
- import com .google .devtools .build .lib .analysis .starlark .StarlarkRuleConfiguredTargetUtil ;
26
25
import com .google .devtools .build .lib .analysis .starlark .StarlarkRuleContext ;
27
26
import com .google .devtools .build .lib .cmdline .Label ;
28
27
import com .google .devtools .build .lib .cmdline .RepositoryName ;
31
30
import com .google .devtools .build .lib .packages .RuleClass .ConfiguredTargetFactory .RuleErrorException ;
32
31
import com .google .devtools .build .lib .packages .StarlarkDefinedAspect ;
33
32
import com .google .devtools .build .lib .packages .StarlarkInfo ;
34
- import com .google .devtools .build .lib .packages .StructImpl ;
35
33
import com .google .devtools .build .lib .packages .StructProvider ;
36
- import java .util .Map ;
37
34
import javax .annotation .Nullable ;
38
- import net .starlark .java .eval .Dict ;
39
35
import net .starlark .java .eval .EvalException ;
40
36
import net .starlark .java .eval .Starlark ;
41
- import net .starlark .java .eval .StarlarkValue ;
42
37
43
38
/** A factory for aspects that are defined in Starlark. */
44
39
public class StarlarkAspectFactory implements ConfiguredAspectFactory {
@@ -87,13 +82,15 @@ public ConfiguredAspect create(
87
82
88
83
if (ruleContext .hasErrors () && !allowAnalysisFailures ) {
89
84
return errorConfiguredAspect (ruleContext , requiredConfigFragments );
90
- } else if (!(aspectStarlarkObject instanceof StructImpl )
91
- && !(aspectStarlarkObject instanceof Iterable )
85
+ } else if (aspectStarlarkObject instanceof Info info
86
+ && info .getProvider ().getKey ().equals (StructProvider .STRUCT .getKey ())) {
87
+ ruleContext .ruleError (
88
+ "Returning a struct from an aspect implementation function is deprecated." );
89
+ } else if (!(aspectStarlarkObject instanceof Iterable )
92
90
&& !(aspectStarlarkObject instanceof Info )) {
93
91
ruleContext .ruleError (
94
92
String .format (
95
- "Aspect implementation should return a struct, a list, or a provider "
96
- + "instance, but got %s" ,
93
+ "Aspect implementation should return a list, or a provider instance, but got %s" ,
97
94
Starlark .type (aspectStarlarkObject )));
98
95
return errorConfiguredAspect (ruleContext , requiredConfigFragments );
99
96
}
@@ -127,37 +124,17 @@ private static ConfiguredAspect createAspect(
127
124
if (requiredConfigFragments != null ) {
128
125
builder .addProvider (requiredConfigFragments );
129
126
}
130
- if (aspectStarlarkObject instanceof Iterable <?> iterable ) {
127
+ // not instanceof Info, because OutputGroupInfo is both Iterable and Info
128
+ if (!(aspectStarlarkObject instanceof Info )
129
+ && aspectStarlarkObject instanceof Iterable <?> iterable ) {
131
130
addDeclaredProviders (builder , iterable );
132
131
} else {
133
- // Either an old-style struct or a single declared provider (not in a list)
132
+ // A single declared provider (not in a list)
134
133
Info info = (Info ) aspectStarlarkObject ;
135
- if (info .getProvider ().getKey ().equals (StructProvider .STRUCT .getKey ())) {
136
- // Old-style struct, that may contain declared providers.
137
- StructImpl struct = (StructImpl ) aspectStarlarkObject ;
138
- for (String field : struct .getFieldNames ()) {
139
- if (field .equals ("output_groups" )) {
140
- addOutputGroups (struct .getValue (field ), builder );
141
- } else if (field .equals ("providers" )) {
142
- Object providers = struct .getValue (field );
143
- // TODO(adonovan): can we be more specific than iterable, and use Sequence.cast?
144
- if (!(providers instanceof Iterable )) {
145
- throw Starlark .errorf (
146
- "The value for \" providers\" should be a list of declared providers, "
147
- + "got %s instead" ,
148
- Starlark .type (providers ));
149
- }
150
- addDeclaredProviders (builder , (Iterable <?>) providers );
151
- } else {
152
- builder .addStarlarkTransitiveInfo (field , struct .getValue (field ));
153
- }
154
- }
155
- } else {
156
- if (info instanceof StarlarkInfo starlarkInfo ) {
157
- info = starlarkInfo .unsafeOptimizeMemoryLayout ();
158
- }
159
- builder .addStarlarkDeclaredProvider (info );
134
+ if (info instanceof StarlarkInfo starlarkInfo ) {
135
+ info = starlarkInfo .unsafeOptimizeMemoryLayout ();
160
136
}
137
+ builder .addStarlarkDeclaredProvider (info );
161
138
}
162
139
163
140
ConfiguredAspect configuredAspect = builder .build ();
@@ -182,15 +159,4 @@ private static void addDeclaredProviders(
182
159
i ++;
183
160
}
184
161
}
185
-
186
- private static void addOutputGroups (Object outputGroups , ConfiguredAspect .Builder builder )
187
- throws EvalException {
188
- for (Map .Entry <String , StarlarkValue > entry :
189
- Dict .cast (outputGroups , String .class , StarlarkValue .class , "output_groups" ).entrySet ()) {
190
- builder .addOutputGroup (
191
- entry .getKey (),
192
- StarlarkRuleConfiguredTargetUtil .convertToOutputGroupValue (
193
- entry .getKey (), entry .getValue ()));
194
- }
195
- }
196
162
}
0 commit comments