Skip to content

Commit 0f4d461

Browse files
stepanchegfacebook-github-bot
authored andcommitted
Inline DocType
Summary: Less code. Reviewed By: ndmitchell, JakobDegen Differential Revision: D47173334 fbshipit-source-id: 57e7a129636db328410e5a340ac0ab4c8d8b2988
1 parent 924cf03 commit 0f4d461

File tree

8 files changed

+36
-73
lines changed

8 files changed

+36
-73
lines changed

starlark/src/docs/markdown.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::docs::DocObject;
2828
use crate::docs::DocParam;
2929
use crate::docs::DocProperty;
3030
use crate::docs::DocString;
31-
use crate::docs::DocType;
31+
use crate::typing::Ty;
3232

3333
/// The style of output that is being generated
3434
#[derive(Copy, Clone, Dupe)]
@@ -275,7 +275,7 @@ const MAX_LENGTH_BEFORE_MULTILINE: usize = 80;
275275
/// produce a function prototype.
276276
enum TypeRenderer<'a> {
277277
/// A general "type".
278-
Type(&'a Option<DocType>),
278+
Type(&'a Option<Ty>),
279279
/// A function, with some extra formatting options.
280280
Function {
281281
/// The function name in the prototype as well.
@@ -286,14 +286,14 @@ enum TypeRenderer<'a> {
286286

287287
impl<'a> RenderMarkdown for TypeRenderer<'a> {
288288
fn render_markdown_opt(&self, flavor: MarkdownFlavor) -> Option<String> {
289-
fn raw_type(t: &Option<DocType>) -> String {
289+
fn raw_type(t: &Option<Ty>) -> String {
290290
match t {
291-
Some(t) => t.raw_type.to_string(),
291+
Some(t) => t.to_string(),
292292
_ => "\"\"".to_owned(),
293293
}
294294
}
295295

296-
fn raw_type_prefix(prefix: &str, t: &Option<DocType>) -> String {
296+
fn raw_type_prefix(prefix: &str, t: &Option<Ty>) -> String {
297297
if t.is_some() {
298298
format!("{prefix}{}", raw_type(t))
299299
} else {

starlark/src/docs/mod.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,6 @@ pub struct Identifier {
338338
pub location: Option<Location>,
339339
}
340340

341-
/// The type of a given parameter, field, etc.
342-
#[derive(Debug, Clone, PartialEq, Serialize, Allocative)]
343-
pub struct DocType {
344-
/// The type string that one would find in a starlark expression.
345-
pub raw_type: Ty,
346-
}
347-
348341
/// Documents a full module.
349342
#[derive(Debug, Clone, PartialEq, Serialize, Default, Allocative)]
350343
pub struct DocModule {
@@ -456,7 +449,7 @@ impl DocFunction {
456449
.ret
457450
.typ
458451
.as_ref()
459-
.map(|t| format!(" -> {}", t.raw_type))
452+
.map(|t| format!(" -> {}", t))
460453
.unwrap_or_default();
461454

462455
format!("def {}{}{}:\n{} pass", name, params, ret, docstring)
@@ -475,7 +468,7 @@ impl DocFunction {
475468
pub fn from_docstring(
476469
kind: DocStringKind,
477470
mut params: Vec<DocParam>,
478-
return_type: Option<DocType>,
471+
return_type: Option<Ty>,
479472
raw_docstring: Option<&str>,
480473
dot_type: Option<String>,
481474
) -> Self {
@@ -588,7 +581,7 @@ pub enum DocParam {
588581
name: String,
589582
docs: Option<DocString>,
590583
#[serde(rename = "type")]
591-
typ: Option<DocType>,
584+
typ: Option<Ty>,
592585
/// If present, this parameter has a default value. This is the `repr()` of that value.
593586
default_value: Option<String>,
594587
},
@@ -601,14 +594,14 @@ pub enum DocParam {
601594
name: String,
602595
docs: Option<DocString>,
603596
#[serde(rename = "type")]
604-
typ: Option<DocType>,
597+
typ: Option<Ty>,
605598
},
606599
/// Represents the "**kwargs" style of argument.
607600
Kwargs {
608601
name: String,
609602
docs: Option<DocString>,
610603
#[serde(rename = "type")]
611-
typ: Option<DocType>,
604+
typ: Option<Ty>,
612605
},
613606
}
614607

@@ -634,16 +627,16 @@ impl DocParam {
634627
default_value,
635628
..
636629
} => match (typ.as_ref(), default_value.as_ref()) {
637-
(Some(t), Some(default)) => format!("{}: {} = {}", name, t.raw_type, default),
638-
(Some(t), None) => format!("{}: {}", name, t.raw_type),
630+
(Some(t), Some(default)) => format!("{}: {} = {}", name, t, default),
631+
(Some(t), None) => format!("{}: {}", name, t),
639632
(None, Some(default)) => format!("{} = {}", name, default),
640633
(None, None) => name.clone(),
641634
},
642635
DocParam::NoArgs => "*".to_owned(),
643636
DocParam::OnlyPosBefore => "/".to_owned(),
644637
DocParam::Args { name, typ, .. } | DocParam::Kwargs { name, typ, .. } => {
645638
match typ.as_ref() {
646-
Some(typ) => format!("{}: {}", name, typ.raw_type),
639+
Some(typ) => format!("{}: {}", name, typ),
647640
None => name.clone(),
648641
}
649642
}
@@ -657,7 +650,7 @@ pub struct DocReturn {
657650
/// Extra semantic details around the returned value's meaning.
658651
pub docs: Option<DocString>,
659652
#[serde(rename = "type")]
660-
pub typ: Option<DocType>,
653+
pub typ: Option<Ty>,
661654
}
662655

663656
impl DocReturn {
@@ -671,7 +664,7 @@ impl DocReturn {
671664
pub struct DocProperty {
672665
pub docs: Option<DocString>,
673666
#[serde(rename = "type")]
674-
pub typ: Option<DocType>,
667+
pub typ: Option<Ty>,
675668
}
676669

677670
impl DocProperty {
@@ -688,9 +681,9 @@ impl DocProperty {
688681
// }
689682
// (Some(t), None) => format!(r#"_{}: {} = None"#, name, t.raw_type),
690683
(Some(t), Some(ds)) => {
691-
format!("{}\n# type: {}\n_{} = None", ds, t.raw_type, name)
684+
format!("{}\n# type: {}\n_{} = None", ds, t, name)
692685
}
693-
(Some(t), None) => format!("# type: {}\n_{} = None", t.raw_type, name),
686+
(Some(t), None) => format!("# type: {}\n_{} = None", t, name),
694687

695688
(None, Some(ds)) => format!("{}\n_{} = None", ds, name),
696689
(None, None) => format!("_{} = None", name),
@@ -715,9 +708,7 @@ impl DocMember {
715708
Some(DocItem::Property(x)) => DocMember::Property(x),
716709
_ => DocMember::Property(DocProperty {
717710
docs: None,
718-
typ: Some(DocType {
719-
raw_type: value.get_type_starlark_repr(),
720-
}),
711+
typ: Some(value.get_type_starlark_repr()),
721712
}),
722713
}
723714
}
@@ -1167,9 +1158,7 @@ mod tests {
11671158
"#;
11681159

11691160
let kind = DocStringKind::Starlark;
1170-
let return_type = Some(DocType {
1171-
raw_type: Ty::int(),
1172-
});
1161+
let return_type = Some(Ty::int());
11731162
let expected = DocFunction {
11741163
docs: DocString::from_docstring(kind, "This is an example docstring\n\nDetails here"),
11751164
params: vec![
@@ -1245,9 +1234,7 @@ mod tests {
12451234
"#;
12461235

12471236
let kind = DocStringKind::Rust;
1248-
let return_type = Some(DocType {
1249-
raw_type: Ty::int(),
1250-
});
1237+
let return_type = Some(Ty::int());
12511238
let expected = DocFunction {
12521239
docs: DocString::from_docstring(kind, "This is an example docstring\n\nDetails here"),
12531240
params: vec![

starlark/src/eval/compiler/def.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use crate::docs::DocFunction;
4343
use crate::docs::DocItem;
4444
use crate::docs::DocString;
4545
use crate::docs::DocStringKind;
46-
use crate::docs::DocType;
4746
use crate::environment::FrozenModuleData;
4847
use crate::environment::Globals;
4948
use crate::eval::bc::bytecode::Bc;
@@ -76,6 +75,7 @@ use crate::eval::Arguments;
7675
use crate::slice_vec_ext::SliceExt;
7776
use crate::starlark_complex_values;
7877
use crate::syntax::ast::ParameterP;
78+
use crate::typing::Ty;
7979
use crate::values::frozen_ref::AtomicFrozenRefOption;
8080
use crate::values::function::FUNCTION_TYPE;
8181
use crate::values::typing::TypeCompiled;
@@ -544,22 +544,13 @@ impl<'v> Def<'v> {
544544

545545
impl<'v, T1: ValueLike<'v>> DefGen<T1> {
546546
fn docs(&self) -> Option<DocItem> {
547-
let parameter_types: HashMap<usize, DocType> = self
547+
let parameter_types: HashMap<usize, Ty> = self
548548
.parameter_types
549549
.iter()
550-
.map(|(idx, _, ty)| {
551-
(
552-
idx.0 as usize,
553-
DocType {
554-
raw_type: ty.as_ty(),
555-
},
556-
)
557-
})
550+
.map(|(idx, _, ty)| (idx.0 as usize, ty.as_ty()))
558551
.collect();
559552

560-
let return_type = self.return_type.map(|r| DocType {
561-
raw_type: r.as_ty(),
562-
});
553+
let return_type = self.return_type.map(|r| r.as_ty());
563554

564555
let function_docs = DocFunction::from_docstring(
565556
DocStringKind::Starlark,

starlark/src/eval/runtime/params.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ use crate::coerce::Coerce;
3434
use crate::collections::symbol_map::SymbolMap;
3535
use crate::docs::DocParam;
3636
use crate::docs::DocString;
37-
use crate::docs::DocType;
3837
use crate::eval::runtime::arguments::ArgSymbol;
3938
use crate::eval::runtime::arguments::ArgumentsImpl;
4039
use crate::eval::runtime::arguments::FunctionError;
4140
use crate::eval::runtime::arguments::ResolvedArgName;
4241
use crate::eval::Arguments;
4342
use crate::eval::Evaluator;
4443
use crate::hint::unlikely;
44+
use crate::typing::Ty;
4545
use crate::values::dict::Dict;
4646
use crate::values::dict::DictRef;
4747
use crate::values::Heap;
@@ -656,7 +656,7 @@ impl<'v, V: ValueLike<'v>> ParametersSpec<V> {
656656
/// that parameter
657657
pub fn documentation(
658658
&self,
659-
mut parameter_types: HashMap<usize, DocType>,
659+
mut parameter_types: HashMap<usize, Ty>,
660660
mut parameter_docs: HashMap<String, Option<DocString>>,
661661
) -> Vec<DocParam> {
662662
let mut pos_only = 0;
@@ -784,7 +784,6 @@ mod tests {
784784
use crate::docs::DocParam;
785785
use crate::docs::DocString;
786786
use crate::docs::DocStringKind;
787-
use crate::docs::DocType;
788787
use crate::eval::compiler::def::FrozenDef;
789788
use crate::eval::runtime::params::ParameterKind;
790789
use crate::eval::ParametersSpec;
@@ -865,9 +864,7 @@ mod tests {
865864
DocParam::Arg {
866865
name: "a".to_owned(),
867866
docs: None,
868-
typ: Some(DocType {
869-
raw_type: Ty::int(),
870-
}),
867+
typ: Some(Ty::int()),
871868
default_value: Some("_".to_owned()),
872869
},
873870
DocParam::Arg {
@@ -878,12 +875,7 @@ mod tests {
878875
},
879876
];
880877
let mut types = HashMap::new();
881-
types.insert(
882-
1,
883-
DocType {
884-
raw_type: Ty::int(),
885-
},
886-
);
878+
types.insert(1, Ty::int());
887879
let mut docs = HashMap::new();
888880
docs.insert("a".to_owned(), None);
889881
docs.insert(

starlark/src/tests/docs/rustdocs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def with_arguments(*args, **kwargs) -> int.type: pass
126126
x.replace("\\\"int\\\"", "int.type")
127127
.replace("\\\"bool\\\"", "bool.type")
128128
.replace("\\\"string\\\"", "str.type")
129-
.replace("Some(DocType { raw_type: Any })", "None")
129+
.replace("Some(Any)", "None")
130130
.replace("\\\"_\\\"", "_")
131131
}
132132

starlark/src/typing/ty.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ use crate::docs::DocFunction;
3838
use crate::docs::DocMember;
3939
use crate::docs::DocParam;
4040
use crate::docs::DocProperty;
41-
use crate::docs::DocType;
4241
use crate::eval::compiler::constants::Constants;
4342
use crate::eval::compiler::scope::payload::CstIdent;
4443
use crate::eval::compiler::scope::payload::CstPayload;
@@ -862,10 +861,10 @@ impl Ty {
862861
}
863862
}
864863

865-
pub(crate) fn from_docs_type(ty: &Option<DocType>) -> Self {
864+
pub(crate) fn from_docs_type(ty: &Option<Ty>) -> Self {
866865
match ty {
867866
None => Ty::Any,
868-
Some(x) => x.raw_type.clone(),
867+
Some(x) => x.clone(),
869868
}
870869
}
871870
}

starlark/src/values/types/function.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use crate::docs::DocItem;
3434
use crate::docs::DocProperty;
3535
use crate::docs::DocString;
3636
use crate::docs::DocStringKind;
37-
use crate::docs::DocType;
3837
use crate::eval::Arguments;
3938
use crate::eval::Evaluator;
4039
use crate::eval::ParametersParser;
@@ -157,8 +156,8 @@ impl<T> NativeAttr for T where
157156
pub struct NativeCallableRawDocs {
158157
pub rust_docstring: Option<&'static str>,
159158
pub signature: ParametersSpec<FrozenValue>,
160-
pub parameter_types: HashMap<usize, DocType>,
161-
pub return_type: DocType,
159+
pub parameter_types: HashMap<usize, Ty>,
160+
pub return_type: Ty,
162161
pub dot_type: Option<&'static str>,
163162
}
164163

@@ -409,9 +408,7 @@ impl<'v> StarlarkValue<'v> for NativeAttribute {
409408
.docstring
410409
.as_ref()
411410
.and_then(|ds| DocString::from_docstring(DocStringKind::Rust, ds));
412-
let typ = Some(DocType {
413-
raw_type: self.typ.clone(),
414-
});
411+
let typ = Some(self.typ.clone());
415412
Some(DocItem::Property(DocProperty { docs: ds, typ }))
416413
}
417414
}

starlark_derive/src/module/render/fun.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ fn render_documentation(x: &StarFun) -> syn::Result<(Ident, TokenStream)> {
596596
.filter(|(_, a)| a.pass_style != StarArgPassStyle::Args) // these aren't coerced according to their type (Vec vs tuple)
597597
.map(|(i, arg)| {
598598
let typ_str = render_starlark_type(span, arg.without_option());
599-
quote_spanned!(span=> (#i, starlark::docs::DocType { raw_type: #typ_str }) )
599+
quote_spanned!(span=> (#i, #typ_str) )
600600
})
601601
.collect();
602602

@@ -606,14 +606,11 @@ fn render_documentation(x: &StarFun) -> syn::Result<(Ident, TokenStream)> {
606606
let documentation = quote_spanned!(span=>
607607
let #var_name = {
608608
let parameter_types = std::collections::HashMap::from([#(#parameter_types),*]);
609-
let return_type = starlark::docs::DocType {
610-
raw_type: #return_type_str
611-
};
612609
starlark::values::function::NativeCallableRawDocs {
613610
rust_docstring: #docs,
614611
signature: #documentation_signature,
615612
parameter_types,
616-
return_type,
613+
return_type: #return_type_str,
617614
dot_type: #dot_type,
618615
}
619616
};

0 commit comments

Comments
 (0)