GithubHelp home page GithubHelp logo

Comments (11)

TrickyPi avatar TrickyPi commented on July 22, 2024

Hi @kdy1, i'd like to fix it 😊 .

from stc.

kdy1 avatar kdy1 commented on July 22, 2024

Thank you!

from stc.

TrickyPi avatar TrickyPi commented on July 22, 2024

Hi @kdy1

I created a minimal test for debugging
test
the file link

and the error information is
error

I found that the error happens here

pub(crate) fn assign_with_opts(&mut self, data: &mut AssignData, mut opts: AssignOpts, left: &Type, right: &Type) -> VResult<()> {
if self.is_builtin {
return Ok(());
}
opts.is_params_of_method_definition = false;
left.assert_valid();
right.assert_valid();
let _stack = stack::track(opts.span)?;
// if cfg!(debug_assertions) && span.is_dummy() {
// print_backtrace();
// debug_assert!(!span.is_dummy());
// }
// self.verify_before_assign("lhs", left);
// self.verify_before_assign("rhs", right);
let res = self.assign_inner(data, left, right, opts);
match res {
Err(Error::Errors { errors, .. }) if errors.is_empty() => return Ok(()),
_ => {}
}
res.convert_err(|err| match err {
Error::AssignFailed { .. }
| Error::Errors { .. }
| Error::Unimplemented { .. }
| Error::TupleAssignError { .. }
| Error::ObjectAssignFailed { .. } => err,
_ => Error::AssignFailed {
span: opts.span,
left: box left.clone(),
right: box right.clone(),
right_ident: opts.right_ident_span,
cause: vec![err],
},
})

Do you have any advice? Seems the bug isn't here

RPat::Rest(elem) => {
// Rest element is special.
let type_for_rest_arg = match ty {
Some(ty) => self
.get_rest_elements(Some(span), Cow::Owned(ty), idx)
.context(
"tried to get lefting elements of an iterator to declare variables using a rest \
pattern",
)
.map(Cow::into_owned)
.report(&mut self.storage),
None => None,
}
.freezed();
let default = match default {
Some(ty) => self
.get_rest_elements(Some(span), Cow::Owned(ty), idx)
.context(
"tried to get lefting elements of an iterator to declare variables using a rest \
pattern",
)
.map(Cow::into_owned)
.report(&mut self.storage),
None => None,
}
.freezed();
self.add_vars(&elem.arg, type_for_rest_arg, None, default, opts)
.context("tried to declare lefting elements to the arugment of a rest pattern")
.report(&mut self.storage);
break;
}
_ => {}
}

BTW, opts.use_iterator_for_array is true

if opts.use_iterator_for_array {
// Handle tuple
//
// const [a , setA] = useState();
//
let ty = ty.map(|ty| {
self.get_iterator(
span,
Cow::Owned(ty),
GetIteratorOpts {
disallow_str: true,
..Default::default()
},
)
.context("tried to convert a type to an iterator to assign with an array pattern.")
.unwrap_or_else(|err| {
self.storage.report(err);
Cow::Owned(Type::any(span, Default::default()))
})
});
let default = default.map(|ty| {
self.get_iterator(
span,
Cow::Owned(ty),
GetIteratorOpts {
disallow_str: true,
..Default::default()
},
)
.context("tried to convert a type to an iterator to assign with an array pattern (default value)")
.unwrap_or_else(|err| {
self.storage.report(err);
Cow::Owned(Type::any(span, Default::default()))
})
});
for (idx, elem) in arr.elems.iter().enumerate() {
if let Some(elem) = elem {
let elem_ty = ty
.as_ref()
.try_map(|ty| -> VResult<_> {
Ok(self
.get_element_from_iterator(span, Cow::Borrowed(&ty), idx)
.with_context(|| {
format!(
"tried to get the type of {}th element from iterator to declare vars with an array pattern",
idx
)
})?
.into_owned())
})?
.freezed();
let default_elem_ty = default
.as_ref()
.and_then(|ty| {
self.get_element_from_iterator(span, Cow::Borrowed(&ty), idx)
.with_context(|| {
format!(
"tried to get the type of {}th element from iterator to declare vars with an array \
pattern (default value)",
idx
)
})
.ok()
})
.map(Cow::into_owned)
.freezed();
// TODO(kdy1): actual_ty
self.add_vars(elem, elem_ty, None, default_elem_ty, opts)?;
}
}
Ok(())
} else {
for (idx, elem) in arr.elems.iter().enumerate() {
match elem {
Some(elem) => {
match elem {
RPat::Rest(elem) => {
// Rest element is special.
let type_for_rest_arg = match ty {
Some(ty) => self
.get_rest_elements(Some(span), Cow::Owned(ty), idx)
.context(
"tried to get lefting elements of an iterator to declare variables using a rest \
pattern",
)
.map(Cow::into_owned)
.report(&mut self.storage),
None => None,
}
.freezed();
let default = match default {
Some(ty) => self
.get_rest_elements(Some(span), Cow::Owned(ty), idx)
.context(
"tried to get lefting elements of an iterator to declare variables using a rest \
pattern",
)
.map(Cow::into_owned)
.report(&mut self.storage),
None => None,
}
.freezed();
self.add_vars(&elem.arg, type_for_rest_arg, None, default, opts)
.context("tried to declare lefting elements to the arugment of a rest pattern")
.report(&mut self.storage);
break;
}
_ => {}
}
let elem_ty = match &ty {
Some(ty) => self
.access_property(
elem.span(),
&ty,
&Key::Num(RNumber {
span: elem.span(),
value: idx as f64,
}),
TypeOfMode::RValue,
IdCtx::Var,
Default::default(),
)
.context("tried to access property to declare variables using an array pattern")
.report(&mut self.storage),
None => None,
}
.freezed();
let default = match &default {
Some(ty) => self
.access_property(
elem.span(),
&ty,
&Key::Num(RNumber {
span: elem.span(),
value: idx as f64,
}),
TypeOfMode::RValue,
IdCtx::Var,
Default::default(),
)
.context("tried to access property to declare variables using an array pattern")
.report(&mut self.storage),
None => None,
}
.freezed();
// TODO(kdy1): actual_ty
self.add_vars(elem, elem_ty, None, default, opts).report(&mut self.storage);
}
// Skip
None => {}
}
}

from stc.

kdy1 avatar kdy1 commented on July 22, 2024

I guess that get_rest_elements is returning the wrong value.

And there's a TODO for it

iterator.make_clone_cheap();
let ty = iterator.into_owned().expect_tuple();
// TODO: Handle [Type::Rest]
return Ok(Cow::Owned(Type::Tuple(Tuple {
elems: ty.elems.into_iter().skip(start_index).collect(),
..ty
})));

from stc.

TrickyPi avatar TrickyPi commented on July 22, 2024

Thanks for your reply.
Yes, I knew this TODO exists, but I added a "println!" before calling get_rest_elements and it wasn't called.

from stc.

kdy1 avatar kdy1 commented on July 22, 2024

If so, I think you can add some debug prints to scope/vars.rs in the body of the issue

from stc.

TrickyPi avatar TrickyPi commented on July 22, 2024

yeah, i added a debug print to ouput the value of opts.use_iterator_for_array, this value is true.

from stc.

kdy1 avatar kdy1 commented on July 22, 2024

I think you should print types instead. There's dump_type_as_string which can be used to print readable form of types

from stc.

TrickyPi avatar TrickyPi commented on July 22, 2024

Ok, i get it.

from stc.

kdy1 avatar kdy1 commented on July 22, 2024

@TrickyPi Are you working on it? Feel free to tell me if you want me to take it (or unassign)

from stc.

TrickyPi avatar TrickyPi commented on July 22, 2024

I'v been busy with other work lately, and this issue seems hard to me(LOL),so I'm happy you can take it and really appericate your help during this time.

from stc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.