Skip to content

Commit

Permalink
Propagate InternalError
Browse files Browse the repository at this point in the history
Summary:
This one:

https://www.internalfb.com/code/fbsource/[d889453707e1267863d8c7b6d03fc772fa6ce2d9]/fbcode/buck2/starlark-rust/starlark/src/typing/oracle/ctx.rs?lines=866-869

Reviewed By: JakobDegen

Differential Revision: D63430415

fbshipit-source-id: 28a6476ede0a90cca740f19ebd7548ab7c19bfd0
  • Loading branch information
stepancheg authored and facebook-github-bot committed Sep 26, 2024
1 parent c44e502 commit 73cfaa9
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 116 deletions.
26 changes: 19 additions & 7 deletions starlark/src/typing/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,22 @@ impl TypingContext<'_> {
self.result_to_ty(self.oracle.iter_item(Spanned { node: ty, span }))
}

pub(crate) fn validate_type(&self, got: Spanned<&Ty>, require: &Ty) {
pub(crate) fn validate_type(
&self,
got: Spanned<&Ty>,
require: &Ty,
) -> Result<(), InternalError> {
if let Err(e) = self.oracle.validate_type(got, require) {
self.errors.borrow_mut().push(e);
match e {
TypingOrInternalError::Typing(e) => {
self.errors.borrow_mut().push(e);
}
TypingOrInternalError::Internal(e) => {
return Err(e);
}
}
}
Ok(())
}

fn expr_dot(&self, ty: &Ty, attr: &str, span: Span) -> Ty {
Expand Down Expand Up @@ -180,7 +192,7 @@ impl TypingContext<'_> {
// We know about list and dict, everything else we just ignore
if self.types[id].is_list() {
// If we know it MUST be a list, then the index must be an int
self.validate_type(index.as_ref(), &Ty::int());
self.validate_type(index.as_ref(), &Ty::int())?;
}
for ty in self.types[id].iter_union() {
match ty {
Expand All @@ -199,15 +211,15 @@ impl TypingContext<'_> {
Ok(Ty::unions(res))
}
BindExpr::ListAppend(id, e) => {
if self.oracle.probably_a_list(&self.types[id]) {
if self.oracle.probably_a_list(&self.types[id])? {
Ok(Ty::list(self.expression_type(e)?))
} else {
// It doesn't seem to be a list, so let's assume the append is non-mutating
Ok(Ty::never())
}
}
BindExpr::ListExtend(id, e) => {
if self.oracle.probably_a_list(&self.types[id]) {
if self.oracle.probably_a_list(&self.types[id])? {
Ok(Ty::list(
self.from_iterated(&self.expression_type(e)?, e.span),
))
Expand Down Expand Up @@ -335,7 +347,7 @@ impl TypingContext<'_> {

let kwargs_ty = if let Some(star_star) = star_star {
let ty = self.expression_type_spanned(&star_star.node.expr())?;
self.validate_type(ty.as_ref(), &Ty::dict(Ty::string(), Ty::any()));
self.validate_type(ty.as_ref(), &Ty::dict(Ty::string(), Ty::any()))?;
Some(ty)
} else {
None
Expand Down Expand Up @@ -363,7 +375,7 @@ impl TypingContext<'_> {
stride: Option<&CstExpr>,
) -> Result<Ty, InternalError> {
for e in [start, stop, stride].iter().copied().flatten() {
self.validate_type(self.expression_type_spanned(e)?.as_ref(), &Ty::int());
self.validate_type(self.expression_type_spanned(e)?.as_ref(), &Ty::int())?;
}
Ok(self.result_to_ty(self.oracle.expr_slice(span, self.expression_type(x)?)))
}
Expand Down
43 changes: 28 additions & 15 deletions starlark/src/typing/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ use starlark_map::StarlarkHasher;
use crate::codemap::Span;
use crate::typing::call_args::TyCallArgs;
use crate::typing::callable::TyCallable;
use crate::typing::error::InternalError;
use crate::typing::error::TypingNoContextError;
use crate::typing::error::TypingNoContextOrInternalError;
use crate::typing::error::TypingOrInternalError;
use crate::typing::Ty;
use crate::typing::TyBasic;
Expand Down Expand Up @@ -71,16 +73,20 @@ pub trait TyCustomImpl: Debug + Display + Hash + Ord + Allocative + Send + Sync
bin_op: TypingBinOp,
rhs: &TyBasic,
ctx: &TypingOracleCtx,
) -> Result<Ty, TypingNoContextError> {
) -> Result<Ty, TypingNoContextOrInternalError> {
let _unused = (bin_op, rhs, ctx);
Err(TypingNoContextError)
Err(TypingNoContextOrInternalError::Typing)
}
fn iter_item(&self) -> Result<Ty, TypingNoContextError> {
Err(TypingNoContextError)
}
fn index(&self, item: &TyBasic, ctx: &TypingOracleCtx) -> Result<Ty, TypingNoContextError> {
fn index(
&self,
item: &TyBasic,
ctx: &TypingOracleCtx,
) -> Result<Ty, TypingNoContextOrInternalError> {
let _unused = (item, ctx);
Err(TypingNoContextError)
Err(TypingNoContextOrInternalError::Typing)
}
fn attribute(&self, attr: &str) -> Result<Ty, TypingNoContextError>;
fn union2(x: Arc<Self>, other: Arc<Self>) -> Result<Arc<Self>, (Arc<Self>, Arc<Self>)> {
Expand Down Expand Up @@ -117,15 +123,18 @@ pub(crate) trait TyCustomDyn: Debug + Display + Allocative + Send + Sync + 'stat
fn as_callable_dyn(&self) -> Option<TyCallable>;
fn as_function_dyn(&self) -> Option<&TyFunction>;
fn iter_item_dyn(&self) -> Result<Ty, TypingNoContextError>;
fn index_dyn(&self, index: &TyBasic, ctx: &TypingOracleCtx)
-> Result<Ty, TypingNoContextError>;
fn index_dyn(
&self,
index: &TyBasic,
ctx: &TypingOracleCtx,
) -> Result<Ty, TypingNoContextOrInternalError>;
fn attribute_dyn(&self, attr: &str) -> Result<Ty, TypingNoContextError>;
fn bin_op_dyn(
&self,
bin_op: TypingBinOp,
rhs: &TyBasic,
ctx: &TypingOracleCtx,
) -> Result<Ty, TypingNoContextError>;
) -> Result<Ty, TypingNoContextOrInternalError>;
fn union2_dyn(
self: Arc<Self>,
other: Arc<dyn TyCustomDyn>,
Expand Down Expand Up @@ -200,7 +209,7 @@ impl<T: TyCustomImpl> TyCustomDyn for T {
&self,
index: &TyBasic,
ctx: &TypingOracleCtx,
) -> Result<Ty, TypingNoContextError> {
) -> Result<Ty, TypingNoContextOrInternalError> {
self.index(index, ctx)
}

Expand All @@ -209,7 +218,7 @@ impl<T: TyCustomImpl> TyCustomDyn for T {
bin_op: TypingBinOp,
rhs: &TyBasic,
ctx: &TypingOracleCtx,
) -> Result<Ty, TypingNoContextError> {
) -> Result<Ty, TypingNoContextOrInternalError> {
self.bin_op(bin_op, rhs, ctx)
}

Expand Down Expand Up @@ -269,21 +278,25 @@ impl TyCustom {
x.0.intersects_dyn(&*y.0)
}

pub(crate) fn intersects_with(&self, other: &TyBasic, ctx: TypingOracleCtx) -> bool {
pub(crate) fn intersects_with(
&self,
other: &TyBasic,
ctx: TypingOracleCtx,
) -> Result<bool, InternalError> {
if self.0.is_intersects_with_dyn(other) {
return true;
return Ok(true);
}
match other {
TyBasic::Custom(other) => Self::intersects(self, other),
TyBasic::Name(name) => self.as_name() == Some(name.as_str()),
TyBasic::Custom(other) => Ok(Self::intersects(self, other)),
TyBasic::Name(name) => Ok(self.as_name() == Some(name.as_str())),
TyBasic::Callable(c) => {
if let Some(this) = self.0.as_callable_dyn() {
ctx.callables_intersect(&this, c)
} else {
false
Ok(false)
}
}
_ => false,
_ => Ok(false),
}
}

Expand Down
11 changes: 8 additions & 3 deletions starlark/src/typing/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::typing::call_args::TyCallArgs;
use crate::typing::callable::TyCallable;
use crate::typing::custom::TyCustomImpl;
use crate::typing::error::TypingNoContextError;
use crate::typing::error::TypingNoContextOrInternalError;
use crate::typing::error::TypingOrInternalError;
use crate::typing::ParamSpec;
use crate::typing::Ty;
Expand Down Expand Up @@ -100,15 +101,19 @@ impl<F: TyCustomFunctionImpl> TyCustomImpl for TyCustomFunction<F> {
bin_op: TypingBinOp,
_rhs: &TyBasic,
_ctx: &TypingOracleCtx,
) -> Result<Ty, TypingNoContextError> {
) -> Result<Ty, TypingNoContextOrInternalError> {
match bin_op {
// `str | list`.
TypingBinOp::BitOr if self.0.has_type_attr() => Ok(Ty::basic(TyBasic::Type)),
_ => Err(TypingNoContextError),
_ => Err(TypingNoContextOrInternalError::Typing),
}
}

fn index(&self, _item: &TyBasic, _ctx: &TypingOracleCtx) -> Result<Ty, TypingNoContextError> {
fn index(
&self,
_item: &TyBasic,
_ctx: &TypingOracleCtx,
) -> Result<Ty, TypingNoContextOrInternalError> {
// TODO(nga): this is hack for `enum` (type) which pretends to be a function.
// Should be a custom type.
Ok(Ty::any())
Expand Down
Loading

0 comments on commit 73cfaa9

Please sign in to comment.