Skip to content

Commit 7ff0491

Browse files
stepanchegfacebook-github-bot
authored andcommitted
Use StringValue in Arguments
Summary: Parameter names in `Arguments` were `Value`. They are `StringValue` now. Use types to assert values are of correct type. Mostly for documentation. Reviewed By: ndmitchell Differential Revision: D30884359 fbshipit-source-id: 64a9d3e54817e400d52b027dc3f585c270ae99a7
1 parent 488dc72 commit 7ff0491

File tree

8 files changed

+31
-22
lines changed

8 files changed

+31
-22
lines changed

starlark/src/eval/fragment/call.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ use crate::{
3030
Arguments, Evaluator, FrozenDef,
3131
},
3232
syntax::ast::{ArgumentP, ExprP},
33-
values::{function::NativeFunction, AttrType, FrozenValue, StarlarkValue, Value, ValueLike},
33+
values::{
34+
function::NativeFunction, AttrType, FrozenStringValue, FrozenValue, StarlarkValue, Value,
35+
ValueLike,
36+
},
3437
};
3538
use gazebo::coerce::coerce_ref;
3639
use std::mem::MaybeUninit;
@@ -42,7 +45,7 @@ struct ArgsCompiled {
4245
///
4346
/// Note names are guaranteed to be unique here because names are validated in AST:
4447
/// named arguments in [`Expr::Call`] are unique.
45-
names: Vec<(Symbol, FrozenValue)>,
48+
names: Vec<(Symbol, FrozenStringValue)>,
4649
args: Option<ExprCompiled>,
4750
kwargs: Option<ExprCompiled>,
4851
}
@@ -200,7 +203,10 @@ impl Compiler<'_> {
200203
match x.node {
201204
ArgumentP::Positional(x) => res.pos_named.push(self.expr(x).as_compiled()),
202205
ArgumentP::Named(name, value) => {
203-
let fv = self.module_env.frozen_heap().alloc(name.node.as_str());
206+
let fv = self
207+
.module_env
208+
.frozen_heap()
209+
.alloc_string_value(name.node.as_str());
204210
res.names.push((Symbol::new(&name.node), fv));
205211
res.pos_named.push(self.expr(value).as_compiled());
206212
}

starlark/src/eval/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'v, 'a> Evaluator<'v, 'a> {
171171
positional: &[Value<'v>],
172172
named: &[(&str, Value<'v>)],
173173
) -> anyhow::Result<Value<'v>> {
174-
let names = named.map(|(s, _)| (Symbol::new(*s), self.heap().alloc(*s)));
174+
let names = named.map(|(s, _)| (Symbol::new(*s), self.heap().alloc_string_value(*s)));
175175
let named = named.map(|x| x.1);
176176
let params = Arguments {
177177
this: None,

starlark/src/eval/runtime/arguments.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
use crate::collections::BorrowHashed;
18+
use crate::{collections::BorrowHashed, values::StringValue};
1919
/// Deal with all aspects of runtime parameter evaluation.
2020
/// First build a Parameters structure, then use collect to collect the
2121
/// parameters into slots.
@@ -425,7 +425,7 @@ impl<'v, V: ValueLike<'v>> ParametersSpec<V> {
425425
None => {
426426
add_kwargs(
427427
&mut kwargs,
428-
Hashed::new_unchecked(name.small_hash(), *name_value),
428+
Hashed::new_unchecked(name.small_hash(), name_value.to_value()),
429429
*v,
430430
);
431431
}
@@ -633,7 +633,7 @@ pub struct Arguments<'v, 'a> {
633633
/// Names of named arguments.
634634
///
635635
/// `named` length must be equal to `names` length.
636-
pub names: &'a [(Symbol, Value<'v>)],
636+
pub names: &'a [(Symbol, StringValue<'v>)],
637637
/// `*args` argument.
638638
pub args: Option<Value<'v>>,
639639
/// `**kwargs` argument.
@@ -649,7 +649,8 @@ impl<'v, 'a> Arguments<'v, 'a> {
649649
None => {
650650
let mut result = SmallMap::with_capacity(self.names.len());
651651
for (k, v) in self.names.iter().zip(self.named) {
652-
result.insert_hashed(Hashed::new_unchecked(k.0.small_hash(), k.1), *v);
652+
result
653+
.insert_hashed(Hashed::new_unchecked(k.0.small_hash(), k.1.to_value()), *v);
653654
}
654655
Ok(Dict::new(result))
655656
}
@@ -664,7 +665,10 @@ impl<'v, 'a> Arguments<'v, 'a> {
664665
let mut result =
665666
SmallMap::with_capacity(self.names.len() + kwargs.content.len());
666667
for (k, v) in self.names.iter().zip(self.named) {
667-
result.insert_hashed(Hashed::new_unchecked(k.0.small_hash(), k.1), *v);
668+
result.insert_hashed(
669+
Hashed::new_unchecked(k.0.small_hash(), k.1.to_value()),
670+
*v,
671+
);
668672
}
669673
for (k, v) in kwargs.iter_hashed() {
670674
let s = Arguments::unpack_kwargs_key(*k.key())?;
@@ -985,7 +989,7 @@ mod test {
985989
p.kwargs = None;
986990
let named = [Value::new_none()];
987991
p.named = &named;
988-
let names = [(Symbol::new("test"), heap.alloc("test"))];
992+
let names = [(Symbol::new("test"), heap.alloc_string_value("test"))];
989993
p.names = &names;
990994
assert!(p.no_named_args().is_err());
991995
}

starlark/src/stdlib/extra.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ use crate::{
2323
eval::{Arguments, Evaluator, ParametersSpec},
2424
values::{
2525
dict::Dict, function::FUNCTION_TYPE, list::List, none::NoneType, tuple::Tuple,
26-
ComplexValue, Freezer, StarlarkValue, Trace, Value, ValueLike,
26+
ComplexValue, Freezer, FrozenStringValue, FrozenValue, StarlarkValue, StringValue,
27+
StringValueLike, Trace, Value, ValueLike,
2728
},
2829
};
2930
use gazebo::{
@@ -77,11 +78,12 @@ pub fn partial(builder: &mut GlobalsBuilder) {
7778
.content
7879
.keys()
7980
.map(|x| {
81+
let x = StringValue::new(*x).unwrap();
8082
(
8183
// We duplicate string here.
8284
// If this becomes hot, we should do better.
83-
Symbol::new_hashed(x.unpack_starlark_str().unwrap().as_str_hashed()),
84-
*x,
85+
Symbol::new_hashed(x.unpack_starlark_str().as_str_hashed()),
86+
x,
8587
)
8688
})
8789
.collect();
@@ -147,15 +149,17 @@ pub fn abs(builder: &mut GlobalsBuilder) {
147149

148150
#[derive(Debug, Coerce, Trace)]
149151
#[repr(C)]
150-
struct PartialGen<V> {
152+
struct PartialGen<V, S> {
151153
func: V,
152154
pos: Vec<V>,
153155
named: Vec<V>,
154-
names: Vec<(Symbol, V)>,
156+
names: Vec<(Symbol, S)>,
155157
signature: ParametersSpec<V>,
156158
}
157159

158-
starlark_complex_value!(Partial);
160+
type Partial<'v> = PartialGen<Value<'v>, StringValue<'v>>;
161+
type FrozenPartial = PartialGen<FrozenValue, FrozenStringValue>;
162+
starlark_complex_values!(Partial);
159163

160164
impl<'v> ComplexValue<'v> for Partial<'v> {
161165
type Frozen = FrozenPartial;
@@ -172,7 +176,7 @@ impl<'v> ComplexValue<'v> for Partial<'v> {
172176
}
173177
}
174178

175-
impl<'v, V: ValueLike<'v>> StarlarkValue<'v> for PartialGen<V>
179+
impl<'v, V: ValueLike<'v>, S: StringValueLike<'v>> StarlarkValue<'v> for PartialGen<V, S>
176180
where
177181
Self: AnyLifetime<'v>,
178182
{

starlark/src/values/alloc_value.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ impl FrozenHeap {
7070
val.alloc_frozen_value(self)
7171
}
7272

73-
#[allow(dead_code)] // TODO: remove in the following diff
7473
pub(crate) fn alloc_string_value(&self, s: &str) -> FrozenStringValue {
7574
unsafe { FrozenStringValue::new_unchecked(self.alloc_str(s)) }
7675
}
@@ -82,7 +81,6 @@ impl Heap {
8281
x.alloc_value(self)
8382
}
8483

85-
#[allow(dead_code)] // TODO: remove in the following diff
8684
pub(crate) fn alloc_string_value<'v>(&'v self, s: &str) -> StringValue<'v> {
8785
unsafe { StringValue::new_unchecked(self.alloc_str(s)) }
8886
}

starlark/src/values/layout/constant.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ impl FrozenStringValue {
115115
}
116116
}
117117

118-
#[allow(dead_code)] // TODO: remove in the following diff
119118
impl<'v> StringValue<'v> {
120119
/// Construct without a check that the value contains a string.
121120
///

starlark/src/values/layout/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ mod pointer_i32;
2727
mod value;
2828
mod value_captured;
2929

30-
#[allow(unused_imports)] // TODO: remove in the following diff
3130
pub(crate) use constant::StringValueLike;
3231
pub use constant::{ConstFrozenStringN, FrozenStringValue, StringValue};
3332
pub use heap::{Freezer, FrozenHeap, FrozenHeapRef, Heap, Tracer};

starlark/src/values/layout/pointer.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ impl<'p, P> Pointer<'p, P> {
134134
}
135135

136136
/// Unpack pointer when it is known to be not an integer.
137-
#[allow(dead_code)] // TODO: remove in the following diff
138137
pub(crate) unsafe fn unpack_ptr_no_int_unchecked(self) -> &'p P {
139138
let p = self.pointer.get();
140139
debug_assert!(p & TAG_INT == 0);

0 commit comments

Comments
 (0)