From 4581999a3ca1916578d030bc7850f39dc2022d7d Mon Sep 17 00:00:00 2001 From: Tomoya Tanjo Date: Sat, 20 Jul 2013 17:17:19 +0900 Subject: [PATCH 1/6] Make std.array.array @safe if possible --- std/array.d | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/std/array.d b/std/array.d index 20447ad8fb5..0b143b8f826 100644 --- a/std/array.d +++ b/std/array.d @@ -42,7 +42,7 @@ if (isIterable!Range && !isNarrowString!Range) { if(r.length == 0) return null; - auto result = uninitializedArray!(Unqual!(E)[])(r.length); + auto result = ()@trusted{ return uninitializedArray!(Unqual!(E)[])(r.length); }(); size_t i = 0; foreach (e; r) @@ -51,7 +51,8 @@ if (isIterable!Range && !isNarrowString!Range) static if (is(typeof(e.opAssign(e)))) { // this should be in-place construction - emplace!E(result.ptr + i, e); + auto ithptr = ()@trusted{ return result.ptr + i; }(); + emplace!E(ithptr, e); } else { @@ -72,6 +73,30 @@ if (isIterable!Range && !isNarrowString!Range) } } +@safe unittest +{ + auto a = array([1, 2, 3, 4, 5][]); + assert(a == [ 1, 2, 3, 4, 5 ]); +} + +@safe unittest +{ + struct Foo + { + int a; + @safe auto opAssign(Foo foo) + { + a = foo.a; + } + @safe auto opEquals(Foo foo) + { + return a == foo.a; + } + } + auto a = array([Foo(1), Foo(2), Foo(3), Foo(4), Foo(5)][]); + assert(equal(a, [Foo(1), Foo(2), Foo(3), Foo(4), Foo(5)])); +} + /** Convert a narrow string to an array type that fully supports random access. This is handled as a special case and always returns a $(D dchar[]), From f6b41a55eb4a7639b7fde0f5c895256dfd4d81a2 Mon Sep 17 00:00:00 2001 From: Tomoya Tanjo Date: Sat, 3 Aug 2013 20:03:01 +0900 Subject: [PATCH 2/6] Fix array to make it surely safe for some cases --- std/array.d | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/std/array.d b/std/array.d index 0b143b8f826..e81a8138b86 100644 --- a/std/array.d +++ b/std/array.d @@ -48,11 +48,11 @@ if (isIterable!Range && !isNarrowString!Range) foreach (e; r) { // hacky - static if (is(typeof(e.opAssign(e)))) + static if (is(typeof(result[i].opAssign(e))) || + !is(typeof(result[i] == e))) { // this should be in-place construction - auto ithptr = ()@trusted{ return result.ptr + i; }(); - emplace!E(ithptr, e); + emplace!E(result.ptr + i, e); } else { From b059f51751aa74f8e3e351b2a413000a56b393ce Mon Sep 17 00:00:00 2001 From: Tomoya Tanjo Date: Sat, 3 Aug 2013 20:16:46 +0900 Subject: [PATCH 3/6] Update unittests --- std/array.d | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/std/array.d b/std/array.d index e81a8138b86..644795b5760 100644 --- a/std/array.d +++ b/std/array.d @@ -84,11 +84,25 @@ if (isIterable!Range && !isNarrowString!Range) struct Foo { int a; - @safe auto opAssign(Foo foo) + @safe auto opEquals(Foo foo) + { + return a == foo.a; + } + } + auto a = array([Foo(1), Foo(2), Foo(3), Foo(4), Foo(5)][]); + assert(equal(a, [Foo(1), Foo(2), Foo(3), Foo(4), Foo(5)])); +} + +@system unittest +{ + struct Foo + { + int a; + auto opAssign(Foo foo) { a = foo.a; } - @safe auto opEquals(Foo foo) + auto opEquals(Foo foo) { return a == foo.a; } From 9760ee864a80b92695c9c9cc8c09b0fc9b0c3ec9 Mon Sep 17 00:00:00 2001 From: Tomoya Tanjo Date: Sat, 3 Aug 2013 20:43:10 +0900 Subject: [PATCH 4/6] Fix unittests --- std/array.d | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/std/array.d b/std/array.d index 644795b5760..5ee153a6340 100644 --- a/std/array.d +++ b/std/array.d @@ -73,21 +73,17 @@ if (isIterable!Range && !isNarrowString!Range) } } -@safe unittest +@safe pure nothrow unittest { auto a = array([1, 2, 3, 4, 5][]); assert(a == [ 1, 2, 3, 4, 5 ]); } -@safe unittest +@safe pure nothrow unittest { struct Foo { int a; - @safe auto opEquals(Foo foo) - { - return a == foo.a; - } } auto a = array([Foo(1), Foo(2), Foo(3), Foo(4), Foo(5)][]); assert(equal(a, [Foo(1), Foo(2), Foo(3), Foo(4), Foo(5)])); From f4002a5a6398df77abf7bef2a35a73151823008e Mon Sep 17 00:00:00 2001 From: Tomoya Tanjo Date: Sat, 3 Aug 2013 22:21:37 +0900 Subject: [PATCH 5/6] Fix array.array --- std/array.d | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/std/array.d b/std/array.d index 5ee153a6340..5bef3755d9a 100644 --- a/std/array.d +++ b/std/array.d @@ -42,7 +42,7 @@ if (isIterable!Range && !isNarrowString!Range) { if(r.length == 0) return null; - auto result = ()@trusted{ return uninitializedArray!(Unqual!(E)[])(r.length); }(); + auto result = ()@trusted{ return uninitializedArray!(Unqual!E[])(r.length); }(); size_t i = 0; foreach (e; r) @@ -52,7 +52,7 @@ if (isIterable!Range && !isNarrowString!Range) !is(typeof(result[i] == e))) { // this should be in-place construction - emplace!E(result.ptr + i, e); + emplace(result.ptr + i, e); } else { From fc3414d18fcc429b3555da2fec42e2059b7f857e Mon Sep 17 00:00:00 2001 From: Tomoya Tanjo Date: Sun, 4 Aug 2013 12:16:19 +0900 Subject: [PATCH 6/6] Fix for array.array --- std/array.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/array.d b/std/array.d index 5bef3755d9a..67f0ea0edda 100644 --- a/std/array.d +++ b/std/array.d @@ -49,7 +49,7 @@ if (isIterable!Range && !isNarrowString!Range) { // hacky static if (is(typeof(result[i].opAssign(e))) || - !is(typeof(result[i] == e))) + !is(typeof(result[i] = e))) { // this should be in-place construction emplace(result.ptr + i, e);