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

[stdlib] Make StringRef ctor from UnsafePointer[Byte] keyword only #3698

Closed
wants to merge 1 commit into from

Conversation

soraros
Copy link
Contributor

@soraros soraros commented Oct 21, 2024

This issues was reported by @aurelian(@diocletiann) on Discord.
This prevents accidental UnsafePointer[Byte] to String implicit conversion through String ctor from StringRef.

@JoeLoser Should we make the constructor from pointer+length keyword only as well, following String?

@soraros soraros force-pushed the fix-stringref-ctor branch 2 times, most recently from b1e538e to 359fbef Compare October 21, 2024 00:26
@soraros soraros marked this pull request as ready for review October 21, 2024 00:32
@soraros soraros requested a review from a team as a code owner October 21, 2024 00:32
@gryznar
Copy link
Contributor

gryznar commented Oct 21, 2024

I am curious why implicit conversions are present in Mojo at all? It seems that it is source of a lot of bugs

@soraros
Copy link
Contributor Author

soraros commented Oct 21, 2024

@gryznar Even though I'm not a fan of implicit conversions, they seem to be necessary to model things like literals and mimic Python behaviour in the dynamic portion. However, I do agree that we should be able to control them, and only opt-in to implicit conversion when creating a constructor (#1310).

@gryznar
Copy link
Contributor

gryznar commented Oct 21, 2024

This issue should be addressed first IMHO, because switching to keyword-only treats symptoms, not cause

This prevents accidental `UnsafePointer[Byte]` to `String` implicit conversion through `String` ctor from `StringRef`.

Signed-off-by: Yiwu Chen <[email protected]>
@soraros soraros force-pushed the fix-stringref-ctor branch from 359fbef to d322b84 Compare October 22, 2024 12:49
@JoeLoser
Copy link
Collaborator

@JoeLoser Should we make the constructor from pointer+length keyword only as well, following String?

Can you elaborate here? For String, it's implicit I think - see this constructor.

@soraros
Copy link
Contributor Author

soraros commented Oct 22, 2024

@JoeLoser I was always under the impression that it worked this way. I must have mixed it up with another struct.

That said, the unsafe constructors from pointers are really a mess, and I find myself having to look up the keyword names every time. Could we adopt a convention like fn __init__(inout self, *, ptr: UnsafePointer, len: Int)?

  • String: fn __init__(inout self, ptr: UnsafePointer[UInt8], len: Int):
  • StringRef: fn __init__(inout self, ptr: UnsafePointer[c_char], len: Int):
  • StringSlice: fn __init__(inout self, *, unsafe_from_utf8_ptr: UnsafePointer[UInt8], len: Int):
  • Span: fn __init__(inout self, *, unsafe_ptr: UnsafePointer[T], len: Int):
  • List: fn __init__(inout self, *, unsafe_pointer: UnsafePointer[T], size: Int, capacity: Int):

@JoeLoser
Copy link
Collaborator

@JoeLoser I was always under the impression that it worked this way. I must have mixed it up with another struct.

That said, the unsafe constructors from pointers are really a mess, and I find myself having to look up the keyword names every time. Could we adopt a convention like fn __init__(inout self, *, ptr: UnsafePointer, len: Int)?

  • String: fn __init__(inout self, ptr: UnsafePointer[UInt8], len: Int):
  • StringRef: fn __init__(inout self, ptr: UnsafePointer[c_char], len: Int):
  • StringSlice: fn __init__(inout self, *, unsafe_from_utf8_ptr: UnsafePointer[UInt8], len: Int):
  • Span: fn __init__(inout self, *, unsafe_ptr: UnsafePointer[T], len: Int):
  • List: fn __init__(inout self, *, unsafe_pointer: UnsafePointer[T], size: Int, capacity: Int):

Agreed, they're wildly inconsistent. +1 to your proposed convention - feel free to open a PR. I'm also +1 for making them all (including String kw-only for the ptr + len constructor until we fix implicit constructor issue you linked).

@soraros
Copy link
Contributor Author

soraros commented Oct 22, 2024

@JoeLoser I think this one is ready. The CI failure is caused by some Conda issues.

@JoeLoser
Copy link
Collaborator

!sync

@modularbot modularbot added the imported-internally Signals that a given pull request has been imported internally. label Oct 22, 2024
@JoeLoser
Copy link
Collaborator

Looks good still, we'll just need to fix up our internal usage before this lands/merges internally. I'm on PTO tomorrow, but can look on Monday likely.

@modularbot
Copy link
Collaborator

✅🟣 This contribution has been merged 🟣✅

Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the nightly branch during the next Mojo nightly release, typically within the next 24-48 hours.

We use Copybara to merge external contributions, click here to learn more.

@modularbot modularbot added merged-internally Indicates that this pull request has been merged internally merged-externally Merged externally in public mojo repo labels Oct 29, 2024
modularbot pushed a commit that referenced this pull request Oct 30, 2024
…keyword only (#49581)

[External] [stdlib] Make `StringRef` ctor from `UnsafePointer[Byte]`
keyword only

This issues was reported by `@aurelian`(@diocletiann) on Discord.
This prevents accidental `UnsafePointer[Byte]` to `String` implicit
conversion through `String` ctor from `StringRef`.

Make this explicit by explicitly constructing the `StringRef` with the
keyword-only constructor.

Co-authored-by: soraros <[email protected]>
Closes #3698
MODULAR_ORIG_COMMIT_REV_ID: 44c1e061db833fff3cf261f2b6eabfcbef6e1591
@modularbot
Copy link
Collaborator

Landed in 8767f4d! Thank you for your contribution 🎉

@modularbot modularbot closed this Oct 30, 2024
@soraros soraros deleted the fix-stringref-ctor branch October 30, 2024 18:17
Ahajha pushed a commit to Ahajha/mojo that referenced this pull request Oct 31, 2024
…keyword only (#49581)

[External] [stdlib] Make `StringRef` ctor from `UnsafePointer[Byte]`
keyword only

This issues was reported by `@aurelian`(@diocletiann) on Discord.
This prevents accidental `UnsafePointer[Byte]` to `String` implicit
conversion through `String` ctor from `StringRef`.

Make this explicit by explicitly constructing the `StringRef` with the
keyword-only constructor.

Co-authored-by: soraros <[email protected]>
Closes modularml#3698
MODULAR_ORIG_COMMIT_REV_ID: 44c1e061db833fff3cf261f2b6eabfcbef6e1591
modularbot pushed a commit that referenced this pull request Dec 17, 2024
…keyword only (#49581)

[External] [stdlib] Make `StringRef` ctor from `UnsafePointer[Byte]`
keyword only

This issues was reported by `@aurelian`(@diocletiann) on Discord.
This prevents accidental `UnsafePointer[Byte]` to `String` implicit
conversion through `String` ctor from `StringRef`.

Make this explicit by explicitly constructing the `StringRef` with the
keyword-only constructor.

Co-authored-by: soraros <[email protected]>
Closes #3698
MODULAR_ORIG_COMMIT_REV_ID: 44c1e061db833fff3cf261f2b6eabfcbef6e1591
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imported-internally Signals that a given pull request has been imported internally. merged-externally Merged externally in public mojo repo merged-internally Indicates that this pull request has been merged internally
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants