From 1407b1a0dc07f27f9431c77383d1c471bf1ce325 Mon Sep 17 00:00:00 2001 From: "Sean T. Allen" Date: Fri, 4 Dec 2026 01:35:10 -0500 Subject: [PATCH] Only accept a tuple as Any when each of its elements is is_tuple_sub_nominal accepted any tuple as a subtype of a top type and checked only the tuple's capability. A tuple could hold a struct, a bare lambda, a \nosupertype\ type or an unconstrained type parameter and still go into Any, though none of those can on its own. Matching such a tuple out of Any segfaulted, sending one with a struct element to an Any val parameter hung in the GC, and a tuple pattern with a \nosupertype\ element tripped an assertion in is_nominal_match_tuple. Each element must now be a subtype of the top type on its own, ignoring capability. is_nominal_match_tuple treats such an element as "can never match" and lets it win over a capability or descriptor denial from another element. could_be_subtype_ignore_cap gets a tuple case so that type-argument patterns like Box[(A, U32)] against Box[X: Any val] still unify. Closes #7023 Closes #7024 Closes #6770 --- .release-notes/tuple-elements-in-any.md | 40 +++++ src/libponyc/type/matchtype.c | 95 +++++++++--- src/libponyc/type/subtype.c | 22 +++ .../expected-stdout.txt | 2 + .../main.pony | 11 ++ .../expected-stdout.txt | 2 + .../main.pony | 16 ++ .../expected-stdout.txt | 1 + .../match-tuple-pattern-from-any/main.pony | 11 ++ .../expected-compile-errors.txt | 6 + .../main.pony | 13 ++ .../expected-stdout.txt | 3 + .../main.pony | 24 +++ .../expected-stdout.txt | 2 + .../main.pony | 28 ++++ .../expected-stdout.txt | 1 + .../main.pony | 13 ++ .../expected-compile-errors.txt | 3 + .../tuple-with-bare-element-not-any/main.pony | 6 + .../expected-compile-errors.txt | 3 + .../main.pony | 10 ++ test/libponyc/matchtype.cc | 60 ++++++++ test/libponyc/type_check_subtype.cc | 139 ++++++++++++++++++ 23 files changed, 490 insertions(+), 21 deletions(-) create mode 100644 .release-notes/tuple-elements-in-any.md create mode 100644 test/full-program-tests/iftype-tuple-with-nosupertype-element/expected-stdout.txt create mode 100644 test/full-program-tests/iftype-tuple-with-nosupertype-element/main.pony create mode 100644 test/full-program-tests/match-generic-tuple-pattern-nosupertype-type-argument/expected-stdout.txt create mode 100644 test/full-program-tests/match-generic-tuple-pattern-nosupertype-type-argument/main.pony create mode 100644 test/full-program-tests/match-tuple-pattern-from-any/expected-stdout.txt create mode 100644 test/full-program-tests/match-tuple-pattern-from-any/main.pony create mode 100644 test/full-program-tests/match-tuple-pattern-nosupertype-element-from-any/expected-compile-errors.txt create mode 100644 test/full-program-tests/match-tuple-pattern-nosupertype-element-from-any/main.pony create mode 100644 test/full-program-tests/match-tuple-pattern-nosupertype-element-from-union/expected-stdout.txt create mode 100644 test/full-program-tests/match-tuple-pattern-nosupertype-element-from-union/main.pony create mode 100644 test/full-program-tests/match-tuple-type-argument-with-type-parameter/expected-stdout.txt create mode 100644 test/full-program-tests/match-tuple-type-argument-with-type-parameter/main.pony create mode 100644 test/full-program-tests/match-tuple-with-nosupertype-element-against-any-pattern/expected-stdout.txt create mode 100644 test/full-program-tests/match-tuple-with-nosupertype-element-against-any-pattern/main.pony create mode 100644 test/full-program-tests/tuple-with-bare-element-not-any/expected-compile-errors.txt create mode 100644 test/full-program-tests/tuple-with-bare-element-not-any/main.pony create mode 100644 test/full-program-tests/tuple-with-struct-element-not-any/expected-compile-errors.txt create mode 100644 test/full-program-tests/tuple-with-struct-element-not-any/main.pony diff --git a/.release-notes/tuple-elements-in-any.md b/.release-notes/tuple-elements-in-any.md new file mode 100644 index 00000000000..9b4ba4c84c8 --- /dev/null +++ b/.release-notes/tuple-elements-in-any.md @@ -0,0 +1,40 @@ +## Only accept a tuple as `Any` when each of its elements is + +A tuple is now a subtype of `Any`, or of another interface with no methods, only when each of its elements is a subtype of that interface on its own. Before, the compiler checked only the tuple's capability and accepted any elements. A struct, a bare lambda, a `\nosupertype\` type and an unconstrained type parameter are not subtypes of `Any` on their own, but a tuple that held one was accepted as a subtype of `Any`. + +That caused crashes. Matching a tuple with a bare lambda element out of `Any val` segfaulted. Sending a tuple with a struct element to an `Any val` parameter hung, or segfaulted in the garbage collector. A tuple pattern with a `\nosupertype\` element, matched against `Any`, made `ponyc` abort with an assertion failure. That pattern now gets "this pattern can never match". + +Some programs that still compile now behave differently at run time. An `iftype` that tests a type parameter against `Any` now takes its `else` branch when the type argument is such a tuple. A match whose operand is a union that holds such a tuple no longer takes an `Any` case for that tuple. It takes another case that matches, or its `else` branch. + +A match whose value is used, and that relied on an `Any` case to cover such a tuple, is no longer exhaustive, so it now fails to compile. So does a tuple type argument with such an element, when the type parameter is constrained by `Any`, as in `Promise[A: Any #share]`. So does code that puts such a tuple into `Any`: + +```pony +struct S + +actor Sink + be take(a: Any val) => None + +actor Main + new create(env: Env) => + // Before: compiled, and could hang or crash at run time. + // Now: "argument not assignable to parameter", because the element + // S val^ is not a subtype of Any val^. + Sink.take((recover val S end, U64(3))) +``` + +```pony +// After: the parameter names the tuple's element types. +actor Sink + be take(a: (S val, U64)) => None +``` + +For a struct, bare lambda or `\nosupertype\` element, pass the tuple through a type that names its elements, such as `(S val, U64)`, instead of `Any`. You can also wrap a struct in a class and put the class in the tuple. `Pointer[A]` and `NullablePointer[A]` are structs, so a tuple such as `(Pointer[U8], USize)` from FFI code needs the same change. A type parameter element needs a constraint that makes it a subtype of `Any`: + +```pony +// Before: compiled. Now: "function body isn't the result type", because +// the element A is not a subtype of Any tag. +fun f[A](a: A): Any tag => (a, U32(1)) + +// After: +fun f[A: Any #any](a: A): Any tag => (a, U32(1)) +``` diff --git a/src/libponyc/type/matchtype.c b/src/libponyc/type/matchtype.c index 66a807d4ecf..5b536e0e328 100644 --- a/src/libponyc/type/matchtype.c +++ b/src/libponyc/type/matchtype.c @@ -532,38 +532,60 @@ static matchtype_t is_nominal_match_tuple(ast_t* operand, ast_t* pattern, return MATCHTYPE_REJECT; } - ast_t* child = ast_child(pattern); + // An element that can never match out of the top type makes the whole + // pattern impossible, whatever the other elements return, so it wins over a + // capability or descriptor problem in any other element. + matchtype_t deny = MATCHTYPE_ACCEPT; + ast_t* deny_child = NULL; - while(child != NULL) + for(ast_t* child = ast_child(pattern); child != NULL; + child = ast_sibling(child)) { - matchtype_t r = is_x_match_x(operand, child, errorf, false, opt); - pony_assert(r != MATCHTYPE_REJECT); + matchtype_t r = is_x_match_x(operand, child, NULL, false, opt); - if(r == MATCHTYPE_DENY_CAP) + if(r == MATCHTYPE_REJECT) { - if(errorf != NULL) + if((errorf != NULL) && report_reject) { ast_error_frame(errorf, pattern, - "matching %s with %s could violate capabilities", - ast_print_type(operand, opt->strtab), ast_print_type(pattern, opt->strtab)); + "%s cannot match %s: the element %s is not a subtype of %s", + ast_print_type(operand, opt->strtab), + ast_print_type(pattern, opt->strtab), + ast_print_type(child, opt->strtab), + ast_print_type(operand, opt->strtab)); + is_x_match_x(operand, child, errorf, true, opt); } return r; - } else if (r == MATCHTYPE_DENY_NODESC) { - if(errorf != NULL) - { - ast_error_frame(errorf, pattern, - "matching %s with %s is not possible, since a struct lacks a type descriptor", - ast_print_type(operand, opt->strtab), ast_print_type(pattern, opt->strtab)); - } + } - return r; + if((r != MATCHTYPE_ACCEPT) && (deny == MATCHTYPE_ACCEPT)) + { + deny = r; + deny_child = child; } + } - child = ast_sibling(child); + if(deny == MATCHTYPE_ACCEPT) + return MATCHTYPE_ACCEPT; + + if(errorf != NULL) + { + is_x_match_x(operand, deny_child, errorf, false, opt); + + if(deny == MATCHTYPE_DENY_CAP) + { + ast_error_frame(errorf, pattern, + "matching %s with %s could violate capabilities", + ast_print_type(operand, opt->strtab), ast_print_type(pattern, opt->strtab)); + } else { + ast_error_frame(errorf, pattern, + "matching %s with %s is not possible, since a struct lacks a type descriptor", + ast_print_type(operand, opt->strtab), ast_print_type(pattern, opt->strtab)); + } } - return MATCHTYPE_ACCEPT; + return deny; } static matchtype_t is_typeparam_match_typeparam(ast_t* operand, ast_t* pattern, @@ -901,11 +923,13 @@ static bool constraints_could_overlap(ast_t* a, ast_t* b, pass_opt_t* opt) // - A union could only if each of its members could. // - An intersection or arrow that contains a type parameter anywhere // could. The function returns true without checking its other members. +// - A tuple could be a subtype of a union if it could be a subtype of one of +// its members, and of a top type only if each of its elements could. // - A type alias is unfolded first. // -// Nominal and tuple types get a plain subtype check, even when their type -// arguments contain type parameters. That check can return false for a type -// that some reification would make a subtype. +// Nominal types, and tuples against any other super, get a plain subtype +// check, even when their type arguments contain type parameters. That check +// can return false for a type that some reification would make a subtype. static bool could_be_subtype_ignore_cap(ast_t* sub, ast_t* super, pass_opt_t* opt) { @@ -941,6 +965,35 @@ static bool could_be_subtype_ignore_cap(ast_t* sub, ast_t* super, return true; break; + case TK_TUPLETYPE: + { + if(ast_id(super) == TK_UNIONTYPE) + { + for(ast_t* member = ast_child(super); + member != NULL; + member = ast_sibling(member)) + { + if(could_be_subtype_ignore_cap(sub, member, opt)) + return true; + } + + break; + } + + if(!is_top_type(super, true, opt)) + break; + + for(ast_t* child = ast_child(sub); + child != NULL; + child = ast_sibling(child)) + { + if(!could_be_subtype_ignore_cap(child, super, opt)) + return false; + } + + return true; + } + case TK_TYPEALIASREF: { ast_t* unfolded = typealias_unfold(sub); diff --git a/src/libponyc/type/subtype.c b/src/libponyc/type/subtype.c index 4a602d95d19..fd7ddd2f702 100644 --- a/src/libponyc/type/subtype.c +++ b/src/libponyc/type/subtype.c @@ -1271,6 +1271,28 @@ static bool is_tuple_sub_nominal(ast_t* sub, ast_t* super, if(is_top_type(super, true, opt)) { + // The tuple's capability is checked below, so ignore each element's + // capability. + for(ast_t* child = ast_child(sub); child != NULL; + child = ast_sibling(child)) + { + if(!is_x_sub_x(child, super, CHECK_CAP_IGNORE, NULL, opt)) + { + if(errorf != NULL) + { + ast_error_frame(errorf, sub, + "%s is not a subtype of %s: the element %s is not a subtype of %s", + ast_print_type(sub, opt->strtab), + ast_print_type(super, opt->strtab), + ast_print_type(child, opt->strtab), + ast_print_type(super, opt->strtab)); + is_x_sub_x(child, super, CHECK_CAP_IGNORE, errorf, opt); + } + + return false; + } + } + if(check_cap != CHECK_CAP_IGNORE) { token_id sub_cap; diff --git a/test/full-program-tests/iftype-tuple-with-nosupertype-element/expected-stdout.txt b/test/full-program-tests/iftype-tuple-with-nosupertype-element/expected-stdout.txt new file mode 100644 index 00000000000..f3736e6eb9d --- /dev/null +++ b/test/full-program-tests/iftype-tuple-with-nosupertype-element/expected-stdout.txt @@ -0,0 +1,2 @@ +(N, N): not a subtype of Any +(U32, U32): subtype of Any diff --git a/test/full-program-tests/iftype-tuple-with-nosupertype-element/main.pony b/test/full-program-tests/iftype-tuple-with-nosupertype-element/main.pony new file mode 100644 index 00000000000..d1a1c8edf80 --- /dev/null +++ b/test/full-program-tests/iftype-tuple-with-nosupertype-element/main.pony @@ -0,0 +1,11 @@ +// A tuple is a subtype of `Any` only when each of its elements is. +class \nosupertype\ N + +primitive Check[A] + fun apply(): String => + iftype A <: Any #any then "subtype of Any" else "not a subtype of Any" end + +actor Main + new create(env: Env) => + env.out.print("(N, N): " + Check[(N, N)]()) + env.out.print("(U32, U32): " + Check[(U32, U32)]()) diff --git a/test/full-program-tests/match-generic-tuple-pattern-nosupertype-type-argument/expected-stdout.txt b/test/full-program-tests/match-generic-tuple-pattern-nosupertype-type-argument/expected-stdout.txt new file mode 100644 index 00000000000..1d474d52557 --- /dev/null +++ b/test/full-program-tests/match-generic-tuple-pattern-nosupertype-type-argument/expected-stdout.txt @@ -0,0 +1,2 @@ +false +true diff --git a/test/full-program-tests/match-generic-tuple-pattern-nosupertype-type-argument/main.pony b/test/full-program-tests/match-generic-tuple-pattern-nosupertype-type-argument/main.pony new file mode 100644 index 00000000000..b1428ac4b97 --- /dev/null +++ b/test/full-program-tests/match-generic-tuple-pattern-nosupertype-type-argument/main.pony @@ -0,0 +1,16 @@ +// A type argument can make a tuple pattern something that can never come out +// of `Any`. The program compiles, and that case never matches. +class \nosupertype\ N + +primitive G + fun m[A](a: Any val): Bool => + match a + | (let x: A, let y: U32) => true + else + false + end + +actor Main + new create(env: Env) => + env.out.print(G.m[N val]((U32(1), U32(2))).string()) + env.out.print(G.m[U32]((U32(1), U32(2))).string()) diff --git a/test/full-program-tests/match-tuple-pattern-from-any/expected-stdout.txt b/test/full-program-tests/match-tuple-pattern-from-any/expected-stdout.txt new file mode 100644 index 00000000000..0c806bc32be --- /dev/null +++ b/test/full-program-tests/match-tuple-pattern-from-any/expected-stdout.txt @@ -0,0 +1 @@ +1 x 2 diff --git a/test/full-program-tests/match-tuple-pattern-from-any/main.pony b/test/full-program-tests/match-tuple-pattern-from-any/main.pony new file mode 100644 index 00000000000..cdbcfc52b12 --- /dev/null +++ b/test/full-program-tests/match-tuple-pattern-from-any/main.pony @@ -0,0 +1,11 @@ +// A tuple whose elements are all subtypes of `Any` still goes into `Any` and +// matches back out. +actor Main + new create(env: Env) => + let a: Any val = (U32(1), "x", (None, U8(2))) + match a + | (let x: U32, let s: String, (None, let y: U8)) => + env.out.print(x.string() + " " + s + " " + y.string()) + else + env.out.print("no match") + end diff --git a/test/full-program-tests/match-tuple-pattern-nosupertype-element-from-any/expected-compile-errors.txt b/test/full-program-tests/match-tuple-pattern-nosupertype-element-from-any/expected-compile-errors.txt new file mode 100644 index 00000000000..e13d2922c8b --- /dev/null +++ b/test/full-program-tests/match-tuple-pattern-nosupertype-element-from-any/expected-compile-errors.txt @@ -0,0 +1,6 @@ +main.pony:9:20: this pattern can never match + main.pony:9:20: Any val cannot match (N val^, U32 val^): the element N val^ is not a subtype of Any val + main.pony:9:15: Any val cannot match N val^: N isn't a subtype of Any +main.pony:12:18: this pattern can never match + main.pony:12:18: Any val cannot match (U32 val^, (N val^, U32 val^)): the element (N val^, U32 val^) is not a subtype of Any val + main.pony:12:33: Any val cannot match (N val^, U32 val^): the element N val^ is not a subtype of Any val diff --git a/test/full-program-tests/match-tuple-pattern-nosupertype-element-from-any/main.pony b/test/full-program-tests/match-tuple-pattern-nosupertype-element-from-any/main.pony new file mode 100644 index 00000000000..13c9f77e1f0 --- /dev/null +++ b/test/full-program-tests/match-tuple-pattern-nosupertype-element-from-any/main.pony @@ -0,0 +1,13 @@ +// A \nosupertype\ type can't come out of `Any`, so neither can a tuple that +// holds one. +class \nosupertype\ N + +actor Main + new create(env: Env) => + let a: Any val = U32(1) + match a + | (let n: N val, let m: U32) => None + end + match a + | (let x: U32, (let n: N val, let m: U32)) => None + end diff --git a/test/full-program-tests/match-tuple-pattern-nosupertype-element-from-union/expected-stdout.txt b/test/full-program-tests/match-tuple-pattern-nosupertype-element-from-union/expected-stdout.txt new file mode 100644 index 00000000000..dd8b4fb5115 --- /dev/null +++ b/test/full-program-tests/match-tuple-pattern-nosupertype-element-from-union/expected-stdout.txt @@ -0,0 +1,3 @@ +tuple: 1 +ref first: matched +ref last: matched diff --git a/test/full-program-tests/match-tuple-pattern-nosupertype-element-from-union/main.pony b/test/full-program-tests/match-tuple-pattern-nosupertype-element-from-union/main.pony new file mode 100644 index 00000000000..542e0700094 --- /dev/null +++ b/test/full-program-tests/match-tuple-pattern-nosupertype-element-from-union/main.pony @@ -0,0 +1,24 @@ +// A tuple with a \nosupertype\ element matches out of a union that names that +// tuple type. The other union member can't hold the tuple, whatever the order +// of the tuple's elements. +class \nosupertype\ N + new create() => None + +actor Main + new create(env: Env) => + let u: (Any val | (N val, U32)) = (recover val N end, U32(1)) + match u + | (let n: N val, let v: U32) => env.out.print("tuple: " + v.string()) + else + env.out.print("tuple: no match") + end + + let s: (Any tag | (String ref, N ref)) = (String, N) + match s + | (let a: String ref, let n: N ref) => env.out.print("ref first: matched") + end + + let t: (Any tag | (N ref, String ref)) = (N, String) + match t + | (let n: N ref, let a: String ref) => env.out.print("ref last: matched") + end diff --git a/test/full-program-tests/match-tuple-type-argument-with-type-parameter/expected-stdout.txt b/test/full-program-tests/match-tuple-type-argument-with-type-parameter/expected-stdout.txt new file mode 100644 index 00000000000..bb101b641b9 --- /dev/null +++ b/test/full-program-tests/match-tuple-type-argument-with-type-parameter/expected-stdout.txt @@ -0,0 +1,2 @@ +true +true diff --git a/test/full-program-tests/match-tuple-type-argument-with-type-parameter/main.pony b/test/full-program-tests/match-tuple-type-argument-with-type-parameter/main.pony new file mode 100644 index 00000000000..7767daf0cb9 --- /dev/null +++ b/test/full-program-tests/match-tuple-type-argument-with-type-parameter/main.pony @@ -0,0 +1,28 @@ +// A tuple type argument with an unconstrained type parameter element could +// equal a type parameter constrained by `Any val`, or by a union that holds +// `Any val`, so the pattern can match. +class \nosupertype\ N + +class Box[T] + new val create() => None + +primitive G + fun m[X: Any val, A](b: Box[X] val): Bool => + match b + | let bb: Box[(A, U32)] val => true + else + false + end + + fun u[X: (Any val | N val), A](b: Box[X] val): Bool => + match b + | let bb: Box[(A, U32)] val => true + else + false + end + +actor Main + new create(env: Env) => + let b: Box[(U32, U32)] val = Box[(U32, U32)] + env.out.print(G.m[(U32, U32), U32](b).string()) + env.out.print(G.u[(U32, U32), U32](b).string()) diff --git a/test/full-program-tests/match-tuple-with-nosupertype-element-against-any-pattern/expected-stdout.txt b/test/full-program-tests/match-tuple-with-nosupertype-element-against-any-pattern/expected-stdout.txt new file mode 100644 index 00000000000..db3498f085d --- /dev/null +++ b/test/full-program-tests/match-tuple-with-nosupertype-element-against-any-pattern/expected-stdout.txt @@ -0,0 +1 @@ +any: no match diff --git a/test/full-program-tests/match-tuple-with-nosupertype-element-against-any-pattern/main.pony b/test/full-program-tests/match-tuple-with-nosupertype-element-against-any-pattern/main.pony new file mode 100644 index 00000000000..e8bc5bda128 --- /dev/null +++ b/test/full-program-tests/match-tuple-with-nosupertype-element-against-any-pattern/main.pony @@ -0,0 +1,13 @@ +// A tuple with a \nosupertype\ element isn't a subtype of `Any`, so it doesn't +// match an `Any` case. +class \nosupertype\ N + new val create() => None + +actor Main + new create(env: Env) => + let w: ((N val, U32) | U32) = (N, U32(2)) + match w + | let a: Any val => env.out.print("any: matched") + else + env.out.print("any: no match") + end diff --git a/test/full-program-tests/tuple-with-bare-element-not-any/expected-compile-errors.txt b/test/full-program-tests/tuple-with-bare-element-not-any/expected-compile-errors.txt new file mode 100644 index 00000000000..a8e6ea8fc72 --- /dev/null +++ b/test/full-program-tests/tuple-with-bare-element-not-any/expected-compile-errors.txt @@ -0,0 +1,3 @@ +main.pony:6:20: right side must be a subtype of left side + main.pony:6:24: (@{(U32): U32} val, U32 val^) is not a subtype of Any val^: the element @{(U32): U32} val is not a subtype of Any val^ + main.pony:5:13: @{(U32): U32} val is not a subtype of Any val^: their bareness differ diff --git a/test/full-program-tests/tuple-with-bare-element-not-any/main.pony b/test/full-program-tests/tuple-with-bare-element-not-any/main.pony new file mode 100644 index 00000000000..c84a72017e4 --- /dev/null +++ b/test/full-program-tests/tuple-with-bare-element-not-any/main.pony @@ -0,0 +1,6 @@ +// A bare lambda isn't a subtype of `Any`, so neither is a tuple that holds +// one. +actor Main + new create(env: Env) => + let f = @{(x: U32): U32 => x} + let a: Any val = (f, U32(1)) diff --git a/test/full-program-tests/tuple-with-struct-element-not-any/expected-compile-errors.txt b/test/full-program-tests/tuple-with-struct-element-not-any/expected-compile-errors.txt new file mode 100644 index 00000000000..526c9510e33 --- /dev/null +++ b/test/full-program-tests/tuple-with-struct-element-not-any/expected-compile-errors.txt @@ -0,0 +1,3 @@ +main.pony:10:15: argument not assignable to parameter + main.pony:10:33: (S val^, U64 val^) is not a subtype of Any val^: the element S val^ is not a subtype of Any val^ + main.pony:2:1: S val^ is not a subtype of Any val^: a struct can't be a subtype of an interface diff --git a/test/full-program-tests/tuple-with-struct-element-not-any/main.pony b/test/full-program-tests/tuple-with-struct-element-not-any/main.pony new file mode 100644 index 00000000000..ac5f4cce1e5 --- /dev/null +++ b/test/full-program-tests/tuple-with-struct-element-not-any/main.pony @@ -0,0 +1,10 @@ +// A struct isn't a subtype of `Any`, so neither is a tuple that holds one. +struct S + var d: U64 = 0 + +actor Sink + be take(a: Any val) => None + +actor Main + new create(env: Env) => + Sink.take((recover val S end, U64(3))) diff --git a/test/libponyc/matchtype.cc b/test/libponyc/matchtype.cc index 392a5464c4d..28ab61d5188 100644 --- a/test/libponyc/matchtype.cc +++ b/test/libponyc/matchtype.cc @@ -3061,3 +3061,63 @@ TEST_F(MatchTypeTest, TypeParamInTypeArgArrowConstraintReject) is_matchtype(type_of("cell_this_a"), type_of("cell_this_wrap_b"), NULL, &opt)); } + + +TEST_F(MatchTypeTest, TupleWithNosupertypeElementRejectedFromTopType) +{ + // A tuple can come out of a top type only if each element can. A + // \nosupertype\ type can't be a subtype of Any, so the tuple pattern can + // never match. + const char* src = + "class \\nosupertype\\ N\n" + "struct S\n" + + "interface Test\n" + " fun z(any: Any val, any_tag: Any tag, n_u32: (N val, U32 val),\n" + " s_u32: (String val, U32 val), st_n: (S val, N val),\n" + " rf_n: (String ref, N ref))"; + + TEST_COMPILE(src); + + ASSERT_EQ(MATCHTYPE_REJECT, + is_matchtype(type_of("any"), type_of("n_u32"), NULL, &opt)); + ASSERT_EQ(MATCHTYPE_ACCEPT, + is_matchtype(type_of("any"), type_of("s_u32"), NULL, &opt)); + + // The element that can never match decides the result, even when an earlier + // element would give a descriptor or capability denial. + ASSERT_EQ(MATCHTYPE_REJECT, + is_matchtype(type_of("any"), type_of("st_n"), NULL, &opt)); + ASSERT_EQ(MATCHTYPE_REJECT, + is_matchtype(type_of("any_tag"), type_of("rf_n"), NULL, &opt)); +} + + +TEST_F(MatchTypeTest, TypeParamInTypeArgTupleTopType) +{ + // A tuple type argument could equal a type parameter constrained by a top + // type only if each of its elements could be a subtype of that top type. + const char* src = + "class \\nosupertype\\ N\n" + "class val Box[T]\n" + + "interface Test\n" + " fun z[X: Any val, Y: (Any val | None), A]\n" + " (box_x: Box[X], box_y: Box[Y], box_a_u32: Box[(A, U32)],\n" + " box_n_a: Box[(N val, A)], box_u32_a_u32: Box[(U32, (A, U32))])"; + + TEST_COMPILE(src); + + ASSERT_EQ(MATCHTYPE_ACCEPT, + is_matchtype(type_of("box_x"), type_of("box_a_u32"), NULL, &opt)); + ASSERT_EQ(MATCHTYPE_REJECT, + is_matchtype(type_of("box_x"), type_of("box_n_a"), NULL, &opt)); + ASSERT_EQ(MATCHTYPE_ACCEPT, + is_matchtype(type_of("box_x"), type_of("box_u32_a_u32"), NULL, &opt)); + + // A union constraint that holds the top type. + ASSERT_EQ(MATCHTYPE_ACCEPT, + is_matchtype(type_of("box_y"), type_of("box_a_u32"), NULL, &opt)); + ASSERT_EQ(MATCHTYPE_REJECT, + is_matchtype(type_of("box_y"), type_of("box_n_a"), NULL, &opt)); +} diff --git a/test/libponyc/type_check_subtype.cc b/test/libponyc/type_check_subtype.cc index 147be752311..b784ffd6014 100644 --- a/test/libponyc/type_check_subtype.cc +++ b/test/libponyc/type_check_subtype.cc @@ -1771,6 +1771,145 @@ TEST_F(SubTypeTest, TupleValRefNotSubAnyShare) } +TEST_F(SubTypeTest, TupleWithNosupertypeElementNotSubAny) +{ + // A tuple is a subtype of a top type only when each element is a subtype + // of that type on its own. + const char* src = + "class \\nosupertype\\ N\n" + + "primitive P\n" + " fun apply(x: (N tag, U32 val)) =>\n" + " let x': Any tag = consume x"; + + TEST_ERRORS_1(src, "right side must be a subtype of left side"); +} + + +TEST_F(SubTypeTest, TupleWithBareElementNotSubAny) +{ + const char* src = + "primitive P\n" + " fun apply(x: (@{(U32): U32} val, U32 val)) =>\n" + " let x': Any val = consume x"; + + TEST_ERRORS_1(src, "right side must be a subtype of left side"); +} + + +TEST_F(SubTypeTest, TupleWithStructElementNotSubAny) +{ + const char* src = + "struct S\n" + + "primitive P\n" + " fun apply(x: (S val, U32 val)) =>\n" + " let x': Any val = consume x"; + + TEST_ERRORS_1(src, "right side must be a subtype of left side"); +} + + +TEST_F(SubTypeTest, TupleWithStructElementNotSubUnionWithAny) +{ + const char* src = + "struct S\n" + + "primitive P\n" + " fun apply(x: (S val, U32 val)) =>\n" + " let x': (Any val | None) = consume x"; + + TEST_ERRORS_1(src, "right side must be a subtype of left side"); +} + + +TEST_F(SubTypeTest, TupleWithNosupertypeElementNotSubEmptyInterface) +{ + const char* src = + "class \\nosupertype\\ N\n" + "interface Empty\n" + + "primitive P\n" + " fun apply(x: (N tag, U32 val)) =>\n" + " let x': Empty tag = consume x"; + + TEST_ERRORS_1(src, "right side must be a subtype of left side"); +} + + +TEST_F(SubTypeTest, TupleWithNestedNosupertypeElementNotSubAny) +{ + const char* src = + "class \\nosupertype\\ N\n" + + "primitive P\n" + " fun apply(x: (U32 val, (N tag, U32 val))) =>\n" + " let x': Any tag = consume x"; + + TEST_ERRORS_1(src, "right side must be a subtype of left side"); +} + + +TEST_F(SubTypeTest, TupleWithUnionElementContainingNosupertypeNotSubAny) +{ + const char* src = + "class \\nosupertype\\ N\n" + + "primitive P\n" + " fun apply(x: ((N tag | U32 val), U32 val)) =>\n" + " let x': Any tag = consume x"; + + TEST_ERRORS_1(src, "right side must be a subtype of left side"); +} + + +TEST_F(SubTypeTest, TupleWithNosupertypeElementOutsideAnyConstraint) +{ + const char* src = + "class \\nosupertype\\ N\n" + "class C[A: Any #any]\n" + + "primitive P\n" + " fun apply() =>\n" + " C[(N, U32)]"; + + TEST_ERRORS_1(src, "type argument is outside its constraint"); +} + + +TEST_F(SubTypeTest, TupleWithUnconstrainedTypeParamElementNotSubAny) +{ + const char* src = + "primitive P\n" + " fun apply[A](x: (A, U32 val)) =>\n" + " let x': Any tag = consume x"; + + TEST_ERRORS_1(src, "right side must be a subtype of left side"); +} + + +TEST_F(SubTypeTest, TupleWithAnyAnyConstrainedTypeParamElementSubAnyTag) +{ + const char* src = + "primitive P\n" + " fun apply[A: Any #any](x: (A, U32 val)) =>\n" + " let x': Any tag = consume x"; + + TEST_COMPILE(src); +} + + +TEST_F(SubTypeTest, TupleWithAnyConstrainedTypeParamElementSubAny) +{ + const char* src = + "primitive P\n" + " fun apply[A: Any val](x: (A, U32 val)) =>\n" + " let x': Any val = consume x"; + + TEST_COMPILE(src); +} + + TEST_F(SubTypeTest, TupleValTagDerivedCapIsVal) { // A tuple of (val, tag) elements has derived cap val.