Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 18913 - Cannot move static array of non-copyable type #6810

Merged
merged 1 commit into from
Dec 23, 2018

Conversation

edi33416
Copy link
Contributor

No description provided.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @edi33416! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the annotated coverage diff directly on GitHub with CodeCov's browser extension
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Auto-close Bugzilla Severity Description
18913 major Cannot move static array of non-copyable type

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + phobos#6810"

@edi33416
Copy link
Contributor Author

While working at this, I've noticed that move doesn't nullify references.
This, imho, is a big issue. See code snippet

@safe
void main()
{
  int[] x = [1, 2, 3];
  int[] nx;

  move(x, nx); // nx should take ownership, but it just points to the same address
  
  assert(x == nx); // oho
  x[0] = 2; // should fail, but, instead, it silently modifies nx's content
  assert(nx == [1, 2, 3]); // fails because of bug
}

// Primitive data (including pointers and arrays) or class -
// assignment works great
target = source;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write the whole thing as:

static if (is(T == struct))
{
    // ...
}
else static if (isStaticArray!T)
    moveEmplaceAll(source[], target[]);
else
    target = source;

Copy link
Contributor Author

@edi33416 edi33416 Dec 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the code inside else static if.

Can't use moveEmplaceAll as source[] fails the isInputRange check

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, unfortunately the isInputRange constraint is too strict for move[Emplace]{All|Some} functions, as a range, whose elements are not copy-able is the primary use-case for move-ing.

If we adjust isInputRange like so

@@ -168,7 +168,10 @@
 enum bool isInputRange(R) =
     is(typeof(R.init) == R)
     && is(ReturnType!((R r) => r.empty) == bool)
-    && is(typeof((return ref R r) => r.front))
+    && (
+            is(typeof((return ref R r) => r.front)) ||
+            is(typeof((return ref R r) => &r.front()))
+       )
     && !is(ReturnType!((R r) => r.front) == void)
     && is(typeof((R r) => r.popFront));

all tests pass.

Copy link
Member

@PetarKirov PetarKirov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, we can revisit the IsInputRange question in the future.

@dlang-bot dlang-bot merged commit e034998 into dlang:master Dec 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants