diff --git a/crates/rue-compiler/src/compile/expr/call_inference.rs b/crates/rue-compiler/src/compile/expr/call_inference.rs index 29f1c753..4188b493 100644 --- a/crates/rue-compiler/src/compile/expr/call_inference.rs +++ b/crates/rue-compiler/src/compile/expr/call_inference.rs @@ -16,7 +16,10 @@ use rue_types::{ contains_generic, substitute_with_mappings, }; -use crate::{ArgumentFrame, Compiler, ParameterWrap, compile_expr, compile_lambda_argument}; +use crate::{ + ArgumentFrame, Compiler, ParameterWrap, compile_expr, compile_lambda_argument, + occurs_contravariantly, +}; /// What inferring a call's generics gave. pub(crate) struct InferredCall { @@ -71,6 +74,11 @@ pub(crate) fn infer_call( // only uses the type expected of it for the generics its arguments don't give a type (see // `refine_element_expected`), so it isn't solved again for it either let fallback_only = ctx.is_expected_type_fallback() && !args.is_empty(); + // The type expected of the call, if it's a type of its own that the call has to be assignable + // to. One that's only a hint doesn't count (see `Compiler::with_expected_type_hint`), such as one + // that comes from what an enclosing call has inferred so far, which it can still widen + let final_expected = expected_type.filter(|_| !ctx.is_expected_type_hint() && !fallback_only); + let solver = ctx.call_solver(expected); let solver = Rc::new(if fallback_only { solver.fallback_only() @@ -140,11 +148,18 @@ pub(crate) fn infer_call( })); // The parameter's type is only a hint while it references the callee's generics, since - // what they're inferred as so far can still widen - let hint = function - .generics - .iter() - .any(|&generic| contains_generic(ctx.types_mut(), param, generic)); + // what they're inferred as so far can still widen. It isn't one when the call's own expected + // type is final and those generics only occur where values flow out of the argument: then + // a solution that fits the call's expected type has the argument fit it, so + // `wrap(map(fn(x) => x, 5))` with `(5, Int)` expected solves `map` for `5`. Where values + // flow into the argument, such as `A` in `fn(a: A) -> B`, what they're inferred as so far + // can be wider than the solution, which would ask more of the argument + let writes = order.writes(i); + let hint = !writes.is_empty() + && (final_expected.is_none() + || writes + .iter() + .any(|&generic| occurs_contravariantly(ctx, param, generic))); // A lambda expected to be exactly the parameter's function type wraps the callee's // generics in its parameters (see `ParameterWrap`) @@ -200,11 +215,9 @@ pub(crate) fn infer_call( let mut mappings = ctx.solve_call(&solver, order.merged().clone()); // Only a call that the arguments don't solve otherwise is solved again with lambdas' untyped - // parameters as vars, so that nothing that compiled before changes. A type expected of the call - // that's only a hint doesn't count (see `Compiler::with_expected_type_hint`), such as one that - // comes from what an enclosing call has inferred so far, which it can still widen. The checks - // are made with the renamed generics restored, as the call is checked - let expected_type = expected_type.filter(|_| !ctx.is_expected_type_hint() && !fallback_only); + // parameters as vars, so that nothing that compiled before changes, and only for a final + // expected type. The checks are made with the renamed generics restored, as the call is checked + let expected_type = final_expected; restore_renamed(ctx, &mut mappings, renamed); @@ -741,6 +754,11 @@ impl ArgumentOrder { argument.is_lambda() && self.last_settled && !argument.returns_reads() } + /// The callee's generics in the argument's parameter type (see `Argument::writes`). + fn writes(&self, i: usize) -> &IndexSet { + &self.arguments[i].writes + } + /// Whether every argument has been compiled. fn is_done(&self) -> bool { self.pending.is_empty() diff --git a/crates/rue-compiler/src/compile/expr/lambda.rs b/crates/rue-compiler/src/compile/expr/lambda.rs index 03477e91..098a1680 100644 --- a/crates/rue-compiler/src/compile/expr/lambda.rs +++ b/crates/rue-compiler/src/compile/expr/lambda.rs @@ -338,7 +338,7 @@ fn make_references(ctx: &mut Compiler, wrapped: &[(TypeId, TypeId)]) { /// Whether the generic occurs in the type where values flow out of whoever holds a value of it, /// such as a parameter of a function type in it. -fn occurs_contravariantly(ctx: &mut Compiler, ty: TypeId, generic: TypeId) -> bool { +pub(crate) fn occurs_contravariantly(ctx: &mut Compiler, ty: TypeId, generic: TypeId) -> bool { let ty = rue_types::substitute(ctx.types_mut(), ty); let mut visited = std::collections::HashSet::new(); let mut stack = vec![(ty, true)]; diff --git a/tests/types/generic_inference_lambda_parameter_vars.rue b/tests/types/generic_inference_lambda_parameter_vars.rue index 802cc72f..90b2de7c 100644 --- a/tests/types/generic_inference_lambda_parameter_vars.rue +++ b/tests/types/generic_inference_lambda_parameter_vars.rue @@ -179,3 +179,50 @@ test fn excluded_generic_next_to_a_wrapped_one() -> Int { let result: (5, Int) = mixed_map(fn(p, callback) => (p.first, p.rest + callback(p.rest)), 5, 6); result.first + result.rest } + +fn wrap(value: T) -> (T, Int) { + (value, 1) +} + +test fn argument_of_a_call_with_a_final_expected_type() -> Int { + // `wrap`'s `(5, Int)` is a type of its own, and no other argument can add to its `T`, so the + // `5` its argument is expected to be is what `T` has to be, and `map` is solved for it, as + // `wrap::<5>(map::<5, 5>(...))` is + let result: (5, Int) = wrap(map(fn(x) => x, 5)); + result.first +} + +fn wrap_after(value: T, _last: T) -> (T, Int) { + (value, 1) +} + +test fn argument_before_another_one_with_the_generic() -> Int { + // The `6` after it adds to `T` too, but `T` is still at most `5 | 6`, so `map` is solved for + // what fits that, as `wrap_after::<5 | 6>(map::<5, 5>(...), 6)` is + let result: (5 | 6, Int) = wrap_after(map(fn(x) => x, 5), 6); + result.first +} + +test fn argument_of_a_guarded_call() -> Bool { + // The guard's type is only a hint, so `map` is solved as before and the guard still checks + wrap(map(fn(x) => x, 5)) is (5, Int) +} + +fn chain(first: fn(a: A) -> B, second: fn(a: B) -> C, value: A) -> C { + second(first(value)) +} + +fn feed(_first: fn(a: A) -> B, _second: fn(a: B) -> Int) -> fn(a: A) -> Int { + fn(_x) => 1 +} + +fn compose(function: fn(a: A) -> B) -> fn(a: A) -> B { + function +} + +test fn argument_whose_generic_takes_values() -> Int { + // `compose`'s `A` is where its argument takes values, so what it's inferred as so far is only a + // hint for `feed`'s: solving `feed` for it would give `fn(value: 5) -> Int`, which isn't the + // `fn(a: Int) -> Int` that `compose` ends up needing, and this compiles as before + chain::(compose(fn(_x) => 5), compose(feed(fn(x) => x, five_only)), nil) +} diff --git a/tests/types/generic_inference_lambda_parameter_vars.yaml b/tests/types/generic_inference_lambda_parameter_vars.yaml index f7de2a5d..b7f8a54b 100644 --- a/tests/types/generic_inference_lambda_parameter_vars.yaml +++ b/tests/types/generic_inference_lambda_parameter_vars.yaml @@ -125,6 +125,34 @@ tests: runtime_cost: 2605 byte_cost: 852000 total_cost: 854605 +- name: argument_of_a_call_with_a_final_expected_type + program: (a (q . 2) (q 5)) + debug_program: (a (q 2 (q 5 2) (c (a 6 (a 4 (c (c (q . 2) (c (c (q . 1) (q . 2)) (c (q . 1) ()))) (q . 5)))) 1)) (c (c (q 2 2 (c 3 ())) (q 4 1 (q . 1))) ())) + output: "5" + runtime_cost: 179 + byte_cost: 156000 + total_cost: 156179 +- name: argument_before_another_one_with_the_generic + program: (a (q . 2) (q 5)) + debug_program: (a (q 2 (q 5 2) (c (a 6 (c (a 4 (c (c (q . 2) (c (c (q . 1) (q . 2)) (c (q . 1) ()))) (q . 5))) (q . 6))) 1)) (c (c (q 2 2 (c 3 ())) (q 4 2 (q . 1))) ())) + output: "5" + runtime_cost: 179 + byte_cost: 156000 + total_cost: 156179 +- name: argument_of_a_guarded_call + program: (= (a (q . 2) (q 5)) (q . 5)) + debug_program: (a (q 9 (f (a 6 (a 4 (c (c (q . 2) (c (c (q . 1) (q . 2)) (c (q . 1) ()))) (q . 5))))) (q . 5)) (c (c (q 2 2 (c 3 ())) (q 4 1 (q . 1))) ())) + output: "1" + runtime_cost: 319 + byte_cost: 252000 + total_cost: 252319 +- name: argument_whose_generic_takes_values + program: (a (q 2 (a 2 (q 1 . 1)) (c (a (a 2 (q 1 . 5)) (q ())) ())) (c (q . 1) ())) + debug_program: (a (q 2 30 (c (a 4 (c (q . 2) (c (c (q . 1) (q 1 . 5)) (c (q . 1) ())))) (c (a 4 (a 22 (c (c (q . 2) (c (c (q . 1) (q . 2)) (c (q . 1) ()))) (c (q . 2) (c (c (q . 1) 10) (c (q . 1) ())))))) ()))) (c (c (q . 1) (c (q . 2) (c (q 4 (q . 2) (c (c (q . 1) (q 1 . 1)) (c (q . 1) ()))) (q 2 5 (c (a 2 (c 7 ())) ()))))) ())) + output: "1" + runtime_cost: 969 + byte_cost: 708000 + total_cost: 708969 diagnostics: - |- Binding `result` is returned directly after it's declared at generic_inference_lambda_parameter_vars.rue:61:5 diff --git a/tests/types/generic_inference_lambda_parameter_vars_errors.rue b/tests/types/generic_inference_lambda_parameter_vars_errors.rue index df3b59a9..19f5f23b 100644 --- a/tests/types/generic_inference_lambda_parameter_vars_errors.rue +++ b/tests/types/generic_inference_lambda_parameter_vars_errors.rue @@ -5,7 +5,7 @@ fn map(function: fn(a: A) -> B, value: A) -> B { function(value) } -fn wrap(value: T) -> (T, Int) { +fn wrap_with(_first: T, value: T) -> (T, Int) { (value, 1) } @@ -89,9 +89,9 @@ fn main() -> Int { // The lambda returns `x + 0`, not `x`, so the expected bound doesn't reach `x` let computed: 5 = map(fn(x) => x + 0, 5); - // A call that's an argument of an enclosing call is only expected to be what that call has - // inferred so far, which it can still widen, so it isn't solved again for it - let nested: (5, Int) = wrap(map(fn(x) => x, 5)); + // `wrap_with`'s `T` has to hold the `1` too, so no instantiation gives `(5, Int)`, and `map` is + // solved as before + let nested: (5, Int) = wrap_with(1, map(fn(x) => x, 5)); // `x` would have to be `6`, which `5` isn't, so the call is left as it was and reports `Int` let unfitting: 6 = map(fn(x) => x, 5);