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: 40 additions & 0 deletions .release-notes/tuple-elements-in-any.md
Original file line number Diff line number Diff line change
@@ -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))
```
95 changes: 74 additions & 21 deletions src/libponyc/type/matchtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions src/libponyc/type/subtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(N, N): not a subtype of Any
(U32, U32): subtype of Any
Original file line number Diff line number Diff line change
@@ -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)]())
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
false
true
Original file line number Diff line number Diff line change
@@ -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())
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 x 2
11 changes: 11 additions & 0 deletions test/full-program-tests/match-tuple-pattern-from-any/main.pony
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tuple: 1
ref first: matched
ref last: matched
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
true
true
Original file line number Diff line number Diff line change
@@ -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())
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
any: no match
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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))
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)))
Loading
Loading