Skip to content

Add type restrictions to Digest directory #15696

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Vici37
Copy link

@Vici37 Vici37 commented Apr 21, 2025

This is the output of compiling cr-source-typer and running it with the below incantation:

CRYSTAL_PATH="./src" ./typer spec/std_spec.cr \
  --error-trace --exclude src/crystal/ \
  --stats --progress \
  --union-size-threshold 2 \
  --ignore-private-defs \
  --ignore-protected-defs \
  src/csv

This is related to #15682 .

update(data, initial)
end

def self.update(data, adler32 : UInt32) : UInt32
def self.update(data : Slice(UInt8) | String, adler32 : UInt32) : UInt32
Copy link
Contributor

Choose a reason for hiding this comment

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

Here is where some sort of trait mechanism would have shined - what really is accepted is slices and anything that can be converted to a slice by implementing to_slice. I don't think it is obvious that the latter should necessary be reduced to just strings.

Aside, not related to PR but to the code in general: Here I'd have preferred to have two method variants, one that is locked down to only slice and don't try to call to_slice on it, and one that is more liberal that try to convert and call the other. (If we ever get endless defs then this is a place where those would be great)

Copy link
Member

Choose a reason for hiding this comment

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

I agree to both
For this PR, I'd suggest to leave out the type restriction. It's too strict. And we don't have a means to express the actual type requirement accurately (cf. #15682 (comment)).

Copy link
Contributor

Choose a reason for hiding this comment

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

We should split the method: one with the implementation that takes a Bytes (Slice(UInt8)) and another that will cast #to_slice?

def self.update(data, adler32 : UInt32)
  update(data.to_slice, adler32)
end

def self.update(data : Bytes, adler32 : UInt32)
  # actual implementation
end

The advantage is making sure #to_slice does indeed return a Slice(UInt8) not a Slice(UInt16) or Slice(Char).

The same should be applied to other Digest and Crypto and other places in stdlib that expects bytes. Maybe not in this PR, or not, so we have a potentially breaking change in a focused PR.

@@ -21,7 +21,7 @@ abstract class Digest
# The modules adds convenient class methods as `Digest::MD5.digest`, `Digest::MD5.hexdigest`.
module ClassMethods
# Returns the hash of *data*. *data* must respond to `#to_slice`.
def digest(data) : Bytes
def digest(data : String) : Bytes
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here. The comment above it directly contradict the type marker!

Copy link
Author

Choose a reason for hiding this comment

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

You know, and be a String ;) (will remove restriction)

@@ -85,7 +85,7 @@ abstract class Digest
#
# Digest::SHA1.base64digest("foo") # => "C+7Hteo/D9vJXQ3UfzxbwnXaijM="
# ```
def base64digest(data) : String
def base64digest(data : String) : String
Copy link
Contributor

Choose a reason for hiding this comment

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

and here..

update(data, initial)
end

def self.update(data, crc32 : UInt32) : UInt32
def self.update(data : Slice(UInt8) | String, crc32 : UInt32) : UInt32
Copy link
Member

Choose a reason for hiding this comment

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

issue: This is the same as #15696 (comment). We should drop the restriction because it's too strict.

Suggested change
def self.update(data : Slice(UInt8) | String, crc32 : UInt32) : UInt32
def self.update(data, crc32 : UInt32) : UInt32

Copy link
Author

Choose a reason for hiding this comment

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

This PR is not my lucky PR 😅 Yup, will remove it

@@ -17,16 +17,16 @@ class Digest::CRC32 < ::Digest
LibZ.crc32(0, nil, 0).to_u32
end

def self.checksum(data) : UInt32
def self.checksum(data : String) : UInt32
Copy link
Contributor

@ysbaddaden ysbaddaden Apr 24, 2025

Choose a reason for hiding this comment

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

Even this constraint is too strict: it calls #update internally, and thus can take whatever responds to #to_slice.

Copy link
Author

Choose a reason for hiding this comment

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

Ack, good catch, will remove

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants