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

disallow sink openArray[T] types #544

Draft
wants to merge 1 commit into
base: devel
Choose a base branch
from

Conversation

zerbina
Copy link
Collaborator

@zerbina zerbina commented Feb 15, 2023

Summary

No specification for how sink openArray[T] should work exists, and the current implementation only works without issues in the simple case where the argument is a literal array-constructor expression that is not constant.

For most other cases, either the seq or string used as the argument are not cleaned up or, for constant expression, a segmentation fault occurs at run-time when trying to mutate the openArray.

Passing an immutable openArray to a sink openArray, while in theory supported, also results in a run-time error.

Details

  • remove the "cannot create implicit openArray` report; it's unused now
  • disable the test that made use of sink openArray

## Summary
No specification for how `sink openArray[T]` should work exists, and
the current implementation only works without issues in the simple case
where the argument is a literal array-constructor expression that is not
constant.

For most other cases, either the `seq` or `string` used as the argument
are not cleaned up or, for constant expression, a segmentation fault
occurs at run-time when trying to mutate the `openArray`.

Passing an immutable `openArray` to a `sink openArray`, while in theory
supported, also results in a run-time error.

## Details
- remove the "cannot create implicit openArray` report; it's unused now
- disable the test that made use of `sink openArray`
@zerbina zerbina added the compiler General compiler tag label Feb 15, 2023
@zerbina zerbina requested a review from saem February 15, 2023 16:22
Copy link
Collaborator

@saem saem left a comment

Choose a reason for hiding this comment

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

Bors r+

(•_•)

sink openArray

( •_•)>⌐■-■

You've been sunk for the last time

(⌐■_■)

bors bot added a commit that referenced this pull request Feb 15, 2023
544: disallow `sink openArray[T]` types r=saem a=zerbina

## Summary
No specification for how `sink openArray[T]` should work exists, and the current implementation only works without issues in the simple case where the argument is a literal array-constructor expression that is not constant.

For most other cases, either the `seq` or `string` used as the argument are not cleaned up or, for constant expression, a segmentation fault occurs at run-time when trying to mutate the `openArray`.

Passing an immutable `openArray` to a `sink openArray`, while in theory supported, also results in a run-time error.

## Details
- remove the "cannot create implicit openArray` report; it's unused now
- disable the test that made use of `sink openArray`



Co-authored-by: zerbina <[email protected]>
@bors
Copy link
Contributor

bors bot commented Feb 15, 2023

Build failed:

@zerbina
Copy link
Collaborator Author

zerbina commented Feb 23, 2023

I had not considered generics (i.e. sink T) here: if T resolves to openArray at instantiation time, the compiler now also reports an error (it did so previously only with var and lent). The add procedure for seq uses sink, thus essentially making the usage of openArray[T] as the element type impossible.

A possible solution

If the argument to the sink type operator is an unresolved generic, don't compute the type yet (i.e. by creating a tySink type) but leave it as an uncomputed type expression (i.e. tyFromExpr). Once the generic argument is resolved, the sink operation is applied, but with the special rule of it being a no-op if the resolved argument is an view type.

In other words: if the T in sink T resolves to a view type, don't treat it as an error and use the view type directly.

Thoughts?

@zerbina zerbina added the compiler/sem Related to semantic-analysis system of the compiler label Feb 23, 2023
@haxscramper haxscramper added the simplification Removal of the old, unused, unnecessary or un/under-specified language features. label Feb 25, 2023
@saem
Copy link
Collaborator

saem commented Feb 27, 2023

Given current definitions:

  • sink: "I'm giving this away" or it's "consumed" if we're to use more Pony terminology
  • openArray: a view, which means very much not owned

I think sink openArray[T] should very much be an error.


I think the issue is that openArray is not a view, it's more like an interface-hardcoded-concept-meets-view-thingy. I think first step is figuring out what openArray actually means, then we can figure out how it should interact with sink.

If we need a decision in the interim because this blocks other things, let's keep the behaviour as it is in your PR, I believe it's actually correct, and adapt any stdlib bits if required.

@zerbina
Copy link
Collaborator Author

zerbina commented Feb 27, 2023

I think sink openArray[T] should very much be an error.

I think so too. But I'm not so sure whether it's a good idea to also report an error for sink T where T resolves to openArray.

Adapting the parts in the that are causing the issues is possible:

proc add*[T: not openArray](x: var seq[T], item: sink T) {.magic: "AppendSeqElem".}

proc add*[T: openArray](x: var seq[T], item: T) {.magic: "AppendSeqElem".}

but when using first-class openArrays, other generic code might run into the same problem, hence my suggestion with adjusting the sink type operator.

The PR is not blocking anything else right now, so I'm turning it into draft until we got the details figured out.

@zerbina zerbina marked this pull request as draft February 27, 2023 17:31
@krux02
Copy link
Contributor

krux02 commented Jun 22, 2023

I think the issue is that openArray is not a view, it's more like an interface-hardcoded-concept-meets-view-thingy. I think first step is figuring out what openArray actually means, then we can figure out how it should interact with sink.

In my code I've seen it always as a language supported pointer+length pair. Something that I've done in C with pointer + length as two distinct parameters I can now do In one. Nice. I've also writter C code wrappers that expose a pointer+length pair as an openArray to Nim for seamless interop.

In C a pointer+length parameter can very much move the ownership of the data. But that doesn't apply to Nim. Here an openArray isn't a value type, it is merely the pointer+length tuple that is extracted from either a seq or an array. It doesn't reflect ownership on its own.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/sem Related to semantic-analysis system of the compiler compiler General compiler tag simplification Removal of the old, unused, unnecessary or un/under-specified language features.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants