mirror of
https://github.com/erg-lang/erg.git
synced 2025-10-03 05:54:33 +00:00
Fix a bug
This commit is contained in:
parent
c7096b17f1
commit
208433d337
6 changed files with 26 additions and 6 deletions
|
@ -251,6 +251,7 @@ impl Context {
|
||||||
Private
|
Private
|
||||||
};
|
};
|
||||||
let default = kind.default_info();
|
let default = kind.default_info();
|
||||||
|
let is_var_params = kind.is_var_params();
|
||||||
match &sig.pat {
|
match &sig.pat {
|
||||||
// Literal patterns will be desugared to discard patterns
|
// Literal patterns will be desugared to discard patterns
|
||||||
ast::ParamPattern::Lit(_) => unreachable!(),
|
ast::ParamPattern::Lit(_) => unreachable!(),
|
||||||
|
@ -299,6 +300,11 @@ impl Context {
|
||||||
Normal,
|
Normal,
|
||||||
kind,
|
kind,
|
||||||
)?;
|
)?;
|
||||||
|
let spec_t = if is_var_params {
|
||||||
|
unknown_len_array_t(spec_t)
|
||||||
|
} else {
|
||||||
|
spec_t
|
||||||
|
};
|
||||||
if &name.inspect()[..] == "self" {
|
if &name.inspect()[..] == "self" {
|
||||||
if let Some(self_t) = self.rec_get_self_t() {
|
if let Some(self_t) = self.rec_get_self_t() {
|
||||||
self.sub_unify(&spec_t, &self_t, name.loc(), Some(name.inspect()))?;
|
self.sub_unify(&spec_t, &self_t, name.loc(), Some(name.inspect()))?;
|
||||||
|
@ -506,7 +512,7 @@ impl Context {
|
||||||
};
|
};
|
||||||
let name = &sig.ident.name;
|
let name = &sig.ident.name;
|
||||||
// FIXME: constでない関数
|
// FIXME: constでない関数
|
||||||
let t = self.get_current_scope_var(name).map(|v| &v.t).unwrap();
|
let t = self.get_current_scope_var(name).map(|vi| &vi.t).unwrap();
|
||||||
let non_default_params = t.non_default_params().unwrap();
|
let non_default_params = t.non_default_params().unwrap();
|
||||||
let var_args = t.var_params();
|
let var_args = t.var_params();
|
||||||
let default_params = t.default_params().unwrap();
|
let default_params = t.default_params().unwrap();
|
||||||
|
|
|
@ -176,7 +176,7 @@ impl NestedDisplay for Args {
|
||||||
fmt_lines(self.pos_args.iter(), f, level)?;
|
fmt_lines(self.pos_args.iter(), f, level)?;
|
||||||
}
|
}
|
||||||
if let Some(var_args) = &self.var_args {
|
if let Some(var_args) = &self.var_args {
|
||||||
writeln!(f, "...")?;
|
writeln!(f, "*")?;
|
||||||
var_args.fmt_nest(f, level)?;
|
var_args.fmt_nest(f, level)?;
|
||||||
}
|
}
|
||||||
if !self.kw_args.is_empty() {
|
if !self.kw_args.is_empty() {
|
||||||
|
@ -1683,8 +1683,9 @@ impl Locational for Params {
|
||||||
) {
|
) {
|
||||||
(Some(l), _, Some(r)) => Location::concat(l, r),
|
(Some(l), _, Some(r)) => Location::concat(l, r),
|
||||||
(Some(l), Some(r), None) => Location::concat(l, r.as_ref()),
|
(Some(l), Some(r), None) => Location::concat(l, r.as_ref()),
|
||||||
(Some(l), None, None) => Location::concat(l, self.non_defaults.last().unwrap()),
|
|
||||||
(None, Some(l), Some(r)) => Location::concat(l.as_ref(), r),
|
(None, Some(l), Some(r)) => Location::concat(l.as_ref(), r),
|
||||||
|
(Some(l), None, None) => Location::concat(l, self.non_defaults.last().unwrap()),
|
||||||
|
(None, Some(var), None) => var.loc(),
|
||||||
(None, None, Some(r)) => Location::concat(self.defaults.first().unwrap(), r),
|
(None, None, Some(r)) => Location::concat(self.defaults.first().unwrap(), r),
|
||||||
_ => Location::Unknown,
|
_ => Location::Unknown,
|
||||||
}
|
}
|
||||||
|
|
|
@ -3111,7 +3111,7 @@ impl fmt::Display for Params {
|
||||||
f,
|
f,
|
||||||
"({}, {}, {})",
|
"({}, {}, {})",
|
||||||
fmt_vec(&self.non_defaults),
|
fmt_vec(&self.non_defaults),
|
||||||
fmt_option!(pre "...", &self.var_params),
|
fmt_option!(pre "*", &self.var_params),
|
||||||
fmt_vec(&self.defaults)
|
fmt_vec(&self.defaults)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -3132,8 +3132,9 @@ impl Locational for Params {
|
||||||
) {
|
) {
|
||||||
(Some(l), _, Some(r)) => Location::concat(l, r),
|
(Some(l), _, Some(r)) => Location::concat(l, r),
|
||||||
(Some(l), Some(r), None) => Location::concat(l, r.as_ref()),
|
(Some(l), Some(r), None) => Location::concat(l, r.as_ref()),
|
||||||
(Some(l), None, None) => Location::concat(l, self.non_defaults.last().unwrap()),
|
|
||||||
(None, Some(l), Some(r)) => Location::concat(l.as_ref(), r),
|
(None, Some(l), Some(r)) => Location::concat(l.as_ref(), r),
|
||||||
|
(Some(l), None, None) => Location::concat(l, self.non_defaults.last().unwrap()),
|
||||||
|
(None, Some(var), None) => var.loc(),
|
||||||
(None, None, Some(r)) => Location::concat(self.defaults.first().unwrap(), r),
|
(None, None, Some(r)) => Location::concat(self.defaults.first().unwrap(), r),
|
||||||
_ => Location::Unknown,
|
_ => Location::Unknown,
|
||||||
}
|
}
|
||||||
|
|
5
tests/should_err/var_args.er
Normal file
5
tests/should_err/var_args.er
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
p! *x: Int = print! x
|
||||||
|
p! 1, 2, "a" # ERR
|
||||||
|
|
||||||
|
first *x = x[0]
|
||||||
|
assert first(1, 2, 3) == "b" # ERR
|
|
@ -1,3 +1,5 @@
|
||||||
p! *x: Int = print! x
|
p! *x: Int = print! x
|
||||||
|
|
||||||
p! 1, 2, 3
|
p! 1, 2, 3
|
||||||
|
|
||||||
|
first *x = x[0]
|
||||||
|
assert first(1, 2, 3) == 1
|
||||||
|
|
|
@ -244,3 +244,8 @@ fn exec_quantified_err() -> Result<(), ()> {
|
||||||
fn exec_var_args() -> Result<(), ()> {
|
fn exec_var_args() -> Result<(), ()> {
|
||||||
expect_success("tests/should_ok/var_args.er")
|
expect_success("tests/should_ok/var_args.er")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn exec_var_args_err() -> Result<(), ()> {
|
||||||
|
expect_failure("tests/should_err/var_args.er", 2)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue