From 5224267441d047575659ee47b5c666228678b7d6 Mon Sep 17 00:00:00 2001 From: Joe Eli McIlvain Date: Mon, 30 Apr 2018 05:46:07 +0300 Subject: [PATCH] Type param constraints now respect default cap of the type. Prior to this change, any type named in a type constraint would get an implicit cap of `#any` if no explicit cap was named. The exception to this rule is primitives, which will use an implicit cap of `val` in a type constraint (though I'm not sure why you'd ever use a primitive as a type constraint to begin with). In both cases, the default cap declared by the user in the type declaration was being ignored, and this is the only context where that happens, violating the principle of least surprise. I also don't think this deviation is documented anywhere. As a veteran pony programmer, this still surprises and annoys me regularly when I run into it; I commonly make this mistake and have to recompile with an explicit cap that matches my declared default cap. Furthermore, using #any as a type parameter constraint is rarely what you actually want (not constraining the cap at all turns out to not let you do very much), so it doesn't make much sense to be the implicit cap. To illustrate this, there isn't a single example of a type constraint having a final cap of `#any` in the standard library. The lines that have been affected by this change are all cases of using type intersections where only one of the terms was being used to constrain the cap. After this change the default cap declared by the user will be universally respected. This is a breaking change for anyone who is relying on this behaviour. --- packages/collections/_test.pony | 2 +- packages/collections/persistent/map.pony | 2 +- packages/collections/persistent/set.pony | 2 +- packages/ponytest/test_helper.pony | 4 ++-- src/libponyc/pass/names.c | 14 ++------------ 5 files changed, 7 insertions(+), 17 deletions(-) diff --git a/packages/collections/_test.pony b/packages/collections/_test.pony index 6150605981..d1bc95a358 100644 --- a/packages/collections/_test.pony +++ b/packages/collections/_test.pony @@ -553,7 +553,7 @@ class iso _TestSort is UnitTest [""; "%*&^*&^&"; "***"; "Hello"; "bar"; "f00"; "foo"; "foo"], [""; "Hello"; "foo"; "bar"; "foo"; "f00"; "%*&^*&^&"; "***"]) - fun test_sort[A: (Comparable[A] val & Stringable)]( + fun test_sort[A: (Comparable[A] val & Stringable val)]( h: TestHelper, sorted: Array[A], unsorted: Array[A]) diff --git a/packages/collections/persistent/map.pony b/packages/collections/persistent/map.pony index 3086e17a65..d3a4ac3edc 100644 --- a/packages/collections/persistent/map.pony +++ b/packages/collections/persistent/map.pony @@ -1,6 +1,6 @@ use mut = "collections" -type Map[K: (mut.Hashable val & Equatable[K]), V: Any #share] is +type Map[K: (mut.Hashable val & Equatable[K] val), V: Any #share] is HashMap[K, V, mut.HashEq[K]] """ A map that uses structural equality on the key. diff --git a/packages/collections/persistent/set.pony b/packages/collections/persistent/set.pony index 8b7542cbc1..9a6f227f31 100644 --- a/packages/collections/persistent/set.pony +++ b/packages/collections/persistent/set.pony @@ -1,6 +1,6 @@ use mut = "collections" -type Set[A: (mut.Hashable val & Equatable[A])] is HashSet[A, mut.HashEq[A]] +type Set[A: (mut.Hashable val & Equatable[A] val)] is HashSet[A, mut.HashEq[A]] type SetIs[A: Any #share] is HashSet[A, mut.HashIs[A]] diff --git a/packages/ponytest/test_helper.pony b/packages/ponytest/test_helper.pony index 70a6b67fbd..c008931b5b 100644 --- a/packages/ponytest/test_helper.pony +++ b/packages/ponytest/test_helper.pony @@ -159,7 +159,7 @@ class val TestHelper """ _check_eq[A]("eq", expect, actual, msg, loc) - fun _check_eq[A: (Equatable[A] #read & Stringable)] + fun _check_eq[A: (Equatable[A] #read & Stringable #read)] (check: String, expect: A, actual: A, msg: String, loc: SourceLoc) : Bool => @@ -221,7 +221,7 @@ class val TestHelper """ _check_ne[A]("ne", not_expect, actual, msg, loc) - fun _check_ne[A: (Equatable[A] #read & Stringable)] + fun _check_ne[A: (Equatable[A] #read & Stringable #read)] (check: String, not_expect: A, actual: A, msg: String, loc: SourceLoc) : Bool => diff --git a/src/libponyc/pass/names.c b/src/libponyc/pass/names.c index a91be727fa..35376bc320 100644 --- a/src/libponyc/pass/names.c +++ b/src/libponyc/pass/names.c @@ -215,18 +215,8 @@ static bool names_type(pass_opt_t* opt, ast_t** astp, ast_t* def) if(tcap == TK_NONE) { - if((opt->check.frame->constraint != NULL) || - (opt->check.frame->iftype_constraint != NULL)) - { - // A primitive constraint is a val, otherwise #any. - if(ast_id(def) == TK_PRIMITIVE) - tcap = TK_VAL; - else - tcap = TK_CAP_ANY; - } else { - // Use the default capability. - tcap = ast_id(def_cap); - } + // Use the default capability. + tcap = ast_id(def_cap); } ast_setid(cap, tcap);