Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove tyarg in flat_map #504

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion builtin/enumerable.sk
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module Enumerable<E>

# Like `map` but `f` should return an array and the result is flattened.
def flat_map<R>(f: Fn1<E, Array<R>>) -> Array<R>
self.map<Array<R>>(f).fold(Array<R>.new){|sum: Array<R>, item: Array<R>|
self.map(f).fold(Array<R>.new){|sum: Array<R>, item: Array<R>|
sum.append(item)
sum
}
Expand Down
50 changes: 37 additions & 13 deletions lib/skc_ast2hir/src/convert_exprs/method_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,22 +242,13 @@ pub fn build(
method_tyargs: Vec<TermTy>,
locs: &LocationSpan,
) -> Result<HirExpression> {
check_argument_types(mk, &found.sig, &receiver_hir, &mut arg_hirs, &inf)?;
let receiver_ty = receiver_hir.ty.clone();
let specialized = receiver_hir.ty.is_specialized();
let first_arg_ty = arg_hirs.get(0).map(|x| x.ty.clone());
let arg_types = arg_hirs.iter().map(|x| x.ty.clone()).collect::<Vec<_>>();
let infer_tyargs = found.sig.has_typarams() && method_tyargs.is_empty();

let receiver = Hir::bit_cast(found.owner.to_term_ty(), receiver_hir);
let args = if specialized {
arg_hirs
.into_iter()
.map(|expr| Hir::bit_cast(ty::raw("Object"), expr))
.collect::<Vec<_>>()
} else {
arg_hirs
};
let tyargs = if method_tyargs.is_empty() {
let tyargs = if infer_tyargs {
let err = error::method_tyarg_inference_failed(
format!("Could not infer type arg(s) of {}", found.sig),
locs,
Expand All @@ -267,12 +258,45 @@ pub fn build(
method_tyargs
};

let updated_param_types = if infer_tyargs {
found
.sig
.params
.iter()
.map(|param| param.ty.substitute(Default::default(), &tyargs))
.collect::<Vec<_>>()
} else {
found
.sig
.params
.iter()
.map(|param| param.ty.clone())
.collect::<Vec<_>>()
};
check_argument_types(
mk,
&found.sig,
&receiver_hir,
&mut arg_hirs,
&updated_param_types,
)?;

let args = if specialized {
arg_hirs
.into_iter()
.map(|expr| Hir::bit_cast(ty::raw("Object"), expr))
.collect::<Vec<_>>()
} else {
arg_hirs
};

// Special handling for `Foo.new(x)` where `Foo<T>` is a generic class and
// `T` is inferred from `x`.
if found.is_generic_new(&receiver_ty) {
return Ok(call_specialized_new(mk, &receiver_ty, args, tyargs, locs));
}

let receiver = Hir::bit_cast(found.owner.to_term_ty(), receiver_hir);
let hir = build_hir(mk, &found, receiver, args, tyargs, &inf);
if found.sig.fullname.full_name == "Object#unsafe_cast" {
Ok(Hir::bit_cast(first_arg_ty.unwrap().instance_ty(), hir))
Expand All @@ -286,9 +310,9 @@ fn check_argument_types(
sig: &MethodSignature,
receiver_hir: &HirExpression,
arg_hirs: &mut [HirExpression],
inf: &Option<method_call_inf::MethodCallInf3>,
arg_types: &[TermTy],
) -> Result<()> {
type_checking::check_method_args(&mk.class_dict, sig, receiver_hir, arg_hirs, inf)?;
type_checking::check_method_args(&mk.class_dict, sig, receiver_hir, arg_hirs, arg_types)?;
if let Some(last_arg) = arg_hirs.last_mut() {
check_break_in_block(sig, last_arg)?;
}
Expand Down
36 changes: 12 additions & 24 deletions lib/skc_ast2hir/src/type_system/type_checking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ pub fn check_method_args(
sig: &MethodSignature,
_receiver_hir: &HirExpression,
arg_hirs: &[HirExpression],
inf: &Option<method_call_inf::MethodCallInf3>,
param_types: &[TermTy],
) -> Result<()> {
let mut result = check_method_arity(sig, arg_hirs);
if result.is_ok() {
result = check_arg_types(class_dict, sig, arg_hirs, inf);
result = check_arg_types(class_dict, sig, arg_hirs, param_types);
}

if result.is_err() {
Expand Down Expand Up @@ -152,13 +152,13 @@ fn check_arg_types(
class_dict: &ClassDict,
sig: &MethodSignature,
arg_hirs: &[HirExpression],
inf: &Option<method_call_inf::MethodCallInf3>,
param_types: &[TermTy],
) -> Result<()> {
for i in 0..sig.params.len() {
let param = &sig.params[i];
let arg_hir = &arg_hirs[i];
let inferred = inf.as_ref().map(|x| &x.solved_method_arg_tys[i]);
check_arg_type(class_dict, sig, arg_hir, param, &inferred)?;
let param_ty = &param_types[i];
check_arg_type(class_dict, sig, arg_hir, param, param_ty)?;
}
Ok(())
}
Expand All @@ -169,32 +169,20 @@ fn check_arg_type(
sig: &MethodSignature,
arg_hir: &HirExpression,
param: &MethodParam,
inferred: &Option<&TermTy>,
param_ty: &TermTy,
) -> Result<()> {
if inferred.is_some() {
// Type inferrence succeed == no type error found
return Ok(());
}
let expected = &param.ty;
let arg_ty = &arg_hir.ty;
if class_dict.conforms(arg_ty, expected) {
if class_dict.conforms(arg_ty, param_ty) {
return Ok(());
}

let msg = if inferred.is_some() {
format!(
"the argument `{}' of `{}' is inferred to {} but got {}",
param.name, sig.fullname, expected, arg_ty.fullname
)
} else {
format!(
"the argument `{}' of `{}' should be {} but got {}",
param.name, sig.fullname, param.ty, arg_ty
)
};
let msg = format!(
"the argument `{}' of `{}' should be {} but got {}",
param.name, sig.fullname, param_ty, arg_ty
);
let locs = &arg_hir.locs;
let report = skc_error::build_report(msg, locs, |r, locs_span| {
r.with_label(Label::new(locs_span).with_message(&arg_hir.ty))
r.with_label(Label::new(locs_span).with_message(&arg_ty))
});
Err(type_error(report))
}
Expand Down
4 changes: 4 additions & 0 deletions lib/skc_hir/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ impl MethodSignature {
self.params.iter().any(|p| p.has_default)
}

pub fn has_typarams(&self) -> bool {
!self.typarams.is_empty()
}

pub fn is_class_method(&self) -> bool {
self.fullname.type_name.is_meta()
}
Expand Down
Loading