Skip to content

Commit

Permalink
Merge 81c63a0 into dbff13d
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj authored Aug 23, 2024
2 parents dbff13d + 81c63a0 commit d3873f9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
16 changes: 16 additions & 0 deletions sway-core/src/language/ty/expression/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ impl TypeCheckAnalysis for TyExpression {
ctx: &mut TypeCheckAnalysisContext,
) -> Result<(), ErrorEmitted> {
match &self.expression {
TyExpressionVariant::FunctionApplication {
type_binding: Some(type_binding),
..
} => {
let args = match &type_binding.type_arguments {
TypeArgs::Regular(args) | TypeArgs::Prefix(args) => args,
};
for arg in args {
if let TypeInfo::Placeholder(_) = &*ctx.engines.te().get(arg.type_id) {
let _ = handler.emit_err(CompileError::UnableToInferGeneric {
ty: ctx.engines.help_out(arg.type_id).to_string(),
span: arg.span.clone(),
});
}
}
}
// Check literal "fits" into assigned typed.
TyExpressionVariant::Literal(Literal::Numeric(literal_value)) => {
let t = ctx.engines.te().get(self.return_type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub(crate) fn instantiate_function_application(
let function_is_trait_method_dummy = function_decl.is_trait_method_dummy;
let new_decl_ref = decl_engine
.insert(
function_decl,
function_decl.clone(),
decl_engine
.get_parsed_decl_id(function_decl_ref.id())
.as_ref(),
Expand All @@ -115,13 +115,37 @@ pub(crate) fn instantiate_function_application(
new_decl_ref
};

let call_path = call_path_binding.inner.clone();
let mut type_binding = call_path_binding.strip_inner();

// Normalize all function calls to have the same generic argument count
// as the definition
if function_decl.type_parameters.len() != type_binding.type_arguments.as_slice().len() {
type_binding.type_arguments = TypeArgs::Regular(
function_decl
.type_parameters
.iter()
.map(|x| TypeArgument {
type_id: x.type_id,
initial_type_id: x.initial_type_id,
span: span.clone(),
call_path_tree: None,
})
.collect(),
);
}
assert_eq!(
function_decl.type_parameters.len(),
type_binding.type_arguments.as_slice().len()
);

let exp = ty::TyExpression {
expression: ty::TyExpressionVariant::FunctionApplication {
call_path: call_path_binding.inner.clone(),
call_path,
arguments: typed_arguments_with_names,
fn_ref: new_decl_ref,
selector: None,
type_binding: Some(call_path_binding.strip_inner()),
type_binding: Some(type_binding),
call_path_typeid: None,
contract_call_params: IndexMap::new(),
contract_caller: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ script;

fn main() {
let g: bool = three_generics(true, "foo", 10);

// Should fail because compiler cannot infer generic argument
one_generic();
}

fn three_generics(a: A, b: B, c: C) -> A {
let new_a: A = a;
new_a
}

fn one_generic<T>() {

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ category = "fail"
# check: $()Unknown type name "B"
# check: $()Could not find symbol "C" in this scope.
# check: $()Unknown type name "C"

# check: $()one_generic()
# nextln: $()Cannot infer type for type parameter "T". Insufficient type information provided. Try annotating its type.
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,6 @@ fn test_storage() {
// If these comparisons are done inline just above then it blows out the register allocator due to
// all the ASM blocks.
#[inline(never)]
fn assert_streq<S1, S2>(lhs: S1, rhs: str) {
fn assert_streq<S1>(lhs: S1, rhs: str) {
assert(sha256_str_array(lhs) == sha256(rhs));
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,6 @@ fn test_storage() {
// If these comparisons are done inline just above then it blows out the register allocator due to
// all the ASM blocks.
#[inline(never)]
fn assert_streq<S1, S2>(lhs: S1, rhs: str) {
fn assert_streq<S1>(lhs: S1, rhs: str) {
assert(sha256_str_array(lhs) == sha256(rhs));
}

0 comments on commit d3873f9

Please # to comment.