Skip to content
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
40 changes: 29 additions & 11 deletions crates/rue-compiler/src/compile/expr/call_inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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`)
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<TypeId> {
&self.arguments[i].writes
}

/// Whether every argument has been compiled.
fn is_done(&self) -> bool {
self.pending.is_empty()
Expand Down
2 changes: 1 addition & 1 deletion crates/rue-compiler/src/compile/expr/lambda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
Expand Down
47 changes: 47 additions & 0 deletions tests/types/generic_inference_lambda_parameter_vars.rue
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(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<T>(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<A, B, C>(first: fn(a: A) -> B, second: fn(a: B) -> C, value: A) -> C {
second(first(value))
}

fn feed<A, B>(_first: fn(a: A) -> B, _second: fn(a: B) -> Int) -> fn(a: A) -> Int {
fn(_x) => 1
}

fn compose<A, B>(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::<Int | nil, 5, Int>(compose(fn(_x) => 5), compose(feed(fn(x) => x, five_only)), nil)
}
28 changes: 28 additions & 0 deletions tests/types/generic_inference_lambda_parameter_vars.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn map<A, B>(function: fn(a: A) -> B, value: A) -> B {
function(value)
}

fn wrap<T>(value: T) -> (T, Int) {
fn wrap_with<T>(_first: T, value: T) -> (T, Int) {
(value, 1)
}

Expand Down Expand Up @@ -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);
Expand Down