23
23
import com .google .devtools .build .lib .analysis .util .MockRule ;
24
24
import com .google .devtools .build .lib .analysis .util .MockRuleDefaults ;
25
25
import com .google .devtools .build .lib .packages .Attribute .ComputedDefault ;
26
+ import com .google .devtools .build .lib .packages .Attribute .LateBoundDefault ;
26
27
import com .google .devtools .build .lib .testutil .TestRuleClassProvider ;
27
28
import com .google .testing .junit .testparameterinjector .TestParameter ;
28
29
import com .google .testing .junit .testparameterinjector .TestParameterInjector ;
@@ -39,6 +40,7 @@ public final class RuleAttributeStorageTest extends BuildViewTestCase {
39
40
40
41
private static final String STRING_DEFAULT = Type .STRING .getDefaultValue ();
41
42
private static final int COMPUTED_DEFAULT_OFFSET = 1 ;
43
+ private static final int LATE_BOUND_DEFAULT_OFFSET = 2 ;
42
44
43
45
private enum ContainerSize {
44
46
SMALL (16 ),
@@ -60,6 +62,8 @@ private enum ContainerSize {
60
62
private Attribute lastCustomAttr ;
61
63
private int computedDefaultIndex ;
62
64
private Attribute computedDefaultAttr ;
65
+ private int lateBoundDefaultIndex ;
66
+ private Attribute lateBoundDefaultAttr ;
63
67
64
68
@ Override
65
69
protected ConfiguredRuleClassProvider createRuleClassProvider () {
@@ -72,18 +76,29 @@ protected ConfiguredRuleClassProvider createRuleClassProvider() {
72
76
IntStream .range (0 , numCustomAttrs )
73
77
.mapToObj (
74
78
i -> {
75
- var attr = attr ("attr" + i , Type .STRING );
76
- // Make one of the attributes a computed default.
79
+ // Make one attribute a computed default and one a late bound default.
77
80
if (i == COMPUTED_DEFAULT_OFFSET ) {
78
- attr .value (
79
- new ComputedDefault () {
80
- @ Override
81
- public Object getDefault (AttributeMap rule ) {
82
- return "computed" ;
83
- }
84
- });
81
+ return attr ("attr" + i + "_computed_default" , Type .STRING )
82
+ .value (
83
+ new ComputedDefault () {
84
+ @ Override
85
+ public Object getDefault (AttributeMap rule ) {
86
+ return "computed" ;
87
+ }
88
+ });
85
89
}
86
- return attr ;
90
+ if (i == LATE_BOUND_DEFAULT_OFFSET ) {
91
+ return attr (":attr" + i + "_late_bound_default" , Type .STRING )
92
+ .value (
93
+ new LateBoundDefault <>(Void .class , "late_bound" ) {
94
+ @ Override
95
+ public String resolve (
96
+ Rule rule , AttributeMap attributes , Void input ) {
97
+ return "late_bound" ;
98
+ }
99
+ });
100
+ }
101
+ return attr ("attr" + i , Type .STRING );
87
102
})
88
103
.toArray (Attribute .Builder []::new ));
89
104
var builder = new ConfiguredRuleClassProvider .Builder ().addRuleDefinition (exampleRule );
@@ -111,6 +126,8 @@ public void setUpForRule() throws Exception {
111
126
lastCustomAttr = attrAt (lastCustomAttrIndex );
112
127
computedDefaultIndex = firstCustomAttrIndex + COMPUTED_DEFAULT_OFFSET ;
113
128
computedDefaultAttr = attrAt (computedDefaultIndex );
129
+ lateBoundDefaultIndex = firstCustomAttrIndex + LATE_BOUND_DEFAULT_OFFSET ;
130
+ lateBoundDefaultAttr = attrAt (lateBoundDefaultIndex );
114
131
}
115
132
116
133
@ Test
@@ -122,9 +139,9 @@ public void attributeSettingAndRetrieval(@TestParameter boolean frozen) {
122
139
rule .freeze ();
123
140
}
124
141
125
- assertThat (rule .getRawAttrValue (firstCustomAttrIndex )).isEqualTo ("val1" );
142
+ assertThat (rule .getAttrIfStored (firstCustomAttrIndex )).isEqualTo ("val1" );
126
143
assertThat (rule .isAttributeValueExplicitlySpecified (firstCustomAttr )).isTrue ();
127
- assertThat (rule .getRawAttrValue (lastCustomAttrIndex )).isEqualTo ("val2" );
144
+ assertThat (rule .getAttrIfStored (lastCustomAttrIndex )).isEqualTo ("val2" );
128
145
assertThat (rule .isAttributeValueExplicitlySpecified (lastCustomAttr )).isTrue ();
129
146
}
130
147
@@ -134,7 +151,7 @@ public void indexOutOfBounds_throws(@TestParameter boolean frozen) {
134
151
rule .freeze ();
135
152
}
136
153
assertThrows (
137
- IndexOutOfBoundsException .class , () -> rule .getRawAttrValue (lastCustomAttrIndex + 1 ));
154
+ IndexOutOfBoundsException .class , () -> rule .getAttrIfStored (lastCustomAttrIndex + 1 ));
138
155
}
139
156
140
157
@ Test
@@ -146,10 +163,10 @@ public void testForOffByOneError(@TestParameter boolean frozen) {
146
163
rule .freeze ();
147
164
}
148
165
149
- assertThat (rule .getRawAttrValue (firstCustomAttrIndex - 1 )).isNull ();
166
+ assertThat (rule .getAttrIfStored (firstCustomAttrIndex - 1 )).isNull ();
150
167
assertThat (rule .isAttributeValueExplicitlySpecified (attrAt (firstCustomAttrIndex - 1 )))
151
168
.isFalse ();
152
- assertThat (rule .getRawAttrValue (firstCustomAttrIndex + 1 )).isNull ();
169
+ assertThat (rule .getAttrIfStored (firstCustomAttrIndex + 1 )).isNull ();
153
170
assertThat (rule .isAttributeValueExplicitlySpecified (attrAt (firstCustomAttrIndex + 1 )))
154
171
.isFalse ();
155
172
}
@@ -166,9 +183,9 @@ public void testFreezeWorks() {
166
183
// Double freezing is a no-op
167
184
rule .freeze ();
168
185
// reads/explicit bits work as expected
169
- assertThat (rule .getRawAttrValue (firstCustomAttrIndex )).isEqualTo ("val1" );
186
+ assertThat (rule .getAttrIfStored (firstCustomAttrIndex )).isEqualTo ("val1" );
170
187
assertThat (rule .isAttributeValueExplicitlySpecified (firstCustomAttr )).isTrue ();
171
- assertThat (rule .getRawAttrValue (lastCustomAttrIndex )).isEqualTo ("val2" );
188
+ assertThat (rule .getAttrIfStored (lastCustomAttrIndex )).isEqualTo ("val2" );
172
189
assertThat (rule .isAttributeValueExplicitlySpecified (lastCustomAttr )).isFalse ();
173
190
// writes no longer work.
174
191
assertThrows (
@@ -189,7 +206,7 @@ public void allAttributesSet(@TestParameter boolean frozen) {
189
206
}
190
207
191
208
for (int i = 1 ; i < size ; i ++) { // Skip attribute 0 (name) which is never stored.
192
- assertThat (rule .getRawAttrValue (i )).isEqualTo ("value " + i );
209
+ assertThat (rule .getAttrIfStored (i )).isEqualTo ("value " + i );
193
210
assertWithMessage ("attribute " + i )
194
211
.that (rule .isAttributeValueExplicitlySpecified (attrAt (i )))
195
212
.isEqualTo (i % 2 == 0 );
@@ -235,7 +252,7 @@ public void boundaryOfFrozenContainer() {
235
252
236
253
assertThat (rule .getAttr ("name" )).isEqualTo (ruleName );
237
254
assertThat (rule .isAttributeValueExplicitlySpecified ("name" )).isTrue ();
238
- assertThat (rule .getRawAttrValue (lastCustomAttrIndex )).isEqualTo ("last" );
255
+ assertThat (rule .getAttrIfStored (lastCustomAttrIndex )).isEqualTo ("last" );
239
256
assertThat (rule .isAttributeValueExplicitlySpecified (lastCustomAttr )).isTrue ();
240
257
}
241
258
@@ -248,7 +265,7 @@ public void nameNotStoredAsRawAttr(@TestParameter boolean frozen) {
248
265
rule .freeze ();
249
266
}
250
267
251
- assertThat (rule .getRawAttrValue (0 )).isNull ();
268
+ assertThat (rule .getAttrIfStored (0 )).isNull ();
252
269
assertThat (rule .getRawAttrValues ()).doesNotContain (ruleName );
253
270
assertThat (rule .getAttr ("name" )).isEqualTo (ruleName );
254
271
assertThat (rule .isAttributeValueExplicitlySpecified ("name" )).isTrue ();
@@ -262,15 +279,15 @@ public void explicitDefaultValue_stored(@TestParameter boolean frozen) {
262
279
rule .freeze ();
263
280
}
264
281
265
- assertThat (rule .getRawAttrValue (firstCustomAttrIndex )).isNotNull ();
282
+ assertThat (rule .getAttrIfStored (firstCustomAttrIndex )).isNotNull ();
266
283
assertThat (rule .isAttributeValueExplicitlySpecified (firstCustomAttr )).isTrue ();
267
284
}
268
285
269
286
@ Test
270
287
public void nonExplicitDefaultValue_mutable_stored () {
271
288
rule .setAttributeValue (firstCustomAttr , STRING_DEFAULT , /* explicit= */ false );
272
289
273
- assertThat (rule .getRawAttrValue (firstCustomAttrIndex )).isNotNull ();
290
+ assertThat (rule .getAttrIfStored (firstCustomAttrIndex )).isNotNull ();
274
291
assertThat (rule .isAttributeValueExplicitlySpecified (firstCustomAttr )).isFalse ();
275
292
}
276
293
@@ -280,37 +297,60 @@ public void nonExplicitDefaultValue_frozen_notStored() {
280
297
281
298
rule .freeze ();
282
299
283
- assertThat (rule .getRawAttrValue (firstCustomAttrIndex )).isNull ();
300
+ assertThat (rule .getAttrIfStored (firstCustomAttrIndex )).isNull ();
284
301
assertThat (rule .isAttributeValueExplicitlySpecified (firstCustomAttr )).isFalse ();
285
302
}
286
303
287
304
@ Test
288
305
public void computedDefault_mutable_stored () {
289
- Attribute attr = rule .getRuleClassObject ().getAttribute (computedDefaultIndex );
290
- var computedDefault = attr .getDefaultValue ();
291
- assertThat (attr .hasComputedDefault ()).isTrue ();
306
+ var computedDefault = computedDefaultAttr .getDefaultValue ();
307
+ assertThat (computedDefaultAttr .hasComputedDefault ()).isTrue ();
292
308
assertThat (computedDefault ).isInstanceOf (ComputedDefault .class );
293
309
294
310
rule .setAttributeValue (computedDefaultAttr , computedDefault , /* explicit= */ false );
295
311
296
- assertThat (rule .getRawAttrValue (computedDefaultIndex )).isEqualTo (computedDefault );
312
+ assertThat (rule .getAttrIfStored (computedDefaultIndex )).isEqualTo (computedDefault );
313
+ assertThat (rule .getAttr (computedDefaultAttr .getName ())).isEqualTo (computedDefault );
297
314
assertThat (rule .isAttributeValueExplicitlySpecified (computedDefaultAttr )).isFalse ();
298
315
}
299
316
300
317
@ Test
301
318
public void computedDefault_frozen_notStored () {
302
- Attribute attr = rule .getRuleClassObject ().getAttribute (computedDefaultIndex );
303
- var computedDefault = attr .getDefaultValue ();
304
- assertThat (attr .hasComputedDefault ()).isTrue ();
319
+ var computedDefault = computedDefaultAttr .getDefaultValue ();
320
+ assertThat (computedDefaultAttr .hasComputedDefault ()).isTrue ();
305
321
assertThat (computedDefault ).isInstanceOf (ComputedDefault .class );
306
322
307
323
rule .setAttributeValue (computedDefaultAttr , computedDefault , /* explicit= */ false );
308
324
rule .freeze ();
309
325
310
- assertThat (rule .getRawAttrValue (computedDefaultIndex )).isNull ();
326
+ assertThat (rule .getAttrIfStored (computedDefaultIndex )).isNull ();
327
+ assertThat (rule .getAttr (computedDefaultAttr .getName ())).isEqualTo (computedDefault );
311
328
assertThat (rule .isAttributeValueExplicitlySpecified (computedDefaultAttr )).isFalse ();
312
329
}
313
330
331
+ @ Test
332
+ public void lateBoundDefault_mutable_stored () {
333
+ var lateBoundDefault = lateBoundDefaultAttr .getLateBoundDefault ();
334
+
335
+ rule .setAttributeValue (lateBoundDefaultAttr , lateBoundDefault , /* explicit= */ false );
336
+
337
+ assertThat (rule .getAttrIfStored (lateBoundDefaultIndex )).isEqualTo (lateBoundDefault );
338
+ assertThat (rule .getAttr (lateBoundDefaultAttr .getName ())).isEqualTo (lateBoundDefault );
339
+ assertThat (rule .isAttributeValueExplicitlySpecified (lateBoundDefaultAttr )).isFalse ();
340
+ }
341
+
342
+ @ Test
343
+ public void lateBoundDefault_frozen_notStored () {
344
+ var lateBoundDefault = lateBoundDefaultAttr .getLateBoundDefault ();
345
+
346
+ rule .setAttributeValue (lateBoundDefaultAttr , lateBoundDefault , /* explicit= */ false );
347
+ rule .freeze ();
348
+
349
+ assertThat (rule .getAttrIfStored (lateBoundDefaultIndex )).isNull ();
350
+ assertThat (rule .getAttr (lateBoundDefaultAttr .getName ())).isEqualTo (lateBoundDefault );
351
+ assertThat (rule .isAttributeValueExplicitlySpecified (lateBoundDefaultAttr )).isFalse ();
352
+ }
353
+
314
354
private Attribute attrAt (int attrIndex ) {
315
355
return rule .getRuleClassObject ().getAttribute (attrIndex );
316
356
}
0 commit comments