diff --git a/crates/rue-compiler/src/compile/field.rs b/crates/rue-compiler/src/compile/field.rs index a2d4c90f..ce51d3ed 100644 --- a/crates/rue-compiler/src/compile/field.rs +++ b/crates/rue-compiler/src/compile/field.rs @@ -1,7 +1,9 @@ +use std::collections::HashSet; + use rue_diagnostic::DiagnosticKind; use rue_hir::{FieldAccess, FieldKind, Hir, HirId, TypePath, Value}; use rue_lint::FieldRead; -use rue_types::{Pair, Struct, Type, TypeId, Union}; +use rue_types::{Struct, Type, TypeId, Union}; use crate::{Compiler, GetTextRange}; @@ -99,7 +101,7 @@ pub fn compile_field(ctx: &mut Compiler, expr: Value, name: &Field<'_>) -> Field ); let first = if pairs.len() == 1 { - wrapped_element(ctx, expr.ty, |pair| pair.first).unwrap_or(pairs[0].first) + wrapped_at(ctx, expr.ty, &[TypePath::First]).unwrap_or(pairs[0].first) } else { ctx.alloc_type(Type::Union(Union::new( pairs.iter().map(|pair| pair.first).collect(), @@ -124,7 +126,7 @@ pub fn compile_field(ctx: &mut Compiler, expr: Value, name: &Field<'_>) -> Field ); let rest = if pairs.len() == 1 { - wrapped_element(ctx, expr.ty, |pair| pair.rest).unwrap_or(pairs[0].rest) + wrapped_at(ctx, expr.ty, &[TypePath::Rest]).unwrap_or(pairs[0].rest) } else { ctx.alloc_type(Type::Union(Union::new( pairs.iter().map(|pair| pair.rest).collect(), @@ -224,6 +226,12 @@ fn compile_struct_field( } } + // The field's type before the struct's type is substituted, which the references are in + let field_type = match ctx.ty(expr.ty) { + Type::Struct(raw) => wrapped_at(ctx, raw.inner, &path), + _ => None, + } + .unwrap_or(field_type); let hir = alloc_field_access(ctx, expr.hir, name, FieldKind::Path(path)); let mut value = Value::new(hir, field_type); @@ -242,19 +250,49 @@ fn alloc_field_access(ctx: &mut Compiler, value: HirId, name: &str, kind: FieldK })) } -/// The element of the value's pair type, if it's a pair whose element is a reference that an untyped -/// lambda parameter's type is made of (see `ParameterWrap`), which the element keeps, so that the -/// lambda's return type still shows where the parameter flows (#362, step 13e). Substituting the type -/// would see through the reference. -fn wrapped_element( - ctx: &Compiler, - ty: TypeId, - element: impl Fn(&Pair) -> TypeId, -) -> Option { - let Type::Pair(pair) = ctx.ty(ty) else { - return None; - }; +/// The part of the type at the path, through its pairs, if it keeps a reference that an untyped +/// lambda parameter's type is made of (see `ParameterWrap`), so that the lambda's return type still +/// shows where the parameter flows (#362, step 13e). Substituting the type would see through the +/// reference. +fn wrapped_at(ctx: &Compiler, ty: TypeId, path: &[TypePath]) -> Option { + let mut ty = ty; + + for step in path { + let Type::Pair(pair) = ctx.ty(ty) else { + return None; + }; + + ty = match step { + TypePath::First => pair.first, + TypePath::Rest => pair.rest, + }; + } + + keeps_reference(ctx, ty).then_some(ty) +} + +/// Whether the type has a reference that an untyped lambda parameter's type is made of in it, through +/// the pairs, unions and structs it's made of, which are what field accesses reach. +fn keeps_reference(ctx: &Compiler, ty: TypeId) -> bool { + let mut visited = HashSet::new(); + let mut stack = vec![ty]; + + while let Some(ty) = stack.pop() { + if !visited.insert(ty) { + continue; + } + + if ctx.is_reported_as_another_type(ty) { + return true; + } + + match ctx.ty(ty) { + Type::Pair(pair) => stack.extend([pair.first, pair.rest]), + Type::Union(union) => stack.extend(union.types.iter().copied()), + Type::Struct(ty) => stack.push(ty.inner), + _ => {} + } + } - let element = element(pair); - ctx.is_reported_as_another_type(element).then_some(element) + false } diff --git a/tests/types/generic_inference_lambda_parameter_vars.rue b/tests/types/generic_inference_lambda_parameter_vars.rue index 90b2de7c..dd880700 100644 --- a/tests/types/generic_inference_lambda_parameter_vars.rue +++ b/tests/types/generic_inference_lambda_parameter_vars.rue @@ -226,3 +226,69 @@ test fn argument_whose_generic_takes_values() -> Int { // `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) } + +fn nested_map(function: fn(p: ((A, Int), Int)) -> B, value: A) -> B { + function(((value, 1), 2)) +} + +fn nested_rest_map(function: fn(p: ((Int, A), Int)) -> B, value: A) -> B { + function(((1, value), 2)) +} + +test fn compound_element_of_a_compound_parameter() -> Int { + // `p.first` keeps the reference to `A` inside it, wherever it is, and so does `p.first.first` + let pair: (5, Int) = nested_map(fn(p) => p.first, 5); + let rest: (Int, 5) = nested_rest_map(fn(p) => p.first, 5); + let element: 5 = nested_map(fn(p) => p.first.first, 5); + pair.first + rest.rest + element +} + +struct Wrapper { + value: T, + other: Int, +} + +struct Last { + other: Int, + value: T, +} + +fn struct_map(function: fn(wrapper: Wrapper) -> B, value: A) -> B { + function(Wrapper:: { value, other: 1 }) +} + +fn last_map(function: fn(last: Last) -> B, value: A) -> B { + function(Last:: { other: 1, value }) +} + +fn pair_struct_map(function: fn(wrapper: Wrapper<(A, Int)>) -> B, value: A) -> B { + function(Wrapper::<(A, Int)> { value: (value, 1), other: 2 }) +} + +test fn field_of_a_struct_parameter() -> Int { + // A struct's fields keep the reference to `A` too, wherever they are in it + let first: 5 = struct_map(fn(wrapper) => wrapper.value, 5); + let last: 5 = last_map(fn(last) => last.value, 5); + let pair: (5, Int) = pair_struct_map(fn(wrapper) => wrapper.value, 5); + first + last + pair.first +} + +fn wrapped_map(function: fn(p: (Wrapper, Int)) -> B, value: A) -> B { + function((Wrapper:: { value, other: 1 }, 2)) +} + +fn optional_map(function: fn(p: ((A, Int) | nil, Int)) -> B, value: A) -> B { + function(((value, 1), 2)) +} + +test fn reference_in_a_struct_or_union_element() -> Int { + // An element keeps the reference to `A` inside a struct or a union in it too + let wrapper: Wrapper<5> = wrapped_map(fn(p) => p.first, 5); + let optional: (5, Int) | nil = optional_map(fn(p) => p.first, 5); + + if optional is (5, Int) { + wrapper.value + optional.first + } else { + wrapper.value + } +} diff --git a/tests/types/generic_inference_lambda_parameter_vars.yaml b/tests/types/generic_inference_lambda_parameter_vars.yaml index b7f8a54b..79248ce1 100644 --- a/tests/types/generic_inference_lambda_parameter_vars.yaml +++ b/tests/types/generic_inference_lambda_parameter_vars.yaml @@ -153,6 +153,27 @@ tests: runtime_cost: 969 byte_cost: 708000 total_cost: 708969 +- name: compound_element_of_a_compound_parameter + program: (a (q 16 (f (a 2 (c (q . 4) (q . 5)))) (r (a (q . 4) (q ((q . 5) . 2)))) (a 2 (c (q . 8) (q . 5)))) (c (q 2 2 (c (c (c 3 (q . 1)) (q . 2)) ())) ())) + debug_program: (a (q 2 (q 16 (+ (f 4) (r 10)) 14) (c (c (a 4 (c (c (q . 2) (c (c (q . 1) (q 5 2)) (c (q . 1) ()))) (q . 5))) (c (a 6 (c (c (q . 2) (c (c (q . 1) (q 5 2)) (c (q . 1) ()))) (q . 5))) (a 4 (c (c (q . 2) (c (c (q . 1) (q 5 (f 2))) (c (q . 1) ()))) (q . 5))))) 1)) (c (c (q 2 2 (c (c (c 3 (q . 1)) (q . 2)) ())) (q 2 2 (c (c (c (q . 1) 3) (q . 2)) ()))) ())) + output: "15" + runtime_cost: 2966 + byte_cost: 1308000 + total_cost: 1310966 +- name: field_of_a_struct_parameter + program: (+ (a (q . 4) (q (f 1))) (a (q . 10) (q (q 5))) (f (a (q . 4) (q ((f . 1) 2))))) + debug_program: (a (q 2 (q 16 (+ 4 10) (f 14)) (c (c (a 14 (c (c (q . 2) (c (c (q . 1) (q 5 2)) (c (q . 1) ()))) (q . 5))) (c (a 10 (c (c (q . 2) (c (c (q . 1) (q 5 (r 2))) (c (q . 1) ()))) (q . 5))) (a 4 (c (c (q . 2) (c (c (q . 1) (q 5 2)) (c (q . 1) ()))) (q . 5))))) 1)) (c (c (q 2 2 (c (c (c 3 (q . 1)) (c (q . 2) ())) ())) (c (q 2 2 (c (c (q . 1) (c 3 ())) ())) (q 2 2 (c (c 3 (c (q . 1) ())) ())))) ())) + output: "15" + runtime_cost: 1663 + byte_cost: 756000 + total_cost: 757663 +- name: reference_in_a_struct_or_union_element + program: (a (q 2 (i 6 (q 16 8 10) (q . 8)) 1) (c (c (a (q . 4) (q ((f 1) . 2))) (a (q . 4) (q ((f . 1) . 2)))) 1)) + debug_program: (a (q 2 (q 2 (i (l 6) (q 16 (f 4) (f 6)) (q 5 4)) 1) (c (c (a 4 (c (c (q . 2) (c (c (q . 1) (q 5 2)) (c (q . 1) ()))) (q . 5))) (a 6 (c (c (q . 2) (c (c (q . 1) (q 5 2)) (c (q . 1) ()))) (q . 5)))) 1)) (c (c (q 2 2 (c (c (c 3 (c (q . 1) ())) (q . 2)) ())) (q 2 2 (c (c (c 3 (q . 1)) (q . 2)) ()))) ())) + output: "10" + runtime_cost: 1752 + byte_cost: 948000 + total_cost: 949752 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 226a4212..4b9973c0 100644 --- a/tests/types/generic_inference_lambda_parameter_vars_errors.rue +++ b/tests/types/generic_inference_lambda_parameter_vars_errors.rue @@ -73,7 +73,19 @@ fn generic_in_a_callback_parameter() -> Int { result } +struct Wrapper { + value: T, + other: Int, +} + +fn struct_map(function: fn(wrapper: Wrapper) -> B, value: A) -> B { + function(Wrapper:: { value, other: 1 }) +} + fn main() -> Int { + // `other` is `Int` whatever `A` is, so it doesn't reference `A`, and isn't narrowed to `1` + let other: 1 = struct_map(fn(wrapper) => wrapper.other, 5); + // The lambda returns `x + 0`, not `x`, so the expected bound doesn't reach `x` let computed: 5 = map(fn(x) => x + 0, 5); @@ -99,5 +111,5 @@ fn main() -> Int { let unbounded = map(fn(x) => x, 5); let five: 5 = unbounded; - computed + nested.first + unfitting + spread + generic + mismatched.first + cast + five + other + computed + nested.first + unfitting + spread + generic + mismatched.first + cast + five } diff --git a/tests/types/generic_inference_lambda_parameter_vars_errors.yaml b/tests/types/generic_inference_lambda_parameter_vars_errors.yaml index 676f5509..354b82a5 100644 --- a/tests/types/generic_inference_lambda_parameter_vars_errors.yaml +++ b/tests/types/generic_inference_lambda_parameter_vars_errors.yaml @@ -9,12 +9,13 @@ diagnostics: - Unused function `callback_map` at generic_inference_lambda_parameter_vars_errors.rue:58:4 - Binding `result` is returned directly after it's declared at generic_inference_lambda_parameter_vars_errors.rue:66:5 - Cannot compare `Int` to `7` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:66:21 -- Cannot compare `Int` to `5` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:78:23 -- Cannot compare `(Int, Int)` to `(5, Int)` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:82:28 -- Cannot compare `Int` to `6` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:85:24 -- Cannot compare `Int` to `5` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:88:21 -- Cannot compare `Int` to `5` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:89:22 -- Unused generic type `T` at generic_inference_lambda_parameter_vars_errors.rue:89:29 -- Cannot assign `(Int, 1)` to `(5, 2)`, since they are incompatible at generic_inference_lambda_parameter_vars_errors.rue:93:30 -- Cannot compare `Int` to `5` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:96:16 -- Cannot compare `Int` to `5` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:100:19 +- Cannot compare `Int` to `1` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:87:20 +- Cannot compare `Int` to `5` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:90:23 +- Cannot compare `(Int, Int)` to `(5, Int)` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:94:28 +- Cannot compare `Int` to `6` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:97:24 +- Cannot compare `Int` to `5` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:100:21 +- Cannot compare `Int` to `5` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:101:22 +- Unused generic type `T` at generic_inference_lambda_parameter_vars_errors.rue:101:29 +- Cannot assign `(Int, 1)` to `(5, 2)`, since they are incompatible at generic_inference_lambda_parameter_vars_errors.rue:105:30 +- Cannot compare `Int` to `5` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:108:16 +- Cannot compare `Int` to `5` without a type guard, since the runtime value must be constrained at generic_inference_lambda_parameter_vars_errors.rue:112:19